SyncButton: Seperate pulling and pushing

This way we also refresh the view after the pulling, as we don't need to
wait for a push for that.
This commit is contained in:
Vishesh Handa
2020-03-30 13:00:28 +02:00
parent 040a1a228a
commit 577b73ff3c
4 changed files with 45 additions and 20 deletions

View File

@ -6,7 +6,8 @@ import 'package:gitjournal/core/notes_folder_fs.dart';
enum SyncStatus { enum SyncStatus {
Unknown, Unknown,
Done, Done,
Loading, Pulling,
Pushing,
Error, Error,
} }

View File

@ -126,18 +126,21 @@ class GitNoteRepository {
return _addNote(note, "Edited Note"); return _addNote(note, "Edited Note");
} }
Future<void> sync() async { Future<void> pull() async {
try { try {
await _gitRepo.pull(); await _gitRepo.pull();
} on GitException catch (ex) { } on GitException catch (ex) {
Fimber.d(ex.toString()); Fimber.d(ex.toString());
} }
}
Future<void> push() async {
try { try {
await _gitRepo.push(); await _gitRepo.push();
} on GitException catch (ex) { } on GitException catch (ex) {
if (ex.cause == 'cannot push non-fastforwardable reference') { if (ex.cause == 'cannot push non-fastforwardable reference') {
return sync(); await pull();
return push();
} }
Fimber.d(ex.toString()); Fimber.d(ex.toString());
rethrow; rethrow;

View File

@ -91,11 +91,19 @@ class StateContainer with ChangeNotifier {
return true; return true;
} }
appState.syncStatus = SyncStatus.Loading; appState.syncStatus = SyncStatus.Pulling;
notifyListeners(); notifyListeners();
Future noteLoadingFuture;
try { try {
await _gitRepo.sync(); await _gitRepo.pull();
appState.syncStatus = SyncStatus.Pushing;
notifyListeners();
noteLoadingFuture = _loadNotes();
await _gitRepo.push();
Fimber.d("Synced!"); Fimber.d("Synced!");
appState.syncStatus = SyncStatus.Done; appState.syncStatus = SyncStatus.Done;
@ -109,7 +117,8 @@ class StateContainer with ChangeNotifier {
} }
if (!doNotThrow) rethrow; if (!doNotThrow) rethrow;
} }
await _loadNotes();
await noteLoadingFuture;
} }
Future<void> _syncNotes() async { Future<void> _syncNotes() async {

View File

@ -47,8 +47,16 @@ class _SyncButtonState extends State<SyncButton> {
}, },
); );
} }
if (appState.syncStatus == SyncStatus.Loading) { if (appState.syncStatus == SyncStatus.Pulling) {
return RotatingIcon(); return BlinkingIcon(
icon: Icon(Icons.arrow_downward),
);
}
if (appState.syncStatus == SyncStatus.Pushing) {
return BlinkingIcon(
icon: Icon(Icons.arrow_upward),
);
} }
return IconButton( return IconButton(
@ -83,12 +91,18 @@ class _SyncButtonState extends State<SyncButton> {
} }
} }
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 @override
_RotatingIconState createState() => _RotatingIconState(); _BlinkingIconState createState() => _BlinkingIconState();
} }
class _RotatingIconState extends State<RotatingIcon> class _BlinkingIconState extends State<BlinkingIcon>
with SingleTickerProviderStateMixin { with SingleTickerProviderStateMixin {
AnimationController _controller; AnimationController _controller;
Animation<double> _animation; Animation<double> _animation;
@ -98,8 +112,8 @@ class _RotatingIconState extends State<RotatingIcon>
super.initState(); super.initState();
_controller = AnimationController( _controller = AnimationController(
duration: Duration(milliseconds: widget.interval),
vsync: this, vsync: this,
duration: const Duration(milliseconds: 1800),
); );
_animation = CurvedAnimation( _animation = CurvedAnimation(
parent: _controller, parent: _controller,
@ -117,14 +131,12 @@ class _RotatingIconState extends State<RotatingIcon>
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var button = IconButton( return FadeTransition(
icon: const Icon(Icons.loop), opacity: _animation,
onPressed: () {}, child: IconButton(
); icon: widget.icon,
onPressed: () {},
return RotationTransition( ),
child: button,
turns: _animation,
); );
} }
} }