1
0
mirror of https://github.com/GitJournal/GitJournal.git synced 2025-07-08 00:45:13 +08:00

Split HomeScreen into JournalListingScreen

The HomeScreen might need to show either a JournalList or some other
kind of listing.
This commit is contained in:
Vishesh Handa
2019-12-02 14:07:57 +01:00
parent abd5f09453
commit dda557e1bf
2 changed files with 169 additions and 151 deletions

@ -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<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
@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: <Widget>[
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<Note> {
// 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<Widget> 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);
}
}

@ -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<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
final List<Note> 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: <Widget>[
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<Note> {
final List<Note> 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<Widget> 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;
}
}