Show the sync indicator only when there's a diff to be applied or uploaded

This commit is contained in:
Vishnu Mohandas 2020-12-04 03:17:06 +05:30
parent ffb13f672e
commit 3b138aa6f4
3 changed files with 42 additions and 32 deletions

View file

@ -17,7 +17,9 @@ class SyncStatusUpdate extends Event {
}
enum SyncStatus {
not_started,
applying_local_diff,
applying_remote_diff,
preparing_for_upload,
in_progress,
paused,
completed,

View file

@ -75,12 +75,13 @@ class SyncService {
return _existingSync;
}
_isSyncInProgress = true;
Bus.instance.fire(SyncStatusUpdate(SyncStatus.not_started));
_existingSync = Future<void>(() async {
_logger.info("Syncing...");
try {
await _doSync();
Bus.instance.fire(SyncStatusUpdate(SyncStatus.completed));
if (_lastSyncStatusEvent != null) {
Bus.instance.fire(SyncStatusUpdate(SyncStatus.completed));
}
} on WiFiUnavailableError {
_logger.warning("Not uploading over mobile data");
Bus.instance.fire(
@ -151,6 +152,7 @@ class SyncService {
getMonthAndYear(DateTime.fromMicrosecondsSinceEpoch(toTime)));
final files = await getDeviceFiles(fromTime, toTime);
if (files.isNotEmpty) {
Bus.instance.fire(SyncStatusUpdate(SyncStatus.applying_local_diff));
_logger.info("Fetched " + files.length.toString() + " files.");
final updatedFiles =
files.where((file) => existingLocalFileIDs.contains(file.localID));
@ -178,6 +180,9 @@ class SyncService {
return Future.error("Account not configured yet");
}
final updatedCollections = await _collectionsService.sync();
if (updatedCollections.isNotEmpty) {
Bus.instance.fire(SyncStatusUpdate(SyncStatus.applying_remote_diff));
}
for (final collection in updatedCollections) {
await _fetchEncryptedFilesDiff(collection.id);
}
@ -217,6 +222,10 @@ class SyncService {
int uploadCounter = 0;
final totalUploads = filesToBeUploaded.length + updatedFileIDs.length;
if (totalUploads > 0) {
Bus.instance.fire(SyncStatusUpdate(SyncStatus.preparing_for_upload));
}
for (final uploadedFileID in updatedFileIDs) {
if (_syncStopRequested) {
_syncStopRequested = false;

View file

@ -16,7 +16,6 @@ class SyncIndicator extends StatefulWidget {
class _SyncIndicatorState extends State<SyncIndicator> {
SyncStatusUpdate _event;
double _containerHeight = 48;
int _latestCompletedCount = 0;
StreamSubscription<SyncStatusUpdate> _subscription;
@override
@ -24,10 +23,6 @@ class _SyncIndicatorState extends State<SyncIndicator> {
_subscription = Bus.instance.on<SyncStatusUpdate>().listen((event) {
setState(() {
_event = event;
if (_event.status == SyncStatus.in_progress &&
_event.completed > _latestCompletedCount) {
_latestCompletedCount = _event.completed;
}
});
});
_event = SyncService.instance.getLastSyncStatusEvent();
@ -70,7 +65,7 @@ class _SyncIndicatorState extends State<SyncIndicator> {
duration: Duration(milliseconds: 300),
height: _containerHeight,
width: double.infinity,
margin: EdgeInsets.all(8),
padding: EdgeInsets.all(8),
alignment: Alignment.center,
child: SingleChildScrollView(
physics: NeverScrollableScrollPhysics(),
@ -83,12 +78,12 @@ class _SyncIndicatorState extends State<SyncIndicator> {
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: 24,
height: 24,
width: 20,
height: 20,
child: icon,
),
Padding(
padding: const EdgeInsets.fromLTRB(8, 4, 0, 0),
padding: const EdgeInsets.fromLTRB(12, 4, 0, 0),
child: Text(_getRefreshingText()),
),
],
@ -104,27 +99,31 @@ class _SyncIndicatorState extends State<SyncIndicator> {
}
String _getRefreshingText() {
if (_event == null || _event.status == SyncStatus.not_started) {
if (_event == null ||
_event.status == SyncStatus.applying_local_diff ||
_event.status == SyncStatus.applying_remote_diff) {
return "Syncing...";
} else {
var s;
if (_event.status == SyncStatus.error) {
s = "Upload failed.";
} else if (_event.status == SyncStatus.completed) {
if (_event.wasStopped) {
s = "Sync stopped.";
} else {
s = "All memories preserved.";
}
} else if (_event.status == SyncStatus.paused) {
s = _event.reason;
} else {
s = _latestCompletedCount.toString() +
"/" +
_event.total.toString() +
" memories preserved";
}
return s;
}
if (_event.status == SyncStatus.preparing_for_upload) {
return "Preparing backup...";
}
if (_event.status == SyncStatus.in_progress) {
return _event.completed.toString() +
"/" +
_event.total.toString() +
" memories preserved.";
}
if (_event.status == SyncStatus.paused) {
return _event.reason;
}
if (_event.status == SyncStatus.completed) {
if (_event.wasStopped) {
return "Sync stopped.";
} else {
return "All memories preserved.";
}
}
// _event.status == SyncStatus.error)
return "Upload failed.";
}
}