mirror of
https://github.com/immich-app/immich
synced 2026-08-15 13:03:57 +00:00
* refactor: existing actions to new structure # Conflicts: # mobile/lib/presentation/actions/action.dart # mobile/lib/presentation/actions/action.widget.dart # mobile/lib/presentation/actions/asset_debug.action.dart # mobile/lib/presentation/actions/favorite.action.dart # mobile/lib/presentation/actions/partner.action.dart # mobile/test/unit/presentation/actions/favorite_action_test.dart * rename to actionitem * review changes * refactor: mobile restore action --------- Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
47 lines
1.8 KiB
Dart
47 lines
1.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:immich_mobile/constants/enums.dart';
|
|
import 'package:immich_mobile/generated/translations.g.dart';
|
|
import 'package:immich_mobile/presentation/actions/action.dart';
|
|
import 'package:immich_mobile/providers/infrastructure/asset.provider.dart';
|
|
import 'package:immich_mobile/providers/infrastructure/toast.provider.dart';
|
|
import 'package:immich_mobile/utils/error_handler.dart';
|
|
|
|
final _stateProvider = Provider.family.autoDispose<List<String>?, ActionSource>((ref, source) {
|
|
final assets = ref.watch(ownedAssetsActionProvider(source));
|
|
final assetIds = assets.trashed().map((asset) => asset.id).toList(growable: false);
|
|
return assetIds.isEmpty ? null : assetIds;
|
|
});
|
|
|
|
class RestoreAction extends AssetActionBuilder {
|
|
const RestoreAction({required super.source});
|
|
|
|
@override
|
|
ActionItem? create(BuildContext context, WidgetRef ref) {
|
|
if (!ref.watch(_stateProvider(source).select((state) => state != null))) {
|
|
return null;
|
|
}
|
|
|
|
return .new(icon: Icons.history_rounded, label: context.t.restore, onAction: () => _restore(context, ref));
|
|
}
|
|
|
|
Future<void> _restore(BuildContext context, WidgetRef ref) async {
|
|
final assetIds = ref.read(_stateProvider(source));
|
|
if (assetIds == null) {
|
|
return;
|
|
}
|
|
|
|
final message = context.t.assets_restored_count(count: assetIds.length);
|
|
final assetService = ref.read(assetServiceProvider);
|
|
final toastService = ref.read(toastServiceProvider);
|
|
final clearSelection = ref.read(clearSelectionProvider(source));
|
|
|
|
try {
|
|
await assetService.restoreTrash(assetIds);
|
|
toastService.success(message);
|
|
clearSelection();
|
|
} catch (error, stack) {
|
|
handleError(error, stack: stack, description: "Failed to restore assets");
|
|
}
|
|
}
|
|
}
|