syncNotes: Catch the exception outside the StateContainer

The StateContainer doesn't have access to the snackbar.
This commit is contained in:
Vishesh Handa
2020-02-09 15:53:21 +01:00
parent 8f76b4d960
commit 0c0fd17543
3 changed files with 40 additions and 29 deletions

View File

@ -49,9 +49,11 @@ class JournalListingScreen extends StatelessWidget {
), ),
floatingActionButton: createButton, floatingActionButton: createButton,
body: Center( body: Center(
child: RefreshIndicator( child: Builder(
child: Scrollbar(child: buildJournalList(notesFolder)), builder: (context) => RefreshIndicator(
onRefresh: () async => _syncRepo(context), child: Scrollbar(child: buildJournalList(notesFolder)),
onRefresh: () async => _syncRepo(context),
),
), ),
), ),
drawer: AppDrawer(), drawer: AppDrawer(),
@ -59,8 +61,12 @@ class JournalListingScreen extends StatelessWidget {
} }
void _syncRepo(BuildContext context) async { void _syncRepo(BuildContext context) async {
final container = StateContainer.of(context); try {
await container.syncNotes(); final container = StateContainer.of(context);
await container.syncNotes();
} catch (e) {
showSnackbar(context, e.toString());
}
} }
void _newPost(BuildContext context) { void _newPost(BuildContext context) {

View File

@ -68,7 +68,7 @@ class StateContainerState extends State<StateContainer> {
} }
_loadNotes(); _loadNotes();
syncNotes(); _syncNotes();
} }
void removeExistingRemoteClone() async { void removeExistingRemoteClone() async {
@ -87,7 +87,7 @@ class StateContainerState extends State<StateContainer> {
await appState.notesFolder.loadRecursively(); await appState.notesFolder.loadRecursively();
} }
Future<void> syncNotes() async { Future<void> syncNotes({bool doNotThrow = false}) async {
if (!appState.remoteGitRepoConfigured) { if (!appState.remoteGitRepoConfigured) {
Fimber.d("Not syncing because RemoteRepo not configured"); Fimber.d("Not syncing because RemoteRepo not configured");
return true; return true;
@ -112,11 +112,15 @@ class StateContainerState extends State<StateContainer> {
if (shouldLogGitException(e)) { if (shouldLogGitException(e)) {
await FlutterCrashlytics().logException(e, stacktrace); await FlutterCrashlytics().logException(e, stacktrace);
} }
showSnackbar(context, e.cause); if (!doNotThrow) rethrow;
} }
await _loadNotes(); await _loadNotes();
} }
Future<void> _syncNotes() {
return syncNotes(doNotThrow: true);
}
void createFolder(NotesFolder parent, String folderName) async { void createFolder(NotesFolder parent, String folderName) async {
var newFolderPath = p.join(parent.folderPath, folderName); var newFolderPath = p.join(parent.folderPath, folderName);
var newFolder = NotesFolder(parent, newFolderPath); var newFolder = NotesFolder(parent, newFolderPath);
@ -126,7 +130,7 @@ class StateContainerState extends State<StateContainer> {
parent.addFolder(newFolder); parent.addFolder(newFolder);
_gitRepo.addFolder(newFolder).then((NoteRepoResult _) { _gitRepo.addFolder(newFolder).then((NoteRepoResult _) {
syncNotes(); _syncNotes();
}); });
} }
@ -135,7 +139,7 @@ class StateContainerState extends State<StateContainer> {
folder.parent.removeFolder(folder); folder.parent.removeFolder(folder);
_gitRepo.removeFolder(folder.folderPath).then((NoteRepoResult _) { _gitRepo.removeFolder(folder.folderPath).then((NoteRepoResult _) {
syncNotes(); _syncNotes();
}); });
} }
@ -146,7 +150,7 @@ class StateContainerState extends State<StateContainer> {
_gitRepo _gitRepo
.renameFolder(oldFolderPath, folder.folderPath) .renameFolder(oldFolderPath, folder.folderPath)
.then((NoteRepoResult _) { .then((NoteRepoResult _) {
syncNotes(); _syncNotes();
}); });
} }
@ -155,7 +159,7 @@ class StateContainerState extends State<StateContainer> {
note.rename(newFileName); note.rename(newFileName);
_gitRepo.renameNote(oldNotePath, note.filePath).then((NoteRepoResult _) { _gitRepo.renameNote(oldNotePath, note.filePath).then((NoteRepoResult _) {
syncNotes(); _syncNotes();
}); });
} }
@ -167,12 +171,17 @@ class StateContainerState extends State<StateContainer> {
note.move(destFolder); note.move(destFolder);
_gitRepo.moveNote(oldNotePath, note.filePath).then((NoteRepoResult _) { _gitRepo.moveNote(oldNotePath, note.filePath).then((NoteRepoResult _) {
syncNotes(); _syncNotes();
}); });
} }
void addNote(Note note) { 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) { void removeNote(Note note) {
@ -184,23 +193,14 @@ class StateContainerState extends State<StateContainer> {
// We wait an aritfical amount of time, so that the user has a change to undo // 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. // their delete operation, and that commit is not synced with the server, till then.
await Future.delayed(const Duration(seconds: 4)); await Future.delayed(const Duration(seconds: 4));
syncNotes(); _syncNotes();
}); });
} }
void undoRemoveNote(Note note) { void undoRemoveNote(Note note) {
note.parent.insert(0, note); note.parent.insert(0, note);
_gitRepo.resetLastCommit().then((NoteRepoResult _) { _gitRepo.resetLastCommit().then((NoteRepoResult _) {
syncNotes(); _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();
}); });
} }
@ -208,7 +208,7 @@ class StateContainerState extends State<StateContainer> {
Fimber.d("State Container updateNote"); Fimber.d("State Container updateNote");
note.updateModified(); note.updateModified();
_gitRepo.updateNote(note).then((NoteRepoResult _) { _gitRepo.updateNote(note).then((NoteRepoResult _) {
syncNotes(); _syncNotes();
}); });
} }
@ -230,7 +230,7 @@ class StateContainerState extends State<StateContainer> {
await _persistConfig(); await _persistConfig();
_loadNotes(); _loadNotes();
syncNotes(); _syncNotes();
setState(() {}); setState(() {});
}(); }();

View File

@ -5,6 +5,7 @@ import 'package:connectivity/connectivity.dart';
import 'package:gitjournal/appstate.dart'; import 'package:gitjournal/appstate.dart';
import 'package:gitjournal/state_container.dart'; import 'package:gitjournal/state_container.dart';
import 'package:gitjournal/utils.dart';
class SyncButton extends StatefulWidget { class SyncButton extends StatefulWidget {
@override @override
@ -59,8 +60,12 @@ class _SyncButtonState extends State<SyncButton> {
} }
void _syncRepo() async { void _syncRepo() async {
final container = StateContainer.of(context); try {
await container.syncNotes(); final container = StateContainer.of(context);
await container.syncNotes();
} catch (e) {
showSnackbar(context, e.toString());
}
} }
IconData _syncStatusIcon() { IconData _syncStatusIcon() {