Skip to content

Commit

Permalink
Embed history and bookmarks page inside home_page.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
AlienKevin committed Dec 31, 2023
1 parent f4bcb4d commit fbac795
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 75 deletions.
19 changes: 0 additions & 19 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import 'package:uuid/uuid_util.dart';
import 'package:wordshk/models/language.dart';
import 'package:wordshk/pages/about_page.dart';
import 'package:wordshk/pages/dictionary_license_page.dart';
import 'package:wordshk/pages/entry_items_page.dart';
import 'package:wordshk/pages/entry_not_published_page.dart';
import 'package:wordshk/pages/entry_page.dart';
import 'package:wordshk/pages/exercise_page.dart';
Expand Down Expand Up @@ -295,24 +294,6 @@ initializeRouter(bool firstTimeUser, SharedPreferences prefs) {
path: '/exercise',
builder: (context, state) => const ExercisePage(),
),
GoRoute(
path: '/bookmarks',
builder: (context, state) => EntryItemsPage<BookmarkState>(
title: AppLocalizations.of(context)!.bookmarks,
emptyMessage: AppLocalizations.of(context)!.noBookmarks,
deletionConfirmationMessage:
AppLocalizations.of(context)!.bookmarkDeleteConfirmation,
),
),
GoRoute(
path: '/history',
builder: (context, state) => EntryItemsPage<HistoryState>(
title: AppLocalizations.of(context)!.history,
emptyMessage: AppLocalizations.of(context)!.noHistory,
deletionConfirmationMessage:
AppLocalizations.of(context)!.historyDeleteConfirmation,
),
),
GoRoute(
path: '/about',
builder: (context, state) => const AboutPage(),
Expand Down
53 changes: 24 additions & 29 deletions lib/pages/entry_items_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:typed_data';

import 'package:flutter/material.dart' hide NavigationDrawer;
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:provider/provider.dart';
import 'package:sentry/sentry.dart';
Expand All @@ -16,16 +17,13 @@ import '../models/embedded.dart';
import '../models/summary_def_language.dart';
import '../states/entry_item_state.dart';
import '../utils.dart';
import '../widgets/navigation_drawer.dart';
import 'entry_page.dart';

class EntryItemsPage<T extends EntryItemState> extends StatefulWidget {
final String title;
final String emptyMessage;
final String deletionConfirmationMessage;
const EntryItemsPage(
{Key? key,
required this.title,
required this.emptyMessage,
required this.deletionConfirmationMessage})
: super(key: key);
Expand Down Expand Up @@ -141,34 +139,31 @@ class _EntryItemsState<T extends EntryItemState>
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: _entryItemSummaries.isEmpty
? []
: [
TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all(
Theme.of(context).colorScheme.onPrimary,
),
),
child: Text(switch (_mode) {
ViewMode() => AppLocalizations.of(context)!.edit,
EditMode() => AppLocalizations.of(context)!.done,
}),
onPressed: () {
setState(() {
_mode = switch (_mode) {
ViewMode() =>
EditMode(selectedEntryItems: HashSet<int>()),
EditMode() => ViewMode(),
};
});
},
floatingActionButton: KeyboardVisibilityBuilder(
builder: (context, isKeyboardVisible) => Visibility(
visible: _entryItemSummaries.isNotEmpty && !isKeyboardVisible,
child: ElevatedButton(
style: ButtonStyle(
padding: MaterialStateProperty.all(
const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
),
],
),
child: Text(
switch (_mode) {
ViewMode() => AppLocalizations.of(context)!.edit,
EditMode() => AppLocalizations.of(context)!.done,
},
),
onPressed: () {
setState(() {
_mode = switch (_mode) {
ViewMode() => EditMode(selectedEntryItems: HashSet<int>()),
EditMode() => ViewMode(),
};
});
},
)),
),
drawer: const NavigationDrawer(),
body: Consumer<T>(
builder: (BuildContext context, EntryItemState s, Widget? child) => s
.items.isEmpty
Expand Down
69 changes: 52 additions & 17 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:io';
import 'dart:math';

import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
Expand All @@ -24,12 +23,14 @@ import '../models/input_mode.dart';
import '../models/search_mode.dart';
import '../models/search_result_type.dart';
import '../models/summary_def_language.dart';
import '../states/bookmark_state.dart';
import '../states/language_state.dart';
import '../states/romanization_state.dart';
import '../states/search_mode_state.dart';
import '../states/search_query_state.dart';
import '../utils.dart';
import '../widgets/navigation_drawer.dart';
import 'entry_items_page.dart';
import 'entry_page.dart';

class HomePage extends StatefulWidget {
Expand All @@ -41,7 +42,8 @@ class HomePage extends StatefulWidget {
State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
class _HomePageState extends State<HomePage>
with SingleTickerProviderStateMixin {
List<PrSearchResult> prSearchResults = [];
List<VariantSearchResult> variantSearchResults = [];
List<EnglishSearchResult> englishSearchResults = [];
Expand All @@ -55,11 +57,15 @@ class _HomePageState extends State<HomePage> {
OverlayEntry? searchModeSelectors;
// The EntryPage corresponding to the select search result (used in wide screens)
EntryPage? selectedSearchResultEntryPage;
late final TabController _historyAndBookmarksTabController;

@override
void initState() {
super.initState();

_historyAndBookmarksTabController =
TabController(vsync: this, initialIndex: 0, length: 2);

WidgetsBinding.instance.addPostFrameCallback((_) {
final query = context.read<SearchQueryState>().query;
if (query.isNotEmpty) {
Expand Down Expand Up @@ -103,6 +109,10 @@ class _HomePageState extends State<HomePage> {
}

onSearchSubmitted(String query) {
if (query.isEmpty) {
return;
}

final state = context.read<SearchModeState>();
if (state.mode == SearchMode.variant || state.mode == SearchMode.combined) {
// No search results should be produced in other modes
Expand Down Expand Up @@ -171,25 +181,44 @@ class _HomePageState extends State<HomePage> {
child: (finishedSearch && isSearchResultsEmpty)
? showResultsNotFound()
: (queryEmptied
? showWatermark()
? showHistoryAndBookmarks()
: showSearchResults(
selectedSearchResultEntryPage)))));
}

Widget showWatermark() {
double watermarkSize = min(MediaQuery.of(context).size.width * 0.4, 300);
return Align(
alignment: Alignment.topCenter,
child: Padding(
padding: const EdgeInsets.only(top: appBarHeight * 1.6),
child: MediaQuery.of(context).platformBrightness == Brightness.light
? Image(
width: watermarkSize,
image: const AssetImage('assets/icon.png'))
: Image(
width: watermarkSize,
image: const AssetImage('assets/icon_grey.png')),
),
Widget showHistoryAndBookmarks() {
return Column(
children: [
TabBar(
controller: _historyAndBookmarksTabController,
tabs: [
Tab(text: AppLocalizations.of(context)!.history),
Tab(text: AppLocalizations.of(context)!.bookmarks),
],
labelColor: Theme.of(context).textTheme.bodyMedium!.color!,
unselectedLabelColor: Theme.of(context).textTheme.bodyMedium!.color!,
indicator: UnderlineTabIndicator(
borderSide: BorderSide(color: Theme.of(context).textTheme.bodyMedium!.color!, width: 2),
// Indicator height
insets: const EdgeInsets.symmetric(
horizontal: 30), // Indicator width
),
),
Expanded(
child: TabBarView(controller: _historyAndBookmarksTabController, children: [
EntryItemsPage<HistoryState>(
emptyMessage: AppLocalizations.of(context)!.noHistory,
deletionConfirmationMessage:
AppLocalizations.of(context)!.historyDeleteConfirmation,
),
EntryItemsPage<BookmarkState>(
emptyMessage: AppLocalizations.of(context)!.noBookmarks,
deletionConfirmationMessage:
AppLocalizations.of(context)!.bookmarkDeleteConfirmation,
)
]),
)
],
);
}

Expand Down Expand Up @@ -697,6 +726,12 @@ class _HomePageState extends State<HomePage> {
],
);
}

@override
void dispose() {
_historyAndBookmarksTabController.dispose();
super.dispose();
}
}

extension Unique<E, Id> on List<E> {
Expand Down
10 changes: 0 additions & 10 deletions lib/widgets/navigation_drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,6 @@ class NavigationDrawer extends StatelessWidget {
"/",
const Key("drawerDictionaryButton")),
const Divider(),
drawerButton(AppLocalizations.of(context)!.bookmarks,
PlatformIcons(context).bookmarkOutline, "/bookmarks"),
const Divider(),
drawerButton(
AppLocalizations.of(context)!.history,
isMaterial(context)
? Icons.access_time_rounded
: CupertinoIcons.time,
"/history"),
const Divider(),
drawerButton(AppLocalizations.of(context)!.exercise,
PlatformIcons(context).volumeUp, "/exercise"),
const Divider(),
Expand Down

0 comments on commit fbac795

Please sign in to comment.