mirror of
https://github.com/h7x4/Jisho-Study-Tool.git
synced 2024-12-21 21:47:29 +01:00
Merge history entries (#41)
This commit is contained in:
parent
241ff1cab6
commit
2ca9988018
@ -2,38 +2,13 @@ import 'package:flutter/material.dart';
|
|||||||
|
|
||||||
import '../../bloc/theme/theme_bloc.dart';
|
import '../../bloc/theme/theme_bloc.dart';
|
||||||
|
|
||||||
class DateDivider extends StatelessWidget {
|
class TextDivider extends StatelessWidget {
|
||||||
final String? text;
|
final String text;
|
||||||
final DateTime? date;
|
|
||||||
|
|
||||||
const DateDivider({
|
const TextDivider({
|
||||||
this.text,
|
|
||||||
this.date,
|
|
||||||
Key? key,
|
Key? key,
|
||||||
}) : assert((text == null) ^ (date == null)),
|
required this.text,
|
||||||
super(key: key);
|
}) : 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';
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
|
Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
|
||||||
@ -47,9 +22,7 @@ class DateDivider extends StatelessWidget {
|
|||||||
horizontal: 10,
|
horizontal: 10,
|
||||||
),
|
),
|
||||||
child: DefaultTextStyle.merge(
|
child: DefaultTextStyle.merge(
|
||||||
child: (text != null)
|
child: Text(text),
|
||||||
? Text(text!)
|
|
||||||
: Text(getHumanReadableDate(date!)),
|
|
||||||
style: TextStyle(color: colors.foreground),
|
style: TextStyle(color: colors.foreground),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -2,40 +2,61 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter_slidable/flutter_slidable.dart';
|
import 'package:flutter_slidable/flutter_slidable.dart';
|
||||||
|
|
||||||
import '../../models/history/search.dart';
|
import '../../models/history/search.dart';
|
||||||
|
import '../../routing/routes.dart';
|
||||||
|
import '../../services/datetime.dart';
|
||||||
import '../../settings.dart';
|
import '../../settings.dart';
|
||||||
|
import 'kanji_box.dart';
|
||||||
|
|
||||||
class SearchItem extends StatelessWidget {
|
class SearchItem extends StatelessWidget {
|
||||||
final DateTime time;
|
final Search search;
|
||||||
final Widget search;
|
// final Widget search;
|
||||||
final int objectKey;
|
final int objectKey;
|
||||||
final void Function()? onDelete;
|
final void Function()? onDelete;
|
||||||
final void Function()? onFavourite;
|
final void Function()? onFavourite;
|
||||||
final void Function()? onTap;
|
|
||||||
|
|
||||||
const SearchItem({
|
const SearchItem({
|
||||||
required this.time,
|
|
||||||
required this.search,
|
required this.search,
|
||||||
required this.objectKey,
|
required this.objectKey,
|
||||||
this.onDelete,
|
this.onDelete,
|
||||||
this.onFavourite,
|
this.onFavourite,
|
||||||
this.onTap,
|
|
||||||
Key? key,
|
Key? key,
|
||||||
}) : super(key: key);
|
}) : super(key: key);
|
||||||
|
|
||||||
String getTime() {
|
Widget get _child => (search.isKanji)
|
||||||
final hours = time.hour.toString().padLeft(2, '0');
|
? KanjiBox(kanji: search.kanjiQuery!.kanji)
|
||||||
final mins = time.minute.toString().padLeft(2, '0');
|
: Text(search.wordQuery!.query);
|
||||||
return '$hours:$mins';
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
void Function() _onTap(context) => search.isKanji
|
||||||
Widget build(BuildContext context) {
|
? () => Navigator.pushNamed(
|
||||||
return Slidable(
|
context,
|
||||||
endActionPane: ActionPane(
|
Routes.kanjiSearch,
|
||||||
motion: const ScrollMotion(),
|
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: [
|
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(
|
SlidableAction(
|
||||||
label: 'Favourite',
|
|
||||||
backgroundColor: Colors.yellow,
|
backgroundColor: Colors.yellow,
|
||||||
icon: Icons.star,
|
icon: Icons.star,
|
||||||
onPressed: (_) {
|
onPressed: (_) {
|
||||||
@ -46,7 +67,6 @@ class SearchItem extends StatelessWidget {
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
SlidableAction(
|
SlidableAction(
|
||||||
label: 'Delete',
|
|
||||||
backgroundColor: Colors.red,
|
backgroundColor: Colors.red,
|
||||||
icon: Icons.delete,
|
icon: Icons.delete,
|
||||||
onPressed: (_) {
|
onPressed: (_) {
|
||||||
@ -55,22 +75,29 @@ class SearchItem extends StatelessWidget {
|
|||||||
onDelete?.call();
|
onDelete?.call();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
];
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Slidable(
|
||||||
|
endActionPane: ActionPane(
|
||||||
|
motion: const ScrollMotion(),
|
||||||
|
children: _actions(context),
|
||||||
),
|
),
|
||||||
child: Container(
|
child: Container(
|
||||||
padding: const EdgeInsets.symmetric(vertical: 10),
|
padding: const EdgeInsets.symmetric(vertical: 10),
|
||||||
child: ListTile(
|
child: ListTile(
|
||||||
onTap: onTap,
|
onTap: _onTap(context),
|
||||||
contentPadding: EdgeInsets.zero,
|
contentPadding: EdgeInsets.zero,
|
||||||
title: Row(
|
title: Row(
|
||||||
children: [
|
children: [
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 20),
|
padding: const EdgeInsets.symmetric(horizontal: 20),
|
||||||
child: Text(getTime()),
|
child: Text(formatTime(search.timestamp)),
|
||||||
),
|
),
|
||||||
DefaultTextStyle.merge(
|
DefaultTextStyle.merge(
|
||||||
style: japaneseFont.textStyle,
|
style: japaneseFont.textStyle,
|
||||||
child: search,
|
child: _child,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import 'package:get_it/get_it.dart';
|
||||||
import 'package:sembast/sembast.dart';
|
import 'package:sembast/sembast.dart';
|
||||||
|
|
||||||
import './kanji_query.dart';
|
import './kanji_query.dart';
|
||||||
@ -7,39 +8,81 @@ export 'package:get_it/get_it.dart';
|
|||||||
export 'package:sembast/sembast.dart';
|
export 'package:sembast/sembast.dart';
|
||||||
|
|
||||||
class Search {
|
class Search {
|
||||||
final DateTime timestamp;
|
|
||||||
final WordQuery? wordQuery;
|
final WordQuery? wordQuery;
|
||||||
final KanjiQuery? kanjiQuery;
|
final KanjiQuery? kanjiQuery;
|
||||||
|
final List<DateTime> timestamps;
|
||||||
|
|
||||||
Search.fromKanjiQuery({
|
Search.fromKanjiQuery({
|
||||||
required this.timestamp,
|
|
||||||
required KanjiQuery this.kanjiQuery,
|
required KanjiQuery this.kanjiQuery,
|
||||||
}) : wordQuery = null;
|
List<DateTime>? timestamps,
|
||||||
|
}) : wordQuery = null,
|
||||||
|
timestamps = timestamps ?? [DateTime.now()];
|
||||||
|
|
||||||
Search.fromWordQuery({
|
Search.fromWordQuery({
|
||||||
required this.timestamp,
|
|
||||||
required WordQuery this.wordQuery,
|
required WordQuery this.wordQuery,
|
||||||
}) : kanjiQuery = null;
|
List<DateTime>? timestamps,
|
||||||
|
}) : kanjiQuery = null,
|
||||||
|
timestamps = timestamps ?? [DateTime.now()];
|
||||||
|
|
||||||
bool get isKanji => wordQuery == null;
|
bool get isKanji => wordQuery == null;
|
||||||
|
|
||||||
|
DateTime get timestamp => timestamps.last;
|
||||||
|
|
||||||
Map<String, Object?> toJson() => {
|
Map<String, Object?> toJson() => {
|
||||||
'timestamp': timestamp.millisecondsSinceEpoch,
|
'timestamps': [for (final ts in timestamps) ts.millisecondsSinceEpoch],
|
||||||
|
'lastTimestamp': timestamps.last.millisecondsSinceEpoch,
|
||||||
'wordQuery': wordQuery?.toJson(),
|
'wordQuery': wordQuery?.toJson(),
|
||||||
'kanjiQuery': kanjiQuery?.toJson(),
|
'kanjiQuery': kanjiQuery?.toJson(),
|
||||||
};
|
};
|
||||||
|
|
||||||
factory Search.fromJson(Map<String, dynamic> json) =>
|
factory Search.fromJson(Map<String, dynamic> json) {
|
||||||
json['wordQuery'] != null
|
final List<DateTime> timestamps = [
|
||||||
|
for (final ts in json['timestamps'] as List<dynamic>)
|
||||||
|
DateTime.fromMillisecondsSinceEpoch(ts as int)
|
||||||
|
];
|
||||||
|
|
||||||
|
return json['wordQuery'] != null
|
||||||
? Search.fromWordQuery(
|
? Search.fromWordQuery(
|
||||||
timestamp:
|
|
||||||
DateTime.fromMillisecondsSinceEpoch(json['timestamp'] as int),
|
|
||||||
wordQuery: WordQuery.fromJson(json['wordQuery']),
|
wordQuery: WordQuery.fromJson(json['wordQuery']),
|
||||||
|
timestamps: timestamps,
|
||||||
)
|
)
|
||||||
: Search.fromKanjiQuery(
|
: Search.fromKanjiQuery(
|
||||||
timestamp: DateTime.fromMillisecondsSinceEpoch(json['timestamp'] as int),
|
|
||||||
kanjiQuery: KanjiQuery.fromJson(json['kanjiQuery']),
|
kanjiQuery: KanjiQuery.fromJson(json['kanjiQuery']),
|
||||||
|
timestamps: timestamps,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
static StoreRef<int, Object?> get store => intMapStoreFactory.store('search');
|
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(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
@ -3,11 +3,10 @@ import 'package:flutter/material.dart';
|
|||||||
import '../screens/home.dart';
|
import '../screens/home.dart';
|
||||||
import '../screens/info/about.dart';
|
import '../screens/info/about.dart';
|
||||||
import '../screens/info/licenses.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/drawing.dart';
|
||||||
import '../screens/search/search_mechanisms/grade_list.dart';
|
import '../screens/search/search_mechanisms/grade_list.dart';
|
||||||
import '../screens/search/search_mechanisms/radical_list.dart';
|
import '../screens/search/search_mechanisms/radical_list.dart';
|
||||||
import '../screens/search/search_results_page.dart';
|
|
||||||
import 'routes.dart';
|
import 'routes.dart';
|
||||||
|
|
||||||
Route<Widget> generateRoute(RouteSettings settings) {
|
Route<Widget> generateRoute(RouteSettings settings) {
|
||||||
@ -20,13 +19,13 @@ Route<Widget> generateRoute(RouteSettings settings) {
|
|||||||
case Routes.search:
|
case Routes.search:
|
||||||
final searchTerm = args! as String;
|
final searchTerm = args! as String;
|
||||||
return MaterialPageRoute(
|
return MaterialPageRoute(
|
||||||
builder: (_) => SearchResultsPage(searchTerm: searchTerm),
|
builder: (_) => ResultPage(searchTerm: searchTerm, isKanji: false),
|
||||||
);
|
);
|
||||||
|
|
||||||
case Routes.kanjiSearch:
|
case Routes.kanjiSearch:
|
||||||
final searchTerm = args! as String;
|
final searchTerm = args! as String;
|
||||||
return MaterialPageRoute(
|
return MaterialPageRoute(
|
||||||
builder: (_) => KanjiResultPage(kanjiSearchTerm: searchTerm),
|
builder: (_) => ResultPage(searchTerm: searchTerm, isKanji: true),
|
||||||
);
|
);
|
||||||
|
|
||||||
case Routes.kanjiSearchDraw:
|
case Routes.kanjiSearchDraw:
|
||||||
|
@ -3,18 +3,15 @@ import 'package:flutter/material.dart';
|
|||||||
import '../components/common/loading.dart';
|
import '../components/common/loading.dart';
|
||||||
import '../components/common/opaque_box.dart';
|
import '../components/common/opaque_box.dart';
|
||||||
import '../components/history/date_divider.dart';
|
import '../components/history/date_divider.dart';
|
||||||
import '../components/history/kanji_box.dart';
|
|
||||||
import '../components/history/search_item.dart';
|
import '../components/history/search_item.dart';
|
||||||
import '../models/history/search.dart';
|
import '../models/history/search.dart';
|
||||||
import '../routing/routes.dart';
|
import '../services/datetime.dart';
|
||||||
|
|
||||||
class HistoryView extends StatelessWidget {
|
class HistoryView extends StatelessWidget {
|
||||||
const HistoryView({Key? key}) : super(key: key);
|
const HistoryView({Key? key}) : super(key: key);
|
||||||
|
|
||||||
Database get _db => GetIt.instance.get<Database>();
|
|
||||||
|
|
||||||
Stream<Map<int, Search>> get searchStream => Search.store
|
Stream<Map<int, Search>> get searchStream => Search.store
|
||||||
.query(finder: Finder(sortOrders: [SortOrder('timestamp', false)]))
|
.query(finder: Finder(sortOrders: [SortOrder('lastTimestamp', false)]))
|
||||||
.onSnapshots(_db)
|
.onSnapshots(_db)
|
||||||
.map(
|
.map(
|
||||||
(snapshot) => Map.fromEntries(
|
(snapshot) => Map.fromEntries(
|
||||||
@ -27,6 +24,8 @@ class HistoryView extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Database get _db => GetIt.instance.get<Database>();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return StreamBuilder<Map<int, Search>>(
|
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(
|
Widget Function(BuildContext, int) historyEntrySeparatorWithData(
|
||||||
List<Search> data,
|
List<Search> data,
|
||||||
) =>
|
) =>
|
||||||
(context, index) {
|
(context, index) {
|
||||||
final Search search = data[index];
|
final Search search = data[index];
|
||||||
final DateTime searchDate = roundToDay(search.timestamp);
|
final DateTime searchDate = search.timestamp;
|
||||||
|
|
||||||
if (index == 0 ||
|
if (index == 0 || !dateIsEqual(data[index - 1].timestamp, searchDate))
|
||||||
dateChangedFromLastSearch(data[index - 1], searchDate)) {
|
return TextDivider(text: formatDate(roundToDay(searchDate)));
|
||||||
if (searchDate == today)
|
|
||||||
return const DateDivider(text: 'Today');
|
|
||||||
else if (searchDate == yesterday)
|
|
||||||
return const DateDivider(text: 'Yesterday');
|
|
||||||
else
|
|
||||||
return DateDivider(date: searchDate);
|
|
||||||
}
|
|
||||||
|
|
||||||
return const Divider(height: 0);
|
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),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
@ -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!);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
56
lib/screens/search/result_page.dart
Normal file
56
lib/screens/search/result_page.dart
Normal 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!,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -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!,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
39
lib/services/datetime.dart
Normal file
39
lib/services/datetime.dart
Normal 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';
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user