2020-07-09 20:06:48 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
2020-07-13 15:18:17 +02:00
|
|
|
import './kanji_event.dart';
|
|
|
|
import './kanji_state.dart';
|
2020-07-09 20:06:48 +02:00
|
|
|
|
2020-07-13 15:18:17 +02:00
|
|
|
import 'package:bloc/bloc.dart';
|
|
|
|
import 'package:jisho_study_tool/services/kanji_search.dart';
|
2020-07-09 20:06:48 +02:00
|
|
|
|
2020-07-13 21:21:33 +02:00
|
|
|
export './kanji_event.dart';
|
|
|
|
export './kanji_state.dart';
|
|
|
|
|
2020-07-09 20:06:48 +02:00
|
|
|
class KanjiBloc extends Bloc<KanjiEvent, KanjiState> {
|
2020-07-13 15:18:17 +02:00
|
|
|
|
2020-07-09 20:06:48 +02:00
|
|
|
KanjiBloc() : super(KanjiSearchInitial());
|
|
|
|
|
|
|
|
@override
|
|
|
|
Stream<KanjiState> mapEventToState(
|
|
|
|
KanjiEvent event,
|
|
|
|
) async* {
|
2020-07-13 15:18:17 +02:00
|
|
|
if (event is GetKanji) {
|
2020-07-14 00:09:54 +02:00
|
|
|
|
|
|
|
yield KanjiSearchLoading();
|
|
|
|
|
2020-07-13 15:18:17 +02:00
|
|
|
try {
|
|
|
|
final _kanji = await fetchKanji(event.kanjiSearchString);
|
2020-07-16 14:08:35 +02:00
|
|
|
if (_kanji.found) yield KanjiSearchFinished(kanji: _kanji);
|
|
|
|
else yield KanjiSearchError('Something went wrong');
|
2020-07-13 15:18:17 +02:00
|
|
|
} on Exception {
|
|
|
|
yield KanjiSearchError('Something went wrong');
|
|
|
|
}
|
2020-07-14 00:09:54 +02:00
|
|
|
|
|
|
|
} else if (event is ReturnToInitialState) {
|
|
|
|
yield KanjiSearchInitial();
|
2020-07-13 15:18:17 +02:00
|
|
|
}
|
2020-07-09 20:06:48 +02:00
|
|
|
}
|
|
|
|
}
|