From dda557e1bffe21cf3daa91fd425ea200ed6a4fab Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 2 Dec 2019 14:07:57 +0100 Subject: [PATCH] Split HomeScreen into JournalListingScreen The HomeScreen might need to show either a JournalList or some other kind of listing. --- lib/screens/home_screen.dart | 154 +--------------------------- lib/screens/journal_listing.dart | 166 +++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+), 151 deletions(-) create mode 100644 lib/screens/journal_listing.dart diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart index e347aa36..f59a5a02 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -1,162 +1,14 @@ -import 'package:badges/badges.dart'; import 'package:flutter/material.dart'; -import 'package:gitjournal/note.dart'; -import 'package:gitjournal/utils.dart'; -import 'package:gitjournal/apis/git.dart'; -import 'package:gitjournal/screens/journal_editor.dart'; -import 'package:gitjournal/screens/journal_browsing.dart'; import 'package:gitjournal/state_container.dart'; -import 'package:gitjournal/widgets/app_drawer.dart'; -import 'package:gitjournal/widgets/journal_list.dart'; -import 'package:gitjournal/themes.dart'; + +import 'journal_listing.dart'; class HomeScreen extends StatelessWidget { - final GlobalKey _scaffoldKey = GlobalKey(); - @override Widget build(BuildContext context) { final container = StateContainer.of(context); final appState = container.appState; - var createButton = FloatingActionButton( - key: const ValueKey("FAB"), - onPressed: () => _newPost(context), - child: Icon(Icons.add), - ); - - Widget journalList = JournalList( - notes: appState.notes, - noteSelectedFunction: (noteIndex) { - var route = MaterialPageRoute( - builder: (context) => JournalBrowsingScreen( - notes: appState.notes, - noteIndex: noteIndex, - ), - ); - Navigator.of(context).push(route); - }, - emptyText: "Why not add your first\n Journal Entry?", - ); - - bool shouldShowBadge = - !appState.remoteGitRepoConfigured && appState.hasJournalEntries; - var appBarMenuButton = BadgeIconButton( - key: const ValueKey("DrawerButton"), - icon: const Icon(Icons.menu), - itemCount: shouldShowBadge ? 1 : 0, - onPressed: () { - _scaffoldKey.currentState.openDrawer(); - }, - ); - - return Scaffold( - key: _scaffoldKey, - appBar: AppBar( - title: const Text('GitJournal'), - leading: appBarMenuButton, - actions: [ - IconButton( - icon: Icon(Icons.search), - onPressed: () { - showSearch( - context: context, - delegate: NoteSearch(), - ); - }, - ) - ], - ), - floatingActionButton: createButton, - body: Center( - child: RefreshIndicator( - child: journalList, - onRefresh: () async { - try { - await container.syncNotes(); - } on GitException catch (exp) { - showSnackbar(context, exp.cause); - } - }), - ), - drawer: AppDrawer(), - ); - } - - void _newPost(BuildContext context) { - var route = MaterialPageRoute(builder: (context) => JournalEditor()); - Navigator.of(context).push(route); - } -} - -class NoteSearch extends SearchDelegate { - // Workaround because of https://github.com/flutter/flutter/issues/32180 - @override - ThemeData appBarTheme(BuildContext context) { - var theme = Theme.of(context); - if (theme.brightness == Brightness.light) { - return theme; - } - - return theme.copyWith( - primaryColor: Themes.dark.primaryColor, - ); - } - - @override - List buildActions(BuildContext context) { - return [ - IconButton( - icon: Icon(Icons.close), - onPressed: () { - query = ''; - }, - ), - ]; - } - - @override - Widget buildLeading(BuildContext context) { - return IconButton( - icon: Icon(Icons.arrow_back), - onPressed: () { - close(context, null); - }, - ); - } - - @override - Widget buildResults(BuildContext context) { - return buildJournalList(context, query); - } - - @override - Widget buildSuggestions(BuildContext context) { - return buildJournalList(context, query); - } - - JournalList buildJournalList(BuildContext context, String query) { - final container = StateContainer.of(context); - final appState = container.appState; - - // TODO: This should be made far more efficient - var q = query.toLowerCase(); - var filteredNotes = appState.notes.where((note) { - return note.body.toLowerCase().contains(q); - }).toList(); - - Widget journalList = JournalList( - notes: filteredNotes, - noteSelectedFunction: (noteIndex) { - var route = MaterialPageRoute( - builder: (context) => JournalBrowsingScreen( - notes: filteredNotes, - noteIndex: noteIndex, - ), - ); - Navigator.of(context).push(route); - }, - emptyText: "No Search Results Found", - ); - return journalList; + return JournalListingScreen(appState.notes); } } diff --git a/lib/screens/journal_listing.dart b/lib/screens/journal_listing.dart new file mode 100644 index 00000000..69caf9f9 --- /dev/null +++ b/lib/screens/journal_listing.dart @@ -0,0 +1,166 @@ +import 'package:badges/badges.dart'; +import 'package:flutter/material.dart'; +import 'package:gitjournal/note.dart'; +import 'package:gitjournal/utils.dart'; +import 'package:gitjournal/apis/git.dart'; +import 'package:gitjournal/screens/journal_editor.dart'; +import 'package:gitjournal/screens/journal_browsing.dart'; +import 'package:gitjournal/state_container.dart'; +import 'package:gitjournal/widgets/app_drawer.dart'; +import 'package:gitjournal/widgets/journal_list.dart'; +import 'package:gitjournal/themes.dart'; + +class JournalListingScreen extends StatelessWidget { + final GlobalKey _scaffoldKey = GlobalKey(); + final List allNotes; + + JournalListingScreen(this.allNotes); + + @override + Widget build(BuildContext context) { + final container = StateContainer.of(context); + final appState = container.appState; + + var createButton = FloatingActionButton( + key: const ValueKey("FAB"), + onPressed: () => _newPost(context), + child: Icon(Icons.add), + ); + + Widget journalList = JournalList( + notes: allNotes, + noteSelectedFunction: (noteIndex) { + var route = MaterialPageRoute( + builder: (context) => JournalBrowsingScreen( + notes: allNotes, + noteIndex: noteIndex, + ), + ); + Navigator.of(context).push(route); + }, + emptyText: "Why not add your first\n Journal Entry?", + ); + + bool shouldShowBadge = + !appState.remoteGitRepoConfigured && appState.hasJournalEntries; + var appBarMenuButton = BadgeIconButton( + key: const ValueKey("DrawerButton"), + icon: const Icon(Icons.menu), + itemCount: shouldShowBadge ? 1 : 0, + onPressed: () { + _scaffoldKey.currentState.openDrawer(); + }, + ); + + return Scaffold( + key: _scaffoldKey, + appBar: AppBar( + title: const Text('GitJournal'), + leading: appBarMenuButton, + actions: [ + IconButton( + icon: Icon(Icons.search), + onPressed: () { + showSearch( + context: context, + delegate: NoteSearch(allNotes), + ); + }, + ) + ], + ), + floatingActionButton: createButton, + body: Center( + child: RefreshIndicator( + child: journalList, + onRefresh: () async { + try { + await container.syncNotes(); + } on GitException catch (exp) { + showSnackbar(context, exp.cause); + } + }), + ), + drawer: AppDrawer(), + ); + } + + void _newPost(BuildContext context) { + var route = MaterialPageRoute(builder: (context) => JournalEditor()); + Navigator.of(context).push(route); + } +} + +class NoteSearch extends SearchDelegate { + final List notes; + + NoteSearch(this.notes); + + // Workaround because of https://github.com/flutter/flutter/issues/32180 + @override + ThemeData appBarTheme(BuildContext context) { + var theme = Theme.of(context); + if (theme.brightness == Brightness.light) { + return theme; + } + + return theme.copyWith( + primaryColor: Themes.dark.primaryColor, + ); + } + + @override + List buildActions(BuildContext context) { + return [ + IconButton( + icon: Icon(Icons.close), + onPressed: () { + query = ''; + }, + ), + ]; + } + + @override + Widget buildLeading(BuildContext context) { + return IconButton( + icon: Icon(Icons.arrow_back), + onPressed: () { + close(context, null); + }, + ); + } + + @override + Widget buildResults(BuildContext context) { + return buildJournalList(context, query); + } + + @override + Widget buildSuggestions(BuildContext context) { + return buildJournalList(context, query); + } + + JournalList buildJournalList(BuildContext context, String query) { + // TODO: This should be made far more efficient + var q = query.toLowerCase(); + var filteredNotes = notes.where((note) { + return note.body.toLowerCase().contains(q); + }).toList(); + + Widget journalList = JournalList( + notes: filteredNotes, + noteSelectedFunction: (noteIndex) { + var route = MaterialPageRoute( + builder: (context) => JournalBrowsingScreen( + notes: filteredNotes, + noteIndex: noteIndex, + ), + ); + Navigator.of(context).push(route); + }, + emptyText: "No Search Results Found", + ); + return journalList; + } +}