From 0c0fd175436c21b27d6fcfb365c9363a8789cef5 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sun, 9 Feb 2020 15:53:21 +0100 Subject: [PATCH] syncNotes: Catch the exception outside the StateContainer The StateContainer doesn't have access to the snackbar. --- lib/screens/journal_listing.dart | 16 ++++++++---- lib/state_container.dart | 44 ++++++++++++++++---------------- lib/widgets/sync_button.dart | 9 +++++-- 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/lib/screens/journal_listing.dart b/lib/screens/journal_listing.dart index 2acf0ba3..4ab4f190 100644 --- a/lib/screens/journal_listing.dart +++ b/lib/screens/journal_listing.dart @@ -49,9 +49,11 @@ class JournalListingScreen extends StatelessWidget { ), floatingActionButton: createButton, body: Center( - child: RefreshIndicator( - child: Scrollbar(child: buildJournalList(notesFolder)), - onRefresh: () async => _syncRepo(context), + child: Builder( + builder: (context) => RefreshIndicator( + child: Scrollbar(child: buildJournalList(notesFolder)), + onRefresh: () async => _syncRepo(context), + ), ), ), drawer: AppDrawer(), @@ -59,8 +61,12 @@ class JournalListingScreen extends StatelessWidget { } void _syncRepo(BuildContext context) async { - final container = StateContainer.of(context); - await container.syncNotes(); + try { + final container = StateContainer.of(context); + await container.syncNotes(); + } catch (e) { + showSnackbar(context, e.toString()); + } } void _newPost(BuildContext context) { diff --git a/lib/state_container.dart b/lib/state_container.dart index 321b3724..f8b87594 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -68,7 +68,7 @@ class StateContainerState extends State { } _loadNotes(); - syncNotes(); + _syncNotes(); } void removeExistingRemoteClone() async { @@ -87,7 +87,7 @@ class StateContainerState extends State { await appState.notesFolder.loadRecursively(); } - Future syncNotes() async { + Future syncNotes({bool doNotThrow = false}) async { if (!appState.remoteGitRepoConfigured) { Fimber.d("Not syncing because RemoteRepo not configured"); return true; @@ -112,11 +112,15 @@ class StateContainerState extends State { if (shouldLogGitException(e)) { await FlutterCrashlytics().logException(e, stacktrace); } - showSnackbar(context, e.cause); + if (!doNotThrow) rethrow; } await _loadNotes(); } + Future _syncNotes() { + return syncNotes(doNotThrow: true); + } + void createFolder(NotesFolder parent, String folderName) async { var newFolderPath = p.join(parent.folderPath, folderName); var newFolder = NotesFolder(parent, newFolderPath); @@ -126,7 +130,7 @@ class StateContainerState extends State { parent.addFolder(newFolder); _gitRepo.addFolder(newFolder).then((NoteRepoResult _) { - syncNotes(); + _syncNotes(); }); } @@ -135,7 +139,7 @@ class StateContainerState extends State { folder.parent.removeFolder(folder); _gitRepo.removeFolder(folder.folderPath).then((NoteRepoResult _) { - syncNotes(); + _syncNotes(); }); } @@ -146,7 +150,7 @@ class StateContainerState extends State { _gitRepo .renameFolder(oldFolderPath, folder.folderPath) .then((NoteRepoResult _) { - syncNotes(); + _syncNotes(); }); } @@ -155,7 +159,7 @@ class StateContainerState extends State { note.rename(newFileName); _gitRepo.renameNote(oldNotePath, note.filePath).then((NoteRepoResult _) { - syncNotes(); + _syncNotes(); }); } @@ -167,12 +171,17 @@ class StateContainerState extends State { note.move(destFolder); _gitRepo.moveNote(oldNotePath, note.filePath).then((NoteRepoResult _) { - syncNotes(); + _syncNotes(); }); } void addNote(Note note) { - insertNote(0, note); + Fimber.d("State Container addNote"); + note.parent.insert(0, note); + note.updateModified(); + _gitRepo.addNote(note).then((NoteRepoResult _) { + _syncNotes(); + }); } void removeNote(Note note) { @@ -184,23 +193,14 @@ class StateContainerState extends State { // We wait an aritfical amount of time, so that the user has a change to undo // their delete operation, and that commit is not synced with the server, till then. await Future.delayed(const Duration(seconds: 4)); - syncNotes(); + _syncNotes(); }); } void undoRemoveNote(Note note) { note.parent.insert(0, note); _gitRepo.resetLastCommit().then((NoteRepoResult _) { - syncNotes(); - }); - } - - void insertNote(int index, Note note) { - Fimber.d("State Container insertNote " + index.toString()); - note.parent.insert(index, note); - note.updateModified(); - _gitRepo.addNote(note).then((NoteRepoResult _) { - syncNotes(); + _syncNotes(); }); } @@ -208,7 +208,7 @@ class StateContainerState extends State { Fimber.d("State Container updateNote"); note.updateModified(); _gitRepo.updateNote(note).then((NoteRepoResult _) { - syncNotes(); + _syncNotes(); }); } @@ -230,7 +230,7 @@ class StateContainerState extends State { await _persistConfig(); _loadNotes(); - syncNotes(); + _syncNotes(); setState(() {}); }(); diff --git a/lib/widgets/sync_button.dart b/lib/widgets/sync_button.dart index 2a81ec38..4cdf84d4 100644 --- a/lib/widgets/sync_button.dart +++ b/lib/widgets/sync_button.dart @@ -5,6 +5,7 @@ import 'package:connectivity/connectivity.dart'; import 'package:gitjournal/appstate.dart'; import 'package:gitjournal/state_container.dart'; +import 'package:gitjournal/utils.dart'; class SyncButton extends StatefulWidget { @override @@ -59,8 +60,12 @@ class _SyncButtonState extends State { } void _syncRepo() async { - final container = StateContainer.of(context); - await container.syncNotes(); + try { + final container = StateContainer.of(context); + await container.syncNotes(); + } catch (e) { + showSnackbar(context, e.toString()); + } } IconData _syncStatusIcon() {