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,
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) {

View File

@ -68,7 +68,7 @@ class StateContainerState extends State<StateContainer> {
}
_loadNotes();
syncNotes();
_syncNotes();
}
void removeExistingRemoteClone() async {
@ -87,7 +87,7 @@ class StateContainerState extends State<StateContainer> {
await appState.notesFolder.loadRecursively();
}
Future<void> syncNotes() async {
Future<void> 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<StateContainer> {
if (shouldLogGitException(e)) {
await FlutterCrashlytics().logException(e, stacktrace);
}
showSnackbar(context, e.cause);
if (!doNotThrow) rethrow;
}
await _loadNotes();
}
Future<void> _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<StateContainer> {
parent.addFolder(newFolder);
_gitRepo.addFolder(newFolder).then((NoteRepoResult _) {
syncNotes();
_syncNotes();
});
}
@ -135,7 +139,7 @@ class StateContainerState extends State<StateContainer> {
folder.parent.removeFolder(folder);
_gitRepo.removeFolder(folder.folderPath).then((NoteRepoResult _) {
syncNotes();
_syncNotes();
});
}
@ -146,7 +150,7 @@ class StateContainerState extends State<StateContainer> {
_gitRepo
.renameFolder(oldFolderPath, folder.folderPath)
.then((NoteRepoResult _) {
syncNotes();
_syncNotes();
});
}
@ -155,7 +159,7 @@ class StateContainerState extends State<StateContainer> {
note.rename(newFileName);
_gitRepo.renameNote(oldNotePath, note.filePath).then((NoteRepoResult _) {
syncNotes();
_syncNotes();
});
}
@ -167,12 +171,17 @@ class StateContainerState extends State<StateContainer> {
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<StateContainer> {
// 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<StateContainer> {
Fimber.d("State Container updateNote");
note.updateModified();
_gitRepo.updateNote(note).then((NoteRepoResult _) {
syncNotes();
_syncNotes();
});
}
@ -230,7 +230,7 @@ class StateContainerState extends State<StateContainer> {
await _persistConfig();
_loadNotes();
syncNotes();
_syncNotes();
setState(() {});
}();

View File

@ -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<SyncButton> {
}
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() {