1
0
mirror of https://github.com/immich-app/immich.git synced 2024-12-23 02:06:15 +02:00
immich/mobile/lib/shared/providers/asset.provider.dart

132 lines
3.6 KiB
Dart
Raw Normal View History

import 'package:flutter/foundation.dart';
2022-02-03 18:06:44 +02:00
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/modules/home/services/asset.service.dart';
2022-10-14 23:57:55 +02:00
import 'package:immich_mobile/modules/home/services/asset_cache.service.dart';
import 'package:immich_mobile/shared/services/device_info.service.dart';
import 'package:collection/collection.dart';
import 'package:intl/intl.dart';
import 'package:openapi/api.dart';
import 'package:photo_manager/photo_manager.dart';
2022-02-03 18:06:44 +02:00
class AssetNotifier extends StateNotifier<List<AssetResponseDto>> {
final AssetService _assetService;
2022-10-14 23:57:55 +02:00
final AssetCacheService _assetCacheService;
final DeviceInfoService _deviceInfoService = DeviceInfoService();
2022-02-03 18:06:44 +02:00
2022-10-14 23:57:55 +02:00
AssetNotifier(this._assetService, this._assetCacheService) : super([]);
2022-10-15 23:20:15 +02:00
_cacheState() {
2022-10-17 14:53:27 +02:00
_assetCacheService.put(state);
2022-10-15 23:20:15 +02:00
}
getAllAsset() async {
final stopwatch = Stopwatch();
if (await _assetCacheService.isValid() && state.isEmpty) {
stopwatch.start();
2022-10-17 16:40:51 +02:00
state = await _assetCacheService.get();
debugPrint("Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms");
stopwatch.reset();
2022-10-14 23:57:55 +02:00
}
stopwatch.start();
var allAssets = await _assetService.getAllAsset();
debugPrint("Query assets from API: ${stopwatch.elapsedMilliseconds}ms");
stopwatch.reset();
2022-02-03 18:06:44 +02:00
if (allAssets != null) {
state = allAssets;
stopwatch.start();
2022-10-15 23:20:15 +02:00
_cacheState();
debugPrint("Store assets in cache: ${stopwatch.elapsedMilliseconds}ms");
stopwatch.reset();
2022-02-03 18:06:44 +02:00
}
}
clearAllAsset() {
state = [];
2022-10-15 23:20:15 +02:00
_cacheState();
}
2022-02-03 18:06:44 +02:00
onNewAssetUploaded(AssetResponseDto newAsset) {
state = [...state, newAsset];
2022-10-15 23:20:15 +02:00
_cacheState();
}
deleteAssets(Set<AssetResponseDto> deleteAssets) async {
var deviceInfo = await _deviceInfoService.getDeviceInfo();
var deviceId = deviceInfo["deviceId"];
var deleteIdList = <String>[];
// Delete asset from device
for (var asset in deleteAssets) {
// Delete asset on device if present
if (asset.deviceId == deviceId) {
var localAsset = await AssetEntity.fromId(asset.deviceAssetId);
if (localAsset != null) {
deleteIdList.add(localAsset.id);
2022-02-03 18:06:44 +02:00
}
}
}
try {
await PhotoManager.editor.deleteWithIds(deleteIdList);
} catch (e) {
debugPrint("Delete asset from device failed: $e");
}
// Delete asset on server
List<DeleteAssetResponseDto>? deleteAssetResult =
await _assetService.deleteAssets(deleteAssets);
if (deleteAssetResult == null) {
return;
}
for (var asset in deleteAssetResult) {
if (asset.status == DeleteAssetStatus.SUCCESS) {
state =
state.where((immichAsset) => immichAsset.id != asset.id).toList();
}
}
2022-10-15 23:20:15 +02:00
_cacheState();
}
2022-02-03 18:06:44 +02:00
}
final assetProvider =
StateNotifierProvider<AssetNotifier, List<AssetResponseDto>>((ref) {
2022-10-14 23:57:55 +02:00
return AssetNotifier(
ref.watch(assetServiceProvider), ref.watch(assetCacheServiceProvider));
2022-02-03 18:06:44 +02:00
});
final assetGroupByDateTimeProvider = StateProvider((ref) {
var assets = ref.watch(assetProvider);
assets.sortByCompare<DateTime>(
(e) => DateTime.parse(e.createdAt),
(a, b) => b.compareTo(a),
);
return assets.groupListsBy(
(element) => DateFormat('y-MM-dd')
.format(DateTime.parse(element.createdAt).toLocal()),
);
});
final assetGroupByMonthYearProvider = StateProvider((ref) {
var assets = ref.watch(assetProvider);
assets.sortByCompare<DateTime>(
(e) => DateTime.parse(e.createdAt),
(a, b) => b.compareTo(a),
);
return assets.groupListsBy(
(element) => DateFormat('MMMM, y')
.format(DateTime.parse(element.createdAt).toLocal()),
);
});