mirror of
https://github.com/immich-app/immich.git
synced 2025-07-08 18:54:18 -04:00
refactor: action provider (#19677)
Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
This commit is contained in:
parent
c435bdb5d3
commit
f2f3db3a79
@ -39,12 +39,19 @@ class ActionNotifier extends Notifier<void> {
|
||||
_service = ref.watch(actionServiceProvider);
|
||||
}
|
||||
|
||||
List<String> _getIdsForSource<T extends BaseAsset>(ActionSource source) {
|
||||
final currentUser = ref.read(currentUserProvider);
|
||||
if (T is RemoteAsset && currentUser == null) {
|
||||
return [];
|
||||
}
|
||||
List<String> _getRemoteIdsForSource(ActionSource source) {
|
||||
return _getIdsForSource<RemoteAsset>(source).toIds().toList();
|
||||
}
|
||||
|
||||
List<String> _getOwnedRemoteForSource(ActionSource source) {
|
||||
final ownerId = ref.read(currentUserProvider)?.id;
|
||||
return _getIdsForSource<RemoteAsset>(source)
|
||||
.ownedAssets(ownerId)
|
||||
.toIds()
|
||||
.toList();
|
||||
}
|
||||
|
||||
Iterable<T> _getIdsForSource<T extends BaseAsset>(ActionSource source) {
|
||||
final Set<BaseAsset> assets = switch (source) {
|
||||
ActionSource.timeline =>
|
||||
ref.read(multiSelectProvider.select((s) => s.selectedAssets)),
|
||||
@ -52,24 +59,17 @@ class ActionNotifier extends Notifier<void> {
|
||||
};
|
||||
|
||||
return switch (T) {
|
||||
const (RemoteAsset) => assets
|
||||
.where(
|
||||
(asset) => asset is RemoteAsset && asset.ownerId == currentUser!.id,
|
||||
)
|
||||
.cast<RemoteAsset>()
|
||||
.map((asset) => asset.id)
|
||||
.toList(),
|
||||
const (LocalAsset) =>
|
||||
assets.whereType<LocalAsset>().map((asset) => asset.id).toList(),
|
||||
_ => [],
|
||||
};
|
||||
const (RemoteAsset) => assets.whereType<RemoteAsset>(),
|
||||
const (LocalAsset) => assets.whereType<LocalAsset>(),
|
||||
_ => <T>[],
|
||||
} as Iterable<T>;
|
||||
}
|
||||
|
||||
Future<ActionResult> shareLink(
|
||||
ActionSource source,
|
||||
BuildContext context,
|
||||
) async {
|
||||
final ids = _getIdsForSource<RemoteAsset>(source);
|
||||
final ids = _getRemoteIdsForSource(source);
|
||||
try {
|
||||
await _service.shareLink(ids, context);
|
||||
return ActionResult(count: ids.length, success: true);
|
||||
@ -84,7 +84,7 @@ class ActionNotifier extends Notifier<void> {
|
||||
}
|
||||
|
||||
Future<ActionResult> favorite(ActionSource source) async {
|
||||
final ids = _getIdsForSource<RemoteAsset>(source);
|
||||
final ids = _getOwnedRemoteForSource(source);
|
||||
try {
|
||||
await _service.favorite(ids);
|
||||
return ActionResult(count: ids.length, success: true);
|
||||
@ -99,7 +99,7 @@ class ActionNotifier extends Notifier<void> {
|
||||
}
|
||||
|
||||
Future<ActionResult> unFavorite(ActionSource source) async {
|
||||
final ids = _getIdsForSource<RemoteAsset>(source);
|
||||
final ids = _getOwnedRemoteForSource(source);
|
||||
try {
|
||||
await _service.unFavorite(ids);
|
||||
return ActionResult(count: ids.length, success: true);
|
||||
@ -114,7 +114,7 @@ class ActionNotifier extends Notifier<void> {
|
||||
}
|
||||
|
||||
Future<ActionResult> archive(ActionSource source) async {
|
||||
final ids = _getIdsForSource<RemoteAsset>(source);
|
||||
final ids = _getOwnedRemoteForSource(source);
|
||||
try {
|
||||
await _service.archive(ids);
|
||||
return ActionResult(count: ids.length, success: true);
|
||||
@ -129,7 +129,7 @@ class ActionNotifier extends Notifier<void> {
|
||||
}
|
||||
|
||||
Future<ActionResult> unArchive(ActionSource source) async {
|
||||
final ids = _getIdsForSource<RemoteAsset>(source);
|
||||
final ids = _getOwnedRemoteForSource(source);
|
||||
try {
|
||||
await _service.unArchive(ids);
|
||||
return ActionResult(count: ids.length, success: true);
|
||||
@ -144,7 +144,7 @@ class ActionNotifier extends Notifier<void> {
|
||||
}
|
||||
|
||||
Future<ActionResult> moveToLockFolder(ActionSource source) async {
|
||||
final ids = _getIdsForSource<RemoteAsset>(source);
|
||||
final ids = _getOwnedRemoteForSource(source);
|
||||
try {
|
||||
await _service.moveToLockFolder(ids);
|
||||
return ActionResult(count: ids.length, success: true);
|
||||
@ -159,7 +159,7 @@ class ActionNotifier extends Notifier<void> {
|
||||
}
|
||||
|
||||
Future<ActionResult> removeFromLockFolder(ActionSource source) async {
|
||||
final ids = _getIdsForSource<RemoteAsset>(source);
|
||||
final ids = _getOwnedRemoteForSource(source);
|
||||
try {
|
||||
await _service.removeFromLockFolder(ids);
|
||||
return ActionResult(count: ids.length, success: true);
|
||||
@ -177,7 +177,7 @@ class ActionNotifier extends Notifier<void> {
|
||||
ActionSource source,
|
||||
BuildContext context,
|
||||
) async {
|
||||
final ids = _getIdsForSource<RemoteAsset>(source);
|
||||
final ids = _getOwnedRemoteForSource(source);
|
||||
try {
|
||||
final isEdited = await _service.editLocation(ids, context);
|
||||
if (!isEdited) {
|
||||
@ -195,3 +195,12 @@ class ActionNotifier extends Notifier<void> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension on Iterable<RemoteAsset> {
|
||||
Iterable<String> toIds() => map((e) => e.id);
|
||||
|
||||
Iterable<RemoteAsset> ownedAssets(String? ownerId) {
|
||||
if (ownerId == null) return [];
|
||||
return whereType<RemoteAsset>().where((a) => a.ownerId == ownerId);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user