diff --git a/lib/appstate.dart b/lib/appstate.dart index 6f80994e..c97f3d94 100644 --- a/lib/appstate.dart +++ b/lib/appstate.dart @@ -6,7 +6,8 @@ import 'package:gitjournal/core/notes_folder_fs.dart'; enum SyncStatus { Unknown, Done, - Loading, + Pulling, + Pushing, Error, } diff --git a/lib/core/git_repo.dart b/lib/core/git_repo.dart index f067b51c..85eb4639 100644 --- a/lib/core/git_repo.dart +++ b/lib/core/git_repo.dart @@ -126,18 +126,21 @@ class GitNoteRepository { return _addNote(note, "Edited Note"); } - Future sync() async { + Future pull() async { try { await _gitRepo.pull(); } on GitException catch (ex) { Fimber.d(ex.toString()); } + } + Future push() async { try { await _gitRepo.push(); } on GitException catch (ex) { if (ex.cause == 'cannot push non-fastforwardable reference') { - return sync(); + await pull(); + return push(); } Fimber.d(ex.toString()); rethrow; diff --git a/lib/state_container.dart b/lib/state_container.dart index 3ec4a009..a16ad774 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -91,11 +91,19 @@ class StateContainer with ChangeNotifier { return true; } - appState.syncStatus = SyncStatus.Loading; + appState.syncStatus = SyncStatus.Pulling; notifyListeners(); + Future noteLoadingFuture; try { - await _gitRepo.sync(); + await _gitRepo.pull(); + + appState.syncStatus = SyncStatus.Pushing; + notifyListeners(); + + noteLoadingFuture = _loadNotes(); + + await _gitRepo.push(); Fimber.d("Synced!"); appState.syncStatus = SyncStatus.Done; @@ -109,7 +117,8 @@ class StateContainer with ChangeNotifier { } if (!doNotThrow) rethrow; } - await _loadNotes(); + + await noteLoadingFuture; } Future _syncNotes() async { diff --git a/lib/widgets/sync_button.dart b/lib/widgets/sync_button.dart index eed30405..185eb990 100644 --- a/lib/widgets/sync_button.dart +++ b/lib/widgets/sync_button.dart @@ -47,8 +47,16 @@ class _SyncButtonState extends State { }, ); } - if (appState.syncStatus == SyncStatus.Loading) { - return RotatingIcon(); + if (appState.syncStatus == SyncStatus.Pulling) { + return BlinkingIcon( + icon: Icon(Icons.arrow_downward), + ); + } + + if (appState.syncStatus == SyncStatus.Pushing) { + return BlinkingIcon( + icon: Icon(Icons.arrow_upward), + ); } return IconButton( @@ -83,12 +91,18 @@ class _SyncButtonState extends State { } } -class RotatingIcon extends StatefulWidget { +class BlinkingIcon extends StatefulWidget { + final Icon icon; + final int interval; + + BlinkingIcon({@required this.icon, this.interval = 500, Key key}) + : super(key: key); + @override - _RotatingIconState createState() => _RotatingIconState(); + _BlinkingIconState createState() => _BlinkingIconState(); } -class _RotatingIconState extends State +class _BlinkingIconState extends State with SingleTickerProviderStateMixin { AnimationController _controller; Animation _animation; @@ -98,8 +112,8 @@ class _RotatingIconState extends State super.initState(); _controller = AnimationController( + duration: Duration(milliseconds: widget.interval), vsync: this, - duration: const Duration(milliseconds: 1800), ); _animation = CurvedAnimation( parent: _controller, @@ -117,14 +131,12 @@ class _RotatingIconState extends State @override Widget build(BuildContext context) { - var button = IconButton( - icon: const Icon(Icons.loop), - onPressed: () {}, - ); - - return RotationTransition( - child: button, - turns: _animation, + return FadeTransition( + opacity: _animation, + child: IconButton( + icon: widget.icon, + onPressed: () {}, + ), ); } }