1
0
mirror of https://github.com/h7x4/Jisho-Study-Tool.git synced 2024-12-21 13:37:29 +01:00

Merge history entries (#41)

This commit is contained in:
Oystein Kristoffer Tveit 2022-01-25 20:44:00 +01:00 committed by GitHub
parent 241ff1cab6
commit 2ca9988018
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 245 additions and 258 deletions

View File

@ -2,38 +2,13 @@ import 'package:flutter/material.dart';
import '../../bloc/theme/theme_bloc.dart';
class DateDivider extends StatelessWidget {
final String? text;
final DateTime? date;
class TextDivider extends StatelessWidget {
final String text;
const DateDivider({
this.text,
this.date,
const TextDivider({
Key? key,
}) : assert((text == null) ^ (date == null)),
super(key: key);
String getHumanReadableDate(DateTime date) {
const List<String> monthTable = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
final int day = date.day;
final String month = monthTable[date.month - 1];
final int year = date.year;
return '$day. $month $year';
}
required this.text,
}) : super(key: key);
@override
Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
@ -47,9 +22,7 @@ class DateDivider extends StatelessWidget {
horizontal: 10,
),
child: DefaultTextStyle.merge(
child: (text != null)
? Text(text!)
: Text(getHumanReadableDate(date!)),
child: Text(text),
style: TextStyle(color: colors.foreground),
),
);

View File

@ -2,75 +2,102 @@ import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import '../../models/history/search.dart';
import '../../routing/routes.dart';
import '../../services/datetime.dart';
import '../../settings.dart';
import 'kanji_box.dart';
class SearchItem extends StatelessWidget {
final DateTime time;
final Widget search;
final Search search;
// final Widget search;
final int objectKey;
final void Function()? onDelete;
final void Function()? onFavourite;
final void Function()? onTap;
const SearchItem({
required this.time,
required this.search,
required this.objectKey,
this.onDelete,
this.onFavourite,
this.onTap,
Key? key,
}) : super(key: key);
String getTime() {
final hours = time.hour.toString().padLeft(2, '0');
final mins = time.minute.toString().padLeft(2, '0');
return '$hours:$mins';
}
Widget get _child => (search.isKanji)
? KanjiBox(kanji: search.kanjiQuery!.kanji)
: Text(search.wordQuery!.query);
void Function() _onTap(context) => search.isKanji
? () => Navigator.pushNamed(
context,
Routes.kanjiSearch,
arguments: search.kanjiQuery!.kanji,
)
: () => Navigator.pushNamed(
context,
Routes.search,
arguments: search.wordQuery!.query,
);
MaterialPageRoute get timestamps => MaterialPageRoute(
builder: (context) => Scaffold(
appBar: AppBar(title: const Text('Last searched')),
body: ListView(
children: [
for (final ts in search.timestamps.reversed)
ListTile(title: Text('${formatDate(ts)} ${formatTime(ts)}'))
],
),
),
);
List<SlidableAction> _actions(context) => [
SlidableAction(
backgroundColor: Colors.blue,
icon: Icons.access_time,
onPressed: (_) => Navigator.push(context, timestamps),
),
SlidableAction(
backgroundColor: Colors.yellow,
icon: Icons.star,
onPressed: (_) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('TODO: implement favourites')),
);
onFavourite?.call();
},
),
SlidableAction(
backgroundColor: Colors.red,
icon: Icons.delete,
onPressed: (_) {
final Database db = GetIt.instance.get<Database>();
Search.store.record(objectKey).delete(db);
onDelete?.call();
},
),
];
@override
Widget build(BuildContext context) {
return Slidable(
endActionPane: ActionPane(
motion: const ScrollMotion(),
children: [
SlidableAction(
label: 'Favourite',
backgroundColor: Colors.yellow,
icon: Icons.star,
onPressed: (_) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('TODO: implement favourites')),
);
onFavourite?.call();
},
),
SlidableAction(
label: 'Delete',
backgroundColor: Colors.red,
icon: Icons.delete,
onPressed: (_) {
final Database db = GetIt.instance.get<Database>();
Search.store.record(objectKey).delete(db);
onDelete?.call();
},
),
],
children: _actions(context),
),
child: Container(
padding: const EdgeInsets.symmetric(vertical: 10),
child: ListTile(
onTap: onTap,
onTap: _onTap(context),
contentPadding: EdgeInsets.zero,
title: Row(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Text(getTime()),
child: Text(formatTime(search.timestamp)),
),
DefaultTextStyle.merge(
style: japaneseFont.textStyle,
child: search,
child: _child,
),
],
),

View File

@ -1,3 +1,4 @@
import 'package:get_it/get_it.dart';
import 'package:sembast/sembast.dart';
import './kanji_query.dart';
@ -7,39 +8,81 @@ export 'package:get_it/get_it.dart';
export 'package:sembast/sembast.dart';
class Search {
final DateTime timestamp;
final WordQuery? wordQuery;
final KanjiQuery? kanjiQuery;
final List<DateTime> timestamps;
Search.fromKanjiQuery({
required this.timestamp,
required KanjiQuery this.kanjiQuery,
}) : wordQuery = null;
List<DateTime>? timestamps,
}) : wordQuery = null,
timestamps = timestamps ?? [DateTime.now()];
Search.fromWordQuery({
required this.timestamp,
required WordQuery this.wordQuery,
}) : kanjiQuery = null;
List<DateTime>? timestamps,
}) : kanjiQuery = null,
timestamps = timestamps ?? [DateTime.now()];
bool get isKanji => wordQuery == null;
DateTime get timestamp => timestamps.last;
Map<String, Object?> toJson() => {
'timestamp': timestamp.millisecondsSinceEpoch,
'timestamps': [for (final ts in timestamps) ts.millisecondsSinceEpoch],
'lastTimestamp': timestamps.last.millisecondsSinceEpoch,
'wordQuery': wordQuery?.toJson(),
'kanjiQuery': kanjiQuery?.toJson(),
};
factory Search.fromJson(Map<String, dynamic> json) =>
json['wordQuery'] != null
? Search.fromWordQuery(
timestamp:
DateTime.fromMillisecondsSinceEpoch(json['timestamp'] as int),
wordQuery: WordQuery.fromJson(json['wordQuery']),
)
: Search.fromKanjiQuery(
timestamp: DateTime.fromMillisecondsSinceEpoch(json['timestamp'] as int),
kanjiQuery: KanjiQuery.fromJson(json['kanjiQuery']),
);
factory Search.fromJson(Map<String, dynamic> json) {
final List<DateTime> timestamps = [
for (final ts in json['timestamps'] as List<dynamic>)
DateTime.fromMillisecondsSinceEpoch(ts as int)
];
return json['wordQuery'] != null
? Search.fromWordQuery(
wordQuery: WordQuery.fromJson(json['wordQuery']),
timestamps: timestamps,
)
: Search.fromKanjiQuery(
kanjiQuery: KanjiQuery.fromJson(json['kanjiQuery']),
timestamps: timestamps,
);
}
static StoreRef<int, Object?> get store => intMapStoreFactory.store('search');
}
Future<void> addSearchToDatabase({
required String searchTerm,
required bool isKanji,
}) async {
final DateTime now = DateTime.now();
final db = GetIt.instance.get<Database>();
final Filter filter = Filter.equals(
isKanji ? 'kanjiQuery.kanji' : 'wordQuery.query',
searchTerm,
);
final RecordSnapshot<int, Object?>? previousSearch =
await Search.store.findFirst(db, finder: Finder(filter: filter));
if (previousSearch != null) {
final search =
Search.fromJson(previousSearch.value! as Map<String, Object?>);
search.timestamps.add(now);
Search.store.record(previousSearch.key).put(db, search.toJson());
return;
}
Search.store.add(
db,
isKanji
? Search.fromKanjiQuery(kanjiQuery: KanjiQuery(kanji: searchTerm))
.toJson()
: Search.fromWordQuery(wordQuery: WordQuery(query: searchTerm))
.toJson(),
);
}

View File

@ -3,11 +3,10 @@ import 'package:flutter/material.dart';
import '../screens/home.dart';
import '../screens/info/about.dart';
import '../screens/info/licenses.dart';
import '../screens/search/kanji_result_page.dart';
import '../screens/search/result_page.dart';
import '../screens/search/search_mechanisms/drawing.dart';
import '../screens/search/search_mechanisms/grade_list.dart';
import '../screens/search/search_mechanisms/radical_list.dart';
import '../screens/search/search_results_page.dart';
import 'routes.dart';
Route<Widget> generateRoute(RouteSettings settings) {
@ -20,13 +19,13 @@ Route<Widget> generateRoute(RouteSettings settings) {
case Routes.search:
final searchTerm = args! as String;
return MaterialPageRoute(
builder: (_) => SearchResultsPage(searchTerm: searchTerm),
builder: (_) => ResultPage(searchTerm: searchTerm, isKanji: false),
);
case Routes.kanjiSearch:
final searchTerm = args! as String;
return MaterialPageRoute(
builder: (_) => KanjiResultPage(kanjiSearchTerm: searchTerm),
builder: (_) => ResultPage(searchTerm: searchTerm, isKanji: true),
);
case Routes.kanjiSearchDraw:

View File

@ -3,18 +3,15 @@ import 'package:flutter/material.dart';
import '../components/common/loading.dart';
import '../components/common/opaque_box.dart';
import '../components/history/date_divider.dart';
import '../components/history/kanji_box.dart';
import '../components/history/search_item.dart';
import '../models/history/search.dart';
import '../routing/routes.dart';
import '../services/datetime.dart';
class HistoryView extends StatelessWidget {
const HistoryView({Key? key}) : super(key: key);
Database get _db => GetIt.instance.get<Database>();
Stream<Map<int, Search>> get searchStream => Search.store
.query(finder: Finder(sortOrders: [SortOrder('timestamp', false)]))
.query(finder: Finder(sortOrders: [SortOrder('lastTimestamp', false)]))
.onSnapshots(_db)
.map(
(snapshot) => Map.fromEntries(
@ -27,6 +24,8 @@ class HistoryView extends StatelessWidget {
),
);
Database get _db => GetIt.instance.get<Database>();
@override
Widget build(BuildContext context) {
return StreamBuilder<Map<int, Search>>(
@ -52,72 +51,27 @@ class HistoryView extends StatelessWidget {
);
}
Widget Function(BuildContext, int) historyEntryWithData(
Map<int, Search> data,
) =>
(context, index) {
if (index == 0) return const SizedBox.shrink();
final Search search = data.values.toList()[index - 1];
final int objectKey = data.keys.toList()[index - 1];
late final Widget child;
late final void Function() onTap;
if (search.isKanji) {
child = KanjiBox(kanji: search.kanjiQuery!.kanji);
onTap = () => Navigator.pushNamed(
context,
Routes.kanjiSearch,
arguments: search.kanjiQuery!.kanji,
);
} else {
child = Text(search.wordQuery!.query);
onTap = () => Navigator.pushNamed(
context,
Routes.search,
arguments: search.wordQuery!.query,
);
}
return SearchItem(
time: search.timestamp,
search: child,
objectKey: objectKey,
onTap: onTap,
onDelete: () => build(context),
);
};
DateTime roundToDay(DateTime date) =>
DateTime(date.year, date.month, date.day);
bool dateChangedFromLastSearch(Search prevSearch, DateTime searchDate) {
final DateTime prevSearchDate = roundToDay(prevSearch.timestamp);
return prevSearchDate != searchDate;
}
DateTime get today => roundToDay(DateTime.now());
DateTime get yesterday =>
roundToDay(DateTime.now().subtract(const Duration(days: 1)));
Widget Function(BuildContext, int) historyEntrySeparatorWithData(
List<Search> data,
) =>
(context, index) {
final Search search = data[index];
final DateTime searchDate = roundToDay(search.timestamp);
final DateTime searchDate = search.timestamp;
if (index == 0 ||
dateChangedFromLastSearch(data[index - 1], searchDate)) {
if (searchDate == today)
return const DateDivider(text: 'Today');
else if (searchDate == yesterday)
return const DateDivider(text: 'Yesterday');
else
return DateDivider(date: searchDate);
}
if (index == 0 || !dateIsEqual(data[index - 1].timestamp, searchDate))
return TextDivider(text: formatDate(roundToDay(searchDate)));
return const Divider(height: 0);
};
Widget Function(BuildContext, int) historyEntryWithData(
Map<int, Search> data,
) =>
(context, index) => (index == 0)
? const SizedBox.shrink()
: SearchItem(
search: data.values.toList()[index - 1],
objectKey: data.keys.toList()[index - 1],
onDelete: () => build(context),
);
}

View File

@ -1,50 +0,0 @@
import 'package:flutter/material.dart';
import '../../components/common/loading.dart';
import '../../components/kanji/kanji_result_body.dart';
import '../../models/history/kanji_query.dart';
import '../../models/history/search.dart';
import '../../services/jisho_api/kanji_search.dart';
class KanjiResultPage extends StatefulWidget {
final String kanjiSearchTerm;
const KanjiResultPage({
Key? key,
required this.kanjiSearchTerm,
}) : super(key: key);
@override
_KanjiResultPageState createState() => _KanjiResultPageState();
}
class _KanjiResultPageState extends State<KanjiResultPage> {
bool addedToDatabase = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder<KanjiResult>(
future: fetchKanji(widget.kanjiSearchTerm),
builder: (context, snapshot) {
if (!snapshot.hasData) return const LoadingScreen();
if (snapshot.hasError) return ErrorWidget(snapshot.error!);
if (!addedToDatabase) {
Search.store.add(
GetIt.instance.get<Database>(),
Search.fromKanjiQuery(
timestamp: DateTime.now(),
kanjiQuery: KanjiQuery(kanji: widget.kanjiSearchTerm),
).toJson(),
);
addedToDatabase = true;
}
return KanjiResultBody(result: snapshot.data!);
},
),
);
}
}

View File

@ -0,0 +1,56 @@
import 'package:flutter/material.dart';
import '../../components/common/loading.dart';
import '../../components/kanji/kanji_result_body.dart';
import '../../components/search/search_result_body.dart';
import '../../models/history/search.dart';
import '../../services/jisho_api/jisho_search.dart';
import '../../services/jisho_api/kanji_search.dart';
class ResultPage extends StatefulWidget {
final String searchTerm;
final bool isKanji;
const ResultPage({
Key? key,
required this.searchTerm,
required this.isKanji,
}) : super(key: key);
@override
_ResultPageState createState() => _ResultPageState();
}
class _ResultPageState extends State<ResultPage> {
bool addedToDatabase = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder(
future: widget.isKanji
? fetchKanji(widget.searchTerm)
: fetchJishoResults(widget.searchTerm),
builder: (context, snapshot) {
if (!snapshot.hasData) return const LoadingScreen();
if (snapshot.hasError) return ErrorWidget(snapshot.error!);
if (!addedToDatabase) {
addSearchToDatabase(
searchTerm: widget.searchTerm,
isKanji: widget.isKanji,
);
addedToDatabase = true;
}
return widget.isKanji
? KanjiResultBody(result: snapshot.data! as KanjiResult)
: SearchResultsBody(
results: (snapshot.data! as JishoAPIResult).data!,
);
},
),
);
}
}

View File

@ -1,54 +0,0 @@
import 'package:flutter/material.dart';
import '../../components/common/loading.dart';
import '../../components/search/search_result_body.dart';
import '../../models/history/search.dart';
import '../../models/history/word_query.dart';
import '../../services/jisho_api/jisho_search.dart';
// TODO: merge with KanjiResultPage
class SearchResultsPage extends StatefulWidget {
final String searchTerm;
const SearchResultsPage({
Key? key,
required this.searchTerm,
}) : super(key: key);
@override
_SearchResultsPageState createState() => _SearchResultsPageState();
}
class _SearchResultsPageState extends State<SearchResultsPage> {
bool addedToDatabase = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder<JishoAPIResult>(
future: fetchJishoResults(widget.searchTerm),
builder: (context, snapshot) {
if (!snapshot.hasData) return const LoadingScreen();
if (snapshot.hasError || snapshot.data!.data == null)
return ErrorWidget(snapshot.error!);
if (!addedToDatabase) {
Search.store.add(
GetIt.instance.get<Database>(),
Search.fromWordQuery(
timestamp: DateTime.now(),
wordQuery: WordQuery(query: widget.searchTerm),
).toJson(),
);
addedToDatabase = true;
}
return SearchResultsBody(
results: snapshot.data!.data!,
);
},
),
);
}
}

View File

@ -0,0 +1,39 @@
DateTime roundToDay(DateTime date) => DateTime(date.year, date.month, date.day);
bool dateIsEqual(DateTime date1, DateTime date2) =>
roundToDay(date1) == roundToDay(date2);
DateTime get today => roundToDay(DateTime.now());
DateTime get yesterday =>
roundToDay(DateTime.now().subtract(const Duration(days: 1)));
String formatTime(DateTime timestamp) {
final hours = timestamp.hour.toString().padLeft(2, '0');
final mins = timestamp.minute.toString().padLeft(2, '0');
return '$hours:$mins';
}
String formatDate(DateTime date) {
if (date == today) return 'Today';
if (date == yesterday) return 'Yesterday';
const List<String> monthTable = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
final int day = date.day;
final String month = monthTable[date.month - 1];
final int year = date.year;
return '$day. $month $year';
}