2022-11-08 19:00:24 +02:00
|
|
|
import 'dart:collection';
|
|
|
|
|
2022-11-26 18:16:02 +02:00
|
|
|
import 'package:hive/hive.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-11-26 18:16:02 +02:00
|
|
|
import 'package:immich_mobile/constants/hive_box.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
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';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:immich_mobile/shared/models/asset.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-12-02 22:55:10 +02:00
|
|
|
import 'package:immich_mobile/utils/tuple.dart';
|
2022-02-13 23:10:42 +02:00
|
|
|
import 'package:intl/intl.dart';
|
2022-11-27 22:34:19 +02:00
|
|
|
import 'package:logging/logging.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-11-08 19:00:24 +02:00
|
|
|
class AssetNotifier extends StateNotifier<List<Asset>> {
|
2022-06-25 20:46:51 +02:00
|
|
|
final AssetService _assetService;
|
2022-10-14 23:57:55 +02:00
|
|
|
final AssetCacheService _assetCacheService;
|
2022-11-27 22:34:19 +02:00
|
|
|
final log = Logger('AssetNotifier');
|
2022-02-13 23:10:42 +02:00
|
|
|
final DeviceInfoService _deviceInfoService = DeviceInfoService();
|
2022-11-08 19:00:24 +02:00
|
|
|
bool _getAllAssetInProgress = false;
|
|
|
|
bool _deleteInProgress = false;
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2022-10-14 23:57:55 +02:00
|
|
|
AssetNotifier(this._assetService, this._assetCacheService) : super([]);
|
2022-02-07 04:31:32 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-02-13 23:10:42 +02:00
|
|
|
getAllAsset() async {
|
2022-11-08 19:00:24 +02:00
|
|
|
if (_getAllAssetInProgress || _deleteInProgress) {
|
|
|
|
// guard against multiple calls to this method while it's still working
|
|
|
|
return;
|
|
|
|
}
|
2022-10-16 09:50:31 +02:00
|
|
|
final stopwatch = Stopwatch();
|
2022-11-08 19:00:24 +02:00
|
|
|
try {
|
|
|
|
_getAllAssetInProgress = true;
|
|
|
|
final bool isCacheValid = await _assetCacheService.isValid();
|
2022-11-26 18:16:02 +02:00
|
|
|
stopwatch.start();
|
2022-12-02 22:55:10 +02:00
|
|
|
final Box box = Hive.box(userInfoBox);
|
2022-11-26 18:16:02 +02:00
|
|
|
final localTask = _assetService.getLocalAssets(urgent: !isCacheValid);
|
2022-12-02 22:55:10 +02:00
|
|
|
final remoteTask = _assetService.getRemoteAssets(
|
|
|
|
etag: isCacheValid ? box.get(assetEtagKey) : null,
|
|
|
|
);
|
2022-11-08 19:00:24 +02:00
|
|
|
if (isCacheValid && state.isEmpty) {
|
|
|
|
state = await _assetCacheService.get();
|
2022-11-27 22:34:19 +02:00
|
|
|
log.info(
|
2022-11-21 14:13:14 +02:00
|
|
|
"Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms",
|
|
|
|
);
|
2022-11-08 19:00:24 +02:00
|
|
|
stopwatch.reset();
|
|
|
|
}
|
2022-10-16 09:50:31 +02:00
|
|
|
|
2022-11-26 18:16:02 +02:00
|
|
|
int remoteBegin = state.indexWhere((a) => a.isRemote);
|
|
|
|
remoteBegin = remoteBegin == -1 ? state.length : remoteBegin;
|
|
|
|
final List<Asset> currentLocal = state.slice(0, remoteBegin);
|
2022-12-02 22:55:10 +02:00
|
|
|
final Pair<List<Asset>?, String?> remoteResult = await remoteTask;
|
|
|
|
List<Asset>? newRemote = remoteResult.first;
|
2022-11-26 18:16:02 +02:00
|
|
|
List<Asset>? newLocal = await localTask;
|
2022-11-27 22:34:19 +02:00
|
|
|
log.info("Load assets: ${stopwatch.elapsedMilliseconds}ms");
|
2022-10-16 09:50:31 +02:00
|
|
|
stopwatch.reset();
|
2022-11-26 18:16:02 +02:00
|
|
|
if (newRemote == null &&
|
|
|
|
(newLocal == null || currentLocal.equals(newLocal))) {
|
2022-11-27 22:34:19 +02:00
|
|
|
log.info("state is already up-to-date");
|
2022-11-26 18:16:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
newRemote ??= state.slice(remoteBegin);
|
|
|
|
newLocal ??= [];
|
|
|
|
state = _combineLocalAndRemoteAssets(local: newLocal, remote: newRemote);
|
2022-11-27 22:34:19 +02:00
|
|
|
log.info("Combining assets: ${stopwatch.elapsedMilliseconds}ms");
|
2022-12-02 22:55:10 +02:00
|
|
|
|
|
|
|
stopwatch.reset();
|
|
|
|
_cacheState();
|
|
|
|
box.put(assetEtagKey, remoteResult.second);
|
|
|
|
log.info("Store assets in cache: ${stopwatch.elapsedMilliseconds}ms");
|
2022-11-08 19:00:24 +02:00
|
|
|
} finally {
|
|
|
|
_getAllAssetInProgress = false;
|
2022-10-14 23:57:55 +02:00
|
|
|
}
|
2022-11-26 18:16:02 +02: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 18:06:44 +02:00
|
|
|
}
|
|
|
|
|
2022-02-13 23:10:42 +02:00
|
|
|
clearAllAsset() {
|
|
|
|
state = [];
|
2022-10-15 23:20:15 +02:00
|
|
|
_cacheState();
|
2022-02-13 23:10:42 +02:00
|
|
|
}
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
onNewAssetUploaded(AssetResponseDto newAsset) {
|
2022-11-08 19:00:24 +02: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 23:20:15 +02:00
|
|
|
_cacheState();
|
2022-02-14 18:40:41 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 19:00:24 +02: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 23:10:42 +02:00
|
|
|
var deviceInfo = await _deviceInfoService.getDeviceInfo();
|
|
|
|
var deviceId = deviceInfo["deviceId"];
|
2022-11-08 19:00:24 +02:00
|
|
|
final List<String> local = [];
|
2022-02-13 23:10:42 +02:00
|
|
|
// Delete asset from device
|
2022-11-08 19:00:24 +02: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-23 06:14:14 +02:00
|
|
|
var localAsset = await AssetEntity.fromId(asset.deviceAssetId);
|
2022-02-13 23:10:42 +02:00
|
|
|
if (localAsset != null) {
|
2022-11-08 19:00:24 +02:00
|
|
|
local.add(localAsset.id);
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 19:00:24 +02:00
|
|
|
if (local.isNotEmpty) {
|
|
|
|
try {
|
|
|
|
return await PhotoManager.editor.deleteWithIds(local);
|
2022-11-27 22:34:19 +02:00
|
|
|
} catch (e, stack) {
|
|
|
|
log.severe("Failed to delete asset from device", e, stack);
|
2022-02-07 04:31:32 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-08 19:00:24 +02:00
|
|
|
return [];
|
|
|
|
}
|
2022-10-15 23:20:15 +02:00
|
|
|
|
2022-11-08 19:00:24 +02: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-07 04:31:32 +02:00
|
|
|
}
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 19:00:24 +02:00
|
|
|
final assetProvider = StateNotifierProvider<AssetNotifier, List<Asset>>((ref) {
|
2022-10-14 23:57:55 +02:00
|
|
|
return AssetNotifier(
|
2022-11-21 14:13:14 +02:00
|
|
|
ref.watch(assetServiceProvider),
|
|
|
|
ref.watch(assetCacheServiceProvider),
|
|
|
|
);
|
2022-02-03 18:06:44 +02:00
|
|
|
});
|
2022-02-13 23:10:42 +02:00
|
|
|
|
|
|
|
final assetGroupByDateTimeProvider = StateProvider((ref) {
|
2022-11-08 19:00:24 +02: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 23:10:42 +02:00
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
assets.sortByCompare<DateTime>(
|
2022-11-08 19:00:24 +02:00
|
|
|
(e) => e.createdAt,
|
2022-07-13 14:23:48 +02:00
|
|
|
(a, b) => b.compareTo(a),
|
|
|
|
);
|
|
|
|
return assets.groupListsBy(
|
2022-11-08 19:00:24 +02:00
|
|
|
(element) => DateFormat('y-MM-dd').format(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) {
|
2022-11-08 19:00:24 +02: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-24 04:08:45 +02:00
|
|
|
|
2022-06-22 07:23:35 +02:00
|
|
|
assets.sortByCompare<DateTime>(
|
2022-11-08 19:00:24 +02:00
|
|
|
(e) => e.createdAt,
|
2022-07-13 14:23:48 +02:00
|
|
|
(a, b) => b.compareTo(a),
|
|
|
|
);
|
|
|
|
|
|
|
|
return assets.groupListsBy(
|
2022-11-08 19:00:24 +02:00
|
|
|
(element) => DateFormat('MMMM, y').format(element.createdAt.toLocal()),
|
2022-07-13 14:23:48 +02:00
|
|
|
);
|
2022-04-24 04:08:45 +02:00
|
|
|
});
|