tabs_bloc.dart 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import 'package:appflowy/plugins/util.dart';
  2. import 'package:appflowy/startup/plugin/plugin.dart';
  3. import 'package:appflowy/startup/startup.dart';
  4. import 'package:appflowy/workspace/application/view/view_ext.dart';
  5. import 'package:appflowy/workspace/presentation/home/home_stack.dart';
  6. import 'package:appflowy/workspace/presentation/home/menu/menu_shared_state.dart';
  7. import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
  8. import 'package:bloc/bloc.dart';
  9. import 'package:flutter/foundation.dart';
  10. import 'package:freezed_annotation/freezed_annotation.dart';
  11. part 'tabs_event.dart';
  12. part 'tabs_state.dart';
  13. part 'tabs_bloc.freezed.dart';
  14. class TabsBloc extends Bloc<TabsEvent, TabsState> {
  15. late final MenuSharedState menuSharedState;
  16. TabsBloc() : super(TabsState()) {
  17. menuSharedState = getIt<MenuSharedState>();
  18. on<TabsEvent>((event, emit) async {
  19. event.when(
  20. selectTab: (int index) {
  21. if (index != state.currentIndex &&
  22. index >= 0 &&
  23. index < state.pages) {
  24. emit(state.copyWith(newIndex: index));
  25. _setLatestOpenView();
  26. }
  27. },
  28. moveTab: () {},
  29. closeTab: (String pluginId) {
  30. emit(state.closeView(pluginId));
  31. _setLatestOpenView();
  32. },
  33. closeCurrentTab: () {
  34. emit(state.closeView(state.currentPageManager.plugin.id));
  35. _setLatestOpenView();
  36. },
  37. openTab: (Plugin plugin, ViewPB view) {
  38. emit(state.openView(plugin, view));
  39. _setLatestOpenView(view);
  40. },
  41. openPlugin: (Plugin plugin, ViewPB? view) {
  42. emit(state.openPlugin(plugin: plugin));
  43. _setLatestOpenView(view);
  44. },
  45. );
  46. });
  47. }
  48. void _setLatestOpenView([ViewPB? view]) {
  49. if (view != null) {
  50. menuSharedState.latestOpenView = view;
  51. } else {
  52. final pageManager = state.currentPageManager;
  53. final notifier = pageManager.plugin.notifier;
  54. if (notifier is ViewPluginNotifier) {
  55. menuSharedState.latestOpenView = notifier.view;
  56. }
  57. }
  58. }
  59. /// Adds a [TabsEvent.openTab] event for the provided [ViewPB]
  60. void openTab(ViewPB view) =>
  61. add(TabsEvent.openTab(plugin: view.plugin(), view: view));
  62. /// Adds a [TabsEvent.openPlugin] event for the provided [ViewPB]
  63. void openPlugin(ViewPB view) =>
  64. add(TabsEvent.openPlugin(plugin: view.plugin(), view: view));
  65. }