1
0
mirror of https://github.com/h7x4/Jisho-Study-Tool.git synced 2024-12-22 13:57:29 +01:00
Jisho-Study-Tool/lib/bloc/kanji/kanji_bloc.dart
h7x4 76d2b090f3
JST 4 - Add kanji search suggestions (#3)
* Add division package

* Add base logic and widget

* Finish layout

* Fix kanji regex

* Add bloc logic
2020-07-21 23:29:02 +02:00

42 lines
1.1 KiB
Dart

import 'dart:async';
import './kanji_event.dart';
import './kanji_state.dart';
import 'package:bloc/bloc.dart';
import 'package:jisho_study_tool/services/kanji_search.dart';
import 'package:jisho_study_tool/services/kanji_suggestions.dart';
export './kanji_event.dart';
export './kanji_state.dart';
class KanjiBloc extends Bloc<KanjiEvent, KanjiState> {
KanjiBloc() : super(KanjiSearchInitial());
@override
Stream<KanjiState> mapEventToState(
KanjiEvent event,
) async* {
if (event is GetKanji) {
yield KanjiSearchLoading();
try {
final _kanji = await fetchKanji(event.kanjiSearchString);
if (_kanji.found) yield KanjiSearchFinished(kanji: _kanji);
else yield KanjiSearchError('Something went wrong');
} on Exception {
yield KanjiSearchError('Something went wrong');
}
} else if (event is GetKanjiSuggestions) {
final suggestions = kanjiSuggestions(event.searchString);
yield KanjiSearchInput(suggestions);
} else if (event is ReturnToInitialState) {
yield KanjiSearchInitial();
}
}
}