2022-06-24 08:02:09 -07:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-02-03 09:06:44 -07:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:immich_mobile/modules/home/services/asset.service.dart';
|
2022-10-14 14:57:55 -07:00
|
|
|
import 'package:immich_mobile/modules/home/services/asset_cache.service.dart';
|
2022-02-13 14:10:42 -07:00
|
|
|
import 'package:immich_mobile/shared/services/device_info.service.dart';
|
2022-02-06 19:31:32 -07:00
|
|
|
import 'package:collection/collection.dart';
|
2022-02-13 14:10:42 -07:00
|
|
|
import 'package:intl/intl.dart';
|
2022-07-13 05:23:48 -07:00
|
|
|
import 'package:openapi/api.dart';
|
2022-02-13 14:10:42 -07:00
|
|
|
import 'package:photo_manager/photo_manager.dart';
|
2022-02-03 09:06:44 -07:00
|
|
|
|
2022-07-13 05:23:48 -07:00
|
|
|
class AssetNotifier extends StateNotifier<List<AssetResponseDto>> {
|
2022-06-25 11:46:51 -07:00
|
|
|
final AssetService _assetService;
|
2022-10-14 14:57:55 -07:00
|
|
|
final AssetCacheService _assetCacheService;
|
|
|
|
|
2022-02-13 14:10:42 -07:00
|
|
|
final DeviceInfoService _deviceInfoService = DeviceInfoService();
|
2022-02-03 09:06:44 -07:00
|
|
|
|
2022-10-14 14:57:55 -07:00
|
|
|
AssetNotifier(this._assetService, this._assetCacheService) : super([]);
|
2022-02-06 19:31:32 -07:00
|
|
|
|
2022-10-15 14:20:15 -07:00
|
|
|
_cacheState() {
|
|
|
|
_assetCacheService.putAssets(state);
|
|
|
|
}
|
|
|
|
|
2022-02-13 14:10:42 -07:00
|
|
|
getAllAsset() async {
|
2022-10-14 14:57:55 -07:00
|
|
|
if (_assetCacheService.isValid() && state.isEmpty) {
|
|
|
|
state = await _assetCacheService.getAssetsAsync();
|
|
|
|
}
|
|
|
|
|
2022-07-13 05:23:48 -07:00
|
|
|
var allAssets = await _assetService.getAllAsset();
|
2022-02-03 09:06:44 -07:00
|
|
|
|
2022-02-13 14:10:42 -07:00
|
|
|
if (allAssets != null) {
|
|
|
|
state = allAssets;
|
2022-10-15 14:20:15 -07:00
|
|
|
_cacheState();
|
2022-02-03 09:06:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 14:10:42 -07:00
|
|
|
clearAllAsset() {
|
|
|
|
state = [];
|
2022-10-15 14:20:15 -07:00
|
|
|
_cacheState();
|
2022-02-13 14:10:42 -07:00
|
|
|
}
|
2022-02-03 09:06:44 -07:00
|
|
|
|
2022-07-13 05:23:48 -07:00
|
|
|
onNewAssetUploaded(AssetResponseDto newAsset) {
|
2022-02-14 09:40:41 -07:00
|
|
|
state = [...state, newAsset];
|
2022-10-15 14:20:15 -07:00
|
|
|
_cacheState();
|
2022-02-14 09:40:41 -07:00
|
|
|
}
|
|
|
|
|
2022-07-13 05:23:48 -07:00
|
|
|
deleteAssets(Set<AssetResponseDto> deleteAssets) async {
|
2022-02-13 14:10:42 -07:00
|
|
|
var deviceInfo = await _deviceInfoService.getDeviceInfo();
|
|
|
|
var deviceId = deviceInfo["deviceId"];
|
2022-06-22 21:14:14 -07:00
|
|
|
var deleteIdList = <String>[];
|
2022-02-13 14:10:42 -07:00
|
|
|
// Delete asset from device
|
|
|
|
for (var asset in deleteAssets) {
|
|
|
|
// Delete asset on device if present
|
|
|
|
if (asset.deviceId == deviceId) {
|
2022-06-22 21:14:14 -07:00
|
|
|
var localAsset = await AssetEntity.fromId(asset.deviceAssetId);
|
2022-02-13 14:10:42 -07:00
|
|
|
|
|
|
|
if (localAsset != null) {
|
|
|
|
deleteIdList.add(localAsset.id);
|
2022-02-03 09:06:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-25 13:12:47 -07:00
|
|
|
|
2022-06-24 08:02:09 -07:00
|
|
|
try {
|
|
|
|
await PhotoManager.editor.deleteWithIds(deleteIdList);
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Delete asset from device failed: $e");
|
|
|
|
}
|
2022-02-06 19:31:32 -07:00
|
|
|
|
2022-02-13 14:10:42 -07:00
|
|
|
// Delete asset on server
|
2022-07-13 05:23:48 -07:00
|
|
|
List<DeleteAssetResponseDto>? deleteAssetResult =
|
2022-06-21 22:23:35 -07:00
|
|
|
await _assetService.deleteAssets(deleteAssets);
|
2022-07-13 05:23:48 -07:00
|
|
|
|
2022-02-13 14:10:42 -07:00
|
|
|
if (deleteAssetResult == null) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-06 19:31:32 -07:00
|
|
|
|
2022-02-13 14:10:42 -07:00
|
|
|
for (var asset in deleteAssetResult) {
|
2022-07-13 05:23:48 -07:00
|
|
|
if (asset.status == DeleteAssetStatus.SUCCESS) {
|
2022-06-21 22:23:35 -07:00
|
|
|
state =
|
|
|
|
state.where((immichAsset) => immichAsset.id != asset.id).toList();
|
2022-02-06 19:31:32 -07:00
|
|
|
}
|
|
|
|
}
|
2022-10-15 14:20:15 -07:00
|
|
|
|
|
|
|
_cacheState();
|
2022-02-06 19:31:32 -07:00
|
|
|
}
|
2022-02-03 09:06:44 -07:00
|
|
|
}
|
|
|
|
|
2022-06-21 22:23:35 -07:00
|
|
|
final assetProvider =
|
2022-07-13 05:23:48 -07:00
|
|
|
StateNotifierProvider<AssetNotifier, List<AssetResponseDto>>((ref) {
|
2022-10-14 14:57:55 -07:00
|
|
|
return AssetNotifier(
|
|
|
|
ref.watch(assetServiceProvider), ref.watch(assetCacheServiceProvider));
|
2022-02-03 09:06:44 -07:00
|
|
|
});
|
2022-02-13 14:10:42 -07:00
|
|
|
|
|
|
|
final assetGroupByDateTimeProvider = StateProvider((ref) {
|
2022-02-14 09:40:41 -07:00
|
|
|
var assets = ref.watch(assetProvider);
|
2022-02-13 14:10:42 -07:00
|
|
|
|
2022-06-21 22:23:35 -07:00
|
|
|
assets.sortByCompare<DateTime>(
|
2022-07-13 05:23:48 -07:00
|
|
|
(e) => DateTime.parse(e.createdAt),
|
|
|
|
(a, b) => b.compareTo(a),
|
|
|
|
);
|
|
|
|
return assets.groupListsBy(
|
2022-09-22 13:58:17 -07:00
|
|
|
(element) => DateFormat('y-MM-dd')
|
|
|
|
.format(DateTime.parse(element.createdAt).toLocal()),
|
2022-07-13 05:23:48 -07:00
|
|
|
);
|
2022-02-13 14:10:42 -07:00
|
|
|
});
|
2022-04-23 19:08:45 -07:00
|
|
|
|
|
|
|
final assetGroupByMonthYearProvider = StateProvider((ref) {
|
|
|
|
var assets = ref.watch(assetProvider);
|
|
|
|
|
2022-06-21 22:23:35 -07:00
|
|
|
assets.sortByCompare<DateTime>(
|
2022-07-13 05:23:48 -07:00
|
|
|
(e) => DateTime.parse(e.createdAt),
|
|
|
|
(a, b) => b.compareTo(a),
|
|
|
|
);
|
|
|
|
|
|
|
|
return assets.groupListsBy(
|
2022-09-22 13:58:17 -07:00
|
|
|
(element) => DateFormat('MMMM, y')
|
|
|
|
.format(DateTime.parse(element.createdAt).toLocal()),
|
2022-07-13 05:23:48 -07:00
|
|
|
);
|
2022-04-23 19:08:45 -07:00
|
|
|
});
|