2022-11-08 10:00:24 -07:00
|
|
|
import 'dart:collection';
|
|
|
|
|
2023-01-18 08:59:23 -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';
|
2023-02-09 10:32:08 -07:00
|
|
|
import 'package:immich_mobile/shared/models/store.dart';
|
2023-02-04 13:42:42 -07:00
|
|
|
import 'package:immich_mobile/shared/services/asset.service.dart';
|
|
|
|
import 'package:immich_mobile/shared/services/asset_cache.service.dart';
|
2023-01-18 08:59:23 -07:00
|
|
|
import 'package:immich_mobile/modules/home/ui/asset_grid/asset_grid_data_structure.dart';
|
|
|
|
import 'package:immich_mobile/modules/settings/providers/app_settings.provider.dart';
|
|
|
|
import 'package:immich_mobile/modules/settings/services/app_settings.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-12-02 13:55:10 -07:00
|
|
|
import 'package:immich_mobile/utils/tuple.dart';
|
2022-02-13 14:10:42 -07:00
|
|
|
import 'package:intl/intl.dart';
|
2022-11-27 13:34:19 -07:00
|
|
|
import 'package:logging/logging.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
|
|
|
|
2023-01-18 08:59:23 -07:00
|
|
|
class AssetsState {
|
|
|
|
final List<Asset> allAssets;
|
|
|
|
final RenderList? renderList;
|
|
|
|
|
|
|
|
AssetsState(this.allAssets, {this.renderList});
|
|
|
|
|
|
|
|
Future<AssetsState> withRenderDataStructure(int groupSize) async {
|
|
|
|
return AssetsState(
|
|
|
|
allAssets,
|
|
|
|
renderList:
|
|
|
|
await RenderList.fromAssetGroups(await _groupByDate(), groupSize),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
AssetsState withAdditionalAssets(List<Asset> toAdd) {
|
|
|
|
return AssetsState([...allAssets, ...toAdd]);
|
|
|
|
}
|
|
|
|
|
2023-02-04 13:42:42 -07:00
|
|
|
Future<Map<String, List<Asset>>> _groupByDate() async {
|
2023-01-18 08:59:23 -07:00
|
|
|
sortCompare(List<Asset> assets) {
|
|
|
|
assets.sortByCompare<DateTime>(
|
|
|
|
(e) => e.createdAt,
|
|
|
|
(a, b) => b.compareTo(a),
|
|
|
|
);
|
|
|
|
return assets.groupListsBy(
|
|
|
|
(element) => DateFormat('y-MM-dd').format(element.createdAt.toLocal()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await compute(sortCompare, allAssets.toList());
|
|
|
|
}
|
|
|
|
|
2023-02-04 13:42:42 -07:00
|
|
|
static AssetsState fromAssetList(List<Asset> assets) {
|
2023-01-18 08:59:23 -07:00
|
|
|
return AssetsState(assets);
|
|
|
|
}
|
|
|
|
|
2023-02-04 13:42:42 -07:00
|
|
|
static AssetsState empty() {
|
2023-01-18 08:59:23 -07:00
|
|
|
return AssetsState([]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CombineAssetsComputeParameters {
|
|
|
|
final Iterable<Asset> local;
|
|
|
|
final Iterable<Asset> remote;
|
|
|
|
final String deviceId;
|
|
|
|
|
|
|
|
_CombineAssetsComputeParameters(this.local, this.remote, this.deviceId);
|
|
|
|
}
|
|
|
|
|
|
|
|
class AssetNotifier extends StateNotifier<AssetsState> {
|
2022-06-25 11:46:51 -07:00
|
|
|
final AssetService _assetService;
|
2022-10-14 14:57:55 -07:00
|
|
|
final AssetCacheService _assetCacheService;
|
2023-01-18 08:59:23 -07:00
|
|
|
final AppSettingsService _settingsService;
|
2022-11-27 13:34:19 -07:00
|
|
|
final log = Logger('AssetNotifier');
|
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
|
|
|
|
2023-01-18 08:59:23 -07:00
|
|
|
AssetNotifier(
|
|
|
|
this._assetService,
|
|
|
|
this._assetCacheService,
|
|
|
|
this._settingsService,
|
|
|
|
) : super(AssetsState.fromAssetList([]));
|
|
|
|
|
2023-02-04 13:42:42 -07:00
|
|
|
Future<void> _updateAssetsState(
|
|
|
|
List<Asset> newAssetList, {
|
|
|
|
bool cache = true,
|
|
|
|
}) async {
|
2023-01-18 08:59:23 -07:00
|
|
|
if (cache) {
|
|
|
|
_assetCacheService.put(newAssetList);
|
|
|
|
}
|
2022-02-06 19:31:32 -07:00
|
|
|
|
2023-01-18 08:59:23 -07:00
|
|
|
state =
|
|
|
|
await AssetsState.fromAssetList(newAssetList).withRenderDataStructure(
|
|
|
|
_settingsService.getSetting(AppSettingsEnum.tilesPerRow),
|
|
|
|
);
|
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;
|
2023-02-04 13:42:42 -07:00
|
|
|
bool isCacheValid = await _assetCacheService.isValid();
|
2022-11-26 09:16:02 -07:00
|
|
|
stopwatch.start();
|
2023-02-04 13:42:42 -07:00
|
|
|
if (isCacheValid && state.allAssets.isEmpty) {
|
|
|
|
final List<Asset>? cachedData = await _assetCacheService.get();
|
|
|
|
if (cachedData == null) {
|
|
|
|
isCacheValid = false;
|
|
|
|
log.warning("Cached asset data is invalid, fetching new data");
|
|
|
|
} else {
|
|
|
|
await _updateAssetsState(cachedData, cache: false);
|
|
|
|
log.info(
|
|
|
|
"Reading assets ${state.allAssets.length} from cache: ${stopwatch.elapsedMilliseconds}ms",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
stopwatch.reset();
|
|
|
|
}
|
2022-11-26 09:16:02 -07:00
|
|
|
final localTask = _assetService.getLocalAssets(urgent: !isCacheValid);
|
2022-12-02 13:55:10 -07:00
|
|
|
final remoteTask = _assetService.getRemoteAssets(
|
2023-02-09 10:32:08 -07:00
|
|
|
etag: isCacheValid ? Store.get(StoreKey.assetETag) : null,
|
2022-12-02 13:55:10 -07:00
|
|
|
);
|
2022-10-16 00:50:31 -07:00
|
|
|
|
2023-01-18 08:59:23 -07:00
|
|
|
int remoteBegin = state.allAssets.indexWhere((a) => a.isRemote);
|
|
|
|
remoteBegin = remoteBegin == -1 ? state.allAssets.length : remoteBegin;
|
|
|
|
|
|
|
|
final List<Asset> currentLocal = state.allAssets.slice(0, remoteBegin);
|
|
|
|
|
2022-12-02 13:55:10 -07:00
|
|
|
final Pair<List<Asset>?, String?> remoteResult = await remoteTask;
|
|
|
|
List<Asset>? newRemote = remoteResult.first;
|
2022-11-26 09:16:02 -07:00
|
|
|
List<Asset>? newLocal = await localTask;
|
2022-11-27 13:34:19 -07:00
|
|
|
log.info("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))) {
|
2022-11-27 13:34:19 -07:00
|
|
|
log.info("state is already up-to-date");
|
2022-11-26 09:16:02 -07:00
|
|
|
return;
|
|
|
|
}
|
2023-01-18 08:59:23 -07:00
|
|
|
newRemote ??= state.allAssets.slice(remoteBegin);
|
2022-11-26 09:16:02 -07:00
|
|
|
newLocal ??= [];
|
2023-01-18 08:59:23 -07:00
|
|
|
|
|
|
|
final combinedAssets = await _combineLocalAndRemoteAssets(
|
|
|
|
local: newLocal,
|
|
|
|
remote: newRemote,
|
|
|
|
);
|
|
|
|
await _updateAssetsState(combinedAssets);
|
|
|
|
|
2022-11-27 13:34:19 -07:00
|
|
|
log.info("Combining assets: ${stopwatch.elapsedMilliseconds}ms");
|
2022-12-02 13:55:10 -07:00
|
|
|
|
2023-02-09 10:32:08 -07:00
|
|
|
Store.put(StoreKey.assetETag, remoteResult.second);
|
2022-11-08 10:00:24 -07:00
|
|
|
} finally {
|
|
|
|
_getAllAssetInProgress = false;
|
2022-10-14 14:57:55 -07:00
|
|
|
}
|
2022-11-26 09:16:02 -07:00
|
|
|
}
|
|
|
|
|
2023-01-18 08:59:23 -07:00
|
|
|
static Future<List<Asset>> _computeCombine(
|
|
|
|
_CombineAssetsComputeParameters data,
|
|
|
|
) async {
|
|
|
|
var local = data.local;
|
|
|
|
var remote = data.remote;
|
|
|
|
final deviceId = data.deviceId;
|
|
|
|
|
2022-11-26 09:16:02 -07:00
|
|
|
final List<Asset> assets = [];
|
|
|
|
if (remote.isNotEmpty && local.isNotEmpty) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-18 08:59:23 -07:00
|
|
|
Future<List<Asset>> _combineLocalAndRemoteAssets({
|
|
|
|
required Iterable<Asset> local,
|
|
|
|
required List<Asset> remote,
|
|
|
|
}) async {
|
|
|
|
final String deviceId = Hive.box(userInfoBox).get(deviceIdKey);
|
|
|
|
return await compute(
|
|
|
|
_computeCombine,
|
|
|
|
_CombineAssetsComputeParameters(local, remote, deviceId),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-13 14:10:42 -07:00
|
|
|
clearAllAsset() {
|
2023-01-18 08:59:23 -07:00
|
|
|
_updateAssetsState([]);
|
2022-02-13 14:10:42 -07:00
|
|
|
}
|
2022-02-03 09:06:44 -07:00
|
|
|
|
2023-02-04 13:42:42 -07:00
|
|
|
void onNewAssetUploaded(Asset newAsset) {
|
2023-01-18 08:59:23 -07:00
|
|
|
final int i = state.allAssets.indexWhere(
|
2022-11-08 10:00:24 -07:00
|
|
|
(a) =>
|
|
|
|
a.isRemote ||
|
|
|
|
(a.id == newAsset.deviceAssetId && a.deviceId == newAsset.deviceId),
|
|
|
|
);
|
|
|
|
|
2023-01-18 08:59:23 -07:00
|
|
|
if (i == -1 || state.allAssets[i].deviceAssetId != newAsset.deviceAssetId) {
|
2023-02-04 13:42:42 -07:00
|
|
|
_updateAssetsState([...state.allAssets, newAsset]);
|
2022-11-08 10:00:24 -07:00
|
|
|
} else {
|
|
|
|
// order is important to keep all local-only assets at the beginning!
|
2023-01-18 08:59:23 -07:00
|
|
|
_updateAssetsState([
|
|
|
|
...state.allAssets.slice(0, i),
|
|
|
|
...state.allAssets.slice(i + 1),
|
2023-02-04 13:42:42 -07:00
|
|
|
newAsset,
|
2023-01-18 08:59:23 -07:00
|
|
|
]);
|
2022-11-08 10:00:24 -07:00
|
|
|
// 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-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) {
|
2023-01-18 08:59:23 -07:00
|
|
|
_updateAssetsState(
|
|
|
|
state.allAssets.where((a) => !deleted.contains(a.id)).toList(),
|
|
|
|
);
|
2022-11-08 10:00:24 -07:00
|
|
|
}
|
|
|
|
} 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) {
|
2023-02-04 13:42:42 -07:00
|
|
|
local.add(asset.localId!);
|
2022-11-08 10:00:24 -07:00
|
|
|
} 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);
|
2022-11-27 13:34:19 -07:00
|
|
|
} catch (e, stack) {
|
|
|
|
log.severe("Failed to delete asset from device", e, stack);
|
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 {
|
2023-02-04 13:42:42 -07:00
|
|
|
final Iterable<Asset> remote = assetsToDelete.where((e) => e.isRemote);
|
2022-11-08 10:00:24 -07:00
|
|
|
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
|
|
|
}
|
2023-02-04 20:25:11 -07:00
|
|
|
|
|
|
|
Future<bool> toggleFavorite(Asset asset, bool status) async {
|
|
|
|
final newAsset = await _assetService.changeFavoriteStatus(asset, status);
|
|
|
|
|
|
|
|
if (newAsset == null) {
|
|
|
|
log.severe("Change favorite status failed for asset ${asset.id}");
|
|
|
|
return asset.isFavorite;
|
|
|
|
}
|
|
|
|
|
2023-02-06 05:59:56 -07:00
|
|
|
final index = state.allAssets.indexWhere((a) => asset.id == a.id);
|
|
|
|
if (index > 0) {
|
2023-02-09 10:32:08 -07:00
|
|
|
state.allAssets[index] = newAsset;
|
2023-02-06 05:59:56 -07:00
|
|
|
_updateAssetsState(state.allAssets);
|
|
|
|
}
|
2023-02-04 20:25:11 -07:00
|
|
|
|
|
|
|
return newAsset.isFavorite;
|
|
|
|
}
|
2022-02-03 09:06:44 -07:00
|
|
|
}
|
|
|
|
|
2023-01-18 08:59:23 -07:00
|
|
|
final assetProvider = StateNotifierProvider<AssetNotifier, AssetsState>((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),
|
2023-01-18 08:59:23 -07:00
|
|
|
ref.watch(appSettingsServiceProvider),
|
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
|
2023-01-18 08:59:23 -07:00
|
|
|
final assets =
|
|
|
|
ref.watch(assetProvider).allAssets.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
|
|
|
});
|