|
1 | 1 | import 'package:bloc/bloc.dart';
|
| 2 | +import 'package:core/core.dart'; |
| 3 | +import 'package:data_repository/data_repository.dart'; |
2 | 4 | import 'package:equatable/equatable.dart';
|
3 | 5 |
|
4 | 6 | part 'archived_topics_event.dart';
|
5 | 7 | part 'archived_topics_state.dart';
|
6 | 8 |
|
7 |
| -class ArchivedTopicsBloc extends Bloc<ArchivedTopicsEvent, ArchivedTopicsState> { |
8 |
| - ArchivedTopicsBloc() : super(ArchivedTopicsInitial()) { |
9 |
| - on<ArchivedTopicsEvent>((event, emit) { |
10 |
| - // TODO: implement event handler |
11 |
| - }); |
| 9 | +class ArchivedTopicsBloc |
| 10 | + extends Bloc<ArchivedTopicsEvent, ArchivedTopicsState> { |
| 11 | + ArchivedTopicsBloc({ |
| 12 | + required DataRepository<Topic> topicsRepository, |
| 13 | + }) : _topicsRepository = topicsRepository, |
| 14 | + super(const ArchivedTopicsState()) { |
| 15 | + on<LoadArchivedTopicsRequested>(_onLoadArchivedTopicsRequested); |
| 16 | + on<RestoreTopicRequested>(_onRestoreTopicRequested); |
| 17 | + } |
| 18 | + |
| 19 | + final DataRepository<Topic> _topicsRepository; |
| 20 | + |
| 21 | + Future<void> _onLoadArchivedTopicsRequested( |
| 22 | + LoadArchivedTopicsRequested event, |
| 23 | + Emitter<ArchivedTopicsState> emit, |
| 24 | + ) async { |
| 25 | + emit(state.copyWith(status: ArchivedTopicsStatus.loading)); |
| 26 | + try { |
| 27 | + final isPaginating = event.startAfterId != null; |
| 28 | + final previousTopics = isPaginating ? state.topics : <Topic>[]; |
| 29 | + |
| 30 | + final paginatedTopics = await _topicsRepository.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: ArchivedTopicsStatus.success, |
| 41 | + topics: [...previousTopics, ...paginatedTopics.items], |
| 42 | + cursor: paginatedTopics.cursor, |
| 43 | + hasMore: paginatedTopics.hasMore, |
| 44 | + ), |
| 45 | + ); |
| 46 | + } on HttpException catch (e) { |
| 47 | + emit( |
| 48 | + state.copyWith( |
| 49 | + status: ArchivedTopicsStatus.failure, |
| 50 | + exception: e, |
| 51 | + ), |
| 52 | + ); |
| 53 | + } catch (e) { |
| 54 | + emit( |
| 55 | + state.copyWith( |
| 56 | + status: ArchivedTopicsStatus.failure, |
| 57 | + exception: UnknownException('An unexpected error occurred: $e'), |
| 58 | + ), |
| 59 | + ); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + Future<void> _onRestoreTopicRequested( |
| 64 | + RestoreTopicRequested event, |
| 65 | + Emitter<ArchivedTopicsState> emit, |
| 66 | + ) async { |
| 67 | + final originalTopics = List<Topic>.from(state.topics); |
| 68 | + final topicIndex = originalTopics.indexWhere((t) => t.id == event.id); |
| 69 | + if (topicIndex == -1) return; |
| 70 | + |
| 71 | + final topicToRestore = originalTopics[topicIndex]; |
| 72 | + final updatedTopics = originalTopics..removeAt(topicIndex); |
| 73 | + |
| 74 | + emit(state.copyWith(topics: updatedTopics)); |
| 75 | + |
| 76 | + try { |
| 77 | + await _topicsRepository.update( |
| 78 | + id: event.id, |
| 79 | + item: topicToRestore.copyWith(status: ContentStatus.active), |
| 80 | + ); |
| 81 | + } on HttpException catch (e) { |
| 82 | + emit(state.copyWith(topics: originalTopics, exception: e)); |
| 83 | + } catch (e) { |
| 84 | + emit( |
| 85 | + state.copyWith( |
| 86 | + topics: originalTopics, |
| 87 | + exception: UnknownException('An unexpected error occurred: $e'), |
| 88 | + ), |
| 89 | + ); |
| 90 | + } |
12 | 91 | }
|
13 | 92 | }
|
0 commit comments