@@ -24,10 +24,15 @@ class ContentManagementBloc
24
24
required DataRepository <Headline > headlinesRepository,
25
25
required DataRepository <Topic > topicsRepository,
26
26
required DataRepository <Source > sourcesRepository,
27
- }) : _headlinesRepository = headlinesRepository,
28
- _topicsRepository = topicsRepository,
29
- _sourcesRepository = sourcesRepository,
30
- super (const ContentManagementState ()) {
27
+ required DataRepository <Country > countriesRepository,
28
+ required DataRepository <Language > languagesRepository,
29
+ }) : _headlinesRepository = headlinesRepository,
30
+ _topicsRepository = topicsRepository,
31
+ _sourcesRepository = sourcesRepository,
32
+ _countriesRepository = countriesRepository,
33
+ _languagesRepository = languagesRepository,
34
+ super (const ContentManagementState ()) {
35
+ on < SharedDataRequested > (_onSharedDataRequested);
31
36
on < ContentManagementTabChanged > (_onContentManagementTabChanged);
32
37
on < LoadHeadlinesRequested > (_onLoadHeadlinesRequested);
33
38
on < HeadlineUpdated > (_onHeadlineUpdated);
@@ -43,6 +48,94 @@ class ContentManagementBloc
43
48
final DataRepository <Headline > _headlinesRepository;
44
49
final DataRepository <Topic > _topicsRepository;
45
50
final DataRepository <Source > _sourcesRepository;
51
+ final DataRepository <Country > _countriesRepository;
52
+ final DataRepository <Language > _languagesRepository;
53
+
54
+ Future <void > _onSharedDataRequested (
55
+ SharedDataRequested event,
56
+ Emitter <ContentManagementState > emit,
57
+ ) async {
58
+ // Helper function to fetch all items of a given type.
59
+ Future <List <T >> fetchAll <T >({
60
+ required DataRepository <T > repository,
61
+ required List <SortOption > sort,
62
+ }) async {
63
+ final allItems = < T > [];
64
+ String ? cursor;
65
+ bool hasMore;
66
+
67
+ do {
68
+ final response = await repository.readAll (
69
+ sort: sort,
70
+ pagination: PaginationOptions (cursor: cursor),
71
+ filter: {'status' : ContentStatus .active.name},
72
+ );
73
+ allItems.addAll (response.items);
74
+ cursor = response.cursor;
75
+ hasMore = response.hasMore;
76
+ } while (hasMore);
77
+
78
+ return allItems;
79
+ }
80
+
81
+ // Check if data is already loaded or is currently loading to prevent
82
+ // redundant fetches.
83
+ if (state.allCountriesStatus == ContentManagementStatus .success &&
84
+ state.allLanguagesStatus == ContentManagementStatus .success) {
85
+ return ;
86
+ }
87
+
88
+ // Set loading status for both lists.
89
+ emit (
90
+ state.copyWith (
91
+ allCountriesStatus: ContentManagementStatus .loading,
92
+ allLanguagesStatus: ContentManagementStatus .loading,
93
+ ),
94
+ );
95
+
96
+ try {
97
+ // Fetch both lists in parallel.
98
+ final results = await Future .wait ([
99
+ fetchAll <Country >(
100
+ repository: _countriesRepository,
101
+ sort: [const SortOption ('name' , SortOrder .asc)],
102
+ ),
103
+ fetchAll <Language >(
104
+ repository: _languagesRepository,
105
+ sort: [const SortOption ('name' , SortOrder .asc)],
106
+ ),
107
+ ]);
108
+
109
+ final countries = results[0 ] as List <Country >;
110
+ final languages = results[1 ] as List <Language >;
111
+
112
+ // Update the state with the complete lists.
113
+ emit (
114
+ state.copyWith (
115
+ allCountries: countries,
116
+ allCountriesStatus: ContentManagementStatus .success,
117
+ allLanguages: languages,
118
+ allLanguagesStatus: ContentManagementStatus .success,
119
+ ),
120
+ );
121
+ } on HttpException catch (e) {
122
+ emit (
123
+ state.copyWith (
124
+ allCountriesStatus: ContentManagementStatus .failure,
125
+ allLanguagesStatus: ContentManagementStatus .failure,
126
+ exception: e,
127
+ ),
128
+ );
129
+ } catch (e) {
130
+ emit (
131
+ state.copyWith (
132
+ allCountriesStatus: ContentManagementStatus .failure,
133
+ allLanguagesStatus: ContentManagementStatus .failure,
134
+ exception: UnknownException ('An unexpected error occurred: $e ' ),
135
+ ),
136
+ );
137
+ }
138
+ }
46
139
47
140
void _onContentManagementTabChanged (
48
141
ContentManagementTabChanged event,
0 commit comments