Provide a hook to pause ongoing backups

This commit is contained in:
Vishnu Mohandas 2020-09-18 01:10:08 +05:30
parent 1cc7db043f
commit c77fd5da11
3 changed files with 77 additions and 5 deletions

View file

@ -2,6 +2,12 @@ class PhotoUploadEvent {
final int completed;
final int total;
final bool hasError;
final bool wasStopped;
PhotoUploadEvent({this.completed, this.total, this.hasError = false});
PhotoUploadEvent({
this.completed,
this.total,
this.hasError = false,
this.wasStopped = false,
});
}

View file

@ -24,6 +24,7 @@ class PhotoSyncManager {
final _uploader = FileUploader();
final _downloader = DiffFetcher();
bool _isSyncInProgress = false;
bool _syncStopRequested = false;
Future<void> _existingSync;
SharedPreferences _prefs;
@ -46,6 +47,7 @@ class PhotoSyncManager {
}
Future<void> sync() async {
_syncStopRequested = false;
if (_isSyncInProgress) {
_logger.warning("Sync already in progress, skipping.");
return _existingSync;
@ -64,6 +66,15 @@ class PhotoSyncManager {
return _existingSync;
}
void stopSync() {
_logger.info("Sync stop requested");
_syncStopRequested = true;
}
bool shouldStopSync() {
return _syncStopRequested;
}
bool hasScannedDisk() {
return _prefs.containsKey(_dbUpdationTimeKey);
}
@ -198,11 +209,13 @@ class PhotoSyncManager {
List<File> filesToBeUploaded =
await _db.getFilesToBeUploadedWithinFolders(foldersToBackUp);
for (int i = 0; i < filesToBeUploaded.length; i++) {
if (_syncStopRequested) {
_syncStopRequested = false;
Bus.instance.fire(PhotoUploadEvent(wasStopped: true));
return;
}
File file = filesToBeUploaded[i];
try {
if (!foldersToBackUp.contains(file.deviceFolder)) {
continue;
}
var uploadedFile;
if (Configuration.instance.hasOptedForE2E()) {
uploadedFile = await _uploader.encryptAndUploadFile(file);

View file

@ -1,8 +1,10 @@
import 'dart:async';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:photos/core/event_bus.dart';
import 'package:photos/events/photo_upload_event.dart';
import 'package:photos/photo_sync_manager.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
class SyncIndicator extends StatefulWidget {
@ -17,6 +19,7 @@ class SyncIndicator extends StatefulWidget {
class _SyncIndicatorState extends State<SyncIndicator> {
PhotoUploadEvent _event;
StreamSubscription<PhotoUploadEvent> _subscription;
String _completeText = "Sync completed.";
@override
void initState() {
@ -40,10 +43,58 @@ class _SyncIndicatorState extends State<SyncIndicator> {
idleText: "Pull down to sync.",
refreshingText: _getRefreshingText(),
releaseText: "Release to sync.",
completeText: "Sync completed.",
completeText: _completeText,
failedText: "Sync unsuccessful.",
completeDuration: const Duration(milliseconds: 1000),
refreshStyle: RefreshStyle.UnFollow,
refreshingIcon: Container(
width: 24,
height: 24,
child: GestureDetector(
onTap: () {
AlertDialog alert = AlertDialog(
title: Text("Pause?"),
content: Text(
"Are you sure that you want to pause backing up your memories?"),
actions: [
FlatButton(
child: Text("NO"),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text("YES"),
onPressed: () {
Navigator.of(context).pop();
PhotoSyncManager.instance.stopSync();
_completeText = "Sync stopped.";
setState(() {});
widget.refreshController.refreshCompleted();
},
),
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
},
child: Stack(
children: [
Icon(
Icons.pause_circle_outline,
size: 24,
color: Colors.pink,
),
CircularProgressIndicator(strokeWidth: 2),
],
),
),
),
);
}
@ -55,6 +106,8 @@ class _SyncIndicatorState extends State<SyncIndicator> {
if (_event.hasError) {
widget.refreshController.refreshFailed();
s = "Upload failed.";
} else if (_event.wasStopped) {
s = "Sync stopped.";
} else {
s = "Backing up " +
_event.completed.toString() +