@@ -31,16 +31,16 @@ class ContentManagementBloc
31
31
super (const ContentManagementState ()) {
32
32
on < ContentManagementTabChanged > (_onContentManagementTabChanged);
33
33
on < LoadHeadlinesRequested > (_onLoadHeadlinesRequested);
34
- on < CreateHeadlineRequested > (_onCreateHeadlineRequested );
35
- on < UpdateHeadlineRequested > (_onUpdateHeadlineRequested );
34
+ on < HeadlineAdded > (_onHeadlineAdded );
35
+ on < HeadlineUpdated > (_onHeadlineUpdated );
36
36
on < DeleteHeadlineRequested > (_onDeleteHeadlineRequested);
37
37
on < LoadCategoriesRequested > (_onLoadCategoriesRequested);
38
- on < CreateCategoryRequested > (_onCreateCategoryRequested );
39
- on < UpdateCategoryRequested > (_onUpdateCategoryRequested );
38
+ on < CategoryAdded > (_onCategoryAdded );
39
+ on < CategoryUpdated > (_onCategoryUpdated );
40
40
on < DeleteCategoryRequested > (_onDeleteCategoryRequested);
41
41
on < LoadSourcesRequested > (_onLoadSourcesRequested);
42
- on < CreateSourceRequested > (_onCreateSourceRequested );
43
- on < UpdateSourceRequested > (_onUpdateSourceRequested );
42
+ on < SourceAdded > (_onSourceAdded );
43
+ on < SourceUpdated > (_onSourceUpdated );
44
44
on < DeleteSourceRequested > (_onOnDeleteSourceRequested);
45
45
}
46
46
@@ -93,73 +93,37 @@ class ContentManagementBloc
93
93
}
94
94
}
95
95
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
+ );
122
104
}
123
105
124
- Future < void > _onUpdateHeadlineRequested (
125
- UpdateHeadlineRequested event,
106
+ void _onHeadlineUpdated (
107
+ HeadlineUpdated event,
126
108
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));
149
115
}
150
116
}
151
117
152
118
Future <void > _onDeleteHeadlineRequested (
153
119
DeleteHeadlineRequested event,
154
120
Emitter <ContentManagementState > emit,
155
121
) async {
156
- emit (state.copyWith (headlinesStatus: ContentManagementStatus .loading));
157
122
try {
158
123
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));
163
127
} on HtHttpException catch (e) {
164
128
emit (
165
129
state.copyWith (
@@ -215,73 +179,40 @@ class ContentManagementBloc
215
179
}
216
180
}
217
181
218
- Future < void > _onCreateCategoryRequested (
219
- CreateCategoryRequested event,
182
+ void _onCategoryAdded (
183
+ CategoryAdded event,
220
184
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
+ );
244
193
}
245
194
246
- Future < void > _onUpdateCategoryRequested (
247
- UpdateCategoryRequested event,
195
+ void _onCategoryUpdated (
196
+ CategoryUpdated event,
248
197
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));
271
204
}
272
205
}
273
206
274
207
Future <void > _onDeleteCategoryRequested (
275
208
DeleteCategoryRequested event,
276
209
Emitter <ContentManagementState > emit,
277
210
) async {
278
- emit (state.copyWith (categoriesStatus: ContentManagementStatus .loading));
279
211
try {
280
212
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));
285
216
} on HtHttpException catch (e) {
286
217
emit (
287
218
state.copyWith (
@@ -337,73 +268,37 @@ class ContentManagementBloc
337
268
}
338
269
}
339
270
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
+ );
366
279
}
367
280
368
- Future < void > _onUpdateSourceRequested (
369
- UpdateSourceRequested event,
281
+ void _onSourceUpdated (
282
+ SourceUpdated event,
370
283
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));
393
290
}
394
291
}
395
292
396
293
Future <void > _onOnDeleteSourceRequested (
397
294
DeleteSourceRequested event,
398
295
Emitter <ContentManagementState > emit,
399
296
) async {
400
- emit (state.copyWith (sourcesStatus: ContentManagementStatus .loading));
401
297
try {
402
298
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));
407
302
} on HtHttpException catch (e) {
408
303
emit (
409
304
state.copyWith (
0 commit comments