From d652fc0685477f20930be6aa28ddfdbafd7d5efe Mon Sep 17 00:00:00 2001 From: h7x4 Date: Sun, 8 May 2022 15:33:03 +0200 Subject: [PATCH] Update API --- lib/components/kanji/kanji_result_body.dart | 4 +++- lib/services/jisho_api/kanji_search.dart | 24 ++------------------- lib/services/kanji_grade_conversion.dart | 23 ++++++++++++++++++++ pubspec.yaml | 2 +- 4 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 lib/services/kanji_grade_conversion.dart diff --git a/lib/components/kanji/kanji_result_body.dart b/lib/components/kanji/kanji_result_body.dart index 4f8fd9e..614beab 100644 --- a/lib/components/kanji/kanji_result_body.dart +++ b/lib/components/kanji/kanji_result_body.dart @@ -9,6 +9,7 @@ import './kanji_result_body/radical.dart'; import './kanji_result_body/rank.dart'; import './kanji_result_body/stroke_order_gif.dart'; import './kanji_result_body/yomi_chips.dart'; +import '../../services/kanji_grade_conversion.dart'; class KanjiResultBody extends StatelessWidget { late final String query; @@ -24,6 +25,7 @@ class KanjiResultBody extends StatelessWidget { resultData = result.data!; } + // TODO: add compart link Widget get headerRow => Container( margin: const EdgeInsets.fromLTRB(20.0, 20.0, 20.0, 30.0), child: Row( @@ -62,7 +64,7 @@ class KanjiResultBody extends StatelessWidget { Row( children: [ const Text('Grade: ', style: TextStyle(fontSize: 20.0)), - Grade(grade: resultData.taughtIn), + Grade(grade: resultData.grade), ], ), Row( diff --git a/lib/services/jisho_api/kanji_search.dart b/lib/services/jisho_api/kanji_search.dart index 4717d30..fe8f722 100644 --- a/lib/services/jisho_api/kanji_search.dart +++ b/lib/services/jisho_api/kanji_search.dart @@ -1,25 +1,5 @@ import 'package:unofficial_jisho_api/api.dart' as jisho; export 'package:unofficial_jisho_api/api.dart' show KanjiResult; -String? _convertGrade(String grade) { - const conversionTable = { - 'grade 1': '小1', - 'grade 2': '小2', - 'grade 3': '小3', - 'grade 4': '小4', - 'grade 5': '小5', - 'grade 6': '小6', - 'junior high': '中' - }; - - return conversionTable[grade]; -} - -// TODO: fix this logic - -Future fetchKanji(String kanji) async { - final result = await jisho.searchForKanji(kanji); - if (result.data != null && result.data?.taughtIn != null) - result.data!.taughtIn = _convertGrade(result.data!.taughtIn!); - return result; -} +Future fetchKanji(String kanji) => + jisho.searchForKanji(kanji); diff --git a/lib/services/kanji_grade_conversion.dart b/lib/services/kanji_grade_conversion.dart new file mode 100644 index 0000000..81bb57b --- /dev/null +++ b/lib/services/kanji_grade_conversion.dart @@ -0,0 +1,23 @@ +import 'package:unofficial_jisho_api/api.dart'; + +extension GradeConversion on KanjiResultData { + String? get grade => { + 'grade 1': '小1', + 'grade 2': '小2', + 'grade 3': '小3', + 'grade 4': '小4', + 'grade 5': '小5', + 'grade 6': '小6', + 'junior high': '中' + }[taughtIn]; + + int? get gradeNum => { + 'grade 1': 1, + 'grade 2': 2, + 'grade 3': 3, + 'grade 4': 4, + 'grade 5': 5, + 'grade 6': 6, + 'junior high': 7, + }[taughtIn]; +} diff --git a/pubspec.yaml b/pubspec.yaml index 96fe905..1ce7037 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -28,7 +28,7 @@ dependencies: test: ^1.19.5 shared_preferences: ^2.0.6 signature: ^5.0.0 - unofficial_jisho_api: ^2.0.4 + unofficial_jisho_api: ^3.0.0 url_launcher: ^6.0.9 dev_dependencies: