Skip to content

Commit 7f0eb5f

Browse files
committed
refactor(archived_sources_bloc): improve error handling in source restoration process
1 parent ca80750 commit 7f0eb5f

File tree

1 file changed

+84
-5
lines changed

1 file changed

+84
-5
lines changed
Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,92 @@
11
import 'package:bloc/bloc.dart';
2+
import 'package:core/core.dart';
3+
import 'package:data_repository/data_repository.dart';
24
import 'package:equatable/equatable.dart';
35

46
part 'archived_sources_event.dart';
57
part 'archived_sources_state.dart';
68

7-
class ArchivedSourcesBloc extends Bloc<ArchivedSourcesEvent, ArchivedSourcesState> {
8-
ArchivedSourcesBloc() : super(ArchivedSourcesInitial()) {
9-
on<ArchivedSourcesEvent>((event, emit) {
10-
// TODO: implement event handler
11-
});
9+
class ArchivedSourcesBloc
10+
extends Bloc<ArchivedSourcesEvent, ArchivedSourcesState> {
11+
ArchivedSourcesBloc({
12+
required DataRepository<Source> sourcesRepository,
13+
}) : _sourcesRepository = sourcesRepository,
14+
super(const ArchivedSourcesState()) {
15+
on<LoadArchivedSourcesRequested>(_onLoadArchivedSourcesRequested);
16+
on<RestoreSourceRequested>(_onRestoreSourceRequested);
17+
}
18+
19+
final DataRepository<Source> _sourcesRepository;
20+
21+
Future<void> _onLoadArchivedSourcesRequested(
22+
LoadArchivedSourcesRequested event,
23+
Emitter<ArchivedSourcesState> emit,
24+
) async {
25+
emit(state.copyWith(status: ArchivedSourcesStatus.loading));
26+
try {
27+
final isPaginating = event.startAfterId != null;
28+
final previousSources = isPaginating ? state.sources : <Source>[];
29+
30+
final paginatedSources = await _sourcesRepository.readAll(
31+
filter: {'status': ContentStatus.archived.name},
32+
sort: [const SortOption('updatedAt', SortOrder.desc)],
33+
pagination: PaginationOptions(
34+
cursor: event.startAfterId,
35+
limit: event.limit,
36+
),
37+
);
38+
emit(
39+
state.copyWith(
40+
status: ArchivedSourcesStatus.success,
41+
sources: [...previousSources, ...paginatedSources.items],
42+
cursor: paginatedSources.cursor,
43+
hasMore: paginatedSources.hasMore,
44+
),
45+
);
46+
} on HttpException catch (e) {
47+
emit(
48+
state.copyWith(
49+
status: ArchivedSourcesStatus.failure,
50+
exception: e,
51+
),
52+
);
53+
} catch (e) {
54+
emit(
55+
state.copyWith(
56+
status: ArchivedSourcesStatus.failure,
57+
exception: UnknownException('An unexpected error occurred: $e'),
58+
),
59+
);
60+
}
61+
}
62+
63+
Future<void> _onRestoreSourceRequested(
64+
RestoreSourceRequested event,
65+
Emitter<ArchivedSourcesState> emit,
66+
) async {
67+
final originalSources = List<Source>.from(state.sources);
68+
final sourceIndex = originalSources.indexWhere((s) => s.id == event.id);
69+
if (sourceIndex == -1) return;
70+
71+
final sourceToRestore = originalSources[sourceIndex];
72+
final updatedSources = originalSources..removeAt(sourceIndex);
73+
74+
emit(state.copyWith(sources: updatedSources));
75+
76+
try {
77+
await _sourcesRepository.update(
78+
id: event.id,
79+
item: sourceToRestore.copyWith(status: ContentStatus.active),
80+
);
81+
} on HttpException catch (e) {
82+
emit(state.copyWith(sources: originalSources, exception: e));
83+
} catch (e) {
84+
emit(
85+
state.copyWith(
86+
sources: originalSources,
87+
exception: UnknownException('An unexpected error occurred: $e'),
88+
),
89+
);
90+
}
1291
}
1392
}

0 commit comments

Comments
 (0)