Add progress dialog

This commit is contained in:
Vishnu 2021-06-28 21:22:21 +05:30
parent edaff6b3ee
commit 3419e42095
3 changed files with 110 additions and 36 deletions

View file

@ -139,7 +139,7 @@ class _FreeSpacePageState extends State<FreeSpacePage> {
Container(
width: double.infinity,
height: 64,
padding: const EdgeInsets.fromLTRB(80, 0, 80, 0),
padding: const EdgeInsets.fromLTRB(60, 0, 60, 0),
child: button(
"free up " + formatBytes(status.size),
onPressed: () async {
@ -154,14 +154,7 @@ class _FreeSpacePageState extends State<FreeSpacePage> {
}
Future<void> _freeStorage(BackupStatus status) async {
final dialog = createProgressDialog(
context,
"deleting " +
status.localIDs.length.toString() +
" backed up files...");
await dialog.show();
await deleteLocalFiles(status.localIDs);
await dialog.hide();
await deleteLocalFiles(context, status.localIDs);
Navigator.of(context).pop(true);
}
}

View file

@ -0,0 +1,47 @@
import 'package:flutter/material.dart';
class LinearProgressDialog extends StatefulWidget {
final String message;
const LinearProgressDialog(this.message, {Key key}) : super(key: key);
@override
LinearProgressDialogState createState() => LinearProgressDialogState();
}
class LinearProgressDialogState extends State<LinearProgressDialog> {
double _progress;
@override
void initState() {
_progress = 0;
super.initState();
}
void setProgress(double progress) {
setState(() {
_progress = progress;
});
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async => false,
child: AlertDialog(
title: Text(
widget.message,
style: TextStyle(
fontSize: 16,
),
textAlign: TextAlign.center,
),
content: LinearProgressIndicator(
value: _progress,
valueColor:
AlwaysStoppedAnimation<Color>(Theme.of(context).buttonColor),
),
),
);
}
}

View file

@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';
import 'package:photo_manager/photo_manager.dart';
@ -13,6 +14,7 @@ import 'package:photos/events/local_photos_updated_event.dart';
import 'package:photos/models/file.dart';
import 'package:photos/services/remote_sync_service.dart';
import 'package:photos/services/sync_service.dart';
import 'package:photos/ui/linear_progress_dialog.dart';
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/toast_util.dart';
@ -138,36 +140,13 @@ Future<void> deleteFilesOnDeviceOnly(
await dialog.hide();
}
Future<void> deleteLocalFiles(List<String> localIDs) async {
Future<void> deleteLocalFiles(
BuildContext context, List<String> localIDs) async {
List<String> deletedIDs = [];
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);
}
}
}
}
await _deleteLocalFilesOnAndroid(context, localIDs, deletedIDs);
} else {
try {
deletedIDs.addAll(await PhotoManager.editor.deleteWithIds(localIDs));
} catch (e, s) {
_logger.severe("Could not delete files ", e, s);
}
await _deleteLocalFilesOnIOS(context, localIDs, deletedIDs);
}
if (deletedIDs.isNotEmpty) {
final deletedFiles = await FilesDB.instance.getLocalFiles(deletedIDs);
@ -177,3 +156,58 @@ Future<void> deleteLocalFiles(List<String> localIDs) async {
.fire(LocalPhotosUpdatedEvent(deletedFiles, type: EventType.deleted));
}
}
Future<void> _deleteLocalFilesOnIOS(BuildContext context, List<String> localIDs,
List<String> deletedIDs) async {
final dialog = createProgressDialog(
context, "deleting " + localIDs.length.toString() + " files...");
await dialog.show();
try {
deletedIDs.addAll(await PhotoManager.editor.deleteWithIds(localIDs));
} catch (e, s) {
_logger.severe("Could not delete files ", e, s);
}
await dialog.hide();
}
Future<void> _deleteLocalFilesOnAndroid(BuildContext context,
List<String> localIDs, List<String> deletedIDs) async {
final dialogKey = GlobalKey<LinearProgressDialogState>();
final dialog = LinearProgressDialog(
"deleting " + localIDs.length.toString() + " files...",
key: dialogKey,
);
showDialog(
context: context,
builder: (context) {
return dialog;
},
);
const batchSize = 100;
for (int index = 0; index < localIDs.length; index += batchSize) {
if (dialogKey.currentState != null) {
dialogKey.currentState.setProgress(index / localIDs.length);
}
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);
}
}
}
}
Navigator.of(dialogKey.currentContext, rootNavigator: true).pop('dialog');
}