mirror of
				https://github.com/immich-app/immich.git
				synced 2025-11-03 19:17:11 -05: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
 | 
					  @override
 | 
				
			||||||
  Widget build(BuildContext context, WidgetRef ref) {
 | 
					  Widget build(BuildContext context, WidgetRef ref) {
 | 
				
			||||||
    return BaseActionButton(
 | 
					    return BaseActionButton(
 | 
				
			||||||
 | 
				
			|||||||
@ -40,10 +40,6 @@ class FavoriteActionButton extends ConsumerWidget {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  void viewerAction(WidgetRef _) {
 | 
					 | 
				
			||||||
    UnimplementedError("Viewer action for favorite is not implemented yet.");
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
  Widget build(BuildContext context, WidgetRef ref) {
 | 
					  Widget build(BuildContext context, WidgetRef ref) {
 | 
				
			||||||
    return BaseActionButton(
 | 
					    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
 | 
					  @override
 | 
				
			||||||
  Widget build(BuildContext context, WidgetRef ref) {
 | 
					  Widget build(BuildContext context, WidgetRef ref) {
 | 
				
			||||||
    return BaseActionButton(
 | 
					    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
 | 
					  @override
 | 
				
			||||||
  Widget build(BuildContext context, WidgetRef ref) {
 | 
					  Widget build(BuildContext context, WidgetRef ref) {
 | 
				
			||||||
    return BaseActionButton(
 | 
					    return BaseActionButton(
 | 
				
			||||||
 | 
				
			|||||||
@ -1,44 +1,42 @@
 | 
				
			|||||||
import 'package:auto_route/auto_route.dart';
 | 
					 | 
				
			||||||
import 'package:flutter/material.dart';
 | 
					import 'package:flutter/material.dart';
 | 
				
			||||||
 | 
					import 'package:fluttertoast/fluttertoast.dart';
 | 
				
			||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
 | 
					import 'package:hooks_riverpod/hooks_riverpod.dart';
 | 
				
			||||||
import 'package:immich_mobile/constants/enums.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/extensions/translate_extensions.dart';
 | 
				
			||||||
import 'package:immich_mobile/presentation/widgets/action_buttons/base_action_button.widget.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/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 {
 | 
					class ShareLinkActionButton extends ConsumerWidget {
 | 
				
			||||||
  final ActionSource source;
 | 
					  final ActionSource source;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const ShareLinkActionButton({super.key, required this.source});
 | 
					  const ShareLinkActionButton({super.key, required this.source});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  onAction(BuildContext context, WidgetRef ref) {
 | 
					  _onTap(BuildContext context, WidgetRef ref) async {
 | 
				
			||||||
    switch (source) {
 | 
					    if (!context.mounted) {
 | 
				
			||||||
      case ActionSource.timeline:
 | 
					      return;
 | 
				
			||||||
        timelineAction(context, ref);
 | 
					 | 
				
			||||||
      case ActionSource.viewer:
 | 
					 | 
				
			||||||
        viewerAction(ref);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  void timelineAction(BuildContext context, WidgetRef ref) {
 | 
					    final result =
 | 
				
			||||||
    final ids = ref
 | 
					        await ref.read(actionProvider.notifier).shareLink(source, context);
 | 
				
			||||||
        .read(multiSelectProvider.select((value) => value.selectedAssets))
 | 
					    ref.read(multiSelectProvider.notifier).reset();
 | 
				
			||||||
        .whereType<RemoteAsset>()
 | 
					 | 
				
			||||||
        .toList()
 | 
					 | 
				
			||||||
        .map((asset) => asset.id)
 | 
					 | 
				
			||||||
        .toList();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    context.pushRoute(
 | 
					    final successMessage = 'share_link_action_prompt'.t(
 | 
				
			||||||
      SharedLinkEditRoute(
 | 
					      context: context,
 | 
				
			||||||
        assetsList: ids,
 | 
					      args: {'count': result.count.toString()},
 | 
				
			||||||
      ),
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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,
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
  void viewerAction(WidgetRef _) {
 | 
					 | 
				
			||||||
    UnimplementedError("Viewer action for favorite is not implemented yet.");
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
@ -46,7 +44,7 @@ class ShareLinkActionButton extends ConsumerWidget {
 | 
				
			|||||||
    return BaseActionButton(
 | 
					    return BaseActionButton(
 | 
				
			||||||
      iconData: Icons.link_rounded,
 | 
					      iconData: Icons.link_rounded,
 | 
				
			||||||
      label: "share_link".t(context: context),
 | 
					      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/constants/enums.dart';
 | 
				
			||||||
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
 | 
					import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
 | 
				
			||||||
import 'package:immich_mobile/providers/infrastructure/timeline.provider.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 {
 | 
					  Future<ActionResult> favorite(ActionSource source) async {
 | 
				
			||||||
    final ids = _getIdsForSource<RemoteAsset>(source);
 | 
					    final ids = _getIdsForSource<RemoteAsset>(source);
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
@ -127,7 +146,7 @@ class ActionNotifier extends Notifier<void> {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Future<ActionResult> moveToLockFolder(ActionSource source) async {
 | 
					  Future<ActionResult> moveToLockFolder(ActionSource source) async {
 | 
				
			||||||
    final ids = _getIdsForSource<LocalAsset>(source);
 | 
					    final ids = _getIdsForSource<RemoteAsset>(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);
 | 
				
			||||||
@ -142,7 +161,7 @@ class ActionNotifier extends Notifier<void> {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Future<ActionResult> removeFromLockFolder(ActionSource source) async {
 | 
					  Future<ActionResult> removeFromLockFolder(ActionSource source) async {
 | 
				
			||||||
    final ids = _getIdsForSource<LocalAsset>(source);
 | 
					    final ids = _getIdsForSource<RemoteAsset>(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);
 | 
				
			||||||
 | 
				
			|||||||
@ -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/constants/enums.dart';
 | 
				
			||||||
import 'package:immich_mobile/domain/models/asset/base_asset.model.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/infrastructure/repositories/remote_asset.repository.dart';
 | 
				
			||||||
import 'package:immich_mobile/repositories/asset_api.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';
 | 
					import 'package:riverpod_annotation/riverpod_annotation.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
final actionServiceProvider = Provider<ActionService>(
 | 
					final actionServiceProvider = Provider<ActionService>(
 | 
				
			||||||
@ -17,6 +20,14 @@ class ActionService {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  const ActionService(this._assetApiRepository, this._remoteAssetRepository);
 | 
					  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 {
 | 
					  Future<void> favorite(List<String> remoteIds) async {
 | 
				
			||||||
    await _assetApiRepository.updateFavorite(remoteIds, true);
 | 
					    await _assetApiRepository.updateFavorite(remoteIds, true);
 | 
				
			||||||
    await _remoteAssetRepository.updateFavorite(remoteIds, true);
 | 
					    await _remoteAssetRepository.updateFavorite(remoteIds, true);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user