Skip to content

Commit

Permalink
feat: add ability to navigate a up and down a list from vocab/kanji s…
Browse files Browse the repository at this point in the history
…creen
  • Loading branch information
Moseco committed Oct 1, 2024
1 parent 7fcc687 commit 8721b1f
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/ui/views/dictionary_list/dictionary_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class _VocabList extends ViewModelWidget<DictionaryListViewModel> {

return VocabListItem(
vocab: current,
onPressed: () => viewModel.navigateToVocab(current),
onPressed: () => viewModel.navigateToVocab(current, index),
);
},
);
Expand All @@ -112,7 +112,7 @@ class _KanjiList extends ViewModelWidget<DictionaryListViewModel> {

return KanjiListItem(
kanji: current,
onPressed: () => viewModel.navigateToKanji(current),
onPressed: () => viewModel.navigateToKanji(current, index),
);
},
);
Expand Down
16 changes: 12 additions & 4 deletions lib/ui/views/dictionary_list/dictionary_list_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,25 @@ class DictionaryListViewModel extends FutureViewModel {
}
}

Future<void> navigateToVocab(Vocab vocab) async {
Future<void> navigateToVocab(Vocab vocab, int index) async {
await _navigationService.navigateTo(
Routes.vocabView,
arguments: VocabViewArguments(vocab: vocab),
arguments: VocabViewArguments(
vocab: vocab,
vocabListIndex: index,
vocabList: vocabList,
),
);
}

Future<void> navigateToKanji(Kanji kanji) async {
Future<void> navigateToKanji(Kanji kanji, int index) async {
await _navigationService.navigateTo(
Routes.kanjiView,
arguments: KanjiViewArguments(kanji: kanji),
arguments: KanjiViewArguments(
kanji: kanji,
kanjiListIndex: index,
kanjiList: kanjiList,
),
);
}

Expand Down
21 changes: 19 additions & 2 deletions lib/ui/views/kanji/kanji_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,34 @@ import 'kanji_viewmodel.dart';

class KanjiView extends StackedView<KanjiViewModel> {
final Kanji kanji;
final int? kanjiListIndex;
final List<Kanji>? kanjiList;

const KanjiView(this.kanji, {super.key});
const KanjiView(this.kanji, {this.kanjiListIndex, this.kanjiList, super.key});

@override
KanjiViewModel viewModelBuilder(context) => KanjiViewModel(kanji);
KanjiViewModel viewModelBuilder(context) =>
KanjiViewModel(kanji, kanjiListIndex, kanjiList);

@override
Widget builder(context, viewModel, child) {
return Scaffold(
appBar: AppBar(
actions: [
if (kanjiList != null)
IconButton(
onPressed: kanjiListIndex == 0
? null
: viewModel.navigateToPreviousKanji,
icon: const Icon(Icons.chevron_left),
),
if (kanjiList != null)
IconButton(
onPressed: kanjiListIndex! == kanjiList!.length - 1
? null
: viewModel.navigateToNextKanji,
icon: const Icon(Icons.chevron_right),
),
IconButton(
onPressed: viewModel.openMyDictionaryListsSheet,
icon: Icon(
Expand Down
26 changes: 25 additions & 1 deletion lib/ui/views/kanji/kanji_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class KanjiViewModel extends FutureViewModel {
final _sharedPreferencesService = locator<SharedPreferencesService>();

final Kanji kanji;
final int? kanjiListIndex;
final List<Kanji>? kanjiList;

Radical? radical;
List<Kanji>? components;
Expand All @@ -31,7 +33,7 @@ class KanjiViewModel extends FutureViewModel {
bool get strokeDiagramStartExpanded =>
_sharedPreferencesService.getStrokeDiagramStartExpanded();

KanjiViewModel(this.kanji);
KanjiViewModel(this.kanji, this.kanjiListIndex, this.kanjiList);

@override
Future<void> futureToRun() async {
Expand Down Expand Up @@ -155,6 +157,28 @@ class KanjiViewModel extends FutureViewModel {
_sharedPreferencesService.setStrokeDiagramStartExpanded(value);
}

void navigateToPreviousKanji() {
_navigationService.replaceWith(
Routes.kanjiView,
arguments: KanjiViewArguments(
kanji: kanjiList![kanjiListIndex! - 1],
kanjiListIndex: kanjiListIndex! - 1,
kanjiList: kanjiList,
),
);
}

void navigateToNextKanji() {
_navigationService.replaceWith(
Routes.kanjiView,
arguments: KanjiViewArguments(
kanji: kanjiList![kanjiListIndex! + 1],
kanjiListIndex: kanjiListIndex! + 1,
kanjiList: kanjiList,
),
);
}

@override
void dispose() {
_myDictionaryListsWatcher?.cancel();
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/views/kanji_compounds/kanji_compounds_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class KanjiCompoundsView extends StackedView<KanjiCompoundsViewModel> {
itemCount: viewModel.vocabList.length,
itemBuilder: (context, index) => VocabListItem(
vocab: viewModel.vocabList[index],
onPressed: () =>
viewModel.navigateToVocab(viewModel.vocabList[index]),
onPressed: () => viewModel.navigateToVocab(
viewModel.vocabList[index], index),
),
),
),
Expand Down
8 changes: 6 additions & 2 deletions lib/ui/views/kanji_compounds/kanji_compounds_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ class KanjiCompoundsViewModel extends FutureViewModel {
vocabList = onlyKanji + inPrimaryWriting + other;
}

void navigateToVocab(Vocab vocab) {
void navigateToVocab(Vocab vocab, int index) {
_navigationService.navigateTo(
Routes.vocabView,
arguments: VocabViewArguments(vocab: vocab),
arguments: VocabViewArguments(
vocab: vocab,
vocabListIndex: index,
vocabList: vocabList,
),
);
}
}
2 changes: 1 addition & 1 deletion lib/ui/views/kanji_list/kanji_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class KanjiListView extends StackedView<KanjiListViewModel> {
final current = kanjiList[index];
return KanjiListItem(
kanji: current,
onPressed: () => viewModel.navigateToKanji(current),
onPressed: () => viewModel.navigateToKanji(current, index),
);
},
),
Expand Down
8 changes: 6 additions & 2 deletions lib/ui/views/kanji_list/kanji_list_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ class KanjiListViewModel extends BaseViewModel {

KanjiListViewModel(this.kanjiList);

void navigateToKanji(Kanji kanji) {
void navigateToKanji(Kanji kanji, int index) {
_navigationService.navigateTo(
Routes.kanjiView,
arguments: KanjiViewArguments(kanji: kanji),
arguments: KanjiViewArguments(
kanji: kanji,
kanjiListIndex: index,
kanjiList: kanjiList,
),
);
}
}
20 changes: 18 additions & 2 deletions lib/ui/views/vocab/vocab_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ import 'vocab_viewmodel.dart';

class VocabView extends StackedView<VocabViewModel> {
final Vocab vocab;
final int? vocabListIndex;
final List<Vocab>? vocabList;

final pitchAccentKey = GlobalKey();

VocabView(this.vocab, {super.key});
VocabView(this.vocab, {this.vocabListIndex, this.vocabList, super.key});

@override
VocabViewModel viewModelBuilder(context) {
final viewModel = VocabViewModel(vocab);
final viewModel = VocabViewModel(vocab, vocabListIndex, vocabList);

if (viewModel.shouldShowTutorial()) {
// Try to show tutorial after transition animation ends
Expand Down Expand Up @@ -53,6 +55,20 @@ class VocabView extends StackedView<VocabViewModel> {
return Scaffold(
appBar: AppBar(
actions: [
if (vocabList != null)
IconButton(
onPressed: vocabListIndex == 0
? null
: viewModel.navigateToPreviousVocab,
icon: const Icon(Icons.chevron_left),
),
if (vocabList != null)
IconButton(
onPressed: vocabListIndex! == vocabList!.length - 1
? null
: viewModel.navigateToNextVocab,
icon: const Icon(Icons.chevron_right),
),
IconButton(
key: pitchAccentKey,
onPressed: vocab.readings[0].pitchAccents != null
Expand Down
26 changes: 25 additions & 1 deletion lib/ui/views/vocab/vocab_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class VocabViewModel extends FutureViewModel {
final _conjugationUtils = const ConjugationUtils();

final Vocab vocab;
final int? vocabListIndex;
final List<Vocab>? vocabList;

late final List<WritingReadingPair> writingReadingPairs;
List<dynamic> get kanjiList => isBusy ? _kanjiStringList : _kanjiList;
Expand All @@ -40,7 +42,7 @@ class VocabViewModel extends FutureViewModel {

bool get showPitchAccent => _sharedPreferencesService.getShowPitchAccent();

VocabViewModel(this.vocab) {
VocabViewModel(this.vocab, this.vocabListIndex, this.vocabList) {
// Create writing reading pairs
writingReadingPairs = WritingReadingPair.fromVocab(vocab);

Expand Down Expand Up @@ -197,6 +199,28 @@ class VocabViewModel extends FutureViewModel {
}
}

void navigateToPreviousVocab() {
_navigationService.replaceWith(
Routes.vocabView,
arguments: VocabViewArguments(
vocab: vocabList![vocabListIndex! - 1],
vocabListIndex: vocabListIndex! - 1,
vocabList: vocabList,
),
);
}

void navigateToNextVocab() {
_navigationService.replaceWith(
Routes.vocabView,
arguments: VocabViewArguments(
vocab: vocabList![vocabListIndex! + 1],
vocabListIndex: vocabListIndex! + 1,
vocabList: vocabList,
),
);
}

@override
void dispose() {
_myDictionaryListsWatcher?.cancel();
Expand Down

0 comments on commit 8721b1f

Please sign in to comment.