2023-10-06 09:01:14 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:immich_mobile/modules/home/ui/asset_grid/asset_grid_data_structure.dart';
|
2024-05-02 22:59:14 +02:00
|
|
|
import 'package:immich_mobile/services/trash.service.dart';
|
2024-05-01 04:36:40 +02:00
|
|
|
import 'package:immich_mobile/entities/asset.entity.dart';
|
2024-05-02 22:59:14 +02:00
|
|
|
import 'package:immich_mobile/providers/asset.provider.dart';
|
|
|
|
import 'package:immich_mobile/providers/db.provider.dart';
|
|
|
|
import 'package:immich_mobile/providers/user.provider.dart';
|
|
|
|
import 'package:immich_mobile/services/sync.service.dart';
|
2023-11-13 17:54:41 +02:00
|
|
|
import 'package:immich_mobile/utils/renderlist_generator.dart';
|
2023-10-06 09:01:14 +02:00
|
|
|
import 'package:isar/isar.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
|
|
|
class TrashNotifier extends StateNotifier<bool> {
|
|
|
|
final Isar _db;
|
|
|
|
final Ref _ref;
|
|
|
|
final TrashService _trashService;
|
|
|
|
final _log = Logger('TrashNotifier');
|
|
|
|
|
|
|
|
TrashNotifier(
|
|
|
|
this._trashService,
|
|
|
|
this._db,
|
|
|
|
this._ref,
|
|
|
|
) : super(false);
|
|
|
|
|
|
|
|
Future<void> emptyTrash() async {
|
|
|
|
try {
|
|
|
|
final user = _ref.read(currentUserProvider);
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await _trashService.emptyTrash();
|
|
|
|
|
2023-10-25 18:02:59 +02:00
|
|
|
final idsToRemove = await _db.assets
|
2023-10-06 09:01:14 +02:00
|
|
|
.where()
|
|
|
|
.remoteIdIsNotNull()
|
|
|
|
.filter()
|
|
|
|
.ownerIdEqualTo(user.isarId)
|
|
|
|
.isTrashedEqualTo(true)
|
2023-10-25 18:02:59 +02:00
|
|
|
.remoteIdProperty()
|
2023-10-06 09:01:14 +02:00
|
|
|
.findAll();
|
|
|
|
|
2023-11-07 21:19:45 +02:00
|
|
|
// TODO: handle local asset removal on emptyTrash
|
2023-10-25 18:02:59 +02:00
|
|
|
_ref
|
|
|
|
.read(syncServiceProvider)
|
|
|
|
.handleRemoteAssetRemoval(idsToRemove.cast<String>().toList());
|
2023-10-06 09:01:14 +02:00
|
|
|
} catch (error, stack) {
|
2024-02-24 05:38:57 +02:00
|
|
|
_log.severe("Cannot empty trash", error, stack);
|
2023-10-06 09:01:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 22:55:19 +02:00
|
|
|
Future<bool> removeAssets(Iterable<Asset> assetList) async {
|
|
|
|
try {
|
|
|
|
final user = _ref.read(currentUserProvider);
|
|
|
|
if (user == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
final isRemoved = await _ref
|
|
|
|
.read(assetProvider.notifier)
|
|
|
|
.deleteRemoteOnlyAssets(assetList, force: true);
|
|
|
|
|
|
|
|
if (isRemoved) {
|
|
|
|
final idsToRemove =
|
|
|
|
assetList.where((a) => a.isRemote).map((a) => a.remoteId!).toList();
|
|
|
|
|
|
|
|
_ref
|
|
|
|
.read(syncServiceProvider)
|
|
|
|
.handleRemoteAssetRemoval(idsToRemove.cast<String>().toList());
|
|
|
|
}
|
|
|
|
|
|
|
|
return isRemoved;
|
|
|
|
} catch (error, stack) {
|
2024-02-24 05:38:57 +02:00
|
|
|
_log.severe("Cannot remove assets", error, stack);
|
2024-01-18 22:55:19 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-04-26 08:01:03 +02:00
|
|
|
Future<bool> restoreAsset(Asset asset) async {
|
|
|
|
try {
|
|
|
|
final result = await _trashService.restoreAsset(asset);
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
final remoteAsset = asset.isRemote;
|
|
|
|
|
|
|
|
asset.isTrashed = false;
|
|
|
|
|
|
|
|
if (remoteAsset) {
|
|
|
|
await _db.writeTxn(() async {
|
|
|
|
await _db.assets.put(asset);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch (error, stack) {
|
|
|
|
_log.severe("Cannot restore asset", error, stack);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-06 09:01:14 +02:00
|
|
|
Future<bool> restoreAssets(Iterable<Asset> assetList) async {
|
|
|
|
try {
|
|
|
|
final result = await _trashService.restoreAssets(assetList);
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
final remoteAssets = assetList.where((a) => a.isRemote).toList();
|
|
|
|
|
|
|
|
final updatedAssets = remoteAssets.map((e) {
|
|
|
|
e.isTrashed = false;
|
|
|
|
return e;
|
|
|
|
}).toList();
|
|
|
|
|
|
|
|
await _db.writeTxn(() async {
|
|
|
|
await _db.assets.putAll(updatedAssets);
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch (error, stack) {
|
2024-02-24 05:38:57 +02:00
|
|
|
_log.severe("Cannot restore assets", error, stack);
|
2023-10-06 09:01:14 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> restoreTrash() async {
|
|
|
|
try {
|
|
|
|
final user = _ref.read(currentUserProvider);
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await _trashService.restoreTrash();
|
|
|
|
|
|
|
|
final assets = await _db.assets
|
|
|
|
.where()
|
|
|
|
.remoteIdIsNotNull()
|
|
|
|
.filter()
|
|
|
|
.ownerIdEqualTo(user.isarId)
|
|
|
|
.isTrashedEqualTo(true)
|
|
|
|
.findAll();
|
|
|
|
|
|
|
|
final updatedAssets = assets.map((e) {
|
|
|
|
e.isTrashed = false;
|
|
|
|
return e;
|
|
|
|
}).toList();
|
|
|
|
|
|
|
|
await _db.writeTxn(() async {
|
|
|
|
await _db.assets.putAll(updatedAssets);
|
|
|
|
});
|
|
|
|
} catch (error, stack) {
|
2024-02-24 05:38:57 +02:00
|
|
|
_log.severe("Cannot restore trash", error, stack);
|
2023-10-06 09:01:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final trashProvider = StateNotifierProvider<TrashNotifier, bool>((ref) {
|
|
|
|
return TrashNotifier(
|
|
|
|
ref.watch(trashServiceProvider),
|
|
|
|
ref.watch(dbProvider),
|
|
|
|
ref,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-11-13 17:54:41 +02:00
|
|
|
final trashedAssetsProvider = StreamProvider<RenderList>((ref) {
|
2023-10-06 09:01:14 +02:00
|
|
|
final user = ref.read(currentUserProvider);
|
2023-11-13 17:54:41 +02:00
|
|
|
if (user == null) return const Stream.empty();
|
2023-10-06 09:01:14 +02:00
|
|
|
final query = ref
|
|
|
|
.watch(dbProvider)
|
|
|
|
.assets
|
|
|
|
.filter()
|
|
|
|
.ownerIdEqualTo(user.isarId)
|
|
|
|
.isTrashedEqualTo(true)
|
|
|
|
.sortByFileCreatedAt();
|
2023-11-13 17:54:41 +02:00
|
|
|
return renderListGeneratorWithGroupBy(query, GroupAssetsBy.none);
|
2023-10-06 09:01:14 +02:00
|
|
|
});
|