Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
686 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export 'search_bloc.dart'; | ||
export 'search_event.dart'; | ||
export 'search_state.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:youtube_search_tutorial/data/repository/youtube_repository.dart'; | ||
import 'package:youtube_search_tutorial/ui/search/search.dart'; | ||
import 'package:youtube_search_tutorial/data/model/search/youtube_search_error.dart'; | ||
|
||
class SearchBloc extends Bloc<SearchEvent, SearchState> { | ||
final YoutubeRepository _youtubeRepository; | ||
|
||
SearchBloc(this._youtubeRepository) : super(); | ||
|
||
void onSearchInitiated(String query) { | ||
dispatch(SearchInitiated((b) => b..query = query)); | ||
} | ||
|
||
void fetchNextResultPage() { | ||
dispatch(FetchNextResultPage()); | ||
} | ||
|
||
@override | ||
SearchState get initialState => SearchState.initial(); | ||
|
||
@override | ||
Stream<SearchState> mapEventToState( | ||
SearchState currentState, SearchEvent event) async* { | ||
if (event is SearchInitiated) { | ||
yield* mapSearchInitiated(event); | ||
} else if (event is FetchNextResultPage) { | ||
yield* mapFetchNextResultPage(); | ||
} | ||
} | ||
|
||
Stream<SearchState> mapSearchInitiated(SearchInitiated event) async* { | ||
if (event.query.isEmpty) { | ||
yield SearchState.initial(); | ||
} else { | ||
yield SearchState.loading(); | ||
|
||
try { | ||
final searchResult = await _youtubeRepository.searchVideos(event.query); | ||
yield SearchState.success(searchResult); | ||
} on YoutubeSearchError catch (e) { | ||
yield SearchState.failure(e.message); | ||
} on NoSearchResultsException catch (e) { | ||
yield SearchState.failure(e.message); | ||
} | ||
} | ||
} | ||
|
||
Stream<SearchState> mapFetchNextResultPage() async* { | ||
try { | ||
final nextPageResults = await _youtubeRepository.fetchNextResultPage(); | ||
yield SearchState.success(currentState.searchResults + nextPageResults); | ||
} on NoNextPageTokenException catch (_) { | ||
yield currentState.rebuild((b) => b..hasReachedEndOfResults = true); | ||
} on SearchNotInitiatedException catch (e) { | ||
yield SearchState.failure(e.message); | ||
} on YoutubeSearchError catch (e) { | ||
yield SearchState.failure(e.message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
library search_event; | ||
|
||
import 'package:built_value/built_value.dart'; | ||
|
||
part 'search_event.g.dart'; | ||
|
||
abstract class SearchEvent {} | ||
|
||
abstract class SearchInitiated extends SearchEvent | ||
implements Built<SearchInitiated, SearchInitiatedBuilder> { | ||
String get query; | ||
|
||
SearchInitiated._(); | ||
|
||
factory SearchInitiated([updates(SearchInitiatedBuilder b)]) = | ||
_$SearchInitiated; | ||
} | ||
|
||
class FetchNextResultPage extends SearchEvent {} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
library search_state; | ||
|
||
import 'dart:convert'; | ||
|
||
import 'package:built_collection/built_collection.dart'; | ||
import 'package:built_value/built_value.dart'; | ||
import 'package:built_value/serializer.dart'; | ||
import 'package:youtube_search_tutorial/data/model/search/model_search.dart'; | ||
|
||
part 'search_state.g.dart'; | ||
|
||
abstract class SearchState implements Built<SearchState, SearchStateBuilder> { | ||
bool get isLoading; | ||
BuiltList<SearchItem> get searchResults; | ||
String get error; | ||
bool get hasReachedEndOfResults; | ||
|
||
bool get isInitial => !isLoading && searchResults.isEmpty && error == ''; | ||
bool get isSuccessful => | ||
!isLoading && searchResults.isNotEmpty && error == ''; | ||
|
||
SearchState._(); | ||
|
||
factory SearchState([updates(SearchStateBuilder b)]) = _$SearchState; | ||
|
||
factory SearchState.initial() { | ||
return SearchState((b) => b | ||
..isLoading = false | ||
..searchResults.replace(BuiltList<SearchItem>()) | ||
..error = '' | ||
..hasReachedEndOfResults = false); | ||
} | ||
|
||
factory SearchState.loading() { | ||
return SearchState((b) => b | ||
..isLoading = true | ||
..searchResults.replace(BuiltList<SearchItem>()) | ||
..error = '' | ||
..hasReachedEndOfResults = false); | ||
} | ||
|
||
factory SearchState.failure(String error) { | ||
return SearchState((b) => b | ||
..isLoading = false | ||
..searchResults.replace(BuiltList<SearchItem>()) | ||
..error = error | ||
..hasReachedEndOfResults = false); | ||
} | ||
|
||
factory SearchState.success(BuiltList<SearchItem> resultList) { | ||
return SearchState((b) => b | ||
..isLoading = false | ||
..searchResults.replace(resultList) | ||
..error = '' | ||
..hasReachedEndOfResults = false); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.