Skip to content

Commit

Permalink
fix: handle case where identified kanji does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Moseco committed Oct 1, 2024
1 parent 03d969f commit 6fd6d57
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 40 deletions.
2 changes: 1 addition & 1 deletion lib/services/dictionary_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class DictionaryService {
(a.common ? 1 : 0);
}

Future<Kanji> getKanji(String kanji) async {
Future<Kanji?> getKanji(String kanji) async {
return _database.kanjisDao.getKanji(kanji);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/ui/views/flashcards/flashcards_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -660,8 +660,8 @@ class FlashcardsViewModel extends FutureViewModel {
if (kanjiStrings.contains(foundKanji[0]!)) continue;
kanjiStrings.add(foundKanji[0]!);
// Load from database
vocab.includedKanji!
.add(await _dictionaryService.getKanji(foundKanji[0]!));
final kanji = await _dictionaryService.getKanji(foundKanji[0]!);
if (kanji != null) vocab.includedKanji!.add(kanji);
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/ui/views/proper_noun/proper_noun_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ProperNounView extends StackedView<ProperNounViewModel> {
children: [
const _WritingReading(),
const _RomajiAndType(),
if (viewModel.kanjiStringList.isNotEmpty) const _KanjiList(),
if (viewModel.kanjiList.isNotEmpty) const _KanjiList(),
SizedBox(height: MediaQuery.of(context).padding.bottom),
],
),
Expand Down Expand Up @@ -135,7 +135,7 @@ class _KanjiList extends ViewModelWidget<ProperNounViewModel> {
child: Column(
children: viewModel.isBusy
? List.generate(
viewModel.kanjiStringList.length,
viewModel.kanjiList.length,
(index) {
return Padding(
padding: const EdgeInsets.symmetric(
Expand All @@ -151,7 +151,7 @@ class _KanjiList extends ViewModelWidget<ProperNounViewModel> {
right: 12,
),
child: Text(
viewModel.kanjiStringList[index],
viewModel.kanjiList[index],
style: const TextStyle(fontSize: 24),
),
),
Expand Down
11 changes: 6 additions & 5 deletions lib/ui/views/proper_noun/proper_noun_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class ProperNounViewModel extends FutureViewModel {

final ProperNoun properNoun;

late final List<String> kanjiStringList;
late final List<Kanji> kanjiList;
List<dynamic> get kanjiList => isBusy ? _kanjiStringList : _kanjiList;
late final List<String> _kanjiStringList;
late final List<Kanji> _kanjiList;

ProperNounViewModel(this.properNoun) {
// Get list of kanji to be loaded during initialize function
Expand All @@ -26,14 +27,14 @@ class ProperNounViewModel extends FutureViewModel {
kanjiStringSet.add(foundKanji[0]!);
}
}
kanjiStringList = kanjiStringSet.toList();
_kanjiStringList = kanjiStringSet.toList();
}

@override
Future<void> futureToRun() async {
// Load kanji from database that were found during class constructor
kanjiList = await _dictionaryService
.getKanjiList(kanjiStringList.map((e) => e.kanjiCodePoint()).toList());
_kanjiList = await _dictionaryService
.getKanjiList(_kanjiStringList.map((e) => e.kanjiCodePoint()).toList());
}

Future<void> navigateToKanji(Kanji kanji) async {
Expand Down
6 changes: 3 additions & 3 deletions lib/ui/views/vocab/vocab_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class VocabView extends StackedView<VocabViewModel> {
children: [
_KanjiReadingPairs(viewModel.writingReadingPairs),
const _Definitions(),
if (viewModel.kanjiStringList.isNotEmpty) const _KanjiList(),
if (viewModel.kanjiList.isNotEmpty) const _KanjiList(),
const _Examples(),
if (viewModel.conjugations != null) const _Conjugations(),
SizedBox(height: MediaQuery.of(context).padding.bottom),
Expand Down Expand Up @@ -655,7 +655,7 @@ class _KanjiList extends ViewModelWidget<VocabViewModel> {
child: Column(
children: viewModel.isBusy
? List.generate(
viewModel.kanjiStringList.length,
viewModel.kanjiList.length,
(index) {
return Padding(
padding: const EdgeInsets.symmetric(
Expand All @@ -671,7 +671,7 @@ class _KanjiList extends ViewModelWidget<VocabViewModel> {
right: 12,
),
child: Text(
viewModel.kanjiStringList[index],
viewModel.kanjiList[index],
style: const TextStyle(fontSize: 24),
),
),
Expand Down
11 changes: 6 additions & 5 deletions lib/ui/views/vocab/vocab_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ class VocabViewModel extends FutureViewModel {
final Vocab vocab;

late final List<WritingReadingPair> writingReadingPairs;
late final List<String> kanjiStringList;
late final List<Kanji> kanjiList;
List<dynamic> get kanjiList => isBusy ? _kanjiStringList : _kanjiList;
late final List<String> _kanjiStringList;
late final List<Kanji> _kanjiList;

List<int> _myDictionaryListsContainingVocab = [];
bool get inMyDictionaryList => _myDictionaryListsContainingVocab.isNotEmpty;
Expand All @@ -53,7 +54,7 @@ class VocabViewModel extends FutureViewModel {
}
}
}
kanjiStringList = kanjiStringSet.toList();
_kanjiStringList = kanjiStringSet.toList();

// Get conjugations
conjugations = _conjugationUtils.getConjugations(vocab);
Expand All @@ -75,8 +76,8 @@ class VocabViewModel extends FutureViewModel {
rebuildUi();

// Load kanji from database that were found during class constructor
kanjiList = await _dictionaryService
.getKanjiList(kanjiStringList.map((e) => e.kanjiCodePoint()).toList());
_kanjiList = await _dictionaryService
.getKanjiList(_kanjiStringList.map((e) => e.kanjiCodePoint()).toList());
}

void _startWatcher() {
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: "003d79c"
resolved-ref: "003d79c6228e5e3299e8c22e93ec3820f5dee4d4"
ref: "428d744"
resolved-ref: "428d74484705dd96e2bd43b39e7c9963aebc6182"
url: "https://github.com/Moseco/sagase_dictionary"
source: git
version: "1.0.0"
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ dependencies:
sagase_dictionary:
git:
url: https://github.com/Moseco/sagase_dictionary
ref: '003d79c'
ref: '428d744'
url_launcher: ^6.1.12
dio: ^5.3.3
in_app_review: ^2.0.8
Expand Down
16 changes: 8 additions & 8 deletions test/services/dictionary_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void main() {
);
await service.addToMyDictionaryList(
dictionaryList,
await service.getKanji('二'),
(await service.getKanji('二'))!,
);

// Create flashcard set
Expand Down Expand Up @@ -258,7 +258,7 @@ void main() {
);
await service.addToMyDictionaryList(
dictionaryList,
await service.getKanji('三'),
(await service.getKanji('三'))!,
);
await service.renameMyDictionaryList(dictionaryList, 'new name');

Expand All @@ -275,13 +275,13 @@ void main() {
);
await service.setSpacedRepetitionData(
SpacedRepetitionData.initial(
dictionaryItem: await service.getKanji('二'),
dictionaryItem: (await service.getKanji('二'))!,
frontType: FrontType.japanese,
).copyWith(interval: 4),
);
await service.setSpacedRepetitionData(
SpacedRepetitionData.initial(
dictionaryItem: await service.getKanji('三'),
dictionaryItem: (await service.getKanji('三'))!,
frontType: FrontType.english,
).copyWith(interval: 4),
);
Expand Down Expand Up @@ -431,11 +431,11 @@ void main() {
);
await service.addToMyDictionaryList(
dictionaryList,
await service.getKanji('二'),
(await service.getKanji('二'))!,
);
await service.addToMyDictionaryList(
dictionaryList,
await service.getKanji('三'),
(await service.getKanji('三'))!,
);

// Create flashcard set
Expand Down Expand Up @@ -468,13 +468,13 @@ void main() {
);
await service.setSpacedRepetitionData(
SpacedRepetitionData.initial(
dictionaryItem: await service.getKanji('三'),
dictionaryItem: (await service.getKanji('三'))!,
frontType: FrontType.japanese,
).copyWith(interval: 3),
);
await service.setSpacedRepetitionData(
SpacedRepetitionData.initial(
dictionaryItem: await service.getKanji('三'),
dictionaryItem: (await service.getKanji('三'))!,
frontType: FrontType.english,
).copyWith(interval: 4),
);
Expand Down
20 changes: 10 additions & 10 deletions test/ui/views/flashcards/flashcards_viewmodel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -585,25 +585,25 @@ void main() {
);
}
await dictionaryService.addToMyDictionaryList(
dictionaryList, await dictionaryService.getKanji('一'));
dictionaryList, (await dictionaryService.getKanji('一'))!);
await dictionaryService.addToMyDictionaryList(
dictionaryList, await dictionaryService.getKanji('二'));
dictionaryList, (await dictionaryService.getKanji('二'))!);
await dictionaryService.addToMyDictionaryList(
dictionaryList, await dictionaryService.getKanji('三'));
dictionaryList, (await dictionaryService.getKanji('三'))!);
await dictionaryService.addToMyDictionaryList(
dictionaryList, await dictionaryService.getKanji('四'));
dictionaryList, (await dictionaryService.getKanji('四'))!);
await dictionaryService.addToMyDictionaryList(
dictionaryList, await dictionaryService.getKanji('五'));
dictionaryList, (await dictionaryService.getKanji('五'))!);
await dictionaryService.addToMyDictionaryList(
dictionaryList, await dictionaryService.getKanji('六'));
dictionaryList, (await dictionaryService.getKanji('六'))!);
await dictionaryService.addToMyDictionaryList(
dictionaryList, await dictionaryService.getKanji('七'));
dictionaryList, (await dictionaryService.getKanji('七'))!);
await dictionaryService.addToMyDictionaryList(
dictionaryList, await dictionaryService.getKanji('八'));
dictionaryList, (await dictionaryService.getKanji('八'))!);
await dictionaryService.addToMyDictionaryList(
dictionaryList, await dictionaryService.getKanji('九'));
dictionaryList, (await dictionaryService.getKanji('九'))!);
await dictionaryService.addToMyDictionaryList(
dictionaryList, await dictionaryService.getKanji('十'));
dictionaryList, (await dictionaryService.getKanji('十'))!);

// Create flashcard set and assign lists
final flashcardSet = await dictionaryService.createFlashcardSet('name');
Expand Down

0 comments on commit 6fd6d57

Please sign in to comment.