menu_bloc.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/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(appsChanged: _handleAppsOrFail);
  23. await _fetchApps(emit);
  24. },
  25. openPage: (e) async {
  26. emit(state.copyWith(plugin: e.plugin));
  27. },
  28. createApp: (_CreateApp event) async {
  29. await _performActionOnCreateApp(event, emit);
  30. },
  31. didReceiveApps: (e) async {
  32. emit(e.appsOrFail.fold(
  33. (apps) => state.copyWith(apps: apps, successOrFailure: left(unit)),
  34. (err) => state.copyWith(successOrFailure: right(err)),
  35. ));
  36. },
  37. moveApp: (_MoveApp value) {
  38. if (state.apps.length > value.fromIndex) {
  39. final app = state.apps[value.fromIndex];
  40. _workspaceService.moveApp(appId: app.id, fromIndex: value.fromIndex, toIndex: value.toIndex);
  41. final apps = List<AppPB>.from(state.apps);
  42. apps.insert(value.toIndex, apps.removeAt(value.fromIndex));
  43. emit(state.copyWith(apps: apps));
  44. }
  45. },
  46. );
  47. });
  48. }
  49. @override
  50. Future<void> close() async {
  51. await listener.stop();
  52. return super.close();
  53. }
  54. Future<void> _performActionOnCreateApp(_CreateApp event, Emitter<MenuState> emit) async {
  55. final result = await _workspaceService.createApp(name: event.name, desc: event.desc ?? "");
  56. result.fold(
  57. (app) => {},
  58. (error) {
  59. Log.error(error);
  60. emit(state.copyWith(successOrFailure: right(error)));
  61. },
  62. );
  63. }
  64. // ignore: unused_element
  65. Future<void> _fetchApps(Emitter<MenuState> emit) async {
  66. final appsOrFail = await _workspaceService.getApps();
  67. emit(appsOrFail.fold(
  68. (apps) => state.copyWith(apps: apps),
  69. (error) {
  70. Log.error(error);
  71. return state.copyWith(successOrFailure: right(error));
  72. },
  73. ));
  74. }
  75. void _handleAppsOrFail(Either<List<AppPB>, FlowyError> appsOrFail) {
  76. appsOrFail.fold(
  77. (apps) => add(MenuEvent.didReceiveApps(left(apps))),
  78. (error) => add(MenuEvent.didReceiveApps(right(error))),
  79. );
  80. }
  81. }
  82. @freezed
  83. class MenuEvent with _$MenuEvent {
  84. const factory MenuEvent.initial() = _Initial;
  85. const factory MenuEvent.openPage(Plugin plugin) = _OpenPage;
  86. const factory MenuEvent.createApp(String name, {String? desc}) = _CreateApp;
  87. const factory MenuEvent.moveApp(int fromIndex, int toIndex) = _MoveApp;
  88. const factory MenuEvent.didReceiveApps(Either<List<AppPB>, FlowyError> appsOrFail) = _ReceiveApps;
  89. }
  90. @freezed
  91. class MenuState with _$MenuState {
  92. const factory MenuState({
  93. required List<AppPB> apps,
  94. required Either<Unit, FlowyError> successOrFailure,
  95. required Plugin plugin,
  96. }) = _MenuState;
  97. factory MenuState.initial() => MenuState(
  98. apps: [],
  99. successOrFailure: left(unit),
  100. plugin: makePlugin(pluginType: DefaultPlugin.blank.type()),
  101. );
  102. }