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