Delete files in batches on Android

This commit is contained in:
Vishnu 2021-06-28 18:24:57 +05:30
parent 8ba259e147
commit 6bd8f413c3
2 changed files with 38 additions and 11 deletions

View file

@ -163,19 +163,20 @@ class _FreeSpacePageState extends State<FreeSpacePage> {
height: 64,
padding: const EdgeInsets.fromLTRB(80, 0, 80, 0),
child: button(
"free up " + convertBytesToReadableFormat(status.size),
"free up " + formatBytes(status.size),
onPressed: () async {
final dialog =
createProgressDialog(context, "freeing up space...");
final dialog = createProgressDialog(
context,
"deleting " +
status.localIDs.length.toString() +
" backed up files...");
await dialog.show();
deleteLocalFiles(status.localIDs);
await deleteLocalFiles(status.localIDs);
await dialog.hide();
// Navigator.of(context).pop();
// _confettiController.play();
AlertDialog alert = AlertDialog(
title: Text("success"),
content: Text("you have successfully freed up " +
convertBytesToReadableFormat(status.size) +
formatBytes(status.size) +
"!"),
actions: [
TextButton(

View file

@ -1,4 +1,6 @@
import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';
@ -138,10 +140,34 @@ Future<void> deleteFilesOnDeviceOnly(
Future<void> deleteLocalFiles(List<String> localIDs) async {
List<String> deletedIDs = [];
try {
deletedIDs = await PhotoManager.editor.deleteWithIds(localIDs);
} catch (e, s) {
_logger.severe("Could not delete files", e, s);
if (Platform.isAndroid) {
const batchSize = 100;
for (int index = 0; index < localIDs.length; index += batchSize) {
final ids = localIDs
.getRange(index, min(localIDs.length, index + batchSize))
.toList();
_logger.info("Trying to delete " + ids.toString());
try {
deletedIDs.addAll(await PhotoManager.editor.deleteWithIds(ids));
_logger.info("Deleted " + ids.toString());
} catch (e, s) {
_logger.severe("Could not delete batch " + ids.toString(), e, s);
for (final id in ids) {
try {
deletedIDs.addAll(await PhotoManager.editor.deleteWithIds([id]));
_logger.info("Deleted " + id);
} catch (e, s) {
_logger.severe("Could not delete file " + id, e, s);
}
}
}
}
} else {
try {
deletedIDs.addAll(await PhotoManager.editor.deleteWithIds(localIDs));
} catch (e, s) {
_logger.severe("Could not delete files ", e, s);
}
}
if (deletedIDs.isNotEmpty) {
final deletedFiles = await FilesDB.instance.getLocalFiles(deletedIDs);