From 5daf9d23f5b1a5fbff1769f8add61136f9b03080 Mon Sep 17 00:00:00 2001 From: Mathias Mogensen <42929161+Xazin@users.noreply.github.com> Date: Mon, 4 Mar 2024 18:24:49 +0100 Subject: [PATCH] fix: update view names in page reference menu (#4802) --- .../document/presentation/editor_page.dart | 6 +- .../base/link_to_page_widget.dart | 4 +- .../base/page_reference_commands.dart | 7 +- .../handlers/date_reference.dart | 9 ++- .../handlers/inline_page_reference.dart | 77 +++++++++++++++---- .../handlers/reminder_reference.dart | 9 ++- .../inline_actions_command.dart | 2 +- .../inline_actions_service.dart | 10 +-- .../inline_actions/service_handler.dart | 7 ++ .../widgets/inline_actions_handler.dart | 2 +- frontend/rust-lib/flowy-folder/src/manager.rs | 11 ++- 11 files changed, 108 insertions(+), 36 deletions(-) create mode 100644 frontend/appflowy_flutter/lib/plugins/inline_actions/service_handler.dart diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart index 22dabd6517..e21e2fd313 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart @@ -81,9 +81,9 @@ class _AppFlowyEditorPageState extends State { InlinePageReferenceService( currentViewId: documentBloc.view.id, limitResults: 5, - ).inlinePageReferenceDelegate, - DateReferenceService(context).dateReferenceDelegate, - ReminderReferenceService(context).reminderReferenceDelegate, + ), + DateReferenceService(context), + ReminderReferenceService(context), ], ); diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/link_to_page_widget.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/link_to_page_widget.dart index bef7ca88d9..0dcb8dd3e6 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/link_to_page_widget.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/link_to_page_widget.dart @@ -32,13 +32,13 @@ Future showLinkToPageMenu( customTitle: titleFromPageType(pageType), insertPage: pageType != ViewLayoutPB.Document, limitResults: 15, - ).inlinePageReferenceDelegate, + ), ], ); final List initialResults = []; for (final handler in service.handlers) { - final group = await handler(); + final group = await handler.search(null); if (group.results.isNotEmpty) { initialResults.add(group); diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/page_reference_commands.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/page_reference_commands.dart index f78b73bef7..42ee1a63d1 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/page_reference_commands.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/page_reference_commands.dart @@ -1,9 +1,10 @@ +import 'package:flutter/material.dart'; + import 'package:appflowy/plugins/inline_actions/handlers/inline_page_reference.dart'; import 'package:appflowy/plugins/inline_actions/inline_actions_menu.dart'; import 'package:appflowy/plugins/inline_actions/inline_actions_result.dart'; import 'package:appflowy/plugins/inline_actions/inline_actions_service.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; -import 'package:flutter/material.dart'; const _bracketChar = '['; const _plusChar = '+'; @@ -89,7 +90,7 @@ Future inlinePageReferenceCommandHandler( InlinePageReferenceService( currentViewId: currentViewId, limitResults: 10, - ).inlinePageReferenceDelegate, + ), ], ); @@ -97,7 +98,7 @@ Future inlinePageReferenceCommandHandler( final List initialResults = []; for (final handler in service.handlers) { - final group = await handler(); + final group = await handler.search(null); if (group.results.isNotEmpty) { initialResults.add(group); diff --git a/frontend/appflowy_flutter/lib/plugins/inline_actions/handlers/date_reference.dart b/frontend/appflowy_flutter/lib/plugins/inline_actions/handlers/date_reference.dart index 6d5012a5d6..6f3bf087a8 100644 --- a/frontend/appflowy_flutter/lib/plugins/inline_actions/handlers/date_reference.dart +++ b/frontend/appflowy_flutter/lib/plugins/inline_actions/handlers/date_reference.dart @@ -1,17 +1,19 @@ +import 'package:flutter/material.dart'; + import 'package:appflowy/date/date_service.dart'; import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/base/string_extension.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/mention_block.dart'; import 'package:appflowy/plugins/inline_actions/inline_actions_result.dart'; +import 'package:appflowy/plugins/inline_actions/service_handler.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:easy_localization/easy_localization.dart'; -import 'package:flutter/material.dart'; final _keywords = [ LocaleKeys.inlineActions_date.tr().toLowerCase(), ]; -class DateReferenceService { +class DateReferenceService extends InlineActionsDelegate { DateReferenceService(this.context) { // Initialize locale _locale = context.locale.toLanguageTag(); @@ -27,7 +29,8 @@ class DateReferenceService { List options = []; - Future dateReferenceDelegate([ + @override + Future search([ String? search, ]) async { // Checks if Locale has changed since last diff --git a/frontend/appflowy_flutter/lib/plugins/inline_actions/handlers/inline_page_reference.dart b/frontend/appflowy_flutter/lib/plugins/inline_actions/handlers/inline_page_reference.dart index caba7d6888..b8d94cee3c 100644 --- a/frontend/appflowy_flutter/lib/plugins/inline_actions/handlers/inline_page_reference.dart +++ b/frontend/appflowy_flutter/lib/plugins/inline_actions/handlers/inline_page_reference.dart @@ -9,16 +9,23 @@ import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/me import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/mention_page_block.dart'; import 'package:appflowy/plugins/inline_actions/inline_actions_menu.dart'; import 'package:appflowy/plugins/inline_actions/inline_actions_result.dart'; +import 'package:appflowy/plugins/inline_actions/service_handler.dart'; +import 'package:appflowy/startup/startup.dart'; +import 'package:appflowy/user/application/auth/auth_service.dart'; import 'package:appflowy/workspace/application/view/view_ext.dart'; import 'package:appflowy/workspace/application/view/view_service.dart'; +import 'package:appflowy/workspace/application/workspace/workspace_listener.dart'; +import 'package:appflowy_backend/dispatch/dispatch.dart'; import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart'; import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart'; +import 'package:appflowy_backend/protobuf/flowy-folder/workspace.pb.dart'; +import 'package:appflowy_backend/protobuf/flowy-user/user_profile.pb.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flowy_infra_ui/widget/dialog/styled_dialogs.dart'; import 'package:flowy_infra_ui/widget/error_page.dart'; -class InlinePageReferenceService { +class InlinePageReferenceService extends InlineActionsDelegate { InlinePageReferenceService({ required this.currentViewId, this.viewLayout, @@ -51,12 +58,67 @@ class InlinePageReferenceService { List _items = []; List _filtered = []; + UserProfilePB? _user; + String? _workspaceId; + WorkspaceListener? _listener; + Future init() async { _items = await _generatePageItems(currentViewId, viewLayout); _filtered = limitResults > 0 ? _items.take(limitResults).toList() : _items; + + await _initWorkspaceListener(); + _initCompleter.complete(); } + Future _initWorkspaceListener() async { + final snapshot = await Future.wait([ + FolderEventGetCurrentWorkspaceSetting().send(), + getIt().getUser(), + ]); + + final (workspaceSettings, userProfile) = (snapshot.first, snapshot.last); + _workspaceId = workspaceSettings.fold( + (s) => (s as WorkspaceSettingPB).workspaceId, + (e) => null, + ); + + _user = userProfile.fold((s) => s as UserProfilePB, (e) => null); + + if (_user != null && _workspaceId != null) { + _listener = WorkspaceListener( + user: _user!, + workspaceId: _workspaceId!, + ); + _listener!.start( + appsChanged: (_) async { + _items = await _generatePageItems(currentViewId, viewLayout); + _filtered = + limitResults > 0 ? _items.take(limitResults).toList() : _items; + }, + ); + } + } + + @override + Future search([ + String? search, + ]) async { + _filtered = await _filterItems(search); + + return InlineActionsResult( + title: customTitle?.isNotEmpty == true + ? customTitle! + : LocaleKeys.inlineActions_pageReference.tr(), + results: _filtered, + ); + } + + @override + Future dispose() async { + await _listener?.stop(); + } + Future> _filterItems(String? search) async { await _initCompleter.future; @@ -76,19 +138,6 @@ class InlinePageReferenceService { : items.toList(); } - Future inlinePageReferenceDelegate([ - String? search, - ]) async { - _filtered = await _filterItems(search); - - return InlineActionsResult( - title: customTitle?.isNotEmpty == true - ? customTitle! - : LocaleKeys.inlineActions_pageReference.tr(), - results: _filtered, - ); - } - Future> _generatePageItems( String currentViewId, ViewLayoutPB? viewLayout, diff --git a/frontend/appflowy_flutter/lib/plugins/inline_actions/handlers/reminder_reference.dart b/frontend/appflowy_flutter/lib/plugins/inline_actions/handlers/reminder_reference.dart index cbb2c5e007..1fdda2a40e 100644 --- a/frontend/appflowy_flutter/lib/plugins/inline_actions/handlers/reminder_reference.dart +++ b/frontend/appflowy_flutter/lib/plugins/inline_actions/handlers/reminder_reference.dart @@ -1,9 +1,12 @@ +import 'package:flutter/material.dart'; + import 'package:appflowy/date/date_service.dart'; import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/plugins/document/application/doc_bloc.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/base/string_extension.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/mention_block.dart'; import 'package:appflowy/plugins/inline_actions/inline_actions_result.dart'; +import 'package:appflowy/plugins/inline_actions/service_handler.dart'; import 'package:appflowy/user/application/reminder/reminder_bloc.dart'; import 'package:appflowy/user/application/reminder/reminder_extension.dart'; import 'package:appflowy/workspace/presentation/widgets/date_picker/widgets/reminder_selector.dart'; @@ -11,7 +14,6 @@ import 'package:appflowy_backend/protobuf/flowy-user/reminder.pb.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:fixnum/fixnum.dart'; -import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:nanoid/nanoid.dart'; @@ -20,7 +22,7 @@ final _keywords = [ LocaleKeys.inlineActions_reminder_shortKeyword.tr().toLowerCase(), ]; -class ReminderReferenceService { +class ReminderReferenceService extends InlineActionsDelegate { ReminderReferenceService(this.context) { // Initialize locale _locale = context.locale.toLanguageTag(); @@ -36,7 +38,8 @@ class ReminderReferenceService { List options = []; - Future reminderReferenceDelegate([ + @override + Future search([ String? search, ]) async { // Checks if Locale has changed since last diff --git a/frontend/appflowy_flutter/lib/plugins/inline_actions/inline_actions_command.dart b/frontend/appflowy_flutter/lib/plugins/inline_actions/inline_actions_command.dart index c1004d63f2..845eaf8c69 100644 --- a/frontend/appflowy_flutter/lib/plugins/inline_actions/inline_actions_command.dart +++ b/frontend/appflowy_flutter/lib/plugins/inline_actions/inline_actions_command.dart @@ -41,7 +41,7 @@ Future inlineActionsCommandHandler( final List initialResults = []; for (final handler in service.handlers) { - final group = await handler(); + final group = await handler.search(null); if (group.results.isNotEmpty) { initialResults.add(group); diff --git a/frontend/appflowy_flutter/lib/plugins/inline_actions/inline_actions_service.dart b/frontend/appflowy_flutter/lib/plugins/inline_actions/inline_actions_service.dart index d3a5623d2f..3bdd5bf61a 100644 --- a/frontend/appflowy_flutter/lib/plugins/inline_actions/inline_actions_service.dart +++ b/frontend/appflowy_flutter/lib/plugins/inline_actions/inline_actions_service.dart @@ -1,9 +1,6 @@ -import 'package:appflowy/plugins/inline_actions/inline_actions_result.dart'; import 'package:flutter/material.dart'; -typedef InlineActionsDelegate = Future Function([ - String? search, -]); +import 'package:appflowy/plugins/inline_actions/service_handler.dart'; abstract class _InlineActionsProvider { void dispose(); @@ -26,7 +23,10 @@ class InlineActionsService extends _InlineActionsProvider { /// we set the [BuildContext] to null. /// @override - void dispose() { + Future dispose() async { + for (final handler in handlers) { + await handler.dispose(); + } context = null; } } diff --git a/frontend/appflowy_flutter/lib/plugins/inline_actions/service_handler.dart b/frontend/appflowy_flutter/lib/plugins/inline_actions/service_handler.dart new file mode 100644 index 0000000000..de537fb964 --- /dev/null +++ b/frontend/appflowy_flutter/lib/plugins/inline_actions/service_handler.dart @@ -0,0 +1,7 @@ +import 'package:appflowy/plugins/inline_actions/inline_actions_result.dart'; + +abstract class InlineActionsDelegate { + Future search(String? search); + + Future dispose() async {} +} diff --git a/frontend/appflowy_flutter/lib/plugins/inline_actions/widgets/inline_actions_handler.dart b/frontend/appflowy_flutter/lib/plugins/inline_actions/widgets/inline_actions_handler.dart index da04567fcc..a19dffbd02 100644 --- a/frontend/appflowy_flutter/lib/plugins/inline_actions/widgets/inline_actions_handler.dart +++ b/frontend/appflowy_flutter/lib/plugins/inline_actions/widgets/inline_actions_handler.dart @@ -92,7 +92,7 @@ class _InlineActionsHandlerState extends State { Future _doSearch() async { final List newResults = []; for (final handler in widget.service.handlers) { - final group = await handler.call(_search); + final group = await handler.search(_search); if (group.results.isNotEmpty) { newResults.add(group); diff --git a/frontend/rust-lib/flowy-folder/src/manager.rs b/frontend/rust-lib/flowy-folder/src/manager.rs index 89c89b9ff3..04c1b2b4fc 100644 --- a/frontend/rust-lib/flowy-folder/src/manager.rs +++ b/frontend/rust-lib/flowy-folder/src/manager.rs @@ -27,7 +27,8 @@ use crate::entities::{ UpdateViewParams, ViewPB, WorkspacePB, WorkspaceSettingPB, }; use crate::manager_observer::{ - notify_child_views_changed, notify_parent_view_did_change, ChildViewChangeReason, + notify_child_views_changed, notify_did_update_workspace, notify_parent_view_did_change, + ChildViewChangeReason, }; use crate::notification::{ send_notification, send_workspace_setting_notification, FolderNotification, @@ -991,7 +992,15 @@ impl FolderManager { send_notification(&view_pb.id, FolderNotification::DidUpdateView) .payload(view_pb) .send(); + + if let Ok(workspace_id) = self.get_current_workspace_id().await { + let folder = &self.mutex_folder.lock(); + if let Some(folder) = folder.as_ref() { + notify_did_update_workspace(&workspace_id, folder); + } + } } + Ok(()) }