refactor: action provider (#19677)

Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
This commit is contained in:
shenlong 2025-07-02 09:38:52 +05:30 committed by GitHub
parent c435bdb5d3
commit f2f3db3a79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -39,12 +39,19 @@ class ActionNotifier extends Notifier<void> {
_service = ref.watch(actionServiceProvider); _service = ref.watch(actionServiceProvider);
} }
List<String> _getIdsForSource<T extends BaseAsset>(ActionSource source) { List<String> _getRemoteIdsForSource(ActionSource source) {
final currentUser = ref.read(currentUserProvider); return _getIdsForSource<RemoteAsset>(source).toIds().toList();
if (T is RemoteAsset && currentUser == null) { }
return [];
}
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) { final Set<BaseAsset> assets = switch (source) {
ActionSource.timeline => ActionSource.timeline =>
ref.read(multiSelectProvider.select((s) => s.selectedAssets)), ref.read(multiSelectProvider.select((s) => s.selectedAssets)),
@ -52,24 +59,17 @@ class ActionNotifier extends Notifier<void> {
}; };
return switch (T) { return switch (T) {
const (RemoteAsset) => assets const (RemoteAsset) => assets.whereType<RemoteAsset>(),
.where( const (LocalAsset) => assets.whereType<LocalAsset>(),
(asset) => asset is RemoteAsset && asset.ownerId == currentUser!.id, _ => <T>[],
) } as Iterable<T>;
.cast<RemoteAsset>()
.map((asset) => asset.id)
.toList(),
const (LocalAsset) =>
assets.whereType<LocalAsset>().map((asset) => asset.id).toList(),
_ => [],
};
} }
Future<ActionResult> shareLink( Future<ActionResult> shareLink(
ActionSource source, ActionSource source,
BuildContext context, BuildContext context,
) async { ) async {
final ids = _getIdsForSource<RemoteAsset>(source); final ids = _getRemoteIdsForSource(source);
try { try {
await _service.shareLink(ids, context); await _service.shareLink(ids, context);
return ActionResult(count: ids.length, success: true); return ActionResult(count: ids.length, success: true);
@ -84,7 +84,7 @@ class ActionNotifier extends Notifier<void> {
} }
Future<ActionResult> favorite(ActionSource source) async { Future<ActionResult> favorite(ActionSource source) async {
final ids = _getIdsForSource<RemoteAsset>(source); final ids = _getOwnedRemoteForSource(source);
try { try {
await _service.favorite(ids); await _service.favorite(ids);
return ActionResult(count: ids.length, success: true); return ActionResult(count: ids.length, success: true);
@ -99,7 +99,7 @@ class ActionNotifier extends Notifier<void> {
} }
Future<ActionResult> unFavorite(ActionSource source) async { Future<ActionResult> unFavorite(ActionSource source) async {
final ids = _getIdsForSource<RemoteAsset>(source); final ids = _getOwnedRemoteForSource(source);
try { try {
await _service.unFavorite(ids); await _service.unFavorite(ids);
return ActionResult(count: ids.length, success: true); return ActionResult(count: ids.length, success: true);
@ -114,7 +114,7 @@ class ActionNotifier extends Notifier<void> {
} }
Future<ActionResult> archive(ActionSource source) async { Future<ActionResult> archive(ActionSource source) async {
final ids = _getIdsForSource<RemoteAsset>(source); final ids = _getOwnedRemoteForSource(source);
try { try {
await _service.archive(ids); await _service.archive(ids);
return ActionResult(count: ids.length, success: true); return ActionResult(count: ids.length, success: true);
@ -129,7 +129,7 @@ class ActionNotifier extends Notifier<void> {
} }
Future<ActionResult> unArchive(ActionSource source) async { Future<ActionResult> unArchive(ActionSource source) async {
final ids = _getIdsForSource<RemoteAsset>(source); final ids = _getOwnedRemoteForSource(source);
try { try {
await _service.unArchive(ids); await _service.unArchive(ids);
return ActionResult(count: ids.length, success: true); return ActionResult(count: ids.length, success: true);
@ -144,7 +144,7 @@ class ActionNotifier extends Notifier<void> {
} }
Future<ActionResult> moveToLockFolder(ActionSource source) async { Future<ActionResult> moveToLockFolder(ActionSource source) async {
final ids = _getIdsForSource<RemoteAsset>(source); final ids = _getOwnedRemoteForSource(source);
try { try {
await _service.moveToLockFolder(ids); await _service.moveToLockFolder(ids);
return ActionResult(count: ids.length, success: true); return ActionResult(count: ids.length, success: true);
@ -159,7 +159,7 @@ class ActionNotifier extends Notifier<void> {
} }
Future<ActionResult> removeFromLockFolder(ActionSource source) async { Future<ActionResult> removeFromLockFolder(ActionSource source) async {
final ids = _getIdsForSource<RemoteAsset>(source); final ids = _getOwnedRemoteForSource(source);
try { try {
await _service.removeFromLockFolder(ids); await _service.removeFromLockFolder(ids);
return ActionResult(count: ids.length, success: true); return ActionResult(count: ids.length, success: true);
@ -177,7 +177,7 @@ class ActionNotifier extends Notifier<void> {
ActionSource source, ActionSource source,
BuildContext context, BuildContext context,
) async { ) async {
final ids = _getIdsForSource<RemoteAsset>(source); final ids = _getOwnedRemoteForSource(source);
try { try {
final isEdited = await _service.editLocation(ids, context); final isEdited = await _service.editLocation(ids, context);
if (!isEdited) { 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);
}
}