diff --git a/frontend/app_flowy/lib/startup/deps_resolver.dart b/frontend/app_flowy/lib/startup/deps_resolver.dart index a0daac273de53..76b694542ba4e 100644 --- a/frontend/app_flowy/lib/startup/deps_resolver.dart +++ b/frontend/app_flowy/lib/startup/deps_resolver.dart @@ -7,9 +7,7 @@ import 'package:app_flowy/workspace/application/grid/prelude.dart'; import 'package:app_flowy/workspace/application/trash/prelude.dart'; import 'package:app_flowy/workspace/application/workspace/prelude.dart'; import 'package:app_flowy/workspace/application/edit_pannel/edit_pannel_bloc.dart'; -import 'package:app_flowy/workspace/application/home/home_bloc.dart'; import 'package:app_flowy/workspace/application/view/prelude.dart'; -import 'package:app_flowy/workspace/application/home/prelude.dart'; import 'package:app_flowy/workspace/application/menu/prelude.dart'; import 'package:app_flowy/user/application/prelude.dart'; import 'package:app_flowy/user/presentation/router.dart'; @@ -45,7 +43,6 @@ void _resolveUserDeps(GetIt getIt) { getIt.registerFactory(() => SignUpBloc(getIt())); getIt.registerFactory(() => SplashRoute()); - getIt.registerFactory(() => HomeBloc()); getIt.registerFactory(() => EditPannelBloc()); getIt.registerFactory(() => SplashBloc()); getIt.registerLazySingleton(() => NetworkListener()); @@ -58,10 +55,6 @@ void _resolveHomeDeps(GetIt getIt) { (user, _) => UserListener(user: user), ); - getIt.registerFactoryParam( - (user, _) => HomeListenBloc(getIt(param1: user)), - ); - // getIt.registerLazySingleton(() => HomeStackManager()); diff --git a/frontend/app_flowy/lib/user/application/user_listener.dart b/frontend/app_flowy/lib/user/application/user_listener.dart index 5b212cc2d05ca..acacb19b029b7 100644 --- a/frontend/app_flowy/lib/user/application/user_listener.dart +++ b/frontend/app_flowy/lib/user/application/user_listener.dart @@ -12,26 +12,55 @@ import 'package:flowy_sdk/protobuf/flowy-user-data-model/user_profile.pb.dart'; import 'package:flowy_sdk/protobuf/flowy-user/dart_notification.pb.dart' as user; import 'package:flowy_sdk/rust_stream.dart'; -typedef UserProfileUpdatedNotifierValue = Either; -typedef AuthNotifierValue = Either; -typedef WorkspaceUpdatedNotifierValue = Either, FlowyError>; +typedef UserProfileDidUpdate = Either; +typedef AuthDidUpdate = Either; +typedef WorkspaceListDidUpdate = Either, FlowyError>; +typedef WorkspaceSettingDidUpdate = Either; class UserListener { StreamSubscription? _subscription; - final profileUpdatedNotifier = PublishNotifier(); - final authDidChangedNotifier = PublishNotifier(); - final workspaceUpdatedNotifier = PublishNotifier(); + final _profileNotifier = PublishNotifier(); + final _authNotifier = PublishNotifier(); + final _workspaceListNotifier = PublishNotifier(); + final _workSettingNotifier = PublishNotifier(); FolderNotificationParser? _workspaceParser; UserNotificationParser? _userParser; - late UserProfile _user; + final UserProfile _user; UserListener({ required UserProfile user, + }) : _user = user; + + void start({ + void Function(AuthDidUpdate)? authDidChange, + void Function(UserProfileDidUpdate)? profileDidUpdate, + void Function(WorkspaceListDidUpdate)? workspaceListDidUpdate, + void Function(WorkspaceSettingDidUpdate)? workspaceSettingDidUpdate, }) { - _user = user; - } + if (authDidChange != null) { + _authNotifier.addListener(() { + authDidChange(_authNotifier.currentValue!); + }); + } + + if (profileDidUpdate != null) { + _profileNotifier.addListener(() { + profileDidUpdate(_profileNotifier.currentValue!); + }); + } + + if (workspaceListDidUpdate != null) { + _workspaceListNotifier.addListener(() { + workspaceListDidUpdate(_workspaceListNotifier.currentValue!); + }); + } + + if (workspaceSettingDidUpdate != null) { + _workSettingNotifier.addListener(() { + workspaceSettingDidUpdate(_workSettingNotifier.currentValue!); + }); + } - void start() { _workspaceParser = FolderNotificationParser(id: _user.token, callback: _notificationCallback); _userParser = UserNotificationParser(id: _user.token, callback: _userNotificationCallback); _subscription = RustStreamReceiver.listen((observable) { @@ -44,9 +73,9 @@ class UserListener { _workspaceParser = null; _userParser = null; await _subscription?.cancel(); - profileUpdatedNotifier.dispose(); - authDidChangedNotifier.dispose(); - workspaceUpdatedNotifier.dispose(); + _profileNotifier.dispose(); + _authNotifier.dispose(); + _workspaceListNotifier.dispose(); } void _notificationCallback(FolderNotification ty, Either result) { @@ -55,16 +84,23 @@ class UserListener { case FolderNotification.UserDeleteWorkspace: case FolderNotification.WorkspaceListUpdated: result.fold( - (payload) => workspaceUpdatedNotifier.value = left(RepeatedWorkspace.fromBuffer(payload).items), - (error) => workspaceUpdatedNotifier.value = right(error), + (payload) => _workspaceListNotifier.value = left(RepeatedWorkspace.fromBuffer(payload).items), + (error) => _workspaceListNotifier.value = right(error), + ); + break; + case FolderNotification.WorkspaceSetting: + result.fold( + (payload) => _workSettingNotifier.value = left(CurrentWorkspaceSetting.fromBuffer(payload)), + (error) => _workSettingNotifier.value = right(error), ); break; case FolderNotification.UserUnauthorized: result.fold( (_) {}, - (error) => authDidChangedNotifier.value = right(FlowyError.create()..code = ErrorCode.UserUnauthorized.value), + (error) => _authNotifier.value = right(FlowyError.create()..code = ErrorCode.UserUnauthorized.value), ); break; + default: break; } @@ -74,8 +110,8 @@ class UserListener { switch (ty) { case user.UserNotification.UserUnauthorized: result.fold( - (payload) => profileUpdatedNotifier.value = left(UserProfile.fromBuffer(payload)), - (error) => profileUpdatedNotifier.value = right(error), + (payload) => _profileNotifier.value = left(UserProfile.fromBuffer(payload)), + (error) => _profileNotifier.value = right(error), ); break; default: diff --git a/frontend/app_flowy/lib/workspace/application/home/home_bloc.dart b/frontend/app_flowy/lib/workspace/application/home/home_bloc.dart index 5ed7ce959df77..f401f0a0ca45f 100644 --- a/frontend/app_flowy/lib/workspace/application/home/home_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/home/home_bloc.dart @@ -1,13 +1,36 @@ +import 'package:app_flowy/user/application/user_listener.dart'; import 'package:app_flowy/workspace/application/edit_pannel/edit_context.dart'; +import 'package:flowy_sdk/log.dart'; +import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart'; +import 'package:flowy_sdk/protobuf/flowy-folder-data-model/workspace.pb.dart' show CurrentWorkspaceSetting; +import 'package:flowy_sdk/protobuf/flowy-user-data-model/errors.pb.dart'; +import 'package:flowy_sdk/protobuf/flowy-user-data-model/user_profile.pb.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:dartz/dartz.dart'; part 'home_bloc.freezed.dart'; class HomeBloc extends Bloc { - HomeBloc() : super(HomeState.initial()) { + final UserListener _listener; + + HomeBloc(UserProfile user, CurrentWorkspaceSetting workspaceSetting) + : _listener = UserListener(user: user), + super(HomeState.initial(workspaceSetting)) { on((event, emit) async { await event.map( + initial: (_Initial value) { + _listener.start( + authDidChange: (result) { + _authDidChanged(result); + }, + workspaceSettingDidUpdate: (result) { + result.fold( + (setting) => add(HomeEvent.didReceiveWorkspaceSetting(setting)), + (r) => Log.error(r), + ); + }, + ); + }, showLoading: (e) async { emit(state.copyWith(isLoading: e.isLoading)); }, @@ -20,22 +43,40 @@ class HomeBloc extends Bloc { forceCollapse: (e) async { emit(state.copyWith(forceCollapse: e.forceCollapse)); }, + didReceiveWorkspaceSetting: (_DidReceiveWorkspaceSetting value) { + emit(state.copyWith(workspaceSetting: value.setting)); + }, + unauthorized: (_Unauthorized value) { + emit(state.copyWith(unauthorized: true)); + }, ); }); } @override - Future close() { + Future close() async { + await _listener.stop(); return super.close(); } + + void _authDidChanged(Either errorOrNothing) { + errorOrNothing.fold((_) {}, (error) { + if (error.code == ErrorCode.UserUnauthorized.value) { + add(HomeEvent.unauthorized(error.msg)); + } + }); + } } @freezed class HomeEvent with _$HomeEvent { + const factory HomeEvent.initial() = _Initial; const factory HomeEvent.showLoading(bool isLoading) = _ShowLoading; const factory HomeEvent.forceCollapse(bool forceCollapse) = _ForceCollapse; const factory HomeEvent.setEditPannel(EditPannelContext editContext) = _ShowEditPannel; const factory HomeEvent.dismissEditPannel() = _DismissEditPannel; + const factory HomeEvent.didReceiveWorkspaceSetting(CurrentWorkspaceSetting setting) = _DidReceiveWorkspaceSetting; + const factory HomeEvent.unauthorized(String msg) = _Unauthorized; } @freezed @@ -44,11 +85,15 @@ class HomeState with _$HomeState { required bool isLoading, required bool forceCollapse, required Option pannelContext, + required CurrentWorkspaceSetting workspaceSetting, + required bool unauthorized, }) = _HomeState; - factory HomeState.initial() => HomeState( + factory HomeState.initial(CurrentWorkspaceSetting workspaceSetting) => HomeState( isLoading: false, forceCollapse: false, pannelContext: none(), + workspaceSetting: workspaceSetting, + unauthorized: false, ); } diff --git a/frontend/app_flowy/lib/workspace/application/home/home_listen_bloc.dart b/frontend/app_flowy/lib/workspace/application/home/home_listen_bloc.dart deleted file mode 100644 index 0ebb572e17680..0000000000000 --- a/frontend/app_flowy/lib/workspace/application/home/home_listen_bloc.dart +++ /dev/null @@ -1,54 +0,0 @@ -import 'package:app_flowy/user/application/user_listener.dart'; -import 'package:flowy_sdk/protobuf/error-code/error_code.pbenum.dart'; -import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart'; -import 'package:freezed_annotation/freezed_annotation.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:dartz/dartz.dart'; -part 'home_listen_bloc.freezed.dart'; - -class HomeListenBloc extends Bloc { - final UserListener listener; - HomeListenBloc(this.listener) : super(const HomeListenState.loading()) { - on((event, emit) async { - await event.map( - started: (_) async { - listener.authDidChangedNotifier.addPublishListener((result) { - _authDidChanged(result); - }); - listener.start(); - }, - stop: (_) async {}, - unauthorized: (e) async { - emit(HomeListenState.unauthorized(e.msg)); - }, - ); - }); - } - - @override - Future close() async { - await listener.stop(); - super.close(); - } - - void _authDidChanged(Either errorOrNothing) { - errorOrNothing.fold((_) {}, (error) { - if (error.code == ErrorCode.UserUnauthorized.value) { - add(HomeListenEvent.unauthorized(error.msg)); - } - }); - } -} - -@freezed -class HomeListenEvent with _$HomeListenEvent { - const factory HomeListenEvent.started() = _Started; - const factory HomeListenEvent.stop() = _Stop; - const factory HomeListenEvent.unauthorized(String msg) = _Unauthorized; -} - -@freezed -class HomeListenState with _$HomeListenState { - const factory HomeListenState.loading() = Loading; - const factory HomeListenState.unauthorized(String msg) = Unauthorized; -} diff --git a/frontend/app_flowy/lib/workspace/application/home/prelude.dart b/frontend/app_flowy/lib/workspace/application/home/prelude.dart index 8d1173c2d6ce2..8b137891791fe 100644 --- a/frontend/app_flowy/lib/workspace/application/home/prelude.dart +++ b/frontend/app_flowy/lib/workspace/application/home/prelude.dart @@ -1 +1 @@ -export 'home_listen_bloc.dart'; + diff --git a/frontend/app_flowy/lib/workspace/application/menu/menu_user_bloc.dart b/frontend/app_flowy/lib/workspace/application/menu/menu_user_bloc.dart index 5129612ecb85c..d0970cae2114d 100644 --- a/frontend/app_flowy/lib/workspace/application/menu/menu_user_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/menu/menu_user_bloc.dart @@ -19,9 +19,10 @@ class MenuUserBloc extends Bloc { on((event, emit) async { await event.map( initial: (_) async { - userListener.profileUpdatedNotifier.addPublishListener(_profileUpdated); - userListener.workspaceUpdatedNotifier.addPublishListener(_workspacesUpdated); - userListener.start(); + userListener.start( + profileDidUpdate: _profileUpdated, + workspaceListDidUpdate: _workspaceListUpdated, + ); await _initUser(); }, fetchWorkspaces: (_FetchWorkspaces value) async {}, @@ -41,7 +42,7 @@ class MenuUserBloc extends Bloc { } void _profileUpdated(Either userOrFailed) {} - void _workspacesUpdated(Either, FlowyError> workspacesOrFailed) { + void _workspaceListUpdated(Either, FlowyError> workspacesOrFailed) { // fetch workspaces // iUserImpl.fetchWorkspaces().then((result) { // result.fold( diff --git a/frontend/app_flowy/lib/workspace/application/workspace/welcome_bloc.dart b/frontend/app_flowy/lib/workspace/application/workspace/welcome_bloc.dart index 8276331d10215..8ad83925b4ba1 100644 --- a/frontend/app_flowy/lib/workspace/application/workspace/welcome_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/workspace/welcome_bloc.dart @@ -16,8 +16,9 @@ class WelcomeBloc extends Bloc { on( (event, emit) async { await event.map(initial: (e) async { - userListener.workspaceUpdatedNotifier.addPublishListener(_workspacesUpdated); - userListener.start(); + userListener.start( + workspaceListDidUpdate: (result) => add(WelcomeEvent.workspacesReveived(result)), + ); // await _fetchWorkspaces(emit); }, openWorkspace: (e) async { @@ -74,10 +75,6 @@ class WelcomeBloc extends Bloc { }, )); } - - void _workspacesUpdated(Either, FlowyError> workspacesOrFail) { - add(WelcomeEvent.workspacesReveived(workspacesOrFail)); - } } @freezed diff --git a/frontend/app_flowy/lib/workspace/presentation/home/home_screen.dart b/frontend/app_flowy/lib/workspace/presentation/home/home_screen.dart index cea29792cb3a8..86fa9fab791e0 100644 --- a/frontend/app_flowy/lib/workspace/presentation/home/home_screen.dart +++ b/frontend/app_flowy/lib/workspace/presentation/home/home_screen.dart @@ -1,6 +1,5 @@ import 'package:app_flowy/plugin/plugin.dart'; import 'package:app_flowy/workspace/application/home/home_bloc.dart'; -import 'package:app_flowy/workspace/application/home/home_listen_bloc.dart'; import 'package:app_flowy/workspace/presentation/widgets/edit_pannel/pannel_animation.dart'; import 'package:app_flowy/workspace/presentation/widgets/float_bubble/question_bubble.dart'; import 'package:app_flowy/startup/startup.dart'; @@ -46,22 +45,20 @@ class _HomeScreenState extends State { Widget build(BuildContext context) { return MultiBlocProvider( providers: [ - BlocProvider( - create: (context) => getIt(param1: widget.user)..add(const HomeListenEvent.started()), + BlocProvider( + create: (context) { + return HomeBloc(widget.user, widget.workspaceSetting)..add(const HomeEvent.initial()); + }, ), - BlocProvider(create: (context) => getIt()), ], child: Scaffold( key: HomeScreen.scaffoldKey, - body: BlocListener( + body: BlocListener( + listenWhen: (p, c) => p.unauthorized != c.unauthorized, listener: (context, state) { - state.map( - loading: (_) {}, - unauthorized: (unauthorized) { - // TODO: push to login screen when user token was invalid - Log.error("Push to login screen when user token was invalid"); - }, - ); + if (state.unauthorized) { + Log.error("Push to login screen when user token was invalid"); + } }, child: BlocBuilder( buildWhen: (previous, current) => previous != current, @@ -73,7 +70,7 @@ class _HomeScreenState extends State { return FlowyContainer( Theme.of(context).colorScheme.surface, // Colors.white, - child: _buildBody(state, context.read().state.forceCollapse), + child: _buildBody(state), ); }, ), @@ -82,14 +79,15 @@ class _HomeScreenState extends State { ); } - Widget _buildBody(HomeState state, bool forceCollapse) { + Widget _buildBody(HomeState state) { return LayoutBuilder( builder: (BuildContext context, BoxConstraints constraints) { - final layout = HomeLayout(context, constraints, forceCollapse); + final layout = HomeLayout(context, constraints, state.forceCollapse); const homeStack = HomeStack(); final menu = _buildHomeMenu( layout: layout, context: context, + state: state, ); final editPannel = _buildEditPannel( homeState: state, @@ -108,20 +106,21 @@ class _HomeScreenState extends State { ); } - Widget _buildHomeMenu({required HomeLayout layout, required BuildContext context}) { - if (initialView == null && widget.workspaceSetting.hasLatestView()) { - initialView = widget.workspaceSetting.latestView; + Widget _buildHomeMenu({required HomeLayout layout, required BuildContext context, required HomeState state}) { + final workspaceSetting = state.workspaceSetting; + if (initialView == null && workspaceSetting.hasLatestView()) { + initialView = workspaceSetting.latestView; final plugin = makePlugin(pluginType: initialView!.pluginType, data: initialView); getIt().setPlugin(plugin); } HomeMenu homeMenu = HomeMenu( user: widget.user, - workspaceSetting: widget.workspaceSetting, + workspaceSetting: workspaceSetting, collapsedNotifier: getIt().collapsedNotifier, ); - final latestView = widget.workspaceSetting.hasLatestView() ? widget.workspaceSetting.latestView : null; + final latestView = workspaceSetting.hasLatestView() ? workspaceSetting.latestView : null; getIt().latestOpenView = latestView; return FocusTraversalGroup(child: RepaintBoundary(child: homeMenu)); diff --git a/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-folder/dart_notification.pbenum.dart b/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-folder/dart_notification.pbenum.dart index 5ecb48f9fd2fa..84042c03f6fc6 100644 --- a/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-folder/dart_notification.pbenum.dart +++ b/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-folder/dart_notification.pbenum.dart @@ -16,6 +16,7 @@ class FolderNotification extends $pb.ProtobufEnum { static const FolderNotification WorkspaceUpdated = FolderNotification._(12, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'WorkspaceUpdated'); static const FolderNotification WorkspaceListUpdated = FolderNotification._(13, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'WorkspaceListUpdated'); static const FolderNotification WorkspaceAppsChanged = FolderNotification._(14, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'WorkspaceAppsChanged'); + static const FolderNotification WorkspaceSetting = FolderNotification._(15, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'WorkspaceSetting'); static const FolderNotification AppUpdated = FolderNotification._(21, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'AppUpdated'); static const FolderNotification AppViewsChanged = FolderNotification._(24, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'AppViewsChanged'); static const FolderNotification ViewUpdated = FolderNotification._(31, const $core.bool.fromEnvironment('protobuf.omit_enum_names') ? '' : 'ViewUpdated'); @@ -31,6 +32,7 @@ class FolderNotification extends $pb.ProtobufEnum { WorkspaceUpdated, WorkspaceListUpdated, WorkspaceAppsChanged, + WorkspaceSetting, AppUpdated, AppViewsChanged, ViewUpdated, diff --git a/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-folder/dart_notification.pbjson.dart b/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-folder/dart_notification.pbjson.dart index 7cbe5621ba049..a58f1e6705c06 100644 --- a/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-folder/dart_notification.pbjson.dart +++ b/frontend/app_flowy/packages/flowy_sdk/lib/protobuf/flowy-folder/dart_notification.pbjson.dart @@ -18,6 +18,7 @@ const FolderNotification$json = const { const {'1': 'WorkspaceUpdated', '2': 12}, const {'1': 'WorkspaceListUpdated', '2': 13}, const {'1': 'WorkspaceAppsChanged', '2': 14}, + const {'1': 'WorkspaceSetting', '2': 15}, const {'1': 'AppUpdated', '2': 21}, const {'1': 'AppViewsChanged', '2': 24}, const {'1': 'ViewUpdated', '2': 31}, @@ -29,4 +30,4 @@ const FolderNotification$json = const { }; /// Descriptor for `FolderNotification`. Decode as a `google.protobuf.EnumDescriptorProto`. -final $typed_data.Uint8List folderNotificationDescriptor = $convert.base64Decode('ChJGb2xkZXJOb3RpZmljYXRpb24SCwoHVW5rbm93bhAAEhcKE1VzZXJDcmVhdGVXb3Jrc3BhY2UQChIXChNVc2VyRGVsZXRlV29ya3NwYWNlEAsSFAoQV29ya3NwYWNlVXBkYXRlZBAMEhgKFFdvcmtzcGFjZUxpc3RVcGRhdGVkEA0SGAoUV29ya3NwYWNlQXBwc0NoYW5nZWQQDhIOCgpBcHBVcGRhdGVkEBUSEwoPQXBwVmlld3NDaGFuZ2VkEBgSDwoLVmlld1VwZGF0ZWQQHxIPCgtWaWV3RGVsZXRlZBAgEhAKDFZpZXdSZXN0b3JlZBAhEhQKEFVzZXJVbmF1dGhvcml6ZWQQZBIRCgxUcmFzaFVwZGF0ZWQQ6Ac='); +final $typed_data.Uint8List folderNotificationDescriptor = $convert.base64Decode('ChJGb2xkZXJOb3RpZmljYXRpb24SCwoHVW5rbm93bhAAEhcKE1VzZXJDcmVhdGVXb3Jrc3BhY2UQChIXChNVc2VyRGVsZXRlV29ya3NwYWNlEAsSFAoQV29ya3NwYWNlVXBkYXRlZBAMEhgKFFdvcmtzcGFjZUxpc3RVcGRhdGVkEA0SGAoUV29ya3NwYWNlQXBwc0NoYW5nZWQQDhIUChBXb3Jrc3BhY2VTZXR0aW5nEA8SDgoKQXBwVXBkYXRlZBAVEhMKD0FwcFZpZXdzQ2hhbmdlZBAYEg8KC1ZpZXdVcGRhdGVkEB8SDwoLVmlld0RlbGV0ZWQQIBIQCgxWaWV3UmVzdG9yZWQQIRIUChBVc2VyVW5hdXRob3JpemVkEGQSEQoMVHJhc2hVcGRhdGVkEOgH'); diff --git a/frontend/rust-lib/flowy-folder/src/dart_notification.rs b/frontend/rust-lib/flowy-folder/src/dart_notification.rs index 9daaa6c3870ad..c062bd4a70568 100644 --- a/frontend/rust-lib/flowy-folder/src/dart_notification.rs +++ b/frontend/rust-lib/flowy-folder/src/dart_notification.rs @@ -10,6 +10,7 @@ pub(crate) enum FolderNotification { WorkspaceUpdated = 12, WorkspaceListUpdated = 13, WorkspaceAppsChanged = 14, + WorkspaceSetting = 15, AppUpdated = 21, AppViewsChanged = 24, ViewUpdated = 31, diff --git a/frontend/rust-lib/flowy-folder/src/protobuf/model/dart_notification.rs b/frontend/rust-lib/flowy-folder/src/protobuf/model/dart_notification.rs index d43a627f4bbba..f57f71114b9f7 100644 --- a/frontend/rust-lib/flowy-folder/src/protobuf/model/dart_notification.rs +++ b/frontend/rust-lib/flowy-folder/src/protobuf/model/dart_notification.rs @@ -31,6 +31,7 @@ pub enum FolderNotification { WorkspaceUpdated = 12, WorkspaceListUpdated = 13, WorkspaceAppsChanged = 14, + WorkspaceSetting = 15, AppUpdated = 21, AppViewsChanged = 24, ViewUpdated = 31, @@ -53,6 +54,7 @@ impl ::protobuf::ProtobufEnum for FolderNotification { 12 => ::std::option::Option::Some(FolderNotification::WorkspaceUpdated), 13 => ::std::option::Option::Some(FolderNotification::WorkspaceListUpdated), 14 => ::std::option::Option::Some(FolderNotification::WorkspaceAppsChanged), + 15 => ::std::option::Option::Some(FolderNotification::WorkspaceSetting), 21 => ::std::option::Option::Some(FolderNotification::AppUpdated), 24 => ::std::option::Option::Some(FolderNotification::AppViewsChanged), 31 => ::std::option::Option::Some(FolderNotification::ViewUpdated), @@ -72,6 +74,7 @@ impl ::protobuf::ProtobufEnum for FolderNotification { FolderNotification::WorkspaceUpdated, FolderNotification::WorkspaceListUpdated, FolderNotification::WorkspaceAppsChanged, + FolderNotification::WorkspaceSetting, FolderNotification::AppUpdated, FolderNotification::AppViewsChanged, FolderNotification::ViewUpdated, @@ -107,14 +110,15 @@ impl ::protobuf::reflect::ProtobufValue for FolderNotification { } static file_descriptor_proto_data: &'static [u8] = b"\ - \n\x17dart_notification.proto*\x9f\x02\n\x12FolderNotification\x12\x0b\n\ + \n\x17dart_notification.proto*\xb5\x02\n\x12FolderNotification\x12\x0b\n\ \x07Unknown\x10\0\x12\x17\n\x13UserCreateWorkspace\x10\n\x12\x17\n\x13Us\ erDeleteWorkspace\x10\x0b\x12\x14\n\x10WorkspaceUpdated\x10\x0c\x12\x18\ \n\x14WorkspaceListUpdated\x10\r\x12\x18\n\x14WorkspaceAppsChanged\x10\ - \x0e\x12\x0e\n\nAppUpdated\x10\x15\x12\x13\n\x0fAppViewsChanged\x10\x18\ - \x12\x0f\n\x0bViewUpdated\x10\x1f\x12\x0f\n\x0bViewDeleted\x10\x20\x12\ - \x10\n\x0cViewRestored\x10!\x12\x14\n\x10UserUnauthorized\x10d\x12\x11\n\ - \x0cTrashUpdated\x10\xe8\x07b\x06proto3\ + \x0e\x12\x14\n\x10WorkspaceSetting\x10\x0f\x12\x0e\n\nAppUpdated\x10\x15\ + \x12\x13\n\x0fAppViewsChanged\x10\x18\x12\x0f\n\x0bViewUpdated\x10\x1f\ + \x12\x0f\n\x0bViewDeleted\x10\x20\x12\x10\n\x0cViewRestored\x10!\x12\x14\ + \n\x10UserUnauthorized\x10d\x12\x11\n\x0cTrashUpdated\x10\xe8\x07b\x06pr\ + oto3\ "; static file_descriptor_proto_lazy: ::protobuf::rt::LazyV2<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::rt::LazyV2::INIT; diff --git a/frontend/rust-lib/flowy-folder/src/protobuf/proto/dart_notification.proto b/frontend/rust-lib/flowy-folder/src/protobuf/proto/dart_notification.proto index 7a87552f7de25..feff6258f6fc8 100644 --- a/frontend/rust-lib/flowy-folder/src/protobuf/proto/dart_notification.proto +++ b/frontend/rust-lib/flowy-folder/src/protobuf/proto/dart_notification.proto @@ -7,6 +7,7 @@ enum FolderNotification { WorkspaceUpdated = 12; WorkspaceListUpdated = 13; WorkspaceAppsChanged = 14; + WorkspaceSetting = 15; AppUpdated = 21; AppViewsChanged = 24; ViewUpdated = 31; diff --git a/frontend/rust-lib/flowy-folder/src/services/view/event_handler.rs b/frontend/rust-lib/flowy-folder/src/services/view/event_handler.rs index e7c91dda3843b..caa8134785893 100644 --- a/frontend/rust-lib/flowy-folder/src/services/view/event_handler.rs +++ b/frontend/rust-lib/flowy-folder/src/services/view/event_handler.rs @@ -1,4 +1,5 @@ -use crate::services::AppController; +use crate::manager::FolderManager; +use crate::services::{notify_workspace_setting_did_change, AppController}; use crate::{ entities::{ trash::Trash, @@ -69,10 +70,12 @@ pub(crate) async fn delete_view_handler( pub(crate) async fn set_latest_view_handler( data: Data, + folder: AppData>, controller: AppData>, ) -> Result<(), FlowyError> { let view_id: ViewId = data.into_inner(); let _ = controller.set_latest_view(&view_id.value)?; + let _ = notify_workspace_setting_did_change(&folder, &view_id).await?; Ok(()) } diff --git a/frontend/rust-lib/flowy-folder/src/services/workspace/controller.rs b/frontend/rust-lib/flowy-folder/src/services/workspace/controller.rs index ddbf7b32bd436..b343c5b46dc30 100644 --- a/frontend/rust-lib/flowy-folder/src/services/workspace/controller.rs +++ b/frontend/rust-lib/flowy-folder/src/services/workspace/controller.rs @@ -1,3 +1,4 @@ +use crate::manager::FolderManager; use crate::{ dart_notification::*, errors::*, @@ -190,6 +191,44 @@ impl WorkspaceController { } } +pub async fn notify_workspace_setting_did_change( + folder_manager: &Arc, + view_id: &str, +) -> FlowyResult<()> { + let user_id = folder_manager.user.user_id()?; + let token = folder_manager.user.token()?; + let workspace_id = get_current_workspace()?; + + let workspace_setting = folder_manager + .persistence + .begin_transaction(|transaction| { + let workspace = folder_manager.workspace_controller.read_local_workspace( + workspace_id.clone(), + &user_id, + &transaction, + )?; + + let setting = match transaction.read_view(view_id) { + Ok(latest_view) => CurrentWorkspaceSetting { + workspace, + latest_view: Some(latest_view), + }, + Err(_) => CurrentWorkspaceSetting { + workspace, + latest_view: None, + }, + }; + + Ok(setting) + }) + .await?; + + send_dart_notification(&token, FolderNotification::WorkspaceSetting) + .payload(workspace_setting) + .send(); + Ok(()) +} + const CURRENT_WORKSPACE_ID: &str = "current_workspace_id"; pub fn set_current_workspace(workspace_id: &str) { diff --git a/shared-lib/flowy-folder-data-model/src/entities/view.rs b/shared-lib/flowy-folder-data-model/src/entities/view.rs index c536d04f507ab..400649dac1b56 100644 --- a/shared-lib/flowy-folder-data-model/src/entities/view.rs +++ b/shared-lib/flowy-folder-data-model/src/entities/view.rs @@ -201,6 +201,14 @@ impl std::convert::From<&str> for ViewId { } } +impl std::ops::Deref for ViewId { + type Target = str; + + fn deref(&self) -> &Self::Target { + &self.value + } +} + #[derive(Default, ProtoBuf)] pub struct RepeatedViewId { #[pb(index = 1)]