From 5a2b0f1f4b58efb78a993f91db8e0a8d713d363d Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Tue, 15 Jan 2019 15:26:15 +0100 Subject: [PATCH] Allow the notes to be refreshed by pulling from the top --- lib/screens/home_screen.dart | 29 ++++++++++++++++++----------- lib/state_container.dart | 27 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart index d8cf37fc..44464109 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -17,22 +17,29 @@ class HomeScreen extends StatelessWidget { child: new Icon(Icons.add), ); + var journalList = JournalList( + notes: appState.notes, + noteSelectedFunction: (noteIndex) { + var route = new MaterialPageRoute( + builder: (context) => new NoteBrowsingScreen( + notes: appState.notes, + noteIndex: noteIndex, + ), + ); + Navigator.of(context).push(route); + }, + ); + return new Scaffold( appBar: new AppBar( title: new Text('GitJournal'), ), floatingActionButton: createButton, - body: new JournalList( - notes: appState.notes, - noteSelectedFunction: (noteIndex) { - var route = new MaterialPageRoute( - builder: (context) => new NoteBrowsingScreen( - notes: appState.notes, - noteIndex: noteIndex, - ), - ); - Navigator.of(context).push(route); - }, + body: Center( + child: RefreshIndicator( + child: journalList, + onRefresh: container.syncNotes, + ), ), drawer: new AppDrawer(), ); diff --git a/lib/state_container.dart b/lib/state_container.dart index 13ce97ee..d76e9f78 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -96,6 +96,33 @@ class StateContainerState extends State { }); } + Future syncNotes() async { + try { + await noteRepo.sync(); + } catch (err, stack) { + print("Notes Repo Sync Error: " + err.toString()); + print(stack.toString()); + return true; + } + + try { + appState.isLoadingFromDisk = true; + var loadedNotes = await noteRepo.listNotes(); + setState(() { + appState.isLoadingFromDisk = false; + appState.notes = loadedNotes; + }); + } catch (err, stack) { + setState(() { + print("Load Notes From Disk Error: " + err.toString()); + print(stack.toString()); + appState.isLoadingFromDisk = false; + }); + } + + return true; + } + void _syncNotes() { print("Starting to syncNOtes"); this.noteRepo.sync().then((loaded) {