Add search bloc
This commit is contained in:
parent
879bbf0581
commit
42a6f5e412
|
@ -5,6 +5,9 @@ import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
|
|
||||||
|
import 'package:jisho_study_tool/services/jisho_search.dart';
|
||||||
|
import 'package:unofficial_jisho_api/parser.dart';
|
||||||
|
|
||||||
part 'search_event.dart';
|
part 'search_event.dart';
|
||||||
part 'search_state.dart';
|
part 'search_state.dart';
|
||||||
|
|
||||||
|
@ -15,6 +18,17 @@ class SearchBloc extends Bloc<SearchEvent, SearchState> {
|
||||||
Stream<SearchState> mapEventToState(
|
Stream<SearchState> mapEventToState(
|
||||||
SearchEvent event,
|
SearchEvent event,
|
||||||
) async* {
|
) async* {
|
||||||
// TODO: implement mapEventToState
|
if (event is GetSearchResults) {
|
||||||
|
yield SearchLoading();
|
||||||
|
|
||||||
|
try {
|
||||||
|
final _searchResults = await fetchJishoResults(event.searchString);
|
||||||
|
yield SearchFinished(_searchResults);
|
||||||
|
} on Exception {
|
||||||
|
yield SearchError('Something went wrong');
|
||||||
|
}
|
||||||
|
} else if (event is ReturnToInitialState) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,15 @@
|
||||||
part of 'search_bloc.dart';
|
part of 'search_bloc.dart';
|
||||||
|
|
||||||
@immutable
|
@immutable
|
||||||
abstract class SearchEvent {}
|
abstract class SearchEvent {
|
||||||
|
const SearchEvent();
|
||||||
|
}
|
||||||
|
|
||||||
|
class GetSearchResults extends SearchEvent {
|
||||||
|
final String searchString;
|
||||||
|
const GetSearchResults(this.searchString);
|
||||||
|
}
|
||||||
|
|
||||||
|
class ReturnToInitialState extends SearchEvent {
|
||||||
|
const ReturnToInitialState();
|
||||||
|
}
|
|
@ -1,12 +1,26 @@
|
||||||
part of 'search_bloc.dart';
|
part of 'search_bloc.dart';
|
||||||
|
|
||||||
@immutable
|
@immutable
|
||||||
abstract class SearchState {}
|
abstract class SearchState {
|
||||||
|
const SearchState();
|
||||||
|
}
|
||||||
|
|
||||||
class SearchInitial extends SearchState {}
|
class SearchInitial extends SearchState {
|
||||||
|
const SearchInitial();
|
||||||
|
}
|
||||||
|
|
||||||
class SearchLoading extends SearchState {}
|
class SearchLoading extends SearchState {
|
||||||
|
const SearchLoading();
|
||||||
|
}
|
||||||
|
|
||||||
class SearchFinished extends SearchState {}
|
class SearchFinished extends SearchState {
|
||||||
|
final JishoAPIResult result;
|
||||||
|
|
||||||
class SearchError extends SearchState {}
|
const SearchFinished(this.result);
|
||||||
|
}
|
||||||
|
|
||||||
|
class SearchError extends SearchState {
|
||||||
|
final String message;
|
||||||
|
|
||||||
|
const SearchError(this.message);
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
import 'package:unofficial_jisho_api/api.dart' as jisho;
|
||||||
|
|
||||||
|
Future<jisho.JishoAPIResult> fetchJishoResults(searchTerm) async {
|
||||||
|
return await jisho.searchForPhrase(searchTerm);
|
||||||
|
}
|
Loading…
Reference in New Issue