Skip to content

Commit c3c0fb6

Browse files
committed
feat(content_management): implement archive functionality for headlines, topics, and sources
- Rename DeleteHeadlineRequested, DeleteTopicRequested, and DeleteSourceRequested events to ArchiveHeadlineRequested, ArchiveTopicRequested, and ArchiveSourceRequested respectively - Update event handlers to archive items instead of deleting them - Implement optimistic UI updates for archiving items - Revert UI changes if archive operation fails
1 parent 351a651 commit c3c0fb6

File tree

1 file changed

+60
-24
lines changed

1 file changed

+60
-24
lines changed

lib/content_management/bloc/content_management_bloc.dart

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ class ContentManagementBloc
3131
on<ContentManagementTabChanged>(_onContentManagementTabChanged);
3232
on<LoadHeadlinesRequested>(_onLoadHeadlinesRequested);
3333
on<HeadlineUpdated>(_onHeadlineUpdated);
34-
on<DeleteHeadlineRequested>(_onDeleteHeadlineRequested);
34+
on<ArchiveHeadlineRequested>(_onArchiveHeadlineRequested);
3535
on<LoadTopicsRequested>(_onLoadTopicsRequested);
3636
on<TopicUpdated>(_onTopicUpdated);
37-
on<DeleteTopicRequested>(_onDeleteTopicRequested);
37+
on<ArchiveTopicRequested>(_onArchiveTopicRequested);
3838
on<LoadSourcesRequested>(_onLoadSourcesRequested);
3939
on<SourceUpdated>(_onSourceUpdated);
40-
on<DeleteSourceRequested>(_onDeleteSourceRequested);
40+
on<ArchiveSourceRequested>(_onArchiveSourceRequested);
4141
}
4242

4343
final DataRepository<Headline> _headlinesRepository;
@@ -92,17 +92,29 @@ class ContentManagementBloc
9292
}
9393
}
9494

95-
Future<void> _onDeleteHeadlineRequested(
96-
DeleteHeadlineRequested event,
95+
Future<void> _onArchiveHeadlineRequested(
96+
ArchiveHeadlineRequested event,
9797
Emitter<ContentManagementState> emit,
9898
) 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+
99109
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+
);
105114
} 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
106118
emit(
107119
state.copyWith(
108120
headlinesStatus: ContentManagementStatus.failure,
@@ -172,17 +184,29 @@ class ContentManagementBloc
172184
}
173185
}
174186

175-
Future<void> _onDeleteTopicRequested(
176-
DeleteTopicRequested event,
187+
Future<void> _onArchiveTopicRequested(
188+
ArchiveTopicRequested event,
177189
Emitter<ContentManagementState> emit,
178190
) 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+
179201
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+
);
185206
} 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
186210
emit(
187211
state.copyWith(
188212
topicsStatus: ContentManagementStatus.failure,
@@ -252,17 +276,29 @@ class ContentManagementBloc
252276
}
253277
}
254278

255-
Future<void> _onDeleteSourceRequested(
256-
DeleteSourceRequested event,
279+
Future<void> _onArchiveSourceRequested(
280+
ArchiveSourceRequested event,
257281
Emitter<ContentManagementState> emit,
258282
) 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+
259293
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+
);
265298
} 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
266302
emit(
267303
state.copyWith(
268304
sourcesStatus: ContentManagementStatus.failure,

0 commit comments

Comments
 (0)