From 747fe40648e6b16c70d794af8c7b86e8431a4ff6 Mon Sep 17 00:00:00 2001 From: Ansah Mohammad Date: Thu, 8 Feb 2024 15:15:54 +0530 Subject: [PATCH] feat: toggle favourites from the documents top bar. (#4382) * feat: init star button * feat: implemented star button on the right bar * feat: Implemented toggle logic * chore: Added ToolTip for favorite button * refactor: renamed star to favorite * refactor: moved appplication logic into favorite_cubit * chore: arranged imports in alphabetical order * refactor: reused favorite_bloc for toggle * fix: fixed the icon no rebuild * refactor: moved the FavoriteBloc higher in the widget tree * refactor: moved FavoriteBloc inside multiBlocProvider of desktop_home_screen * chore: specified bloc type * chore: cleaned code * refactor: use any instead of map --- .../lib/plugins/document/document.dart | 6 +++ .../favorite/favorite_button.dart | 54 +++++++++++++++++++ .../home/desktop_home_screen.dart | 5 ++ .../home/menu/sidebar/sidebar.dart | 3 -- 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 frontend/appflowy_flutter/lib/plugins/document/presentation/favorite/favorite_button.dart diff --git a/frontend/appflowy_flutter/lib/plugins/document/document.dart b/frontend/appflowy_flutter/lib/plugins/document/document.dart index c9cc4f2ef8..e6b0abebe2 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/document.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/document.dart @@ -8,6 +8,7 @@ import 'package:appflowy/plugins/document/document_page.dart'; import 'package:appflowy/plugins/document/presentation/more/cubit/document_appearance_cubit.dart'; import 'package:appflowy/plugins/document/presentation/more/more_button.dart'; import 'package:appflowy/plugins/document/presentation/share/share_button.dart'; +import 'package:appflowy/plugins/document/presentation/favorite/favorite_button.dart'; import 'package:appflowy/plugins/util.dart'; import 'package:appflowy/startup/plugin/plugin.dart'; import 'package:appflowy/workspace/presentation/home/home_stack.dart'; @@ -121,6 +122,11 @@ class DocumentPluginWidgetBuilder extends PluginWidgetBuilder children: [ DocumentShareButton(key: ValueKey(view.id), view: view), const HSpace(4), + DocumentFavoriteButton( + key: ValueKey('favorite_button_${view.id}'), + view: view, + ), + const HSpace(4), const DocumentMoreButton(), ], ); diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/favorite/favorite_button.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/favorite/favorite_button.dart new file mode 100644 index 0000000000..b82b68dee4 --- /dev/null +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/favorite/favorite_button.dart @@ -0,0 +1,54 @@ +import 'package:appflowy/generated/flowy_svgs.g.dart'; +import 'package:appflowy/generated/locale_keys.g.dart'; +import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart'; +import 'package:appflowy/workspace/application/favorite/favorite_bloc.dart'; +import 'package:easy_localization/easy_localization.dart'; +import 'package:flowy_infra_ui/style_widget/hover.dart'; +import 'package:flowy_infra_ui/widget/flowy_tooltip.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; + +class DocumentFavoriteButton extends StatelessWidget { + const DocumentFavoriteButton({ + super.key, + required this.view, + }); + + final ViewPB view; + + @override + Widget build(BuildContext context) { + return BlocBuilder( + builder: (context, state) { + final isFavorite = state.views.any((v) => v.id == view.id); + return _buildFavoriteButton(context, isFavorite); + }, + ); + } + + Widget _buildFavoriteButton(BuildContext context, bool isFavorite) { + return FlowyTooltip( + message: isFavorite + ? LocaleKeys.button_removeFromFavorites.tr() + : LocaleKeys.button_addToFavorites.tr(), + child: FlowyHover( + child: GestureDetector( + onTap: () => + context.read().add(FavoriteEvent.toggle(view)), + child: _buildFavoriteIcon(context, isFavorite), + ), + ), + ); + } + + Widget _buildFavoriteIcon(BuildContext context, bool isFavorite) { + return Padding( + padding: const EdgeInsets.all(6), + child: FlowySvg( + isFavorite ? FlowySvgs.favorite_s : FlowySvgs.unfavorite_s, + size: const Size(18, 18), + color: Theme.of(context).iconTheme.color, + ), + ); + } +} diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/home/desktop_home_screen.dart b/frontend/appflowy_flutter/lib/workspace/presentation/home/desktop_home_screen.dart index 9f17f22799..3335b67c8e 100644 --- a/frontend/appflowy_flutter/lib/workspace/presentation/home/desktop_home_screen.dart +++ b/frontend/appflowy_flutter/lib/workspace/presentation/home/desktop_home_screen.dart @@ -13,6 +13,7 @@ import 'package:appflowy/workspace/application/view/view_ext.dart'; import 'package:appflowy/workspace/presentation/home/errors/workspace_failed_screen.dart'; import 'package:appflowy/workspace/presentation/home/hotkeys.dart'; import 'package:appflowy/workspace/presentation/home/menu/sidebar/sidebar.dart'; +import 'package:appflowy/workspace/application/favorite/favorite_bloc.dart'; import 'package:appflowy/workspace/presentation/widgets/edit_panel/panel_animation.dart'; import 'package:appflowy/workspace/presentation/widgets/float_bubble/question_bubble.dart'; import 'package:appflowy_backend/dispatch/dispatch.dart'; @@ -85,6 +86,10 @@ class DesktopHomeScreen extends StatelessWidget { )..add(const HomeSettingEvent.initial()); }, ), + BlocProvider( + create: (context) => + FavoriteBloc()..add(const FavoriteEvent.initial()), + ), ], child: HomeHotKeys( child: Scaffold( diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar.dart b/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar.dart index f3814a164f..af6f475698 100644 --- a/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar.dart +++ b/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar.dart @@ -51,9 +51,6 @@ class HomeSideBar extends StatelessWidget { workspaceId: workspaceSetting.workspaceId, )..add(const MenuEvent.initial()), ), - BlocProvider( - create: (_) => FavoriteBloc()..add(const FavoriteEvent.initial()), - ), ], child: MultiBlocListener( listeners: [