mirror of
https://github.com/h7x4/Jisho-Study-Tool.git
synced 2024-12-21 13:37:29 +01:00
Add bloc logic
This commit is contained in:
parent
8480bb12a4
commit
2b11f5f8c2
@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
@ -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 {}
|
||||
|
Loading…
Reference in New Issue
Block a user