@@ -31,16 +31,16 @@ class ContentManagementBloc
3131 super (const ContentManagementState ()) {
3232 on < ContentManagementTabChanged > (_onContentManagementTabChanged);
3333 on < LoadHeadlinesRequested > (_onLoadHeadlinesRequested);
34- on < CreateHeadlineRequested > (_onCreateHeadlineRequested );
35- on < UpdateHeadlineRequested > (_onUpdateHeadlineRequested );
34+ on < HeadlineAdded > (_onHeadlineAdded );
35+ on < HeadlineUpdated > (_onHeadlineUpdated );
3636 on < DeleteHeadlineRequested > (_onDeleteHeadlineRequested);
3737 on < LoadCategoriesRequested > (_onLoadCategoriesRequested);
38- on < CreateCategoryRequested > (_onCreateCategoryRequested );
39- on < UpdateCategoryRequested > (_onUpdateCategoryRequested );
38+ on < CategoryAdded > (_onCategoryAdded );
39+ on < CategoryUpdated > (_onCategoryUpdated );
4040 on < DeleteCategoryRequested > (_onDeleteCategoryRequested);
4141 on < LoadSourcesRequested > (_onLoadSourcesRequested);
42- on < CreateSourceRequested > (_onCreateSourceRequested );
43- on < UpdateSourceRequested > (_onUpdateSourceRequested );
42+ on < SourceAdded > (_onSourceAdded );
43+ on < SourceUpdated > (_onSourceUpdated );
4444 on < DeleteSourceRequested > (_onOnDeleteSourceRequested);
4545 }
4646
@@ -93,73 +93,37 @@ class ContentManagementBloc
9393 }
9494 }
9595
96- Future <void > _onCreateHeadlineRequested (
97- CreateHeadlineRequested event,
98- 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- }
96+ void _onHeadlineAdded (HeadlineAdded event, Emitter <ContentManagementState > emit) {
97+ final updatedHeadlines = [event.headline, ...state.headlines];
98+ emit (
99+ state.copyWith (
100+ headlines: updatedHeadlines,
101+ headlinesStatus: ContentManagementStatus .success,
102+ ),
103+ );
122104 }
123105
124- Future < void > _onUpdateHeadlineRequested (
125- UpdateHeadlineRequested event,
106+ void _onHeadlineUpdated (
107+ HeadlineUpdated event,
126108 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- );
109+ ) {
110+ final updatedHeadlines = List <Headline >.from (state.headlines);
111+ final index = updatedHeadlines.indexWhere ((h) => h.id == event.headline.id);
112+ if (index != - 1 ) {
113+ updatedHeadlines[index] = event.headline;
114+ emit (state.copyWith (headlines: updatedHeadlines));
149115 }
150116 }
151117
152118 Future <void > _onDeleteHeadlineRequested (
153119 DeleteHeadlineRequested event,
154120 Emitter <ContentManagementState > emit,
155121 ) async {
156- emit (state.copyWith (headlinesStatus: ContentManagementStatus .loading));
157122 try {
158123 await _headlinesRepository.delete (id: event.id);
159- // Reload headlines after deletion
160- add (
161- const LoadHeadlinesRequested (limit: kDefaultRowsPerPage),
162- );
124+ final updatedHeadlines =
125+ state.headlines.where ((h) => h.id != event.id).toList ();
126+ emit (state.copyWith (headlines: updatedHeadlines));
163127 } on HtHttpException catch (e) {
164128 emit (
165129 state.copyWith (
@@ -215,73 +179,40 @@ class ContentManagementBloc
215179 }
216180 }
217181
218- Future < void > _onCreateCategoryRequested (
219- CreateCategoryRequested event,
182+ void _onCategoryAdded (
183+ CategoryAdded event,
220184 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- }
185+ ) {
186+ final updatedCategories = [event.category, ...state.categories];
187+ emit (
188+ state.copyWith (
189+ categories: updatedCategories,
190+ categoriesStatus: ContentManagementStatus .success,
191+ ),
192+ );
244193 }
245194
246- Future < void > _onUpdateCategoryRequested (
247- UpdateCategoryRequested event,
195+ void _onCategoryUpdated (
196+ CategoryUpdated event,
248197 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- );
198+ ) {
199+ final updatedCategories = List <Category >.from (state.categories);
200+ final index = updatedCategories.indexWhere ((c) => c.id == event.category.id);
201+ if (index != - 1 ) {
202+ updatedCategories[index] = event.category;
203+ emit (state.copyWith (categories: updatedCategories));
271204 }
272205 }
273206
274207 Future <void > _onDeleteCategoryRequested (
275208 DeleteCategoryRequested event,
276209 Emitter <ContentManagementState > emit,
277210 ) async {
278- emit (state.copyWith (categoriesStatus: ContentManagementStatus .loading));
279211 try {
280212 await _categoriesRepository.delete (id: event.id);
281- // Reload categories after deletion
282- add (
283- const LoadCategoriesRequested (limit: kDefaultRowsPerPage),
284- );
213+ final updatedCategories =
214+ state.categories.where ((c) => c.id != event.id).toList ();
215+ emit (state.copyWith (categories: updatedCategories));
285216 } on HtHttpException catch (e) {
286217 emit (
287218 state.copyWith (
@@ -337,73 +268,37 @@ class ContentManagementBloc
337268 }
338269 }
339270
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- }
271+ void _onSourceAdded (SourceAdded event, Emitter <ContentManagementState > emit) {
272+ final updatedSources = [event.source, ...state.sources];
273+ emit (
274+ state.copyWith (
275+ sources: updatedSources,
276+ sourcesStatus: ContentManagementStatus .success,
277+ ),
278+ );
366279 }
367280
368- Future < void > _onUpdateSourceRequested (
369- UpdateSourceRequested event,
281+ void _onSourceUpdated (
282+ SourceUpdated event,
370283 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- );
284+ ) {
285+ final updatedSources = List <Source >.from (state.sources);
286+ final index = updatedSources.indexWhere ((s) => s.id == event.source.id);
287+ if (index != - 1 ) {
288+ updatedSources[index] = event.source;
289+ emit (state.copyWith (sources: updatedSources));
393290 }
394291 }
395292
396293 Future <void > _onOnDeleteSourceRequested (
397294 DeleteSourceRequested event,
398295 Emitter <ContentManagementState > emit,
399296 ) async {
400- emit (state.copyWith (sourcesStatus: ContentManagementStatus .loading));
401297 try {
402298 await _sourcesRepository.delete (id: event.id);
403- // Reload sources after deletion
404- add (
405- const LoadSourcesRequested (limit: kDefaultRowsPerPage),
406- );
299+ final updatedSources =
300+ state.sources.where ((s) => s.id != event.id).toList ();
301+ emit (state.copyWith (sources: updatedSources));
407302 } on HtHttpException catch (e) {
408303 emit (
409304 state.copyWith (
0 commit comments