|
1 | 1 | import 'package:bloc/bloc.dart';
|
2 | 2 | import 'package:equatable/equatable.dart';
|
3 | 3 | import 'package:ht_data_repository/ht_data_repository.dart';
|
4 |
| -import 'package:ht_dashboard/shared/constants/pagination_constants.dart'; |
5 | 4 | import 'package:ht_shared/ht_shared.dart';
|
6 | 5 |
|
7 | 6 | part 'content_management_event.dart';
|
@@ -31,16 +30,16 @@ class ContentManagementBloc
|
31 | 30 | super(const ContentManagementState()) {
|
32 | 31 | on<ContentManagementTabChanged>(_onContentManagementTabChanged);
|
33 | 32 | on<LoadHeadlinesRequested>(_onLoadHeadlinesRequested);
|
34 |
| - on<CreateHeadlineRequested>(_onCreateHeadlineRequested); |
35 |
| - on<UpdateHeadlineRequested>(_onUpdateHeadlineRequested); |
| 33 | + on<HeadlineAdded>(_onHeadlineAdded); |
| 34 | + on<HeadlineUpdated>(_onHeadlineUpdated); |
36 | 35 | on<DeleteHeadlineRequested>(_onDeleteHeadlineRequested);
|
37 | 36 | on<LoadCategoriesRequested>(_onLoadCategoriesRequested);
|
38 |
| - on<CreateCategoryRequested>(_onCreateCategoryRequested); |
39 |
| - on<UpdateCategoryRequested>(_onUpdateCategoryRequested); |
| 37 | + on<CategoryAdded>(_onCategoryAdded); |
| 38 | + on<CategoryUpdated>(_onCategoryUpdated); |
40 | 39 | on<DeleteCategoryRequested>(_onDeleteCategoryRequested);
|
41 | 40 | on<LoadSourcesRequested>(_onLoadSourcesRequested);
|
42 |
| - on<CreateSourceRequested>(_onCreateSourceRequested); |
43 |
| - on<UpdateSourceRequested>(_onUpdateSourceRequested); |
| 41 | + on<SourceAdded>(_onSourceAdded); |
| 42 | + on<SourceUpdated>(_onSourceUpdated); |
44 | 43 | on<DeleteSourceRequested>(_onOnDeleteSourceRequested);
|
45 | 44 | }
|
46 | 45 |
|
@@ -93,73 +92,41 @@ class ContentManagementBloc
|
93 | 92 | }
|
94 | 93 | }
|
95 | 94 |
|
96 |
| - Future<void> _onCreateHeadlineRequested( |
97 |
| - CreateHeadlineRequested event, |
| 95 | + void _onHeadlineAdded( |
| 96 | + HeadlineAdded event, |
98 | 97 | Emitter<ContentManagementState> emit,
|
99 |
| - ) async { |
100 |
| - emit(state.copyWith(headlinesStatus: ContentManagementStatus.loading)); |
101 |
| - try { |
102 |
| - await _headlinesRepository.create(item: event.headline); |
103 |
| - // Reload headlines after creation |
104 |
| - add( |
105 |
| - const LoadHeadlinesRequested(limit: kDefaultRowsPerPage), |
106 |
| - ); |
107 |
| - } on HtHttpException catch (e) { |
108 |
| - emit( |
109 |
| - state.copyWith( |
110 |
| - headlinesStatus: ContentManagementStatus.failure, |
111 |
| - errorMessage: e.message, |
112 |
| - ), |
113 |
| - ); |
114 |
| - } catch (e) { |
115 |
| - emit( |
116 |
| - state.copyWith( |
117 |
| - headlinesStatus: ContentManagementStatus.failure, |
118 |
| - errorMessage: e.toString(), |
119 |
| - ), |
120 |
| - ); |
121 |
| - } |
| 98 | + ) { |
| 99 | + final updatedHeadlines = [event.headline, ...state.headlines]; |
| 100 | + emit( |
| 101 | + state.copyWith( |
| 102 | + headlines: updatedHeadlines, |
| 103 | + headlinesStatus: ContentManagementStatus.success, |
| 104 | + ), |
| 105 | + ); |
122 | 106 | }
|
123 | 107 |
|
124 |
| - Future<void> _onUpdateHeadlineRequested( |
125 |
| - UpdateHeadlineRequested event, |
| 108 | + void _onHeadlineUpdated( |
| 109 | + HeadlineUpdated event, |
126 | 110 | Emitter<ContentManagementState> emit,
|
127 |
| - ) async { |
128 |
| - emit(state.copyWith(headlinesStatus: ContentManagementStatus.loading)); |
129 |
| - try { |
130 |
| - await _headlinesRepository.update(id: event.id, item: event.headline); |
131 |
| - // Reload headlines after update |
132 |
| - add( |
133 |
| - const LoadHeadlinesRequested(limit: kDefaultRowsPerPage), |
134 |
| - ); |
135 |
| - } on HtHttpException catch (e) { |
136 |
| - emit( |
137 |
| - state.copyWith( |
138 |
| - headlinesStatus: ContentManagementStatus.failure, |
139 |
| - errorMessage: e.message, |
140 |
| - ), |
141 |
| - ); |
142 |
| - } catch (e) { |
143 |
| - emit( |
144 |
| - state.copyWith( |
145 |
| - headlinesStatus: ContentManagementStatus.failure, |
146 |
| - errorMessage: e.toString(), |
147 |
| - ), |
148 |
| - ); |
| 111 | + ) { |
| 112 | + final updatedHeadlines = List<Headline>.from(state.headlines); |
| 113 | + final index = updatedHeadlines.indexWhere((h) => h.id == event.headline.id); |
| 114 | + if (index != -1) { |
| 115 | + updatedHeadlines[index] = event.headline; |
| 116 | + emit(state.copyWith(headlines: updatedHeadlines)); |
149 | 117 | }
|
150 | 118 | }
|
151 | 119 |
|
152 | 120 | Future<void> _onDeleteHeadlineRequested(
|
153 | 121 | DeleteHeadlineRequested event,
|
154 | 122 | Emitter<ContentManagementState> emit,
|
155 | 123 | ) async {
|
156 |
| - emit(state.copyWith(headlinesStatus: ContentManagementStatus.loading)); |
157 | 124 | try {
|
158 | 125 | await _headlinesRepository.delete(id: event.id);
|
159 |
| - // Reload headlines after deletion |
160 |
| - add( |
161 |
| - const LoadHeadlinesRequested(limit: kDefaultRowsPerPage), |
162 |
| - ); |
| 126 | + final updatedHeadlines = state.headlines |
| 127 | + .where((h) => h.id != event.id) |
| 128 | + .toList(); |
| 129 | + emit(state.copyWith(headlines: updatedHeadlines)); |
163 | 130 | } on HtHttpException catch (e) {
|
164 | 131 | emit(
|
165 | 132 | state.copyWith(
|
@@ -215,73 +182,43 @@ class ContentManagementBloc
|
215 | 182 | }
|
216 | 183 | }
|
217 | 184 |
|
218 |
| - Future<void> _onCreateCategoryRequested( |
219 |
| - CreateCategoryRequested event, |
| 185 | + void _onCategoryAdded( |
| 186 | + CategoryAdded event, |
220 | 187 | Emitter<ContentManagementState> emit,
|
221 |
| - ) async { |
222 |
| - emit(state.copyWith(categoriesStatus: ContentManagementStatus.loading)); |
223 |
| - try { |
224 |
| - await _categoriesRepository.create(item: event.category); |
225 |
| - // Reload categories after creation |
226 |
| - add( |
227 |
| - const LoadCategoriesRequested(limit: kDefaultRowsPerPage), |
228 |
| - ); |
229 |
| - } on HtHttpException catch (e) { |
230 |
| - emit( |
231 |
| - state.copyWith( |
232 |
| - categoriesStatus: ContentManagementStatus.failure, |
233 |
| - errorMessage: e.message, |
234 |
| - ), |
235 |
| - ); |
236 |
| - } catch (e) { |
237 |
| - emit( |
238 |
| - state.copyWith( |
239 |
| - categoriesStatus: ContentManagementStatus.failure, |
240 |
| - errorMessage: e.toString(), |
241 |
| - ), |
242 |
| - ); |
243 |
| - } |
| 188 | + ) { |
| 189 | + final updatedCategories = [event.category, ...state.categories]; |
| 190 | + emit( |
| 191 | + state.copyWith( |
| 192 | + categories: updatedCategories, |
| 193 | + categoriesStatus: ContentManagementStatus.success, |
| 194 | + ), |
| 195 | + ); |
244 | 196 | }
|
245 | 197 |
|
246 |
| - Future<void> _onUpdateCategoryRequested( |
247 |
| - UpdateCategoryRequested event, |
| 198 | + void _onCategoryUpdated( |
| 199 | + CategoryUpdated event, |
248 | 200 | Emitter<ContentManagementState> emit,
|
249 |
| - ) async { |
250 |
| - emit(state.copyWith(categoriesStatus: ContentManagementStatus.loading)); |
251 |
| - try { |
252 |
| - await _categoriesRepository.update(id: event.id, item: event.category); |
253 |
| - // Reload categories after update |
254 |
| - add( |
255 |
| - const LoadCategoriesRequested(limit: kDefaultRowsPerPage), |
256 |
| - ); |
257 |
| - } on HtHttpException catch (e) { |
258 |
| - emit( |
259 |
| - state.copyWith( |
260 |
| - categoriesStatus: ContentManagementStatus.failure, |
261 |
| - errorMessage: e.message, |
262 |
| - ), |
263 |
| - ); |
264 |
| - } catch (e) { |
265 |
| - emit( |
266 |
| - state.copyWith( |
267 |
| - categoriesStatus: ContentManagementStatus.failure, |
268 |
| - errorMessage: e.toString(), |
269 |
| - ), |
270 |
| - ); |
| 201 | + ) { |
| 202 | + final updatedCategories = List<Category>.from(state.categories); |
| 203 | + final index = updatedCategories.indexWhere( |
| 204 | + (c) => c.id == event.category.id, |
| 205 | + ); |
| 206 | + if (index != -1) { |
| 207 | + updatedCategories[index] = event.category; |
| 208 | + emit(state.copyWith(categories: updatedCategories)); |
271 | 209 | }
|
272 | 210 | }
|
273 | 211 |
|
274 | 212 | Future<void> _onDeleteCategoryRequested(
|
275 | 213 | DeleteCategoryRequested event,
|
276 | 214 | Emitter<ContentManagementState> emit,
|
277 | 215 | ) async {
|
278 |
| - emit(state.copyWith(categoriesStatus: ContentManagementStatus.loading)); |
279 | 216 | try {
|
280 | 217 | await _categoriesRepository.delete(id: event.id);
|
281 |
| - // Reload categories after deletion |
282 |
| - add( |
283 |
| - const LoadCategoriesRequested(limit: kDefaultRowsPerPage), |
284 |
| - ); |
| 218 | + final updatedCategories = state.categories |
| 219 | + .where((c) => c.id != event.id) |
| 220 | + .toList(); |
| 221 | + emit(state.copyWith(categories: updatedCategories)); |
285 | 222 | } on HtHttpException catch (e) {
|
286 | 223 | emit(
|
287 | 224 | state.copyWith(
|
@@ -337,73 +274,38 @@ class ContentManagementBloc
|
337 | 274 | }
|
338 | 275 | }
|
339 | 276 |
|
340 |
| - Future<void> _onCreateSourceRequested( |
341 |
| - CreateSourceRequested event, |
342 |
| - Emitter<ContentManagementState> emit, |
343 |
| - ) async { |
344 |
| - emit(state.copyWith(sourcesStatus: ContentManagementStatus.loading)); |
345 |
| - try { |
346 |
| - await _sourcesRepository.create(item: event.source); |
347 |
| - // Reload sources after creation |
348 |
| - add( |
349 |
| - const LoadSourcesRequested(limit: kDefaultRowsPerPage), |
350 |
| - ); |
351 |
| - } on HtHttpException catch (e) { |
352 |
| - emit( |
353 |
| - state.copyWith( |
354 |
| - sourcesStatus: ContentManagementStatus.failure, |
355 |
| - errorMessage: e.message, |
356 |
| - ), |
357 |
| - ); |
358 |
| - } catch (e) { |
359 |
| - emit( |
360 |
| - state.copyWith( |
361 |
| - sourcesStatus: ContentManagementStatus.failure, |
362 |
| - errorMessage: e.toString(), |
363 |
| - ), |
364 |
| - ); |
365 |
| - } |
| 277 | + void _onSourceAdded(SourceAdded event, Emitter<ContentManagementState> emit) { |
| 278 | + final updatedSources = [event.source, ...state.sources]; |
| 279 | + emit( |
| 280 | + state.copyWith( |
| 281 | + sources: updatedSources, |
| 282 | + sourcesStatus: ContentManagementStatus.success, |
| 283 | + ), |
| 284 | + ); |
366 | 285 | }
|
367 | 286 |
|
368 |
| - Future<void> _onUpdateSourceRequested( |
369 |
| - UpdateSourceRequested event, |
| 287 | + void _onSourceUpdated( |
| 288 | + SourceUpdated event, |
370 | 289 | Emitter<ContentManagementState> emit,
|
371 |
| - ) async { |
372 |
| - emit(state.copyWith(sourcesStatus: ContentManagementStatus.loading)); |
373 |
| - try { |
374 |
| - await _sourcesRepository.update(id: event.id, item: event.source); |
375 |
| - // Reload sources after update |
376 |
| - add( |
377 |
| - const LoadSourcesRequested(limit: kDefaultRowsPerPage), |
378 |
| - ); |
379 |
| - } on HtHttpException catch (e) { |
380 |
| - emit( |
381 |
| - state.copyWith( |
382 |
| - sourcesStatus: ContentManagementStatus.failure, |
383 |
| - errorMessage: e.message, |
384 |
| - ), |
385 |
| - ); |
386 |
| - } catch (e) { |
387 |
| - emit( |
388 |
| - state.copyWith( |
389 |
| - sourcesStatus: ContentManagementStatus.failure, |
390 |
| - errorMessage: e.toString(), |
391 |
| - ), |
392 |
| - ); |
| 290 | + ) { |
| 291 | + final updatedSources = List<Source>.from(state.sources); |
| 292 | + final index = updatedSources.indexWhere((s) => s.id == event.source.id); |
| 293 | + if (index != -1) { |
| 294 | + updatedSources[index] = event.source; |
| 295 | + emit(state.copyWith(sources: updatedSources)); |
393 | 296 | }
|
394 | 297 | }
|
395 | 298 |
|
396 | 299 | Future<void> _onOnDeleteSourceRequested(
|
397 | 300 | DeleteSourceRequested event,
|
398 | 301 | Emitter<ContentManagementState> emit,
|
399 | 302 | ) async {
|
400 |
| - emit(state.copyWith(sourcesStatus: ContentManagementStatus.loading)); |
401 | 303 | try {
|
402 | 304 | await _sourcesRepository.delete(id: event.id);
|
403 |
| - // Reload sources after deletion |
404 |
| - add( |
405 |
| - const LoadSourcesRequested(limit: kDefaultRowsPerPage), |
406 |
| - ); |
| 305 | + final updatedSources = state.sources |
| 306 | + .where((s) => s.id != event.id) |
| 307 | + .toList(); |
| 308 | + emit(state.copyWith(sources: updatedSources)); |
407 | 309 | } on HtHttpException catch (e) {
|
408 | 310 | emit(
|
409 | 311 | state.copyWith(
|
|
0 commit comments