2022-11-08 10:00:24 -07:00
|
|
|
import 'dart:collection';
|
|
|
|
|
2022-06-24 08:02:09 -07:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-11-26 09:16:02 -07:00
|
|
|
import 'package:hive/hive.dart';
|
2022-02-03 09:06:44 -07:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-11-26 09:16:02 -07:00
|
|
|
import 'package:immich_mobile/constants/hive_box.dart';
|
2022-02-03 09:06:44 -07:00
|
|
|
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-11-08 10:00:24 -07:00
|
|
|
import 'package:immich_mobile/shared/models/asset.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-11-08 10:00:24 -07:00
|
|
|
class AssetNotifier extends StateNotifier<List<Asset>> {
|
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-11-08 10:00:24 -07:00
|
|
|
bool _getAllAssetInProgress = false;
|
|
|
|
bool _deleteInProgress = false;
|
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() {
|
2022-10-17 05:53:27 -07:00
|
|
|
_assetCacheService.put(state);
|
2022-10-15 14:20:15 -07:00
|
|
|
}
|
|
|
|
|
2022-02-13 14:10:42 -07:00
|
|
|
getAllAsset() async {
|
2022-11-08 10:00:24 -07:00
|
|
|
if (_getAllAssetInProgress || _deleteInProgress) {
|
|
|
|
// guard against multiple calls to this method while it's still working
|
|
|
|
return;
|
|
|
|
}
|
2022-10-16 00:50:31 -07:00
|
|
|
final stopwatch = Stopwatch();
|
2022-11-08 10:00:24 -07:00
|
|
|
try {
|
|
|
|
_getAllAssetInProgress = true;
|
|
|
|
final bool isCacheValid = await _assetCacheService.isValid();
|
2022-11-26 09:16:02 -07:00
|
|
|
stopwatch.start();
|
|
|
|
final localTask = _assetService.getLocalAssets(urgent: !isCacheValid);
|
|
|
|
final remoteTask = _assetService.getRemoteAssets();
|
2022-11-08 10:00:24 -07:00
|
|
|
if (isCacheValid && state.isEmpty) {
|
|
|
|
state = await _assetCacheService.get();
|
|
|
|
debugPrint(
|
2022-11-21 05:13:14 -07:00
|
|
|
"Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms",
|
|
|
|
);
|
2022-11-08 10:00:24 -07:00
|
|
|
stopwatch.reset();
|
|
|
|
}
|
2022-10-16 00:50:31 -07:00
|
|
|
|
2022-11-26 09:16:02 -07:00
|
|
|
int remoteBegin = state.indexWhere((a) => a.isRemote);
|
|
|
|
remoteBegin = remoteBegin == -1 ? state.length : remoteBegin;
|
|
|
|
final List<Asset> currentLocal = state.slice(0, remoteBegin);
|
|
|
|
List<Asset>? newRemote = await remoteTask;
|
|
|
|
List<Asset>? newLocal = await localTask;
|
|
|
|
debugPrint("Load assets: ${stopwatch.elapsedMilliseconds}ms");
|
2022-10-16 00:50:31 -07:00
|
|
|
stopwatch.reset();
|
2022-11-26 09:16:02 -07:00
|
|
|
if (newRemote == null &&
|
|
|
|
(newLocal == null || currentLocal.equals(newLocal))) {
|
|
|
|
debugPrint("state is already up-to-date");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
newRemote ??= state.slice(remoteBegin);
|
|
|
|
newLocal ??= [];
|
|
|
|
state = _combineLocalAndRemoteAssets(local: newLocal, remote: newRemote);
|
|
|
|
debugPrint("Combining assets: ${stopwatch.elapsedMilliseconds}ms");
|
2022-11-08 10:00:24 -07:00
|
|
|
} finally {
|
|
|
|
_getAllAssetInProgress = false;
|
2022-10-14 14:57:55 -07:00
|
|
|
}
|
2022-11-08 10:00:24 -07:00
|
|
|
debugPrint("[getAllAsset] setting new asset state");
|
2022-10-14 14:57:55 -07:00
|
|
|
|
2022-11-26 09:16:02 -07:00
|
|
|
stopwatch.reset();
|
2022-11-08 10:00:24 -07:00
|
|
|
_cacheState();
|
|
|
|
debugPrint("Store assets in cache: ${stopwatch.elapsedMilliseconds}ms");
|
2022-11-26 09:16:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
List<Asset> _combineLocalAndRemoteAssets({
|
|
|
|
required Iterable<Asset> local,
|
|
|
|
required List<Asset> remote,
|
|
|
|
}) {
|
|
|
|
final List<Asset> assets = [];
|
|
|
|
if (remote.isNotEmpty && local.isNotEmpty) {
|
|
|
|
final String deviceId = Hive.box(userInfoBox).get(deviceIdKey);
|
|
|
|
final Set<String> existingIds = remote
|
|
|
|
.where((e) => e.deviceId == deviceId)
|
|
|
|
.map((e) => e.deviceAssetId)
|
|
|
|
.toSet();
|
|
|
|
local = local.where((e) => !existingIds.contains(e.id));
|
|
|
|
}
|
|
|
|
assets.addAll(local);
|
|
|
|
// the order (first all local, then remote assets) is important!
|
|
|
|
assets.addAll(remote);
|
|
|
|
return assets;
|
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-11-08 10:00:24 -07:00
|
|
|
final int i = state.indexWhere(
|
|
|
|
(a) =>
|
|
|
|
a.isRemote ||
|
|
|
|
(a.id == newAsset.deviceAssetId && a.deviceId == newAsset.deviceId),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (i == -1 || state[i].deviceAssetId != newAsset.deviceAssetId) {
|
|
|
|
state = [...state, Asset.remote(newAsset)];
|
|
|
|
} else {
|
|
|
|
// order is important to keep all local-only assets at the beginning!
|
|
|
|
state = [
|
|
|
|
...state.slice(0, i),
|
|
|
|
...state.slice(i + 1),
|
|
|
|
Asset.remote(newAsset),
|
|
|
|
];
|
|
|
|
// TODO here is a place to unify local/remote assets by replacing the
|
|
|
|
// local-only asset in the state with a local&remote asset
|
|
|
|
}
|
2022-10-15 14:20:15 -07:00
|
|
|
_cacheState();
|
2022-02-14 09:40:41 -07:00
|
|
|
}
|
|
|
|
|
2022-11-08 10:00:24 -07:00
|
|
|
deleteAssets(Set<Asset> deleteAssets) async {
|
|
|
|
_deleteInProgress = true;
|
|
|
|
try {
|
|
|
|
final localDeleted = await _deleteLocalAssets(deleteAssets);
|
|
|
|
final remoteDeleted = await _deleteRemoteAssets(deleteAssets);
|
|
|
|
final Set<String> deleted = HashSet();
|
|
|
|
deleted.addAll(localDeleted);
|
|
|
|
deleted.addAll(remoteDeleted);
|
|
|
|
if (deleted.isNotEmpty) {
|
|
|
|
state = state.where((a) => !deleted.contains(a.id)).toList();
|
|
|
|
_cacheState();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
_deleteInProgress = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<String>> _deleteLocalAssets(Set<Asset> assetsToDelete) async {
|
2022-02-13 14:10:42 -07:00
|
|
|
var deviceInfo = await _deviceInfoService.getDeviceInfo();
|
|
|
|
var deviceId = deviceInfo["deviceId"];
|
2022-11-08 10:00:24 -07:00
|
|
|
final List<String> local = [];
|
2022-02-13 14:10:42 -07:00
|
|
|
// Delete asset from device
|
2022-11-08 10:00:24 -07:00
|
|
|
for (final Asset asset in assetsToDelete) {
|
|
|
|
if (asset.isLocal) {
|
|
|
|
local.add(asset.id);
|
|
|
|
} else if (asset.deviceId == deviceId) {
|
|
|
|
// Delete asset on device if it is still present
|
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) {
|
2022-11-08 10:00:24 -07:00
|
|
|
local.add(localAsset.id);
|
2022-02-03 09:06:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 10:00:24 -07:00
|
|
|
if (local.isNotEmpty) {
|
|
|
|
try {
|
|
|
|
return await PhotoManager.editor.deleteWithIds(local);
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Delete asset from device failed: $e");
|
2022-02-06 19:31:32 -07:00
|
|
|
}
|
|
|
|
}
|
2022-11-08 10:00:24 -07:00
|
|
|
return [];
|
|
|
|
}
|
2022-10-15 14:20:15 -07:00
|
|
|
|
2022-11-08 10:00:24 -07:00
|
|
|
Future<Iterable<String>> _deleteRemoteAssets(
|
|
|
|
Set<Asset> assetsToDelete,
|
|
|
|
) async {
|
|
|
|
final Iterable<AssetResponseDto> remote =
|
|
|
|
assetsToDelete.where((e) => e.isRemote).map((e) => e.remote!);
|
|
|
|
final List<DeleteAssetResponseDto> deleteAssetResult =
|
|
|
|
await _assetService.deleteAssets(remote) ?? [];
|
|
|
|
return deleteAssetResult
|
|
|
|
.where((a) => a.status == DeleteAssetStatus.SUCCESS)
|
|
|
|
.map((a) => a.id);
|
2022-02-06 19:31:32 -07:00
|
|
|
}
|
2022-02-03 09:06:44 -07:00
|
|
|
}
|
|
|
|
|
2022-11-08 10:00:24 -07:00
|
|
|
final assetProvider = StateNotifierProvider<AssetNotifier, List<Asset>>((ref) {
|
2022-10-14 14:57:55 -07:00
|
|
|
return AssetNotifier(
|
2022-11-21 05:13:14 -07:00
|
|
|
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-11-08 10:00:24 -07:00
|
|
|
final assets = ref.watch(assetProvider).toList();
|
|
|
|
// `toList()` ist needed to make a copy as to NOT sort the original list/state
|
2022-02-13 14:10:42 -07:00
|
|
|
|
2022-06-21 22:23:35 -07:00
|
|
|
assets.sortByCompare<DateTime>(
|
2022-11-08 10:00:24 -07:00
|
|
|
(e) => e.createdAt,
|
2022-07-13 05:23:48 -07:00
|
|
|
(a, b) => b.compareTo(a),
|
|
|
|
);
|
|
|
|
return assets.groupListsBy(
|
2022-11-08 10:00:24 -07:00
|
|
|
(element) => DateFormat('y-MM-dd').format(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) {
|
2022-11-08 10:00:24 -07:00
|
|
|
// TODO: remove `where` once temporary workaround is no longer needed (to only
|
|
|
|
// allow remote assets to be added to album). Keep `toList()` as to NOT sort
|
|
|
|
// the original list/state
|
|
|
|
final assets = ref.watch(assetProvider).where((e) => e.isRemote).toList();
|
2022-04-23 19:08:45 -07:00
|
|
|
|
2022-06-21 22:23:35 -07:00
|
|
|
assets.sortByCompare<DateTime>(
|
2022-11-08 10:00:24 -07:00
|
|
|
(e) => e.createdAt,
|
2022-07-13 05:23:48 -07:00
|
|
|
(a, b) => b.compareTo(a),
|
|
|
|
);
|
|
|
|
|
|
|
|
return assets.groupListsBy(
|
2022-11-08 10:00:24 -07:00
|
|
|
(element) => DateFormat('MMMM, y').format(element.createdAt.toLocal()),
|
2022-07-13 05:23:48 -07:00
|
|
|
);
|
2022-04-23 19:08:45 -07:00
|
|
|
});
|