Factor out code to delete a file into a util
This commit is contained in:
parent
f04a0a4505
commit
d2ebcb12ca
4 changed files with 113 additions and 105 deletions
|
@ -11,6 +11,7 @@ import 'package:photos/models/file.dart';
|
|||
import 'package:photos/ui/video_widget.dart';
|
||||
import 'package:photos/ui/zoomable_image.dart';
|
||||
import 'package:photos/utils/date_time_util.dart';
|
||||
import 'package:photos/utils/delete_file_util.dart';
|
||||
import 'package:photos/utils/dialog_util.dart';
|
||||
import 'package:photos/utils/file_util.dart';
|
||||
import 'package:photos/utils/share_util.dart';
|
||||
|
|
|
@ -13,8 +13,8 @@ import 'package:photos/services/billing_service.dart';
|
|||
import 'package:photos/services/collections_service.dart';
|
||||
import 'package:photos/ui/create_collection_page.dart';
|
||||
import 'package:photos/ui/share_collection_widget.dart';
|
||||
import 'package:photos/utils/delete_file_util.dart';
|
||||
import 'package:photos/utils/dialog_util.dart';
|
||||
import 'package:photos/utils/file_util.dart';
|
||||
import 'package:photos/utils/share_util.dart';
|
||||
import 'package:photos/utils/toast_util.dart';
|
||||
|
||||
|
|
111
lib/utils/delete_file_util.dart
Normal file
111
lib/utils/delete_file_util.dart
Normal file
|
@ -0,0 +1,111 @@
|
|||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:photo_manager/photo_manager.dart';
|
||||
import 'package:photos/core/event_bus.dart';
|
||||
import 'package:photos/db/files_db.dart';
|
||||
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/services/sync_service.dart';
|
||||
import 'package:photos/utils/dialog_util.dart';
|
||||
import 'package:photos/utils/toast_util.dart';
|
||||
|
||||
final _logger = Logger("DeleteFileUtil");
|
||||
|
||||
Future<void> deleteFilesFromEverywhere(
|
||||
BuildContext context, List<File> files) async {
|
||||
final dialog = createProgressDialog(context, "deleting...");
|
||||
await dialog.show();
|
||||
final localIDs = List<String>();
|
||||
for (final file in files) {
|
||||
if (file.localID != null) {
|
||||
localIDs.add(file.localID);
|
||||
}
|
||||
}
|
||||
var deletedIDs;
|
||||
try {
|
||||
deletedIDs = (await PhotoManager.editor.deleteWithIds(localIDs)).toSet();
|
||||
} catch (e, s) {
|
||||
_logger.severe("Could not delete file", e, s);
|
||||
}
|
||||
final updatedCollectionIDs = Set<int>();
|
||||
final List<int> uploadedFileIDsToBeDeleted = [];
|
||||
final List<File> deletedFiles = [];
|
||||
for (final file in files) {
|
||||
if (file.localID != null) {
|
||||
// Remove only those files that have been removed from disk
|
||||
if (deletedIDs.contains(file.localID)) {
|
||||
deletedFiles.add(file);
|
||||
if (file.uploadedFileID != null) {
|
||||
uploadedFileIDsToBeDeleted.add(file.uploadedFileID);
|
||||
updatedCollectionIDs.add(file.collectionID);
|
||||
} else {
|
||||
await FilesDB.instance.deleteLocalFile(file.localID);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
uploadedFileIDsToBeDeleted.add(file.uploadedFileID);
|
||||
}
|
||||
}
|
||||
if (uploadedFileIDsToBeDeleted.isNotEmpty) {
|
||||
try {
|
||||
await SyncService.instance
|
||||
.deleteFilesOnServer(uploadedFileIDsToBeDeleted);
|
||||
await FilesDB.instance
|
||||
.deleteMultipleUploadedFiles(uploadedFileIDsToBeDeleted);
|
||||
} catch (e) {
|
||||
_logger.severe(e);
|
||||
await dialog.hide();
|
||||
showGenericErrorDialog(context);
|
||||
throw e;
|
||||
}
|
||||
for (final collectionID in updatedCollectionIDs) {
|
||||
Bus.instance.fire(CollectionUpdatedEvent(
|
||||
collectionID,
|
||||
deletedFiles
|
||||
.where((file) => file.collectionID == collectionID)
|
||||
.toList()));
|
||||
}
|
||||
}
|
||||
if (deletedFiles.isNotEmpty) {
|
||||
Bus.instance
|
||||
.fire(LocalPhotosUpdatedEvent(deletedFiles, type: EventType.deleted));
|
||||
}
|
||||
await dialog.hide();
|
||||
showToast("deleted from everywhere");
|
||||
if (uploadedFileIDsToBeDeleted.isNotEmpty) {
|
||||
SyncService.instance.syncWithRemote(silently: true);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteFilesOnDeviceOnly(
|
||||
BuildContext context, List<File> files) async {
|
||||
final dialog = createProgressDialog(context, "deleting...");
|
||||
await dialog.show();
|
||||
final localIDs = List<String>();
|
||||
for (final file in files) {
|
||||
if (file.localID != null) {
|
||||
localIDs.add(file.localID);
|
||||
}
|
||||
}
|
||||
final deletedIDs =
|
||||
(await PhotoManager.editor.deleteWithIds(localIDs)).toSet();
|
||||
final List<File> deletedFiles = [];
|
||||
for (final file in files) {
|
||||
// Remove only those files that have been removed from disk
|
||||
if (deletedIDs.contains(file.localID)) {
|
||||
deletedFiles.add(file);
|
||||
file.localID = null;
|
||||
FilesDB.instance.update(file);
|
||||
}
|
||||
}
|
||||
if (deletedFiles.isNotEmpty) {
|
||||
Bus.instance
|
||||
.fire(LocalPhotosUpdatedEvent(deletedFiles, type: EventType.deleted));
|
||||
}
|
||||
await dialog.hide();
|
||||
}
|
|
@ -3,132 +3,28 @@ import 'dart:io' as io;
|
|||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_sodium/flutter_sodium.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||||
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
||||
import 'package:photo_manager/photo_manager.dart';
|
||||
import 'package:photos/core/cache/image_cache.dart';
|
||||
import 'package:photos/core/cache/thumbnail_cache.dart';
|
||||
import 'package:photos/core/cache/thumbnail_cache_manager.dart';
|
||||
import 'package:photos/core/cache/video_cache_manager.dart';
|
||||
import 'package:photos/core/configuration.dart';
|
||||
import 'package:photos/core/constants.dart';
|
||||
import 'package:photos/core/event_bus.dart';
|
||||
import 'package:photos/core/network.dart';
|
||||
import 'package:photos/db/files_db.dart';
|
||||
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/file_type.dart';
|
||||
import 'package:photos/services/collections_service.dart';
|
||||
import 'package:photos/services/sync_service.dart';
|
||||
import 'package:photos/utils/dialog_util.dart';
|
||||
import 'package:photos/utils/thumbnail_util.dart';
|
||||
import 'package:photos/utils/toast_util.dart';
|
||||
|
||||
import 'crypto_util.dart';
|
||||
|
||||
final _logger = Logger("FileUtil");
|
||||
|
||||
Future<void> deleteFilesFromEverywhere(
|
||||
BuildContext context, List<File> files) async {
|
||||
final dialog = createProgressDialog(context, "deleting...");
|
||||
await dialog.show();
|
||||
final localIDs = List<String>();
|
||||
for (final file in files) {
|
||||
if (file.localID != null) {
|
||||
localIDs.add(file.localID);
|
||||
}
|
||||
}
|
||||
var deletedIDs;
|
||||
try {
|
||||
deletedIDs = (await PhotoManager.editor.deleteWithIds(localIDs)).toSet();
|
||||
} catch (e, s) {
|
||||
_logger.severe("Could not delete file", e, s);
|
||||
}
|
||||
final updatedCollectionIDs = Set<int>();
|
||||
final List<int> uploadedFileIDsToBeDeleted = [];
|
||||
final List<File> deletedFiles = [];
|
||||
for (final file in files) {
|
||||
if (file.localID != null) {
|
||||
// Remove only those files that have been removed from disk
|
||||
if (deletedIDs.contains(file.localID)) {
|
||||
deletedFiles.add(file);
|
||||
if (file.uploadedFileID != null) {
|
||||
uploadedFileIDsToBeDeleted.add(file.uploadedFileID);
|
||||
updatedCollectionIDs.add(file.collectionID);
|
||||
} else {
|
||||
await FilesDB.instance.deleteLocalFile(file.localID);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
uploadedFileIDsToBeDeleted.add(file.uploadedFileID);
|
||||
}
|
||||
}
|
||||
if (uploadedFileIDsToBeDeleted.isNotEmpty) {
|
||||
try {
|
||||
await SyncService.instance
|
||||
.deleteFilesOnServer(uploadedFileIDsToBeDeleted);
|
||||
await FilesDB.instance
|
||||
.deleteMultipleUploadedFiles(uploadedFileIDsToBeDeleted);
|
||||
} catch (e) {
|
||||
_logger.severe(e);
|
||||
await dialog.hide();
|
||||
showGenericErrorDialog(context);
|
||||
throw e;
|
||||
}
|
||||
for (final collectionID in updatedCollectionIDs) {
|
||||
Bus.instance.fire(CollectionUpdatedEvent(
|
||||
collectionID,
|
||||
deletedFiles
|
||||
.where((file) => file.collectionID == collectionID)
|
||||
.toList()));
|
||||
}
|
||||
}
|
||||
if (deletedFiles.isNotEmpty) {
|
||||
Bus.instance
|
||||
.fire(LocalPhotosUpdatedEvent(deletedFiles, type: EventType.deleted));
|
||||
}
|
||||
await dialog.hide();
|
||||
showToast("deleted from everywhere");
|
||||
if (uploadedFileIDsToBeDeleted.isNotEmpty) {
|
||||
SyncService.instance.syncWithRemote(silently: true);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteFilesOnDeviceOnly(
|
||||
BuildContext context, List<File> files) async {
|
||||
final dialog = createProgressDialog(context, "deleting...");
|
||||
await dialog.show();
|
||||
final localIDs = List<String>();
|
||||
for (final file in files) {
|
||||
if (file.localID != null) {
|
||||
localIDs.add(file.localID);
|
||||
}
|
||||
}
|
||||
final deletedIDs =
|
||||
(await PhotoManager.editor.deleteWithIds(localIDs)).toSet();
|
||||
final List<File> deletedFiles = [];
|
||||
for (final file in files) {
|
||||
// Remove only those files that have been removed from disk
|
||||
if (deletedIDs.contains(file.localID)) {
|
||||
deletedFiles.add(file);
|
||||
file.localID = null;
|
||||
FilesDB.instance.update(file);
|
||||
}
|
||||
}
|
||||
if (deletedFiles.isNotEmpty) {
|
||||
Bus.instance
|
||||
.fire(LocalPhotosUpdatedEvent(deletedFiles, type: EventType.deleted));
|
||||
}
|
||||
await dialog.hide();
|
||||
}
|
||||
|
||||
void preloadFile(File file) {
|
||||
if (file.fileType == FileType.video) {
|
||||
return;
|
||||
|
|
Loading…
Reference in a new issue