menu_bloc.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import 'dart:async';
  2. import 'package:app_flowy/startup/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(
  41. appId: app.id,
  42. fromIndex: value.fromIndex,
  43. toIndex: value.toIndex);
  44. final apps = List<AppPB>.from(state.apps);
  45. apps.insert(value.toIndex, apps.removeAt(value.fromIndex));
  46. emit(state.copyWith(apps: apps));
  47. }
  48. },
  49. );
  50. });
  51. }
  52. @override
  53. Future<void> close() async {
  54. await listener.stop();
  55. return super.close();
  56. }
  57. Future<void> _performActionOnCreateApp(
  58. _CreateApp event, Emitter<MenuState> emit) async {
  59. final result = await _workspaceService.createApp(
  60. name: event.name, desc: event.desc ?? "");
  61. result.fold(
  62. (app) => {},
  63. (error) {
  64. Log.error(error);
  65. emit(state.copyWith(successOrFailure: right(error)));
  66. },
  67. );
  68. }
  69. // ignore: unused_element
  70. Future<void> _fetchApps(Emitter<MenuState> emit) async {
  71. final appsOrFail = await _workspaceService.getApps();
  72. emit(appsOrFail.fold(
  73. (apps) => state.copyWith(apps: apps),
  74. (error) {
  75. Log.error(error);
  76. return state.copyWith(successOrFailure: right(error));
  77. },
  78. ));
  79. }
  80. void _handleAppsOrFail(Either<List<AppPB>, FlowyError> appsOrFail) {
  81. appsOrFail.fold(
  82. (apps) => add(MenuEvent.didReceiveApps(left(apps))),
  83. (error) => add(MenuEvent.didReceiveApps(right(error))),
  84. );
  85. }
  86. }
  87. @freezed
  88. class MenuEvent with _$MenuEvent {
  89. const factory MenuEvent.initial() = _Initial;
  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(
  94. Either<List<AppPB>, FlowyError> appsOrFail) = _ReceiveApps;
  95. }
  96. @freezed
  97. class MenuState with _$MenuState {
  98. const factory MenuState({
  99. required List<AppPB> apps,
  100. required Either<Unit, FlowyError> successOrFailure,
  101. required Plugin plugin,
  102. }) = _MenuState;
  103. factory MenuState.initial() => MenuState(
  104. apps: [],
  105. successOrFailure: left(unit),
  106. plugin: makePlugin(pluginType: PluginType.blank),
  107. );
  108. }