Refresh sync indicator on user login

This commit is contained in:
Vishnu Mohandas 2020-11-15 12:49:51 +05:30
parent 1218482d12
commit d1834985d5
3 changed files with 22 additions and 12 deletions

View file

@ -3,13 +3,20 @@ import 'package:photos/events/event.dart';
class SyncStatusUpdate extends Event {
final int completed;
final int total;
final bool hasError;
final bool wasStopped;
final SyncStatus status;
SyncStatusUpdate({
SyncStatusUpdate(
this.status, {
this.completed,
this.total,
this.hasError = false,
this.wasStopped = false,
});
}
enum SyncStatus {
not_started,
in_progress,
completed,
error,
}

View file

@ -56,6 +56,7 @@ class SyncService {
return _existingSync;
}
_isSyncInProgress = true;
Bus.instance.fire(SyncStatusUpdate(SyncStatus.not_started));
_existingSync = Future<void>(() async {
_logger.info("Syncing...");
try {
@ -64,7 +65,7 @@ class SyncService {
_logger.severe(e, s);
} finally {
_isSyncInProgress = false;
Bus.instance.fire(SyncStatusUpdate(hasError: true));
Bus.instance.fire(SyncStatusUpdate(SyncStatus.error));
}
});
return _existingSync;
@ -183,7 +184,8 @@ class SyncService {
for (int i = 0; i < filesToBeUploaded.length; i++) {
if (_syncStopRequested) {
_syncStopRequested = false;
Bus.instance.fire(SyncStatusUpdate(wasStopped: true));
Bus.instance
.fire(SyncStatusUpdate(SyncStatus.completed, wasStopped: true));
return;
}
File file = filesToBeUploaded[i];
@ -194,12 +196,12 @@ class SyncService {
final future = _uploader.upload(file, collectionID).then((value) {
Bus.instance
.fire(CollectionUpdatedEvent(collectionID: file.collectionID));
Bus.instance.fire(SyncStatusUpdate(
Bus.instance.fire(SyncStatusUpdate(SyncStatus.in_progress,
completed: i + 1, total: filesToBeUploaded.length));
});
futures.add(future);
} catch (e, s) {
Bus.instance.fire(SyncStatusUpdate(hasError: true));
Bus.instance.fire(SyncStatusUpdate(SyncStatus.error));
_logger.severe(e, s);
}
}
@ -207,7 +209,7 @@ class SyncService {
await Future.wait(futures);
} catch (e, s) {
_isSyncInProgress = false;
Bus.instance.fire(SyncStatusUpdate(hasError: true));
Bus.instance.fire(SyncStatusUpdate(SyncStatus.error));
_logger.severe("Error in syncing files", e, s);
}
}

View file

@ -23,7 +23,8 @@ class _SyncIndicatorState extends State<SyncIndicator> {
_subscription = Bus.instance.on<SyncStatusUpdate>().listen((event) {
setState(() {
_event = event;
if (!_event.hasError && _event.completed > _latestCompletedCount) {
if (_event.status == SyncStatus.in_progress &&
_event.completed > _latestCompletedCount) {
_latestCompletedCount = _event.completed;
}
});
@ -76,14 +77,14 @@ class _SyncIndicatorState extends State<SyncIndicator> {
}
String _getRefreshingText() {
if (_event == null) {
if (_event == null || _event.status == SyncStatus.not_started) {
return "Syncing...";
} else {
var s;
// TODO: Display errors softly
if (_event.hasError) {
if (_event.status == SyncStatus.error) {
s = "Upload failed.";
} else if (_event.wasStopped) {
} else if (_event.status == SyncStatus.completed && _event.wasStopped) {
s = "Sync stopped.";
} else {
s = _latestCompletedCount.toString() +