@@ -31,13 +31,13 @@ class ContentManagementBloc
31
31
on < ContentManagementTabChanged > (_onContentManagementTabChanged);
32
32
on < LoadHeadlinesRequested > (_onLoadHeadlinesRequested);
33
33
on < HeadlineUpdated > (_onHeadlineUpdated);
34
- on < DeleteHeadlineRequested > (_onDeleteHeadlineRequested );
34
+ on < ArchiveHeadlineRequested > (_onArchiveHeadlineRequested );
35
35
on < LoadTopicsRequested > (_onLoadTopicsRequested);
36
36
on < TopicUpdated > (_onTopicUpdated);
37
- on < DeleteTopicRequested > (_onDeleteTopicRequested );
37
+ on < ArchiveTopicRequested > (_onArchiveTopicRequested );
38
38
on < LoadSourcesRequested > (_onLoadSourcesRequested);
39
39
on < SourceUpdated > (_onSourceUpdated);
40
- on < DeleteSourceRequested > (_onDeleteSourceRequested );
40
+ on < ArchiveSourceRequested > (_onArchiveSourceRequested );
41
41
}
42
42
43
43
final DataRepository <Headline > _headlinesRepository;
@@ -92,17 +92,29 @@ class ContentManagementBloc
92
92
}
93
93
}
94
94
95
- Future <void > _onDeleteHeadlineRequested (
96
- DeleteHeadlineRequested event,
95
+ Future <void > _onArchiveHeadlineRequested (
96
+ ArchiveHeadlineRequested event,
97
97
Emitter <ContentManagementState > emit,
98
98
) async {
99
+ // Optimistically remove the headline from the list
100
+ final originalHeadlines = List <Headline >.from (state.headlines);
101
+ final headlineIndex = originalHeadlines.indexWhere ((h) => h.id == event.id);
102
+ if (headlineIndex == - 1 ) return ; // Headline not found
103
+
104
+ final headlineToArchive = originalHeadlines[headlineIndex];
105
+ final updatedHeadlines = originalHeadlines..removeAt (headlineIndex);
106
+
107
+ emit (state.copyWith (headlines: updatedHeadlines));
108
+
99
109
try {
100
- await _headlinesRepository.delete (id: event.id);
101
- final updatedHeadlines = state.headlines
102
- .where ((h) => h.id != event.id)
103
- .toList ();
104
- emit (state.copyWith (headlines: updatedHeadlines));
110
+ await _headlinesRepository.update (
111
+ id: event.id,
112
+ item: headlineToArchive.copyWith (status: ContentStatus .archived),
113
+ );
105
114
} on HttpException catch (e) {
115
+ // If the update fails, revert the change in the UI
116
+ emit (state.copyWith (headlines: originalHeadlines));
117
+ // And then show the error
106
118
emit (
107
119
state.copyWith (
108
120
headlinesStatus: ContentManagementStatus .failure,
@@ -172,17 +184,29 @@ class ContentManagementBloc
172
184
}
173
185
}
174
186
175
- Future <void > _onDeleteTopicRequested (
176
- DeleteTopicRequested event,
187
+ Future <void > _onArchiveTopicRequested (
188
+ ArchiveTopicRequested event,
177
189
Emitter <ContentManagementState > emit,
178
190
) async {
191
+ // Optimistically remove the topic from the list
192
+ final originalTopics = List <Topic >.from (state.topics);
193
+ final topicIndex = originalTopics.indexWhere ((t) => t.id == event.id);
194
+ if (topicIndex == - 1 ) return ; // Topic not found
195
+
196
+ final topicToArchive = originalTopics[topicIndex];
197
+ final updatedTopics = originalTopics..removeAt (topicIndex);
198
+
199
+ emit (state.copyWith (topics: updatedTopics));
200
+
179
201
try {
180
- await _topicsRepository.delete (id: event.id);
181
- final updatedTopics = state.topics
182
- .where ((c) => c.id != event.id)
183
- .toList ();
184
- emit (state.copyWith (topics: updatedTopics));
202
+ await _topicsRepository.update (
203
+ id: event.id,
204
+ item: topicToArchive.copyWith (status: ContentStatus .archived),
205
+ );
185
206
} on HttpException catch (e) {
207
+ // If the update fails, revert the change in the UI
208
+ emit (state.copyWith (topics: originalTopics));
209
+ // And then show the error
186
210
emit (
187
211
state.copyWith (
188
212
topicsStatus: ContentManagementStatus .failure,
@@ -252,17 +276,29 @@ class ContentManagementBloc
252
276
}
253
277
}
254
278
255
- Future <void > _onDeleteSourceRequested (
256
- DeleteSourceRequested event,
279
+ Future <void > _onArchiveSourceRequested (
280
+ ArchiveSourceRequested event,
257
281
Emitter <ContentManagementState > emit,
258
282
) async {
283
+ // Optimistically remove the source from the list
284
+ final originalSources = List <Source >.from (state.sources);
285
+ final sourceIndex = originalSources.indexWhere ((s) => s.id == event.id);
286
+ if (sourceIndex == - 1 ) return ; // Source not found
287
+
288
+ final sourceToArchive = originalSources[sourceIndex];
289
+ final updatedSources = originalSources..removeAt (sourceIndex);
290
+
291
+ emit (state.copyWith (sources: updatedSources));
292
+
259
293
try {
260
- await _sourcesRepository.delete (id: event.id);
261
- final updatedSources = state.sources
262
- .where ((s) => s.id != event.id)
263
- .toList ();
264
- emit (state.copyWith (sources: updatedSources));
294
+ await _sourcesRepository.update (
295
+ id: event.id,
296
+ item: sourceToArchive.copyWith (status: ContentStatus .archived),
297
+ );
265
298
} on HttpException catch (e) {
299
+ // If the update fails, revert the change in the UI
300
+ emit (state.copyWith (sources: originalSources));
301
+ // And then show the error
266
302
emit (
267
303
state.copyWith (
268
304
sourcesStatus: ContentManagementStatus .failure,
0 commit comments