Skip to content

Commit d61a85d

Browse files
committed
refactor(content_management): optimize archived headlines state management
- Remove pendingDeletions map from ArchivedHeadlinesState - Simplify deletion undo logic by checking item type - Add snackbarHeadlineTitle to state for managing snackbar content - Update related emissions in bloc to reflect changes
1 parent b89bc08 commit d61a85d

File tree

1 file changed

+33
-47
lines changed

1 file changed

+33
-47
lines changed

lib/content_management/bloc/archived_headlines/archived_headlines_bloc.dart

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ArchivedHeadlinesBloc
4949
_pendingDeletionsService; // The injected service
5050

5151
/// Subscription to deletion events from the PendingDeletionsService.
52-
late final StreamSubscription<DeletionEvent> _deletionEventSubscription;
52+
late final StreamSubscription<DeletionEvent<dynamic>> _deletionEventSubscription;
5353

5454
@override
5555
Future<void> close() async {
@@ -123,12 +123,10 @@ class ArchivedHeadlinesBloc
123123
emit(
124124
state.copyWith(
125125
headlines: updatedHeadlines,
126-
// Also remove from pendingDeletions if it was there, as it's being restored.
127-
pendingDeletions: Map<String, Headline>.from(state.pendingDeletions)
128-
..remove(event.id),
129126
lastPendingDeletionId: state.lastPendingDeletionId == event.id
130127
? null
131128
: state.lastPendingDeletionId,
129+
snackbarHeadlineTitle: null, // Clear snackbar when restoring
132130
),
133131
);
134132

@@ -139,13 +137,11 @@ class ArchivedHeadlinesBloc
139137
);
140138
emit(state.copyWith(restoredHeadline: restoredHeadline));
141139
} on HttpException catch (e) {
142-
// If the update fails, revert the change in the UI and re-add to pendingDeletions
140+
// If the update fails, revert the change in the UI
143141
emit(
144142
state.copyWith(
145143
headlines: originalHeadlines,
146144
exception: e,
147-
pendingDeletions: Map<String, Headline>.from(state.pendingDeletions)
148-
..[event.id] = headlineToRestore,
149145
lastPendingDeletionId: state.lastPendingDeletionId,
150146
),
151147
);
@@ -154,8 +150,6 @@ class ArchivedHeadlinesBloc
154150
state.copyWith(
155151
headlines: originalHeadlines,
156152
exception: UnknownException('An unexpected error occurred: $e'),
157-
pendingDeletions: Map<String, Headline>.from(state.pendingDeletions)
158-
..[event.id] = headlineToRestore,
159153
lastPendingDeletionId: state.lastPendingDeletionId,
160154
),
161155
);
@@ -172,55 +166,48 @@ class ArchivedHeadlinesBloc
172166
) async {
173167
final id = event.event.id;
174168
final status = event.event.status;
175-
176-
final headlineInPending = state.pendingDeletions[id];
177-
if (headlineInPending == null) {
178-
// If the headline is not in pending deletions, it might have been
179-
// restored by other means or was never pending.
180-
return;
181-
}
182-
183-
final updatedPendingDeletions = Map<String, Headline>.from(
184-
state.pendingDeletions,
185-
)..remove(id);
169+
final item = event.event.item;
186170

187171
if (status == DeletionStatus.confirmed) {
188-
// Deletion confirmed, simply update pending deletions.
172+
// Deletion confirmed, no action needed in BLoC as it was optimistically removed.
173+
// Ensure lastPendingDeletionId and snackbarHeadlineTitle are cleared if this was the one.
189174
emit(
190175
state.copyWith(
191-
pendingDeletions: updatedPendingDeletions,
192176
lastPendingDeletionId: state.lastPendingDeletionId == id
193177
? null
194178
: state.lastPendingDeletionId,
179+
snackbarHeadlineTitle: null,
195180
),
196181
);
197182
} else if (status == DeletionStatus.undone) {
198183
// Deletion undone, restore the headline to the main list.
199-
final updatedHeadlines = List<Headline>.from(state.headlines)
200-
..insert(
201-
state.headlines.indexWhere(
184+
if (item is Headline) {
185+
final updatedHeadlines = List<Headline>.from(state.headlines)
186+
..insert(
187+
state.headlines.indexWhere(
188+
(h) => h.updatedAt.isBefore(
189+
item.updatedAt,
190+
),
191+
) !=
192+
-1
193+
? state.headlines.indexWhere(
202194
(h) => h.updatedAt.isBefore(
203-
headlineInPending.updatedAt,
195+
item.updatedAt,
204196
),
205-
) !=
206-
-1
207-
? state.headlines.indexWhere(
208-
(h) => h.updatedAt.isBefore(
209-
headlineInPending.updatedAt,
210-
),
211-
)
212-
: state.headlines.length,
213-
headlineInPending,
197+
)
198+
: state.headlines.length,
199+
item,
200+
);
201+
emit(
202+
state.copyWith(
203+
headlines: updatedHeadlines,
204+
lastPendingDeletionId: state.lastPendingDeletionId == id
205+
? null
206+
: state.lastPendingDeletionId,
207+
snackbarHeadlineTitle: null,
208+
),
214209
);
215-
emit(
216-
state.copyWith(
217-
headlines: updatedHeadlines,
218-
pendingDeletions: updatedPendingDeletions,
219-
lastPendingDeletionId: state.lastPendingDeletionId == id
220-
? null
221-
: state.lastPendingDeletionId,
222-
),
223-
);
210+
}
224211
}
225212
}
226213

@@ -243,9 +230,8 @@ class ArchivedHeadlinesBloc
243230
emit(
244231
state.copyWith(
245232
headlines: updatedHeadlines,
246-
pendingDeletions: Map<String, Headline>.from(state.pendingDeletions)
247-
..[event.id] = headlineToDelete,
248233
lastPendingDeletionId: event.id,
234+
snackbarHeadlineTitle: headlineToDelete.title, // Set title for snackbar
249235
),
250236
);
251237

@@ -277,6 +263,6 @@ class ArchivedHeadlinesBloc
277263
ClearRestoredHeadline event,
278264
Emitter<ArchivedHeadlinesState> emit,
279265
) {
280-
emit(state.copyWith(restoredHeadline: null));
266+
emit(state.copyWith(restoredHeadline: null, snackbarHeadlineTitle: null));
281267
}
282268
}

0 commit comments

Comments
 (0)