menu_bloc.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import 'dart:async';
  2. import 'package:app_flowy/plugin/plugin.dart';
  3. import 'package:app_flowy/workspace/application/workspace/workspace_listener.dart';
  4. import 'package:app_flowy/workspace/application/workspace/workspace_service.dart';
  5. import 'package:dartz/dartz.dart';
  6. import 'package:flowy_sdk/log.dart';
  7. import 'package:flowy_sdk/protobuf/flowy-folder-data-model/app.pb.dart';
  8. import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
  9. import 'package:freezed_annotation/freezed_annotation.dart';
  10. import 'package:flutter_bloc/flutter_bloc.dart';
  11. part 'menu_bloc.freezed.dart';
  12. class MenuBloc extends Bloc<MenuEvent, MenuState> {
  13. final WorkspaceService _workspaceService;
  14. final WorkspaceListener listener;
  15. final String workspaceId;
  16. MenuBloc({required this.workspaceId, required this.listener})
  17. : _workspaceService = WorkspaceService(workspaceId: workspaceId),
  18. super(MenuState.initial()) {
  19. on<MenuEvent>((event, emit) async {
  20. await event.map(
  21. initial: (e) async {
  22. listener.start(addAppCallback: _handleAppsOrFail);
  23. await _fetchApps(emit);
  24. },
  25. collapse: (e) async {
  26. final isCollapse = state.isCollapse;
  27. emit(state.copyWith(isCollapse: !isCollapse));
  28. },
  29. openPage: (e) async {
  30. emit(state.copyWith(plugin: e.plugin));
  31. },
  32. createApp: (_CreateApp event) async {
  33. await _performActionOnCreateApp(event, emit);
  34. },
  35. didReceiveApps: (e) async {
  36. emit(e.appsOrFail.fold(
  37. (apps) => state.copyWith(apps: apps, successOrFailure: left(unit)),
  38. (err) => state.copyWith(successOrFailure: right(err)),
  39. ));
  40. },
  41. moveApp: (_MoveApp value) {
  42. if (state.apps.length > value.fromIndex) {
  43. final app = state.apps[value.fromIndex];
  44. _workspaceService.moveApp(appId: app.id, fromIndex: value.fromIndex, toIndex: value.toIndex);
  45. final apps = List<App>.from(state.apps);
  46. apps.insert(value.toIndex, apps.removeAt(value.fromIndex));
  47. emit(state.copyWith(apps: apps));
  48. }
  49. },
  50. );
  51. });
  52. }
  53. @override
  54. Future<void> close() async {
  55. await listener.stop();
  56. return super.close();
  57. }
  58. Future<void> _performActionOnCreateApp(_CreateApp event, Emitter<MenuState> emit) async {
  59. final result = await _workspaceService.createApp(name: event.name, desc: event.desc ?? "");
  60. result.fold(
  61. (app) => {},
  62. (error) {
  63. Log.error(error);
  64. emit(state.copyWith(successOrFailure: right(error)));
  65. },
  66. );
  67. }
  68. // ignore: unused_element
  69. Future<void> _fetchApps(Emitter<MenuState> emit) async {
  70. final appsOrFail = await _workspaceService.getApps();
  71. emit(appsOrFail.fold(
  72. (apps) => state.copyWith(apps: apps),
  73. (error) {
  74. Log.error(error);
  75. return state.copyWith(successOrFailure: right(error));
  76. },
  77. ));
  78. }
  79. void _handleAppsOrFail(Either<List<App>, FlowyError> appsOrFail) {
  80. appsOrFail.fold(
  81. (apps) => add(MenuEvent.didReceiveApps(left(apps))),
  82. (error) => add(MenuEvent.didReceiveApps(right(error))),
  83. );
  84. }
  85. }
  86. @freezed
  87. class MenuEvent with _$MenuEvent {
  88. const factory MenuEvent.initial() = _Initial;
  89. const factory MenuEvent.collapse() = _Collapse;
  90. const factory MenuEvent.openPage(Plugin plugin) = _OpenPage;
  91. const factory MenuEvent.createApp(String name, {String? desc}) = _CreateApp;
  92. const factory MenuEvent.moveApp(int fromIndex, int toIndex) = _MoveApp;
  93. const factory MenuEvent.didReceiveApps(Either<List<App>, FlowyError> appsOrFail) = _ReceiveApps;
  94. }
  95. @freezed
  96. class MenuState with _$MenuState {
  97. const factory MenuState({
  98. required bool isCollapse,
  99. required List<App> apps,
  100. required Either<Unit, FlowyError> successOrFailure,
  101. required Plugin plugin,
  102. }) = _MenuState;
  103. factory MenuState.initial() => MenuState(
  104. isCollapse: false,
  105. apps: [],
  106. successOrFailure: left(unit),
  107. plugin: makePlugin(pluginType: DefaultPlugin.blank.type()),
  108. );
  109. }