2023-04-16 22:02:07 -07:00
|
|
|
import 'package:flutter/material.dart';
|
2022-02-03 09:06:44 -07:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2023-03-03 15:38:30 -07:00
|
|
|
import 'package:immich_mobile/modules/album/services/album.service.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/exif_info.dart';
|
2023-02-09 10:32:08 -07:00
|
|
|
import 'package:immich_mobile/shared/models/store.dart';
|
2023-03-03 15:38:30 -07:00
|
|
|
import 'package:immich_mobile/shared/models/user.dart';
|
|
|
|
import 'package:immich_mobile/shared/providers/db.provider.dart';
|
2023-02-04 13:42:42 -07:00
|
|
|
import 'package:immich_mobile/shared/services/asset.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-06 19:31:32 -07:00
|
|
|
import 'package:collection/collection.dart';
|
2023-03-26 19:35:52 -07:00
|
|
|
import 'package:immich_mobile/shared/services/sync.service.dart';
|
|
|
|
import 'package:immich_mobile/utils/async_mutex.dart';
|
|
|
|
import 'package:immich_mobile/utils/db.dart';
|
2022-02-13 14:10:42 -07:00
|
|
|
import 'package:intl/intl.dart';
|
2023-03-03 15:38:30 -07:00
|
|
|
import 'package:isar/isar.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-04-16 22:02:07 -07:00
|
|
|
/// State does not contain archived assets.
|
|
|
|
/// Use database provider if you want to access the isArchived assets
|
2023-01-18 08:59:23 -07:00
|
|
|
class AssetsState {
|
|
|
|
final List<Asset> allAssets;
|
|
|
|
final RenderList? renderList;
|
|
|
|
|
|
|
|
AssetsState(this.allAssets, {this.renderList});
|
|
|
|
|
2023-02-09 10:35:44 -07:00
|
|
|
Future<AssetsState> withRenderDataStructure(
|
|
|
|
AssetGridLayoutParameters layout,
|
|
|
|
) async {
|
2023-01-18 08:59:23 -07:00
|
|
|
return AssetsState(
|
|
|
|
allAssets,
|
2023-02-09 10:35:44 -07:00
|
|
|
renderList: await RenderList.fromAssets(
|
|
|
|
allAssets,
|
|
|
|
layout,
|
|
|
|
),
|
2023-01-18 08:59:23 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
AssetsState withAdditionalAssets(List<Asset> toAdd) {
|
|
|
|
return AssetsState([...allAssets, ...toAdd]);
|
|
|
|
}
|
|
|
|
|
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 AssetNotifier extends StateNotifier<AssetsState> {
|
2022-06-25 11:46:51 -07:00
|
|
|
final AssetService _assetService;
|
2023-01-18 08:59:23 -07:00
|
|
|
final AppSettingsService _settingsService;
|
2023-03-03 15:38:30 -07:00
|
|
|
final AlbumService _albumService;
|
2023-03-26 19:35:52 -07:00
|
|
|
final SyncService _syncService;
|
2023-03-03 15:38:30 -07:00
|
|
|
final Isar _db;
|
2022-11-27 13:34:19 -07:00
|
|
|
final log = Logger('AssetNotifier');
|
2022-11-08 10:00:24 -07:00
|
|
|
bool _getAllAssetInProgress = false;
|
|
|
|
bool _deleteInProgress = false;
|
2023-03-26 19:35:52 -07:00
|
|
|
final AsyncMutex _stateUpdateLock = AsyncMutex();
|
2022-02-03 09:06:44 -07:00
|
|
|
|
2023-01-18 08:59:23 -07:00
|
|
|
AssetNotifier(
|
|
|
|
this._assetService,
|
|
|
|
this._settingsService,
|
2023-03-03 15:38:30 -07:00
|
|
|
this._albumService,
|
2023-03-26 19:35:52 -07:00
|
|
|
this._syncService,
|
2023-03-03 15:38:30 -07:00
|
|
|
this._db,
|
2023-01-18 08:59:23 -07:00
|
|
|
) : super(AssetsState.fromAssetList([]));
|
|
|
|
|
2023-03-03 15:38:30 -07:00
|
|
|
Future<void> _updateAssetsState(List<Asset> newAssetList) async {
|
2023-02-09 10:35:44 -07:00
|
|
|
final layout = AssetGridLayoutParameters(
|
2023-01-18 08:59:23 -07:00
|
|
|
_settingsService.getSetting(AppSettingsEnum.tilesPerRow),
|
2023-02-09 10:35:44 -07:00
|
|
|
_settingsService.getSetting(AppSettingsEnum.dynamicLayout),
|
2023-03-03 15:38:30 -07:00
|
|
|
GroupAssetsBy
|
|
|
|
.values[_settingsService.getSetting(AppSettingsEnum.groupAssetsBy)],
|
2023-01-18 08:59:23 -07:00
|
|
|
);
|
2023-04-16 22:02:07 -07:00
|
|
|
|
2023-02-09 10:35:44 -07:00
|
|
|
state = await AssetsState.fromAssetList(newAssetList)
|
|
|
|
.withRenderDataStructure(layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just a little helper to trigger a rebuild of the state object
|
|
|
|
Future<void> rebuildAssetGridDataStructure() async {
|
2023-03-03 15:38:30 -07:00
|
|
|
await _updateAssetsState(state.allAssets);
|
2022-10-15 14:20:15 -07:00
|
|
|
}
|
|
|
|
|
2023-03-26 19:35:52 -07:00
|
|
|
Future<void> getAllAsset({bool clear = false}) 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;
|
|
|
|
}
|
2023-03-26 19:35:52 -07:00
|
|
|
final stopwatch = Stopwatch()..start();
|
2022-11-08 10:00:24 -07:00
|
|
|
try {
|
|
|
|
_getAllAssetInProgress = true;
|
2023-03-03 15:38:30 -07:00
|
|
|
final User me = Store.get(StoreKey.currentUser);
|
2023-03-26 19:35:52 -07:00
|
|
|
if (clear) {
|
|
|
|
await clearAssetsAndAlbums(_db);
|
|
|
|
log.info("Manual refresh requested, cleared assets and albums from db");
|
|
|
|
} else if (_stateUpdateLock.enqueued <= 1) {
|
2023-04-18 02:47:24 -07:00
|
|
|
final int cachedCount = await _userAssetQuery(me.isarId).count();
|
2023-03-26 19:35:52 -07:00
|
|
|
if (cachedCount > 0 && cachedCount != state.allAssets.length) {
|
|
|
|
await _stateUpdateLock.run(
|
|
|
|
() async => _updateAssetsState(await _getUserAssets(me.isarId)),
|
|
|
|
);
|
|
|
|
log.info(
|
|
|
|
"Reading assets ${state.allAssets.length} from DB: ${stopwatch.elapsedMilliseconds}ms",
|
|
|
|
);
|
|
|
|
stopwatch.reset();
|
|
|
|
}
|
2023-02-04 13:42:42 -07:00
|
|
|
}
|
2023-03-03 15:38:30 -07:00
|
|
|
final bool newRemote = await _assetService.refreshRemoteAssets();
|
|
|
|
final bool newLocal = await _albumService.refreshDeviceAlbums();
|
2023-04-16 22:02:07 -07:00
|
|
|
debugPrint("newRemote: $newRemote, newLocal: $newLocal");
|
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();
|
2023-03-15 14:29:07 -07:00
|
|
|
if (!newRemote &&
|
|
|
|
!newLocal &&
|
2023-04-18 02:47:24 -07:00
|
|
|
state.allAssets.length == await _userAssetQuery(me.isarId).count()) {
|
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-03-03 15:38:30 -07:00
|
|
|
stopwatch.reset();
|
2023-03-26 19:35:52 -07:00
|
|
|
if (_stateUpdateLock.enqueued <= 1) {
|
|
|
|
_stateUpdateLock.run(() async {
|
|
|
|
final assets = await _getUserAssets(me.isarId);
|
|
|
|
if (!const ListEquality().equals(assets, state.allAssets)) {
|
|
|
|
log.info("setting new asset state");
|
|
|
|
await _updateAssetsState(assets);
|
|
|
|
}
|
|
|
|
});
|
2023-03-03 15:38:30 -07:00
|
|
|
}
|
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-04-18 02:47:24 -07:00
|
|
|
Future<List<Asset>> _getUserAssets(int userId) =>
|
|
|
|
_userAssetQuery(userId).sortByFileCreatedAtDesc().findAll();
|
|
|
|
|
|
|
|
QueryBuilder<Asset, Asset, QAfterFilterCondition> _userAssetQuery(
|
|
|
|
int userId,
|
|
|
|
) =>
|
|
|
|
_db.assets.filter().ownerIdEqualTo(userId).isArchivedEqualTo(false);
|
2023-03-03 15:38:30 -07:00
|
|
|
|
|
|
|
Future<void> clearAllAsset() {
|
|
|
|
state = AssetsState.empty();
|
2023-03-26 19:35:52 -07:00
|
|
|
return clearAssetsAndAlbums(_db);
|
2022-02-13 14:10:42 -07:00
|
|
|
}
|
2022-02-03 09:06:44 -07:00
|
|
|
|
2023-03-03 15:38:30 -07:00
|
|
|
Future<void> onNewAssetUploaded(Asset newAsset) async {
|
2023-03-26 19:35:52 -07:00
|
|
|
final bool ok = await _syncService.syncNewAssetToDb(newAsset);
|
|
|
|
if (ok && _stateUpdateLock.enqueued <= 1) {
|
|
|
|
// run this sequentially if there is at most 1 other task waiting
|
|
|
|
await _stateUpdateLock.run(() async {
|
|
|
|
final userId = Store.get(StoreKey.currentUser).isarId;
|
|
|
|
final assets = await _getUserAssets(userId);
|
|
|
|
await _updateAssetsState(assets);
|
|
|
|
});
|
2022-11-08 10:00:24 -07:00
|
|
|
}
|
2022-02-14 09:40:41 -07:00
|
|
|
}
|
|
|
|
|
2023-03-03 15:38:30 -07:00
|
|
|
Future<void> deleteAssets(Set<Asset> deleteAssets) async {
|
2022-11-08 10:00:24 -07:00
|
|
|
_deleteInProgress = true;
|
|
|
|
try {
|
2023-03-03 15:38:30 -07:00
|
|
|
_updateAssetsState(
|
|
|
|
state.allAssets.whereNot(deleteAssets.contains).toList(),
|
|
|
|
);
|
2022-11-08 10:00:24 -07:00
|
|
|
final localDeleted = await _deleteLocalAssets(deleteAssets);
|
|
|
|
final remoteDeleted = await _deleteRemoteAssets(deleteAssets);
|
2023-03-03 15:38:30 -07:00
|
|
|
if (localDeleted.isNotEmpty || remoteDeleted.isNotEmpty) {
|
|
|
|
final dbIds = deleteAssets.map((e) => e.id).toList();
|
|
|
|
await _db.writeTxn(() async {
|
|
|
|
await _db.exifInfos.deleteAll(dbIds);
|
|
|
|
await _db.assets.deleteAll(dbIds);
|
|
|
|
});
|
2022-11-08 10:00:24 -07:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
_deleteInProgress = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<String>> _deleteLocalAssets(Set<Asset> assetsToDelete) async {
|
2023-03-03 15:38:30 -07:00
|
|
|
final int deviceId = Store.get(StoreKey.deviceIdHash);
|
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-03-03 15:38:30 -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
|
2023-03-03 15:38:30 -07:00
|
|
|
var localAsset = await AssetEntity.fromId(asset.localId);
|
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 {
|
2023-03-03 15:38:30 -07:00
|
|
|
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);
|
2023-04-16 22:02:07 -07:00
|
|
|
if (index != -1) {
|
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;
|
|
|
|
}
|
2023-04-16 22:02:07 -07:00
|
|
|
|
|
|
|
Future<void> toggleArchive(Iterable<Asset> assets, bool status) async {
|
|
|
|
final newAssets = await Future.wait(
|
|
|
|
assets.map((a) => _assetService.changeArchiveStatus(a, status)),
|
|
|
|
);
|
|
|
|
int i = 0;
|
|
|
|
bool unArchived = false;
|
|
|
|
for (Asset oldAsset in assets) {
|
|
|
|
final newAsset = newAssets[i++];
|
|
|
|
if (newAsset == null) {
|
|
|
|
log.severe("Change archive status failed for asset ${oldAsset.id}");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final index = state.allAssets.indexWhere((a) => oldAsset.id == a.id);
|
|
|
|
if (newAsset.isArchived) {
|
|
|
|
// remove from state
|
|
|
|
if (index != -1) {
|
|
|
|
state.allAssets.removeAt(index);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// add to state is difficult because the list is sorted
|
|
|
|
unArchived = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (unArchived) {
|
|
|
|
final User me = Store.get(StoreKey.currentUser);
|
|
|
|
await _stateUpdateLock.run(
|
|
|
|
() async => _updateAssetsState(await _getUserAssets(me.isarId)),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
_updateAssetsState(state.allAssets);
|
|
|
|
}
|
|
|
|
}
|
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),
|
2023-01-18 08:59:23 -07:00
|
|
|
ref.watch(appSettingsServiceProvider),
|
2023-03-03 15:38:30 -07:00
|
|
|
ref.watch(albumServiceProvider),
|
2023-03-26 19:35:52 -07:00
|
|
|
ref.watch(syncServiceProvider),
|
2023-03-03 15:38:30 -07:00
|
|
|
ref.watch(dbProvider),
|
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>(
|
2023-02-19 09:44:53 -07:00
|
|
|
(e) => e.fileCreatedAt,
|
2022-07-13 05:23:48 -07:00
|
|
|
(a, b) => b.compareTo(a),
|
|
|
|
);
|
|
|
|
|
|
|
|
return assets.groupListsBy(
|
2023-02-19 09:44:53 -07:00
|
|
|
(element) => DateFormat('MMMM, y').format(element.fileCreatedAt.toLocal()),
|
2022-07-13 05:23:48 -07:00
|
|
|
);
|
2022-04-23 19:08:45 -07:00
|
|
|
});
|