app_bloc.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import 'package:app_flowy/workspace/infrastructure/repos/app_repo.dart';
  2. import 'package:flowy_sdk/log.dart';
  3. import 'package:flowy_sdk/protobuf/flowy-folder-data-model/app.pb.dart';
  4. import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
  5. import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
  6. import 'package:freezed_annotation/freezed_annotation.dart';
  7. import 'package:flutter_bloc/flutter_bloc.dart';
  8. import 'package:dartz/dartz.dart';
  9. part 'app_bloc.freezed.dart';
  10. class AppBloc extends Bloc<AppEvent, AppState> {
  11. final AppRepository repo;
  12. final AppListener listener;
  13. AppBloc({required App app, required this.repo, required this.listener}) : super(AppState.initial(app)) {
  14. on<AppEvent>((event, emit) async {
  15. await event.map(initial: (e) async {
  16. listener.startListening(
  17. viewsChanged: _handleViewsChanged,
  18. appUpdated: (app) => add(AppEvent.appDidUpdate(app)),
  19. );
  20. await _fetchViews(emit);
  21. }, createView: (CreateView value) async {
  22. final viewOrFailed = await repo.createView(name: value.name, desc: value.desc, viewType: value.viewType);
  23. viewOrFailed.fold(
  24. (view) => emit(state.copyWith(
  25. latestCreatedView: view,
  26. successOrFailure: left(unit),
  27. )),
  28. (error) {
  29. Log.error(error);
  30. emit(state.copyWith(successOrFailure: right(error)));
  31. },
  32. );
  33. }, didReceiveViews: (e) async {
  34. await handleDidReceiveViews(e.views, emit);
  35. }, delete: (e) async {
  36. final result = await repo.delete();
  37. result.fold(
  38. (unit) => emit(state.copyWith(successOrFailure: left(unit))),
  39. (error) => emit(state.copyWith(successOrFailure: right(error))),
  40. );
  41. }, rename: (e) async {
  42. final result = await repo.updateApp(name: e.newName);
  43. result.fold(
  44. (l) => emit(state.copyWith(successOrFailure: left(unit))),
  45. (error) => emit(state.copyWith(successOrFailure: right(error))),
  46. );
  47. }, appDidUpdate: (e) async {
  48. emit(state.copyWith(app: e.app));
  49. });
  50. });
  51. }
  52. @override
  53. Future<void> close() async {
  54. await listener.close();
  55. return super.close();
  56. }
  57. void _handleViewsChanged(Either<List<View>, FlowyError> result) {
  58. result.fold(
  59. (views) => add(AppEvent.didReceiveViews(views)),
  60. (error) {
  61. Log.error(error);
  62. },
  63. );
  64. }
  65. Future<void> handleDidReceiveViews(List<View> views, Emitter<AppState> emit) async {
  66. final latestCreatedView = state.latestCreatedView;
  67. AppState newState = state.copyWith(views: views);
  68. if (latestCreatedView != null) {
  69. final index = views.indexWhere((element) => element.id == latestCreatedView.id);
  70. if (index == -1) {
  71. newState = newState.copyWith(latestCreatedView: null);
  72. }
  73. }
  74. emit(newState);
  75. }
  76. Future<void> _fetchViews(Emitter<AppState> emit) async {
  77. final viewsOrFailed = await repo.getViews();
  78. viewsOrFailed.fold(
  79. (apps) => emit(state.copyWith(views: apps)),
  80. (error) {
  81. Log.error(error);
  82. emit(state.copyWith(successOrFailure: right(error)));
  83. },
  84. );
  85. }
  86. }
  87. @freezed
  88. class AppEvent with _$AppEvent {
  89. const factory AppEvent.initial() = Initial;
  90. const factory AppEvent.createView(String name, String desc, ViewType viewType) = CreateView;
  91. const factory AppEvent.delete() = Delete;
  92. const factory AppEvent.rename(String newName) = Rename;
  93. const factory AppEvent.didReceiveViews(List<View> views) = ReceiveViews;
  94. const factory AppEvent.appDidUpdate(App app) = AppDidUpdate;
  95. }
  96. @freezed
  97. class AppState with _$AppState {
  98. const factory AppState({
  99. required App app,
  100. required bool isLoading,
  101. required List<View>? views,
  102. View? latestCreatedView,
  103. required Either<Unit, FlowyError> successOrFailure,
  104. }) = _AppState;
  105. factory AppState.initial(App app) => AppState(
  106. app: app,
  107. isLoading: false,
  108. views: null,
  109. latestCreatedView: null,
  110. successOrFailure: left(unit),
  111. );
  112. }