diff --git a/mobile/lib/providers/infrastructure/action.provider.dart b/mobile/lib/providers/infrastructure/action.provider.dart index 940adf10ed..57dde6456e 100644 --- a/mobile/lib/providers/infrastructure/action.provider.dart +++ b/mobile/lib/providers/infrastructure/action.provider.dart @@ -39,12 +39,19 @@ class ActionNotifier extends Notifier { _service = ref.watch(actionServiceProvider); } - List _getIdsForSource(ActionSource source) { - final currentUser = ref.read(currentUserProvider); - if (T is RemoteAsset && currentUser == null) { - return []; - } + List _getRemoteIdsForSource(ActionSource source) { + return _getIdsForSource(source).toIds().toList(); + } + List _getOwnedRemoteForSource(ActionSource source) { + final ownerId = ref.read(currentUserProvider)?.id; + return _getIdsForSource(source) + .ownedAssets(ownerId) + .toIds() + .toList(); + } + + Iterable _getIdsForSource(ActionSource source) { final Set assets = switch (source) { ActionSource.timeline => ref.read(multiSelectProvider.select((s) => s.selectedAssets)), @@ -52,24 +59,17 @@ class ActionNotifier extends Notifier { }; return switch (T) { - const (RemoteAsset) => assets - .where( - (asset) => asset is RemoteAsset && asset.ownerId == currentUser!.id, - ) - .cast() - .map((asset) => asset.id) - .toList(), - const (LocalAsset) => - assets.whereType().map((asset) => asset.id).toList(), - _ => [], - }; + const (RemoteAsset) => assets.whereType(), + const (LocalAsset) => assets.whereType(), + _ => [], + } as Iterable; } Future shareLink( ActionSource source, BuildContext context, ) async { - final ids = _getIdsForSource(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 { } Future favorite(ActionSource source) async { - final ids = _getIdsForSource(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 { } Future unFavorite(ActionSource source) async { - final ids = _getIdsForSource(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 { } Future archive(ActionSource source) async { - final ids = _getIdsForSource(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 { } Future unArchive(ActionSource source) async { - final ids = _getIdsForSource(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 { } Future moveToLockFolder(ActionSource source) async { - final ids = _getIdsForSource(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 { } Future removeFromLockFolder(ActionSource source) async { - final ids = _getIdsForSource(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 { ActionSource source, BuildContext context, ) async { - final ids = _getIdsForSource(source); + final ids = _getOwnedRemoteForSource(source); try { final isEdited = await _service.editLocation(ids, context); if (!isEdited) { @@ -195,3 +195,12 @@ class ActionNotifier extends Notifier { } } } + +extension on Iterable { + Iterable toIds() => map((e) => e.id); + + Iterable ownedAssets(String? ownerId) { + if (ownerId == null) return []; + return whereType().where((a) => a.ownerId == ownerId); + } +}