2022-06-24 17:02:09 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-02-13 23:10:42 +02:00
|
|
|
import 'package:immich_mobile/modules/home/models/delete_asset_response.model.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
import 'package:immich_mobile/modules/home/services/asset.service.dart';
|
2022-02-07 04:31:32 +02:00
|
|
|
import 'package:immich_mobile/shared/models/immich_asset.model.dart';
|
2022-02-13 23:10:42 +02:00
|
|
|
import 'package:immich_mobile/shared/services/device_info.service.dart';
|
2022-02-07 04:31:32 +02:00
|
|
|
import 'package:collection/collection.dart';
|
2022-02-13 23:10:42 +02:00
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:photo_manager/photo_manager.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2022-02-13 23:10:42 +02:00
|
|
|
class AssetNotifier extends StateNotifier<List<ImmichAsset>> {
|
2022-06-25 20:46:51 +02:00
|
|
|
final AssetService _assetService;
|
2022-02-13 23:10:42 +02:00
|
|
|
final DeviceInfoService _deviceInfoService = DeviceInfoService();
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
AssetNotifier(this._assetService) : super([]);
|
2022-02-07 04:31:32 +02:00
|
|
|
|
2022-02-13 23:10:42 +02:00
|
|
|
getAllAsset() async {
|
|
|
|
List<ImmichAsset>? allAssets = await _assetService.getAllAsset();
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2022-02-13 23:10:42 +02:00
|
|
|
if (allAssets != null) {
|
|
|
|
state = allAssets;
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 23:10:42 +02:00
|
|
|
clearAllAsset() {
|
|
|
|
state = [];
|
|
|
|
}
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2022-02-14 18:40:41 +02:00
|
|
|
onNewAssetUploaded(ImmichAsset newAsset) {
|
|
|
|
state = [...state, newAsset];
|
|
|
|
}
|
|
|
|
|
2022-02-13 23:10:42 +02:00
|
|
|
deleteAssets(Set<ImmichAsset> deleteAssets) async {
|
|
|
|
var deviceInfo = await _deviceInfoService.getDeviceInfo();
|
|
|
|
var deviceId = deviceInfo["deviceId"];
|
2022-06-23 06:14:14 +02:00
|
|
|
var deleteIdList = <String>[];
|
2022-02-13 23:10:42 +02:00
|
|
|
// Delete asset from device
|
|
|
|
for (var asset in deleteAssets) {
|
|
|
|
// Delete asset on device if present
|
|
|
|
if (asset.deviceId == deviceId) {
|
2022-06-23 06:14:14 +02:00
|
|
|
var localAsset = await AssetEntity.fromId(asset.deviceAssetId);
|
2022-02-13 23:10:42 +02:00
|
|
|
|
|
|
|
if (localAsset != null) {
|
|
|
|
deleteIdList.add(localAsset.id);
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-24 17:02:09 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
await PhotoManager.editor.deleteWithIds(deleteIdList);
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Delete asset from device failed: $e");
|
|
|
|
}
|
2022-02-07 04:31:32 +02:00
|
|
|
|
2022-02-13 23:10:42 +02:00
|
|
|
// Delete asset on server
|
2022-06-22 07:23:35 +02:00
|
|
|
List<DeleteAssetResponse>? deleteAssetResult =
|
|
|
|
await _assetService.deleteAssets(deleteAssets);
|
2022-02-13 23:10:42 +02:00
|
|
|
if (deleteAssetResult == null) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-07 04:31:32 +02:00
|
|
|
|
2022-02-13 23:10:42 +02:00
|
|
|
for (var asset in deleteAssetResult) {
|
|
|
|
if (asset.status == 'success') {
|
2022-06-22 07:23:35 +02:00
|
|
|
state =
|
|
|
|
state.where((immichAsset) => immichAsset.id != asset.id).toList();
|
2022-02-07 04:31:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
final assetProvider =
|
|
|
|
StateNotifierProvider<AssetNotifier, List<ImmichAsset>>((ref) {
|
2022-06-25 20:46:51 +02:00
|
|
|
return AssetNotifier(ref.watch(assetServiceProvider));
|
2022-02-03 18:06:44 +02:00
|
|
|
});
|
2022-02-13 23:10:42 +02:00
|
|
|
|
|
|
|
final assetGroupByDateTimeProvider = StateProvider((ref) {
|
2022-02-14 18:40:41 +02:00
|
|
|
var assets = ref.watch(assetProvider);
|
2022-02-13 23:10:42 +02:00
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
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)));
|
2022-02-13 23:10:42 +02:00
|
|
|
});
|
2022-04-24 04:08:45 +02:00
|
|
|
|
|
|
|
final assetGroupByMonthYearProvider = StateProvider((ref) {
|
|
|
|
var assets = ref.watch(assetProvider);
|
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
assets.sortByCompare<DateTime>(
|
|
|
|
(e) => DateTime.parse(e.createdAt), (a, b) => b.compareTo(a));
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
return assets.groupListsBy((element) =>
|
|
|
|
DateFormat('MMMM, y').format(DateTime.parse(element.createdAt)));
|
2022-04-24 04:08:45 +02:00
|
|
|
});
|