mirror of
				https://github.com/immich-app/immich.git
				synced 2025-10-31 10:37:11 -04:00 
			
		
		
		
	refactor(mobile): action provider (#19669)
* refactor action provider * fix lint
This commit is contained in:
		
							parent
							
								
									fa418d778b
								
							
						
					
					
						commit
						f59b0bab5a
					
				| @ -40,10 +40,6 @@ class ArchiveActionButton extends ConsumerWidget { | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   void viewerAction(WidgetRef _) { | ||||
|     UnimplementedError("Viewer action for archive is not implemented yet."); | ||||
|   } | ||||
| 
 | ||||
|   @override | ||||
|   Widget build(BuildContext context, WidgetRef ref) { | ||||
|     return BaseActionButton( | ||||
|  | ||||
| @ -40,10 +40,6 @@ class FavoriteActionButton extends ConsumerWidget { | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   void viewerAction(WidgetRef _) { | ||||
|     UnimplementedError("Viewer action for favorite is not implemented yet."); | ||||
|   } | ||||
| 
 | ||||
|   @override | ||||
|   Widget build(BuildContext context, WidgetRef ref) { | ||||
|     return BaseActionButton( | ||||
|  | ||||
| @ -41,12 +41,6 @@ class MoveToLockFolderActionButton extends ConsumerWidget { | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   void viewerAction(WidgetRef _) { | ||||
|     UnimplementedError( | ||||
|       "Viewer action for move to locked folder is not implemented yet.", | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   @override | ||||
|   Widget build(BuildContext context, WidgetRef ref) { | ||||
|     return BaseActionButton( | ||||
|  | ||||
| @ -41,12 +41,6 @@ class RemoveFromLockFolderActionButton extends ConsumerWidget { | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   void viewerAction(WidgetRef _) { | ||||
|     UnimplementedError( | ||||
|       "Viewer action for remove from locked folder is not implemented yet.", | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   @override | ||||
|   Widget build(BuildContext context, WidgetRef ref) { | ||||
|     return BaseActionButton( | ||||
|  | ||||
| @ -1,44 +1,42 @@ | ||||
| import 'package:auto_route/auto_route.dart'; | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:fluttertoast/fluttertoast.dart'; | ||||
| import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||||
| import 'package:immich_mobile/constants/enums.dart'; | ||||
| import 'package:immich_mobile/domain/models/asset/base_asset.model.dart'; | ||||
| import 'package:immich_mobile/extensions/translate_extensions.dart'; | ||||
| import 'package:immich_mobile/presentation/widgets/action_buttons/base_action_button.widget.dart'; | ||||
| import 'package:immich_mobile/providers/infrastructure/action.provider.dart'; | ||||
| import 'package:immich_mobile/providers/timeline/multiselect.provider.dart'; | ||||
| import 'package:immich_mobile/routing/router.dart'; | ||||
| import 'package:immich_mobile/widgets/common/immich_toast.dart'; | ||||
| 
 | ||||
| class ShareLinkActionButton extends ConsumerWidget { | ||||
|   final ActionSource source; | ||||
| 
 | ||||
|   const ShareLinkActionButton({super.key, required this.source}); | ||||
| 
 | ||||
|   onAction(BuildContext context, WidgetRef ref) { | ||||
|     switch (source) { | ||||
|       case ActionSource.timeline: | ||||
|         timelineAction(context, ref); | ||||
|       case ActionSource.viewer: | ||||
|         viewerAction(ref); | ||||
|   _onTap(BuildContext context, WidgetRef ref) async { | ||||
|     if (!context.mounted) { | ||||
|       return; | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   void timelineAction(BuildContext context, WidgetRef ref) { | ||||
|     final ids = ref | ||||
|         .read(multiSelectProvider.select((value) => value.selectedAssets)) | ||||
|         .whereType<RemoteAsset>() | ||||
|         .toList() | ||||
|         .map((asset) => asset.id) | ||||
|         .toList(); | ||||
|     final result = | ||||
|         await ref.read(actionProvider.notifier).shareLink(source, context); | ||||
|     ref.read(multiSelectProvider.notifier).reset(); | ||||
| 
 | ||||
|     context.pushRoute( | ||||
|       SharedLinkEditRoute( | ||||
|         assetsList: ids, | ||||
|       ), | ||||
|     final successMessage = 'share_link_action_prompt'.t( | ||||
|       context: context, | ||||
|       args: {'count': result.count.toString()}, | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   void viewerAction(WidgetRef _) { | ||||
|     UnimplementedError("Viewer action for favorite is not implemented yet."); | ||||
|     if (context.mounted) { | ||||
|       ImmichToast.show( | ||||
|         context: context, | ||||
|         msg: result.success | ||||
|             ? successMessage | ||||
|             : 'scaffold_body_error_occurred'.t(context: context), | ||||
|         gravity: ToastGravity.BOTTOM, | ||||
|         toastType: result.success ? ToastType.success : ToastType.error, | ||||
|       ); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   @override | ||||
| @ -46,7 +44,7 @@ class ShareLinkActionButton extends ConsumerWidget { | ||||
|     return BaseActionButton( | ||||
|       iconData: Icons.link_rounded, | ||||
|       label: "share_link".t(context: context), | ||||
|       onPressed: () => onAction(context, ref), | ||||
|       onPressed: () => _onTap(context, ref), | ||||
|     ); | ||||
|   } | ||||
| } | ||||
|  | ||||
| @ -1,3 +1,4 @@ | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:immich_mobile/constants/enums.dart'; | ||||
| import 'package:immich_mobile/domain/models/asset/base_asset.model.dart'; | ||||
| import 'package:immich_mobile/providers/infrastructure/timeline.provider.dart'; | ||||
| @ -66,6 +67,24 @@ class ActionNotifier extends Notifier<void> { | ||||
|     }; | ||||
|   } | ||||
| 
 | ||||
|   Future<ActionResult> shareLink( | ||||
|     ActionSource source, | ||||
|     BuildContext context, | ||||
|   ) async { | ||||
|     final ids = _getIdsForSource<RemoteAsset>(source); | ||||
|     try { | ||||
|       await _service.shareLink(ids, context); | ||||
|       return ActionResult(count: ids.length, success: true); | ||||
|     } catch (error, stack) { | ||||
|       _logger.severe('Failed to create shared link for assets', error, stack); | ||||
|       return ActionResult( | ||||
|         count: ids.length, | ||||
|         success: false, | ||||
|         error: error.toString(), | ||||
|       ); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   Future<ActionResult> favorite(ActionSource source) async { | ||||
|     final ids = _getIdsForSource<RemoteAsset>(source); | ||||
|     try { | ||||
| @ -127,7 +146,7 @@ class ActionNotifier extends Notifier<void> { | ||||
|   } | ||||
| 
 | ||||
|   Future<ActionResult> moveToLockFolder(ActionSource source) async { | ||||
|     final ids = _getIdsForSource<LocalAsset>(source); | ||||
|     final ids = _getIdsForSource<RemoteAsset>(source); | ||||
|     try { | ||||
|       await _service.moveToLockFolder(ids); | ||||
|       return ActionResult(count: ids.length, success: true); | ||||
| @ -142,7 +161,7 @@ class ActionNotifier extends Notifier<void> { | ||||
|   } | ||||
| 
 | ||||
|   Future<ActionResult> removeFromLockFolder(ActionSource source) async { | ||||
|     final ids = _getIdsForSource<LocalAsset>(source); | ||||
|     final ids = _getIdsForSource<RemoteAsset>(source); | ||||
|     try { | ||||
|       await _service.removeFromLockFolder(ids); | ||||
|       return ActionResult(count: ids.length, success: true); | ||||
|  | ||||
| @ -1,7 +1,10 @@ | ||||
| import 'package:auto_route/auto_route.dart'; | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:immich_mobile/constants/enums.dart'; | ||||
| import 'package:immich_mobile/domain/models/asset/base_asset.model.dart'; | ||||
| import 'package:immich_mobile/infrastructure/repositories/remote_asset.repository.dart'; | ||||
| import 'package:immich_mobile/repositories/asset_api.repository.dart'; | ||||
| import 'package:immich_mobile/routing/router.dart'; | ||||
| import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||||
| 
 | ||||
| final actionServiceProvider = Provider<ActionService>( | ||||
| @ -17,6 +20,14 @@ class ActionService { | ||||
| 
 | ||||
|   const ActionService(this._assetApiRepository, this._remoteAssetRepository); | ||||
| 
 | ||||
|   Future<void> shareLink(List<String> remoteIds, BuildContext context) async { | ||||
|     context.pushRoute( | ||||
|       SharedLinkEditRoute( | ||||
|         assetsList: remoteIds, | ||||
|       ), | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   Future<void> favorite(List<String> remoteIds) async { | ||||
|     await _assetApiRepository.updateFavorite(remoteIds, true); | ||||
|     await _remoteAssetRepository.updateFavorite(remoteIds, true); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user