app_bloc.dart 4.2 KB

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