From a1df9dc1031a63a9cb053e7f8adb84952c271ad2 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Fri, 11 Aug 2023 19:36:23 +0530 Subject: [PATCH] Fallback to batch delete if bulkDelete fails to delete --- lib/utils/delete_file_util.dart | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/utils/delete_file_util.dart b/lib/utils/delete_file_util.dart index 412f9656b..029e5d750 100644 --- a/lib/utils/delete_file_util.dart +++ b/lib/utils/delete_file_util.dart @@ -339,9 +339,18 @@ Future deleteLocalFiles( } // In IOS, the library returns no error and fail to delete any file is // there's any shared file. As a stop-gap solution, we initiate deletion in - // batches - if (Platform.isIOS && deletedIDs.isEmpty) { - deletedIDs.addAll(await deleteLocalFilesInBatches(context, localAssetIDs)); + // batches. Similar in Android, for large number of files, we have observed + // that the library fails to delete any file. So, we initiate deletion in + // batches. + if (deletedIDs.isEmpty) { + deletedIDs.addAll( + await deleteLocalFilesInBatches( + context, + localAssetIDs, + maximumBatchSize: 1000, + minimumBatchSize: 10, + ), + ); _logger .severe("iOS free-space fallback, deleted ${deletedIDs.length} files " "in batches}"); @@ -386,8 +395,11 @@ Future> _deleteLocalFilesInOneShot( Future> deleteLocalFilesInBatches( BuildContext context, - List localIDs, -) async { + List localIDs, { + int minimumParts = 10, + int minimumBatchSize = 1, + int maximumBatchSize = 100, +}) async { final dialogKey = GlobalKey(); final dialog = LinearProgressDialog( "Deleting " + localIDs.length.toString() + " backed up files...", @@ -400,9 +412,6 @@ Future> deleteLocalFilesInBatches( }, barrierColor: Colors.black.withOpacity(0.85), ); - const minimumParts = 10; - const minimumBatchSize = 1; - const maximumBatchSize = 100; final batchSize = min( max(minimumBatchSize, (localIDs.length / minimumParts).round()), maximumBatchSize,