1
1
import 'package:bloc/bloc.dart' ;
2
2
import 'package:equatable/equatable.dart' ;
3
3
import 'package:ht_headlines_repository/ht_headlines_repository.dart' ;
4
- import 'package:stream_transform/stream_transform.dart' ;
5
4
6
5
part 'headlines_search_event.dart' ;
7
6
part 'headlines_search_state.dart' ;
@@ -10,88 +9,71 @@ class HeadlinesSearchBloc
10
9
extends Bloc <HeadlinesSearchEvent , HeadlinesSearchState > {
11
10
HeadlinesSearchBloc ({required HtHeadlinesRepository headlinesRepository})
12
11
: _headlinesRepository = headlinesRepository,
13
- super (HeadlinesSearchInitial ()) {
14
- on < HeadlinesSearchTermChanged > (
15
- _onSearchTermChanged,
16
- transformer: (events, mapper) => events
17
- .debounce (const Duration (milliseconds: 300 ))
18
- .asyncExpand (mapper),
19
- );
20
- on < HeadlinesSearchRequested > (_onSearchRequested);
21
- on < HeadlinesSearchLoadMore > (_onSearchLoadMore);
12
+ super (HeadlinesSearchLoading ()) {
13
+ on < HeadlinesSearchFetchRequested > (_onSearchFetchRequested);
22
14
}
23
15
24
16
final HtHeadlinesRepository _headlinesRepository;
25
- String _searchTerm = '' ;
26
17
static const _limit = 10 ;
27
18
28
- Future <void > _onSearchTermChanged (
29
- HeadlinesSearchTermChanged event,
19
+ Future <void > _onSearchFetchRequested (
20
+ HeadlinesSearchFetchRequested event,
30
21
Emitter <HeadlinesSearchState > emit,
31
22
) async {
32
- _searchTerm = event.searchTerm;
33
- if (_searchTerm.isEmpty) {
34
- emit (HeadlinesSearchInitial ());
35
- }
36
- }
37
-
38
- Future <void > _onSearchRequested (
39
- HeadlinesSearchRequested event,
40
- Emitter <HeadlinesSearchState > emit,
41
- ) async {
42
- if (_searchTerm.isEmpty) {
23
+ if (event.searchTerm.isEmpty) {
24
+ emit (const HeadlinesSearchSuccess (
25
+ headlines: [], hasMore: false , lastSearchTerm: '' ,),);
43
26
return ;
44
27
}
45
- emit (HeadlinesSearchLoading ());
46
- try {
47
- final response = await _headlinesRepository.searchHeadlines (
48
- query: _searchTerm,
49
- limit: _limit,
50
- );
51
- emit (
52
- HeadlinesSearchLoaded (
53
- headlines: response.items,
54
- hasReachedMax: ! response.hasMore,
55
- cursor: response.cursor,
56
- ),
57
- );
58
- } on HeadlinesSearchException catch (e) {
59
- emit (HeadlinesSearchError (message: e.message));
60
- } catch (e) {
61
- emit (HeadlinesSearchError (message: e.toString ()));
62
- }
63
- }
64
-
65
- Future <void > _onSearchLoadMore (
66
- HeadlinesSearchLoadMore event,
67
- Emitter <HeadlinesSearchState > emit,
68
- ) async {
69
- if (state is ! HeadlinesSearchLoaded ) return ;
70
-
71
- final currentState = state as HeadlinesSearchLoaded ;
72
28
73
- if (currentState.hasReachedMax) return ;
29
+ if (state is HeadlinesSearchSuccess &&
30
+ event.searchTerm == state.lastSearchTerm) {
31
+ final currentState = state as HeadlinesSearchSuccess ;
32
+ if (! currentState.hasMore) return ;
74
33
75
- try {
76
- final response = await _headlinesRepository.searchHeadlines (
77
- query: _searchTerm,
78
- limit: _limit,
79
- startAfterId: currentState.cursor,
80
- );
81
- emit (
82
- response.items.isEmpty
83
- ? currentState.copyWith (hasReachedMax: true )
84
- : currentState.copyWith (
85
- headlines: List .of (currentState.headlines)
86
- ..addAll (response.items),
87
- hasReachedMax: ! response.hasMore,
88
- cursor: response.cursor,
89
- ),
90
- );
91
- } on HeadlinesSearchException catch (e) {
92
- emit (HeadlinesSearchError (message: e.message));
93
- } catch (e) {
94
- emit (HeadlinesSearchError (message: e.toString ()));
34
+ try {
35
+ final response = await _headlinesRepository.searchHeadlines (
36
+ query: event.searchTerm,
37
+ limit: _limit,
38
+ startAfterId: currentState.cursor,
39
+ );
40
+ emit (
41
+ response.items.isEmpty
42
+ ? currentState.copyWith (hasMore: false )
43
+ : currentState.copyWith (
44
+ headlines: List .of (currentState.headlines)
45
+ ..addAll (response.items),
46
+ hasMore: response.hasMore,
47
+ cursor: response.cursor,
48
+ ),
49
+ );
50
+ } catch (e) {
51
+ emit (currentState.copyWith (errorMessage: e.toString ()));
52
+ }
53
+ } else {
54
+ try {
55
+ final response = await _headlinesRepository.searchHeadlines (
56
+ query: event.searchTerm,
57
+ limit: _limit,
58
+ );
59
+ emit (
60
+ HeadlinesSearchSuccess (
61
+ headlines: response.items,
62
+ hasMore: response.hasMore,
63
+ cursor: response.cursor,
64
+ lastSearchTerm: event.searchTerm,
65
+ ),
66
+ );
67
+ } catch (e) {
68
+ emit (
69
+ HeadlinesSearchSuccess (
70
+ headlines: const [],
71
+ hasMore: false ,
72
+ errorMessage: e.toString (),
73
+ lastSearchTerm: event.searchTerm,
74
+ ),
75
+ );
76
+ }
95
77
}
96
78
}
97
79
}
0 commit comments