Add bloc logic

This commit is contained in:
Oystein Kristoffer Tveit 2020-07-13 15:18:17 +02:00
parent 8480bb12a4
commit 2b11f5f8c2
3 changed files with 42 additions and 15 deletions

View File

@ -1,18 +1,27 @@
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:meta/meta.dart';
import './kanji_event.dart';
import './kanji_state.dart';
part 'kanji_event.dart';
part 'kanji_state.dart';
import 'package:bloc/bloc.dart';
import 'package:jisho_study_tool/services/kanji_search.dart';
class KanjiBloc extends Bloc<KanjiEvent, KanjiState> {
KanjiBloc() : super(KanjiSearchInitial());
@override
Stream<KanjiState> mapEventToState(
KanjiEvent event,
) async* {
// TODO: implement mapEventToState
yield KanjiSearchLoading();
if (event is GetKanji) {
try {
final _kanji = await fetchKanji(event.kanjiSearchString);
yield KanjiSearchFinished(_kanji);
} on Exception {
yield KanjiSearchError('Something went wrong');
}
}
}
}

View File

@ -1,4 +1,9 @@
part of 'kanji_bloc.dart';
abstract class KanjiEvent {
const KanjiEvent();
}
@immutable
abstract class KanjiEvent {}
class GetKanji extends KanjiEvent {
final String kanjiSearchString;
GetKanji(this.kanjiSearchString);
}

View File

@ -1,14 +1,27 @@
part of 'kanji_bloc.dart';
import 'package:unofficial_jisho_api/api.dart';
@immutable
abstract class KanjiState {}
abstract class KanjiState {
const KanjiState();
}
class KanjiSearchInitial extends KanjiState {}
class KanjiSearchInitial extends KanjiState {
KanjiSearchInitial();
}
class KanjiSearchLoading extends KanjiState {}
class KanjiSearchLoading extends KanjiState {
KanjiSearchLoading();
}
class KanjiSearchFinished extends KanjiState {}
class KanjiSearchFinished extends KanjiState {
final KanjiResult kanji;
class KanjiSearchError extends KanjiState {}
KanjiSearchFinished(this.kanji);
}
class KanjiSearchError extends KanjiState {
final String message;
KanjiSearchError(this.message);
}
class ReKanjiSearch extends KanjiState {}