Add support for deleting files from trash

This commit is contained in:
Neeraj Gupta 2021-10-13 04:21:19 +05:30
parent 676a193d4f
commit 49df079bcc
No known key found for this signature in database
GPG key ID: 3C5A1684DC1729E1
3 changed files with 59 additions and 5 deletions

View file

@ -4,6 +4,7 @@ import 'package:photos/core/configuration.dart';
import 'package:photos/core/network.dart';
import 'package:photos/db/files_db.dart';
import 'package:photos/db/trash_db.dart';
import 'package:photos/models/trash_file.dart';
import 'package:photos/models/trash_item_request.dart';
import 'package:photos/utils/trash_diff_fetcher.dart';
import 'package:shared_preferences/shared_preferences.dart';
@ -42,8 +43,8 @@ class TrashSyncService {
}
if (diff.restoredFiles.isNotEmpty) {
_logger.fine("discard ${diff.restoredFiles.length} restored items");
await _trashDB.delete(
diff.restoredFiles.map((e) => e.uploadedFileID).toList());
await _trashDB
.delete(diff.restoredFiles.map((e) => e.uploadedFileID).toList());
}
if (diff.lastSyncedTimeStamp != 0) {
await setSyncTime(diff.lastSyncedTimeStamp);
@ -68,7 +69,6 @@ class TrashSyncService {
final params = <String, dynamic>{};
final includedFileIDs = <int>{};
params["items"] = [];
for (final item in trashRequestItems) {
if (!includedFileIDs.contains(item.fileID)) {
params["items"].add(item.toJson());
@ -85,4 +85,29 @@ class TrashSyncService {
data: params,
);
}
Future<void> deleteFromTrash(List<TrashFile> trashedFiles) async {
final params = <String, dynamic>{};
final uniqueFileIds =
trashedFiles.map((e) => e.uploadedFileID).toSet().toList();
params["fileIDs"] = [];
for (final fileID in uniqueFileIds) {
params["fileIDs"].add(fileID);
}
try {
await _dio.post(
Configuration.instance.getHttpEndpoint() + "/trash/delete",
options: Options(
headers: {
"X-Auth-Token": Configuration.instance.getToken(),
},
),
data: params,
);
_trashDB.delete(uniqueFileIds);
} catch (e, s) {
_logger.severe("failed to delete from trash", e, s);
rethrow;
}
}
}

View file

@ -9,9 +9,12 @@ import 'package:photos/models/magic_metadata.dart';
import 'package:photos/models/trash_file.dart';
import 'package:photos/ui/file_info_dialog.dart';
import 'package:photos/utils/archive_util.dart';
import 'package:photos/utils/delete_file_util.dart';
import 'package:photos/utils/share_util.dart';
import 'package:photos/utils/toast_util.dart';
import 'common/dialogs.dart';
class FadingBottomBar extends StatefulWidget {
final File file;
final Function(File) onEditRequested;
@ -190,8 +193,10 @@ class FadingBottomBarState extends State<FadingBottomBar> {
padding: const EdgeInsets.only(top: 12, bottom: 12),
child: IconButton(
icon: Icon(Icons.delete_forever_outlined),
onPressed: () {
showToast("coming soon");
onPressed: () async {
final trashedFile = <TrashFile>[];
trashedFile.add(widget.file);
await deleteFromTrash(context, trashedFile);
},
),
),

View file

@ -16,10 +16,12 @@ import 'package:photos/events/collection_updated_event.dart';
import 'package:photos/events/files_updated_event.dart';
import 'package:photos/events/local_photos_updated_event.dart';
import 'package:photos/models/file.dart';
import 'package:photos/models/trash_file.dart';
import 'package:photos/models/trash_item_request.dart';
import 'package:photos/services/remote_sync_service.dart';
import 'package:photos/services/sync_service.dart';
import 'package:photos/services/trash_sync_service.dart';
import 'package:photos/ui/common/dialogs.dart';
import 'package:photos/ui/linear_progress_dialog.dart';
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/toast_util.dart';
@ -195,6 +197,28 @@ Future<void> deleteFilesOnDeviceOnly(
await dialog.hide();
}
Future<void> deleteFromTrash(
BuildContext context, List<TrashFile> files) async {
final result = await showChoiceDialog(context, "delete permanently?",
"the files will be permanently removed from your ente account",
firstAction: "delete", actionType: ActionType.critical);
if( result != DialogUserChoice.firstChoice) {
return;
}
final dialog = createProgressDialog(context, "permanently deleting...");
await dialog.show();
try {
await TrashSyncService.instance.deleteFromTrash(files);
await dialog.hide();
showToast("successfully deleted");
Bus.instance.fire(FilesUpdatedEvent(files, type: EventType.deleted));
} catch (e, s) {
Logger("TrashUtil").info("failed to delete from trash", e, s);
await dialog.hide();
await showGenericErrorDialog(context);
}
}
Future<bool> deleteLocalFiles(
BuildContext context, List<String> localIDs) async {
final List<String> deletedIDs = [];