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';
|
|
|
|
import 'package:immich_mobile/modules/home/services/asset.service.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';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:openapi/api.dart';
|
2022-02-13 23:10:42 +02:00
|
|
|
import 'package:photo_manager/photo_manager.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
class AssetNotifier extends StateNotifier<List<AssetResponseDto>> {
|
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 {
|
2022-07-13 14:23:48 +02:00
|
|
|
var 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-07-13 14:23:48 +02:00
|
|
|
onNewAssetUploaded(AssetResponseDto newAsset) {
|
2022-02-14 18:40:41 +02:00
|
|
|
state = [...state, newAsset];
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
deleteAssets(Set<AssetResponseDto> deleteAssets) async {
|
2022-02-13 23:10:42 +02:00
|
|
|
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-25 22:12:47 +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-07-13 14:23:48 +02:00
|
|
|
List<DeleteAssetResponseDto>? deleteAssetResult =
|
2022-06-22 07:23:35 +02:00
|
|
|
await _assetService.deleteAssets(deleteAssets);
|
2022-07-13 14:23:48 +02:00
|
|
|
|
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) {
|
2022-07-13 14:23:48 +02:00
|
|
|
if (asset.status == DeleteAssetStatus.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 =
|
2022-07-13 14:23:48 +02:00
|
|
|
StateNotifierProvider<AssetNotifier, List<AssetResponseDto>>((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>(
|
2022-07-13 14:23:48 +02:00
|
|
|
(e) => DateTime.parse(e.createdAt),
|
|
|
|
(a, b) => b.compareTo(a),
|
|
|
|
);
|
|
|
|
return assets.groupListsBy(
|
2022-09-22 22:58:17 +02:00
|
|
|
(element) => DateFormat('y-MM-dd')
|
|
|
|
.format(DateTime.parse(element.createdAt).toLocal()),
|
2022-07-13 14:23:48 +02:00
|
|
|
);
|
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>(
|
2022-07-13 14:23:48 +02:00
|
|
|
(e) => DateTime.parse(e.createdAt),
|
|
|
|
(a, b) => b.compareTo(a),
|
|
|
|
);
|
|
|
|
|
|
|
|
return assets.groupListsBy(
|
2022-09-22 22:58:17 +02:00
|
|
|
(element) => DateFormat('MMMM, y')
|
|
|
|
.format(DateTime.parse(element.createdAt).toLocal()),
|
2022-07-13 14:23:48 +02:00
|
|
|
);
|
2022-04-24 04:08:45 +02:00
|
|
|
});
|