Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Polish mixed delete bottom sheet

ashilkn14e1fa1cdb97
diff --git a/mobile/apps/photos/lib/utils/delete_file_util.dart b/mobile/apps/photos/lib/utils/delete_file_util.dart
index e308d4dfa9..69fbec57d0 100644
--- a/mobile/apps/photos/lib/utils/delete_file_util.dart
+++ b/mobile/apps/photos/lib/utils/delete_file_util.dart
@@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:io';
import 'dart:math';
+import 'package:ente_components/ente_components.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:photo_manager/photo_manager.dart';
@@ -15,6 +16,7 @@ import 'package:photos/events/local_photos_updated_event.dart';
import 'package:photos/gateways/trash/models/trash_item_request.dart';
import "package:photos/generated/l10n.dart";
import "package:photos/l10n/l10n.dart";
+import "package:photos/models/button_result.dart";
import 'package:photos/models/file/file.dart';
import "package:photos/models/files_split.dart";
import "package:photos/models/freeable_space_info.dart";
@@ -814,7 +816,6 @@ Future<void> showDeleteSheet(
final bool isBothLocalAndRemote = containsUploadedFile && containsLocalFile;
final bool isLocalOnly = !containsUploadedFile;
final bool isRemoteOnly = !containsLocalFile;
- final bool hasSingleDeleteAction = isLocalOnly || isRemoteOnly;
final String? bodyHighlight = isBothLocalAndRemote
? AppLocalizations.of(context).theyWillBeDeletedFromAllAlbums
: null;
@@ -830,6 +831,45 @@ Future<void> showDeleteSheet(
} else {
throw AssertionError("Unexpected state");
}
+
+ if (isBothLocalAndRemote) {
+ final actionResult = await _showMixedDeleteTargetSheet(
+ context: context,
+ body: body,
+ bodyHighlight: bodyHighlight!,
+ onDelete: (target) async {
+ switch (target) {
+ case _MixedDeleteTarget.ente:
+ await deleteFromRemoteOnlyAction(context, deletableFiles).then(
+ (value) {
+ showShortToast(
+ context,
+ AppLocalizations.of(context).movedToTrash,
+ );
+ },
+ onError: (e, s) {
+ showGenericErrorDialog(context: context, error: e);
+ },
+ );
+ case _MixedDeleteTarget.device:
+ await deleteOnDeviceOnlyAction(context, deletableFiles);
+ case _MixedDeleteTarget.both:
+ await deleteFromEverywhereAction(context, deletableFiles);
+ }
+ },
+ );
+ if (actionResult?.action != null &&
+ actionResult!.action == ButtonAction.error) {
+ await showGenericErrorDialog(
+ context: context,
+ error: actionResult.exception,
+ );
+ } else {
+ selectedFiles.clearAll();
+ }
+ return;
+ }
+
final List<ButtonWidget> buttons = [];
if (isBothLocalAndRemote || isRemoteOnly) {
buttons.add(
@@ -837,9 +877,7 @@ Future<void> showDeleteSheet(
labelText: isBothLocalAndRemote
? AppLocalizations.of(context).deleteFromEnte
: AppLocalizations.of(context).yesDelete,
- buttonType: hasSingleDeleteAction
- ? ButtonType.critical
- : ButtonType.neutral,
+ buttonType: ButtonType.critical,
buttonSize: ButtonSize.large,
shouldStickToDarkTheme: true,
buttonAction: ButtonAction.first,
@@ -867,9 +905,7 @@ Future<void> showDeleteSheet(
labelText: isBothLocalAndRemote
? AppLocalizations.of(context).deleteFromDevice
: AppLocalizations.of(context).yesDelete,
- buttonType: hasSingleDeleteAction
- ? ButtonType.critical
- : ButtonType.neutral,
+ buttonType: ButtonType.critical,
buttonSize: ButtonSize.large,
shouldStickToDarkTheme: true,
buttonAction: ButtonAction.second,
@@ -881,23 +917,6 @@ Future<void> showDeleteSheet(
),
);
}
-
- if (isBothLocalAndRemote) {
- buttons.add(
- ButtonWidget(
- labelText: AppLocalizations.of(context).deleteFromBoth,
- buttonType: ButtonType.neutral,
- buttonSize: ButtonSize.large,
- shouldStickToDarkTheme: true,
- buttonAction: ButtonAction.third,
- shouldSurfaceExecutionStates: true,
- isInAlert: true,
- onTap: () async {
- await deleteFromEverywhereAction(context, deletableFiles);
- },
- ),
- );
- }
buttons.add(
ButtonWidget(
labelText: AppLocalizations.of(context).cancel,
@@ -912,11 +931,8 @@ Future<void> showDeleteSheet(
context: context,
buttons: buttons,
actionSheetType: ActionSheetType.defaultActionSheet,
- title: hasSingleDeleteAction
- ? AppLocalizations.of(context).areYouSure
- : null,
+ title: AppLocalizations.of(context).areYouSure,
body: body,
- bodyHighlight: bodyHighlight,
);
if (actionResult?.action != null &&
actionResult!.action == ButtonAction.error) {
@@ -928,3 +944,124 @@ Future<void> showDeleteSheet(
selectedFiles.clearAll();
}
}
+
+enum _MixedDeleteTarget { ente, device, both }
+
+class _MixedDeleteTargetOption {
+ const _MixedDeleteTargetOption({
+ required this.target,
+ required this.action,
+ required this.label,
+ });
+
+ final _MixedDeleteTarget target;
+ final ButtonAction action;
+ final String label;
+}
+
+Future<ButtonResult?> _showMixedDeleteTargetSheet({
+ required BuildContext context,
+ required String body,
+ required String bodyHighlight,
+ required Future<void> Function(_MixedDeleteTarget target) onDelete,
+}) {
+ final l10n = AppLocalizations.of(context);
+ return showBottomSheetComponent<ButtonResult>(
+ context: context,
+ useRootNavigator: Platform.isIOS,
+ builder: (_) => _MixedDeleteTargetSheet(
+ title: l10n.areYouSure,
+ body: body,
+ bodyHighlight: bodyHighlight,
+ closeTooltip: l10n.close,
+ options: [
+ _MixedDeleteTargetOption(
+ target: _MixedDeleteTarget.ente,
+ action: ButtonAction.first,
+ label: l10n.deleteFromEnte,
+ ),
+ _MixedDeleteTargetOption(
+ target: _MixedDeleteTarget.device,
+ action: ButtonAction.second,
+ label: l10n.deleteFromDevice,
+ ),
+ _MixedDeleteTargetOption(
+ target: _MixedDeleteTarget.both,
+ action: ButtonAction.third,
+ label: l10n.deleteFromBoth,
+ ),
+ ],
+ onDelete: onDelete,
+ ),
+ );
+}
+
+class _MixedDeleteTargetSheet extends StatelessWidget {
+ const _MixedDeleteTargetSheet({
+ required this.title,
+ required this.body,
+ required this.bodyHighlight,
+ required this.closeTooltip,
+ required this.options,
+ required this.onDelete,
+ });
+
+ final String title;
+ final String body;
+ final String bodyHighlight;
+ final String closeTooltip;
+ final List<_MixedDeleteTargetOption> options;
+ final Future<void> Function(_MixedDeleteTarget target) onDelete;
+
+ @override
+ Widget build(BuildContext context) {
+ final colors = context.componentColors;
+
+ return BottomSheetComponent(
+ title: title,
+ closeTooltip: closeTooltip,
+ content: Text(
+ '$body\n$bodyHighlight',
+ style: TextStyles.body.copyWith(color: colors.textLight),
+ ),
+ actions: [
+ for (final option in options)
+ ButtonComponent(
+ key: ValueKey('mixedDeleteTarget.${option.target.name}'),
+ label: option.label,
+ variant: _variantFor(option.target),
+ onTap: () => _handleDelete(context, option),
+ ),
+ ],
+ );
+ }
+
+ Future<void> _handleDelete(
+ BuildContext context,
+ _MixedDeleteTargetOption option,
+ ) async {
+ try {
+ await onDelete(option.target);
+ if (!context.mounted) return;
+ Navigator.of(context).pop(ButtonResult(option.action));
+ } catch (error) {
+ if (context.mounted) {
+ Navigator.of(
+ context,
+ ).pop(ButtonResult(ButtonAction.error, _toException(error)));
+ }
+ }
+ }
+}
+
+ButtonComponentVariant _variantFor(_MixedDeleteTarget target) {
+ return switch (target) {
+ _MixedDeleteTarget.ente ||
+ _MixedDeleteTarget.device => ButtonComponentVariant.neutral,
+ _MixedDeleteTarget.both => ButtonComponentVariant.critical,
+ };
+}
+
+Exception _toException(Object error) {
+ return error is Exception ? error : Exception(error.toString());
+}
diff --git a/mobile/apps/photos/test/utils/delete_file_util_test.dart b/mobile/apps/photos/test/utils/delete_file_util_test.dart
index d0af95accf..45915fb84d 100644
--- a/mobile/apps/photos/test/utils/delete_file_util_test.dart
+++ b/mobile/apps/photos/test/utils/delete_file_util_test.dart
@@ -38,7 +38,7 @@ void main() {
group('showDeleteSheet', () {
testWidgets(
- 'matches the legacy mixed local and remote delete action sheet',
+ 'mixed local and remote delete sheet shows mixed component actions',
(tester) async {
final file = _file(generatedID: 1, uploadedID: 11, localID: 'local-1');
final selectedFiles = SelectedFiles()..selectAll({file});
@@ -55,14 +55,37 @@ void main() {
expect(find.byType(BottomSheetComponent), findsOneWidget);
expect(
- find.text('Some items are in both Ente and your device.'),
+ find.text(
+ 'Some items are in both Ente and your device.\n'
+ 'They will be deleted from all albums.',
+ ),
findsOneWidget,
);
+ expect(find.text('Are you sure?'), findsOneWidget);
+ expect(find.byType(RadioComponent), findsNothing);
+ expect(find.byType(MenuGroupComponent), findsNothing);
+ expect(find.byType(MenuComponent), findsNothing);
+ expect(find.byType(ButtonComponent), findsNWidgets(3));
+ final buttons = tester.widgetList<ButtonComponent>(
+ find.byType(ButtonComponent),
+ );
+ expect(buttons.map((button) => button.variant), [
+ ButtonComponentVariant.neutral,
+ ButtonComponentVariant.neutral,
+ ButtonComponentVariant.critical,
+ ]);
expect(
- find.text('They will be deleted from all albums.'),
+ find.byKey(const ValueKey('mixedDeleteTarget.ente')),
+ findsOneWidget,
+ );
+ expect(
+ find.byKey(const ValueKey('mixedDeleteTarget.device')),
+ findsOneWidget,
+ );
+ expect(
+ find.byKey(const ValueKey('mixedDeleteTarget.both')),
findsOneWidget,
);
- expect(find.byType(ButtonComponent), findsNWidgets(3));
expect(find.byTooltip('Close'), findsOneWidget);
expect(find.text('Cancel'), findsNothing);
_expectVisibleButtonsInOrder(tester, [
@@ -73,6 +96,103 @@ void main() {
},
);
+ testWidgets(
+ 'mixed delete from Ente choice uses the remote delete path and clears selection',
+ (tester) async {
+ final file = _file(
+ generatedID: 14,
+ uploadedID: 24,
+ localID: 'local-14',
+ );
+ final selectedFiles = SelectedFiles()..selectAll({file});
+ var remoteDeleteCalls = 0;
+ List<EnteFile>? remoteDeleteFiles;
+
+ await _pumpDeleteSheet(
+ tester,
+ selectedFiles: selectedFiles,
+ split: FilesSplit(
+ pendingUploads: const [],
+ ownedByCurrentUser: [file],
+ ownedByOtherUsers: const [],
+ ),
+ deleteFromRemoteOnlyOverride: (context, files) async {
+ remoteDeleteCalls++;
+ remoteDeleteFiles = List.of(files);
+ },
+ );
+
+ await tester.tap(find.byKey(const ValueKey('mixedDeleteTarget.ente')));
+ await tester.pumpAndSettle();
+
+ expect(remoteDeleteCalls, 1);
+ expect(remoteDeleteFiles, [file]);
+ expect(find.byType(BottomSheetComponent), findsNothing);
+ expect(selectedFiles.files, isEmpty);
+
+ await _settleToast(tester);
+ },
+ );
+
+ testWidgets('mixed delete sheet close clears selection', (tester) async {
+ final file = _file(generatedID: 16, uploadedID: 26, localID: 'local-16');
+ final selectedFiles = SelectedFiles()..selectAll({file});
+
+ await _pumpDeleteSheet(
+ tester,
+ selectedFiles: selectedFiles,
+ split: FilesSplit(
+ pendingUploads: const [],
+ ownedByCurrentUser: [file],
+ ownedByOtherUsers: const [],
+ ),
+ );
+
+ await tester.tap(find.byTooltip('Close'));
+ await tester.pumpAndSettle();
+
+ expect(find.byType(BottomSheetComponent), findsNothing);
+ expect(selectedFiles.files, isEmpty);
+ });
+
+ testWidgets(
+ 'mixed delete from device choice uses the device delete path and clears selection',
+ (tester) async {
+ final file = _file(
+ generatedID: 15,
+ uploadedID: 25,
+ localID: 'local-15',
+ );
+ final selectedFiles = SelectedFiles()..selectAll({file});
+ var deviceDeleteCalls = 0;
+ List<EnteFile>? deviceDeleteFiles;
+
+ await _pumpDeleteSheet(
+ tester,
+ selectedFiles: selectedFiles,
+ split: FilesSplit(
+ pendingUploads: const [],
+ ownedByCurrentUser: [file],
+ ownedByOtherUsers: const [],
+ ),
+ deleteOnDeviceOnlyOverride: (context, files) async {
+ deviceDeleteCalls++;
+ deviceDeleteFiles = List.of(files);
+ },
+ );
+
+ await tester.tap(
+ find.byKey(const ValueKey('mixedDeleteTarget.device')),
+ );
+ await tester.pumpAndSettle();
+
+ expect(deviceDeleteCalls, 1);
+ expect(deviceDeleteFiles, [file]);
+ expect(find.byType(BottomSheetComponent), findsNothing);
+ expect(selectedFiles.files, isEmpty);
+ },
+ );
+
testWidgets('remote-only delete sheet uses title and critical action', (
tester,
) async {
@@ -256,7 +376,7 @@ void main() {
},
);
- await tester.tap(find.text('Delete from both'));
+ await tester.tap(find.byKey(const ValueKey('mixedDeleteTarget.both')));
await tester.pumpAndSettle();
expect(everywhereDeleteCalls, 1);