app_bloc.dart 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import 'dart:collection';
  2. import 'package:appflowy/startup/plugin/plugin.dart';
  3. import 'package:appflowy/startup/startup.dart';
  4. import 'package:appflowy/workspace/application/app/app_listener.dart';
  5. import 'package:appflowy/workspace/application/app/app_service.dart';
  6. import 'package:appflowy/workspace/presentation/home/menu/menu.dart';
  7. import 'package:expandable/expandable.dart';
  8. import 'package:appflowy_backend/log.dart';
  9. import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
  10. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  11. import 'package:flutter/foundation.dart';
  12. import 'package:freezed_annotation/freezed_annotation.dart';
  13. import 'package:flutter_bloc/flutter_bloc.dart';
  14. import 'package:dartz/dartz.dart';
  15. part 'app_bloc.freezed.dart';
  16. class AppBloc extends Bloc<AppEvent, AppState> {
  17. final AppBackendService appService;
  18. final AppListener appListener;
  19. AppBloc({required ViewPB view})
  20. : appService = AppBackendService(),
  21. appListener = AppListener(viewId: view.id),
  22. super(AppState.initial(view)) {
  23. on<AppEvent>((event, emit) async {
  24. await event.map(initial: (e) async {
  25. _startListening();
  26. await _loadViews(emit);
  27. }, createView: (CreateView value) async {
  28. await _createView(value, emit);
  29. }, loadViews: (_) async {
  30. await _loadViews(emit);
  31. }, delete: (e) async {
  32. await _deleteApp(emit);
  33. }, deleteView: (deletedView) async {
  34. await _deleteView(emit, deletedView.viewId);
  35. }, rename: (e) async {
  36. await _renameView(e, emit);
  37. }, appDidUpdate: (e) async {
  38. final latestCreatedView = state.latestCreatedView;
  39. final views = e.app.belongings;
  40. AppState newState = state.copyWith(
  41. views: views,
  42. view: e.app,
  43. );
  44. if (latestCreatedView != null) {
  45. final index =
  46. views.indexWhere((element) => element.id == latestCreatedView.id);
  47. if (index == -1) {
  48. newState = newState.copyWith(latestCreatedView: null);
  49. }
  50. }
  51. emit(newState);
  52. });
  53. });
  54. }
  55. void _startListening() {
  56. appListener.start(
  57. onAppUpdated: (app) {
  58. if (!isClosed) {
  59. add(AppEvent.appDidUpdate(app));
  60. }
  61. },
  62. );
  63. }
  64. Future<void> _renameView(Rename e, Emitter<AppState> emit) async {
  65. final result =
  66. await appService.updateApp(appId: state.view.id, name: e.newName);
  67. result.fold(
  68. (l) => emit(state.copyWith(successOrFailure: left(unit))),
  69. (error) => emit(state.copyWith(successOrFailure: right(error))),
  70. );
  71. }
  72. // Delete the current app
  73. Future<void> _deleteApp(Emitter<AppState> emit) async {
  74. final result = await appService.delete(viewId: state.view.id);
  75. result.fold(
  76. (unit) => emit(state.copyWith(successOrFailure: left(unit))),
  77. (error) => emit(state.copyWith(successOrFailure: right(error))),
  78. );
  79. }
  80. Future<void> _deleteView(Emitter<AppState> emit, String viewId) async {
  81. final result = await appService.deleteView(viewId: viewId);
  82. result.fold(
  83. (unit) => emit(state.copyWith(successOrFailure: left(unit))),
  84. (error) => emit(state.copyWith(successOrFailure: right(error))),
  85. );
  86. }
  87. Future<void> _createView(CreateView value, Emitter<AppState> emit) async {
  88. final result = await appService.createView(
  89. appId: state.view.id,
  90. name: value.name,
  91. desc: value.desc ?? "",
  92. layoutType: value.pluginBuilder.layoutType!,
  93. initialData: value.initialData,
  94. ext: value.ext ?? {},
  95. );
  96. result.fold(
  97. (view) => emit(state.copyWith(
  98. latestCreatedView: view,
  99. successOrFailure: left(unit),
  100. )),
  101. (error) {
  102. Log.error(error);
  103. emit(state.copyWith(successOrFailure: right(error)));
  104. },
  105. );
  106. }
  107. @override
  108. Future<void> close() async {
  109. await appListener.stop();
  110. return super.close();
  111. }
  112. Future<void> _loadViews(Emitter<AppState> emit) async {
  113. final viewsOrFailed = await appService.getViews(viewId: state.view.id);
  114. viewsOrFailed.fold(
  115. (views) => emit(state.copyWith(views: views)),
  116. (error) {
  117. Log.error(error);
  118. emit(state.copyWith(successOrFailure: right(error)));
  119. },
  120. );
  121. }
  122. }
  123. @freezed
  124. class AppEvent with _$AppEvent {
  125. const factory AppEvent.initial() = Initial;
  126. const factory AppEvent.createView(
  127. String name,
  128. PluginBuilder pluginBuilder, {
  129. String? desc,
  130. /// The initial data should be the JSON of the document
  131. /// For example: {"document":{"type":"editor","children":[]}}
  132. String? initialData,
  133. Map<String, String>? ext,
  134. }) = CreateView;
  135. const factory AppEvent.loadViews() = LoadApp;
  136. const factory AppEvent.delete() = DeleteApp;
  137. const factory AppEvent.deleteView(String viewId) = DeleteView;
  138. const factory AppEvent.rename(String newName) = Rename;
  139. const factory AppEvent.appDidUpdate(ViewPB app) = AppDidUpdate;
  140. }
  141. @freezed
  142. class AppState with _$AppState {
  143. const factory AppState({
  144. required ViewPB view,
  145. required List<ViewPB> views,
  146. ViewPB? latestCreatedView,
  147. required Either<Unit, FlowyError> successOrFailure,
  148. }) = _AppState;
  149. factory AppState.initial(ViewPB view) => AppState(
  150. view: view,
  151. views: view.belongings,
  152. successOrFailure: left(unit),
  153. );
  154. }
  155. class AppViewDataContext extends ChangeNotifier {
  156. final String viewId;
  157. final ValueNotifier<List<ViewPB>> _viewsNotifier = ValueNotifier([]);
  158. final ValueNotifier<ViewPB?> _selectedViewNotifier = ValueNotifier(null);
  159. VoidCallback? _menuSharedStateListener;
  160. ExpandableController expandController =
  161. ExpandableController(initialExpanded: false);
  162. AppViewDataContext({required this.viewId}) {
  163. _setLatestView(getIt<MenuSharedState>().latestOpenView);
  164. _menuSharedStateListener =
  165. getIt<MenuSharedState>().addLatestViewListener((view) {
  166. _setLatestView(view);
  167. });
  168. }
  169. VoidCallback addSelectedViewChangeListener(void Function(ViewPB?) callback) {
  170. listener() {
  171. callback(_selectedViewNotifier.value);
  172. }
  173. _selectedViewNotifier.addListener(listener);
  174. return listener;
  175. }
  176. void removeSelectedViewListener(VoidCallback listener) {
  177. _selectedViewNotifier.removeListener(listener);
  178. }
  179. void _setLatestView(ViewPB? view) {
  180. view?.freeze();
  181. if (_selectedViewNotifier.value != view) {
  182. _selectedViewNotifier.value = view;
  183. _expandIfNeed();
  184. notifyListeners();
  185. }
  186. }
  187. ViewPB? get selectedView => _selectedViewNotifier.value;
  188. set views(List<ViewPB> views) {
  189. if (_viewsNotifier.value != views) {
  190. _viewsNotifier.value = views;
  191. _expandIfNeed();
  192. notifyListeners();
  193. }
  194. }
  195. UnmodifiableListView<ViewPB> get views =>
  196. UnmodifiableListView(_viewsNotifier.value);
  197. VoidCallback addViewsChangeListener(
  198. void Function(UnmodifiableListView<ViewPB>) callback) {
  199. listener() {
  200. callback(views);
  201. }
  202. _viewsNotifier.addListener(listener);
  203. return listener;
  204. }
  205. void removeViewsListener(VoidCallback listener) {
  206. _viewsNotifier.removeListener(listener);
  207. }
  208. void _expandIfNeed() {
  209. if (_selectedViewNotifier.value == null) {
  210. return;
  211. }
  212. if (!_viewsNotifier.value.contains(_selectedViewNotifier.value)) {
  213. return;
  214. }
  215. if (expandController.expanded == false) {
  216. // Workaround: Delay 150 milliseconds to make the smooth animation while expanding
  217. Future.delayed(const Duration(milliseconds: 150), () {
  218. expandController.expanded = true;
  219. });
  220. }
  221. }
  222. @override
  223. void dispose() {
  224. if (_menuSharedStateListener != null) {
  225. getIt<MenuSharedState>()
  226. .removeLatestViewListener(_menuSharedStateListener!);
  227. }
  228. super.dispose();
  229. }
  230. }