Force confirmation in case local-only files are being deleted on Android

This commit is contained in:
vishnukvmd 2022-05-12 17:49:36 +05:30
parent b33f9f21f2
commit b1c2c36c31

View file

@ -5,7 +5,6 @@ import 'dart:math';
import 'package:device_info/device_info.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:photos/core/configuration.dart';
@ -36,6 +35,7 @@ Future<void> deleteFilesFromEverywhere(
final List<String> localAssetIDs = [];
final List<String> localSharedMediaIDs = [];
final List<String> alreadyDeletedIDs = []; // to ignore already deleted files
bool hasLocalOnlyFiles = false;
for (final file in files) {
if (file.localID != null) {
if (!(await _localFileExist(file))) {
@ -47,6 +47,16 @@ Future<void> deleteFilesFromEverywhere(
localAssetIDs.add(file.localID);
}
}
if (file.uploadedFileID == null) {
hasLocalOnlyFiles = true;
}
}
if (hasLocalOnlyFiles && Platform.isAndroid) {
final shouldProceed = await shouldProceedWithDeletion(context);
if (!shouldProceed) {
await dialog.hide();
return;
}
}
Set<String> deletedIDs = <String>{};
try {
@ -108,7 +118,11 @@ Future<void> deleteFilesFromEverywhere(
if (deletedFiles.isNotEmpty) {
Bus.instance.fire(LocalPhotosUpdatedEvent(deletedFiles,
type: EventType.deletedFromEverywhere));
showShortToast("moved to trash");
if (hasLocalOnlyFiles && Platform.isAndroid) {
showShortToast("files deleted");
} else {
showShortToast("moved to trash");
}
}
await dialog.hide();
if (uploadedFilesToBeTrashed.isNotEmpty) {
@ -151,8 +165,8 @@ Future<void> deleteFilesFromRemoteOnly(
type: EventType.deletedFromRemote,
));
}
Bus.instance.fire(
LocalPhotosUpdatedEvent(files, type: EventType.deletedFromRemote));
Bus.instance
.fire(LocalPhotosUpdatedEvent(files, type: EventType.deletedFromRemote));
SyncService.instance.sync();
await dialog.hide();
RemoteSyncService.instance.sync(silently: true);
@ -166,6 +180,7 @@ Future<void> deleteFilesOnDeviceOnly(
final List<String> localAssetIDs = [];
final List<String> localSharedMediaIDs = [];
final List<String> alreadyDeletedIDs = []; // to ignore already deleted files
bool hasLocalOnlyFiles = false;
for (final file in files) {
if (file.localID != null) {
if (!(await _localFileExist(file))) {
@ -177,6 +192,16 @@ Future<void> deleteFilesOnDeviceOnly(
localAssetIDs.add(file.localID);
}
}
if (file.uploadedFileID == null) {
hasLocalOnlyFiles = true;
}
}
if (hasLocalOnlyFiles && Platform.isAndroid) {
final shouldProceed = await shouldProceedWithDeletion(context);
if (!shouldProceed) {
await dialog.hide();
return;
}
}
Set<String> deletedIDs = <String>{};
try {
@ -216,8 +241,8 @@ Future<bool> deleteFromTrash(BuildContext context, List<File> files) async {
await TrashSyncService.instance.deleteFromTrash(files);
showToast("successfully deleted");
await dialog.hide();
Bus.instance.fire(
FilesUpdatedEvent(files, type: EventType.deletedFromEverywhere));
Bus.instance
.fire(FilesUpdatedEvent(files, type: EventType.deletedFromEverywhere));
return true;
} catch (e, s) {
_logger.info("failed to delete from trash", e, s);
@ -390,3 +415,15 @@ Future<List<String>> _tryDeleteSharedMediaFiles(List<String> localIDs) {
return Future.value(actuallyDeletedIDs);
}
}
Future<bool> shouldProceedWithDeletion(BuildContext context) async {
final choice = await showChoiceDialog(
context,
"are you sure?",
"some of the files you are trying to delete are only available on your device and cannot be recovered if deleted",
firstAction: "cancel",
secondAction: "delete",
secondActionColor: Colors.red,
);
return choice == DialogUserChoice.secondChoice;
}