2024-06-03 14:27:28 +08:00
|
|
|
import 'package:appflowy/generated/flowy_svgs.g.dart';
|
2025-01-17 20:38:55 +08:00
|
|
|
import 'package:appflowy/generated/locale_keys.g.dart';
|
|
|
|
import 'package:appflowy/plugins/ai_chat/application/chat_select_message_bloc.dart';
|
2024-06-03 14:27:28 +08:00
|
|
|
import 'package:appflowy/plugins/ai_chat/chat_page.dart';
|
|
|
|
import 'package:appflowy/plugins/util.dart';
|
|
|
|
import 'package:appflowy/startup/plugin/plugin.dart';
|
2024-12-02 00:52:36 +01:00
|
|
|
import 'package:appflowy/workspace/application/view/view_ext.dart';
|
2024-06-03 14:27:28 +08:00
|
|
|
import 'package:appflowy/workspace/application/view_info/view_info_bloc.dart';
|
|
|
|
import 'package:appflowy/workspace/presentation/home/home_stack.dart';
|
2025-01-17 20:38:55 +08:00
|
|
|
import 'package:appflowy/workspace/presentation/home/menu/view/view_action_type.dart';
|
|
|
|
import 'package:appflowy/workspace/presentation/widgets/favorite_button.dart';
|
|
|
|
import 'package:appflowy/workspace/presentation/widgets/more_view_actions/more_view_actions.dart';
|
|
|
|
import 'package:appflowy/workspace/presentation/widgets/more_view_actions/widgets/common_view_action.dart';
|
2024-06-03 14:27:28 +08:00
|
|
|
import 'package:appflowy/workspace/presentation/widgets/tab_bar_item.dart';
|
|
|
|
import 'package:appflowy/workspace/presentation/widgets/view_title_bar.dart';
|
2024-08-06 07:56:13 +08:00
|
|
|
import 'package:appflowy_backend/log.dart';
|
2024-06-03 14:27:28 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
|
2025-01-17 20:38:55 +08:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
|
|
|
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
|
2024-07-22 13:35:42 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2024-06-03 14:27:28 +08:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
|
|
|
|
class AIChatPluginBuilder extends PluginBuilder {
|
|
|
|
@override
|
|
|
|
Plugin build(dynamic data) {
|
|
|
|
if (data is ViewPB) {
|
|
|
|
return AIChatPagePlugin(view: data);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw FlowyPluginException.invalidData;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-06-04 20:13:54 +08:00
|
|
|
String get menuName => "AI Chat";
|
2024-06-03 14:27:28 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
FlowySvgData get icon => FlowySvgs.chat_ai_page_s;
|
|
|
|
|
|
|
|
@override
|
|
|
|
PluginType get pluginType => PluginType.chat;
|
|
|
|
|
|
|
|
@override
|
|
|
|
ViewLayoutPB get layoutType => ViewLayoutPB.Chat;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AIChatPluginConfig implements PluginConfig {
|
|
|
|
@override
|
2024-06-03 14:34:48 +08:00
|
|
|
bool get creatable => true;
|
2024-06-03 14:27:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class AIChatPagePlugin extends Plugin {
|
|
|
|
AIChatPagePlugin({
|
|
|
|
required ViewPB view,
|
|
|
|
}) : notifier = ViewPluginNotifier(view: view);
|
|
|
|
|
|
|
|
late final ViewInfoBloc _viewInfoBloc;
|
2025-01-17 20:38:55 +08:00
|
|
|
late final _chatMessageSelectorBloc =
|
|
|
|
ChatSelectMessageBloc(viewNotifier: notifier);
|
2024-06-03 14:27:28 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
final ViewPluginNotifier notifier;
|
|
|
|
|
|
|
|
@override
|
|
|
|
PluginWidgetBuilder get widgetBuilder => AIChatPagePluginWidgetBuilder(
|
2025-01-17 20:38:55 +08:00
|
|
|
viewInfoBloc: _viewInfoBloc,
|
|
|
|
chatMessageSelectorBloc: _chatMessageSelectorBloc,
|
2024-06-03 14:27:28 +08:00
|
|
|
notifier: notifier,
|
|
|
|
);
|
|
|
|
|
|
|
|
@override
|
|
|
|
PluginId get id => notifier.view.id;
|
|
|
|
|
|
|
|
@override
|
|
|
|
PluginType get pluginType => PluginType.chat;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void init() {
|
|
|
|
_viewInfoBloc = ViewInfoBloc(view: notifier.view)
|
|
|
|
..add(const ViewInfoEvent.started());
|
|
|
|
}
|
2024-06-27 02:00:21 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_viewInfoBloc.close();
|
2025-01-17 20:38:55 +08:00
|
|
|
_chatMessageSelectorBloc.close();
|
2024-06-27 02:00:21 +02:00
|
|
|
notifier.dispose();
|
|
|
|
}
|
2024-06-03 14:27:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class AIChatPagePluginWidgetBuilder extends PluginWidgetBuilder
|
|
|
|
with NavigationItem {
|
|
|
|
AIChatPagePluginWidgetBuilder({
|
2025-01-17 20:38:55 +08:00
|
|
|
required this.viewInfoBloc,
|
|
|
|
required this.chatMessageSelectorBloc,
|
2024-06-03 14:27:28 +08:00
|
|
|
required this.notifier,
|
|
|
|
});
|
|
|
|
|
2025-01-17 20:38:55 +08:00
|
|
|
final ViewInfoBloc viewInfoBloc;
|
|
|
|
final ChatSelectMessageBloc chatMessageSelectorBloc;
|
2024-06-03 14:27:28 +08:00
|
|
|
final ViewPluginNotifier notifier;
|
|
|
|
int? deletedViewIndex;
|
|
|
|
|
2024-12-02 00:52:36 +01:00
|
|
|
@override
|
|
|
|
String? get viewName => notifier.view.nameOrDefault;
|
|
|
|
|
2024-06-03 14:27:28 +08:00
|
|
|
@override
|
|
|
|
Widget get leftBarItem =>
|
|
|
|
ViewTitleBar(key: ValueKey(notifier.view.id), view: notifier.view);
|
|
|
|
|
|
|
|
@override
|
2024-12-02 00:52:36 +01:00
|
|
|
Widget tabBarItem(String pluginId, [bool shortForm = false]) =>
|
|
|
|
ViewTabBarItem(view: notifier.view, shortForm: shortForm);
|
2024-06-03 14:27:28 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget buildWidget({
|
|
|
|
required PluginContext context,
|
|
|
|
required bool shrinkWrap,
|
2024-07-22 13:35:42 +08:00
|
|
|
Map<String, dynamic>? data,
|
2024-06-03 14:27:28 +08:00
|
|
|
}) {
|
2024-11-04 03:26:48 +01:00
|
|
|
notifier.isDeleted.addListener(_onDeleted);
|
2024-06-03 14:27:28 +08:00
|
|
|
|
2024-08-06 07:56:13 +08:00
|
|
|
if (context.userProfile == null) {
|
|
|
|
Log.error("User profile is null when opening AI Chat plugin");
|
|
|
|
return const SizedBox();
|
|
|
|
}
|
|
|
|
|
2025-01-17 20:38:55 +08:00
|
|
|
return MultiBlocProvider(
|
|
|
|
providers: [
|
|
|
|
BlocProvider.value(value: chatMessageSelectorBloc),
|
|
|
|
BlocProvider.value(value: viewInfoBloc),
|
|
|
|
],
|
2024-06-03 14:27:28 +08:00
|
|
|
child: AIChatPage(
|
|
|
|
userProfile: context.userProfile!,
|
|
|
|
key: ValueKey(notifier.view.id),
|
|
|
|
view: notifier.view,
|
|
|
|
onDeleted: () =>
|
|
|
|
context.onDeleted?.call(notifier.view, deletedViewIndex),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-04 03:26:48 +01:00
|
|
|
void _onDeleted() {
|
|
|
|
final deletedView = notifier.isDeleted.value;
|
|
|
|
if (deletedView != null && deletedView.hasIndex()) {
|
|
|
|
deletedViewIndex = deletedView.index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-03 14:27:28 +08:00
|
|
|
@override
|
|
|
|
List<NavigationItem> get navigationItems => [this];
|
2024-06-28 16:54:54 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
EdgeInsets get contentPadding => EdgeInsets.zero;
|
2025-01-17 20:38:55 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget? get rightBarItem => MultiBlocProvider(
|
|
|
|
providers: [
|
|
|
|
BlocProvider.value(value: viewInfoBloc),
|
|
|
|
BlocProvider.value(value: chatMessageSelectorBloc),
|
|
|
|
],
|
|
|
|
child: BlocBuilder<ChatSelectMessageBloc, ChatSelectMessageState>(
|
|
|
|
builder: (context, state) {
|
|
|
|
if (state.isSelectingMessages) {
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
ViewFavoriteButton(
|
|
|
|
key: ValueKey('favorite_button_${notifier.view.id}'),
|
|
|
|
view: notifier.view,
|
|
|
|
),
|
|
|
|
const HSpace(4),
|
|
|
|
MoreViewActions(
|
|
|
|
key: ValueKey(notifier.view.id),
|
|
|
|
view: notifier.view,
|
|
|
|
customActions: [
|
|
|
|
CustomViewAction(
|
|
|
|
view: notifier.view,
|
2025-01-21 22:17:53 +08:00
|
|
|
leftIcon: FlowySvgs.ai_add_to_page_s,
|
2025-01-17 20:38:55 +08:00
|
|
|
label: LocaleKeys.moreAction_saveAsNewPage.tr(),
|
|
|
|
onTap: () {
|
|
|
|
chatMessageSelectorBloc.add(
|
|
|
|
const ChatSelectMessageEvent
|
|
|
|
.toggleSelectingMessages(),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
ViewAction(
|
|
|
|
type: ViewMoreActionType.divider,
|
|
|
|
view: notifier.view,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2024-06-03 14:27:28 +08:00
|
|
|
}
|