@@ -16,27 +16,26 @@ class FilteredTodosBloc extends Bloc<FilteredTodosEvent, FilteredTodosState> {
16
16
FilteredTodosBloc ({@required this .todosBloc}) {
17
17
todosSubscription = todosBloc.state.listen ((state) {
18
18
if (state is TodosLoaded ) {
19
- dispatch (TodosUpdated ((todosBloc.currentState as TodosLoaded ).todos));
19
+ dispatch (UpdateTodos ((todosBloc.currentState as TodosLoaded ).todos));
20
20
}
21
21
});
22
22
}
23
23
24
24
@override
25
- FilteredTodosState get initialState => todosBloc.currentState is TodosLoaded
26
- ? FilteredTodosState (
27
- (todosBloc.currentState as TodosLoaded ).todos,
28
- VisibilityFilter .all,
29
- )
30
- : FilteredTodosState ([], VisibilityFilter .all);
25
+ FilteredTodosState get initialState {
26
+ return todosBloc.currentState is TodosLoaded
27
+ ? FilteredTodosLoaded (
28
+ (todosBloc.currentState as TodosLoaded ).todos,
29
+ VisibilityFilter .all,
30
+ )
31
+ : FilteredTodosLoading ();
32
+ }
31
33
32
34
@override
33
- Stream <FilteredTodosState > mapEventToState (
34
- FilteredTodosState currentState,
35
- FilteredTodosEvent event,
36
- ) async * {
35
+ Stream <FilteredTodosState > mapEventToState (FilteredTodosEvent event) async * {
37
36
if (event is UpdateFilter ) {
38
37
yield * _mapUpdateFilterToState (currentState, event);
39
- } else if (event is TodosUpdated ) {
38
+ } else if (event is UpdateTodos ) {
40
39
yield * _mapTodosUpdatedToState (currentState, event);
41
40
}
42
41
}
@@ -46,34 +45,34 @@ class FilteredTodosBloc extends Bloc<FilteredTodosEvent, FilteredTodosState> {
46
45
UpdateFilter event,
47
46
) async * {
48
47
if (todosBloc.currentState is TodosLoaded ) {
49
- yield FilteredTodosState (
48
+ yield FilteredTodosLoaded (
50
49
_mapTodosToFilteredTodos (
51
- (todosBloc.currentState as TodosLoaded ).todos, event.newFilter),
52
- event.newFilter,
50
+ (todosBloc.currentState as TodosLoaded ).todos,
51
+ event.filter,
52
+ ),
53
+ event.filter,
53
54
);
54
55
}
55
56
}
56
57
57
58
Stream <FilteredTodosState > _mapTodosUpdatedToState (
58
59
FilteredTodosState currentState,
59
- TodosUpdated event,
60
+ UpdateTodos event,
60
61
) async * {
61
- yield FilteredTodosState (
62
+ final visibilityFilter = currentState is FilteredTodosLoaded
63
+ ? currentState.activeFilter
64
+ : VisibilityFilter .all;
65
+ yield FilteredTodosLoaded (
62
66
_mapTodosToFilteredTodos (
63
67
(todosBloc.currentState as TodosLoaded ).todos,
64
- currentState.activeFilter ,
68
+ visibilityFilter ,
65
69
),
66
- currentState.activeFilter ,
70
+ visibilityFilter ,
67
71
);
68
72
}
69
73
70
- @override
71
- void dispose () {
72
- todosSubscription.cancel ();
73
- super .dispose ();
74
- }
75
-
76
- _mapTodosToFilteredTodos (List <Todo > todos, VisibilityFilter filter) {
74
+ List <Todo > _mapTodosToFilteredTodos (
75
+ List <Todo > todos, VisibilityFilter filter) {
77
76
return todos.where ((todo) {
78
77
if (filter == VisibilityFilter .all) {
79
78
return true ;
@@ -84,4 +83,10 @@ class FilteredTodosBloc extends Bloc<FilteredTodosEvent, FilteredTodosState> {
84
83
}
85
84
}).toList ();
86
85
}
86
+
87
+ @override
88
+ void dispose () {
89
+ todosSubscription.cancel ();
90
+ super .dispose ();
91
+ }
87
92
}
0 commit comments