trash_bloc.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import 'package:dartz/dartz.dart';
  2. import 'package:appflowy_backend/log.dart';
  3. import 'package:appflowy_backend/protobuf/flowy-folder2/trash.pb.dart';
  4. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  5. import 'package:flutter_bloc/flutter_bloc.dart';
  6. import 'package:freezed_annotation/freezed_annotation.dart';
  7. import 'package:appflowy/plugins/trash/application/trash_service.dart';
  8. import 'package:appflowy/plugins/trash/application/trash_listener.dart';
  9. part 'trash_bloc.freezed.dart';
  10. class TrashBloc extends Bloc<TrashEvent, TrashState> {
  11. final TrashService _service;
  12. final TrashListener _listener;
  13. TrashBloc()
  14. : _service = TrashService(),
  15. _listener = TrashListener(),
  16. super(TrashState.init()) {
  17. on<TrashEvent>((event, emit) async {
  18. await event.map(initial: (e) async {
  19. _listener.start(trashUpdated: _listenTrashUpdated);
  20. final result = await _service.readTrash();
  21. emit(result.fold(
  22. (object) => state.copyWith(
  23. objects: object.items, successOrFailure: left(unit)),
  24. (error) => state.copyWith(successOrFailure: right(error)),
  25. ));
  26. }, didReceiveTrash: (e) async {
  27. emit(state.copyWith(objects: e.trash));
  28. }, putback: (e) async {
  29. final result = await _service.putback(e.trashId);
  30. await _handleResult(result, emit);
  31. }, delete: (e) async {
  32. final result = await _service.deleteViews([e.trash.id]);
  33. await _handleResult(result, emit);
  34. }, deleteAll: (e) async {
  35. final result = await _service.deleteAll();
  36. await _handleResult(result, emit);
  37. }, restoreAll: (e) async {
  38. final result = await _service.restoreAll();
  39. await _handleResult(result, emit);
  40. });
  41. });
  42. }
  43. Future<void> _handleResult(
  44. Either<dynamic, FlowyError> result, Emitter<TrashState> emit) async {
  45. emit(result.fold(
  46. (l) => state.copyWith(successOrFailure: left(unit)),
  47. (error) => state.copyWith(successOrFailure: right(error)),
  48. ));
  49. }
  50. void _listenTrashUpdated(Either<List<TrashPB>, FlowyError> trashOrFailed) {
  51. trashOrFailed.fold(
  52. (trash) {
  53. add(TrashEvent.didReceiveTrash(trash));
  54. },
  55. (error) {
  56. Log.error(error);
  57. },
  58. );
  59. }
  60. @override
  61. Future<void> close() async {
  62. await _listener.close();
  63. return super.close();
  64. }
  65. }
  66. @freezed
  67. class TrashEvent with _$TrashEvent {
  68. const factory TrashEvent.initial() = Initial;
  69. const factory TrashEvent.didReceiveTrash(List<TrashPB> trash) = ReceiveTrash;
  70. const factory TrashEvent.putback(String trashId) = Putback;
  71. const factory TrashEvent.delete(TrashPB trash) = Delete;
  72. const factory TrashEvent.restoreAll() = RestoreAll;
  73. const factory TrashEvent.deleteAll() = DeleteAll;
  74. }
  75. @freezed
  76. class TrashState with _$TrashState {
  77. const factory TrashState({
  78. required List<TrashPB> objects,
  79. required Either<Unit, FlowyError> successOrFailure,
  80. }) = _TrashState;
  81. factory TrashState.init() => TrashState(
  82. objects: [],
  83. successOrFailure: left(unit),
  84. );
  85. }