app_bloc.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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(name: value.name, desc: value.desc, dataType: value.dataType);
  24. viewOrFailed.fold(
  25. (view) => emit(state.copyWith(
  26. latestCreatedView: view,
  27. successOrFailure: left(unit),
  28. )),
  29. (error) {
  30. Log.error(error);
  31. emit(state.copyWith(successOrFailure: right(error)));
  32. },
  33. );
  34. }, didReceiveViews: (e) async {
  35. await handleDidReceiveViews(e.views, emit);
  36. }, delete: (e) async {
  37. final result = await repo.delete();
  38. result.fold(
  39. (unit) => emit(state.copyWith(successOrFailure: left(unit))),
  40. (error) => emit(state.copyWith(successOrFailure: right(error))),
  41. );
  42. }, rename: (e) async {
  43. final result = await repo.updateApp(name: e.newName);
  44. result.fold(
  45. (l) => emit(state.copyWith(successOrFailure: left(unit))),
  46. (error) => emit(state.copyWith(successOrFailure: right(error))),
  47. );
  48. }, appDidUpdate: (e) async {
  49. emit(state.copyWith(app: e.app));
  50. });
  51. });
  52. }
  53. @override
  54. Future<void> close() async {
  55. await listener.close();
  56. return super.close();
  57. }
  58. void _handleViewsChanged(Either<List<View>, FlowyError> result) {
  59. result.fold(
  60. (views) => add(AppEvent.didReceiveViews(views)),
  61. (error) {
  62. Log.error(error);
  63. },
  64. );
  65. }
  66. Future<void> handleDidReceiveViews(List<View> views, Emitter<AppState> emit) async {
  67. final latestCreatedView = state.latestCreatedView;
  68. AppState newState = state.copyWith(views: views);
  69. if (latestCreatedView != null) {
  70. final index = views.indexWhere((element) => element.id == latestCreatedView.id);
  71. if (index == -1) {
  72. newState = newState.copyWith(latestCreatedView: null);
  73. }
  74. }
  75. emit(newState);
  76. }
  77. Future<void> _fetchViews(Emitter<AppState> emit) async {
  78. final viewsOrFailed = await repo.getViews();
  79. viewsOrFailed.fold(
  80. (apps) => emit(state.copyWith(views: apps)),
  81. (error) {
  82. Log.error(error);
  83. emit(state.copyWith(successOrFailure: right(error)));
  84. },
  85. );
  86. }
  87. }
  88. @freezed
  89. class AppEvent with _$AppEvent {
  90. const factory AppEvent.initial() = Initial;
  91. const factory AppEvent.createView(String name, String desc, PluginDataType dataType) = CreateView;
  92. const factory AppEvent.delete() = Delete;
  93. const factory AppEvent.rename(String newName) = Rename;
  94. const factory AppEvent.didReceiveViews(List<View> views) = ReceiveViews;
  95. const factory AppEvent.appDidUpdate(App app) = AppDidUpdate;
  96. }
  97. @freezed
  98. class AppState with _$AppState {
  99. const factory AppState({
  100. required App app,
  101. required bool isLoading,
  102. required List<View>? views,
  103. View? latestCreatedView,
  104. required Either<Unit, FlowyError> successOrFailure,
  105. }) = _AppState;
  106. factory AppState.initial(App app) => AppState(
  107. app: app,
  108. isLoading: false,
  109. views: null,
  110. latestCreatedView: null,
  111. successOrFailure: left(unit),
  112. );
  113. }