| 
									
										
										
										
											2024-06-27 02:00:21 +02:00
										 |  |  | import 'package:flutter/material.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-03 14:27:28 +08:00
										 |  |  | import 'package:appflowy/generated/flowy_svgs.g.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/plugins/ai_chat/chat_page.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/plugins/util.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/startup/plugin/plugin.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/workspace/application/view_info/view_info_bloc.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/workspace/presentation/home/home_stack.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/workspace/presentation/widgets/tab_bar_item.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/workspace/presentation/widgets/view_title_bar.dart'; | 
					
						
							|  |  |  | import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart'; | 
					
						
							|  |  |  | 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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   final ViewPluginNotifier notifier; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   PluginWidgetBuilder get widgetBuilder => AIChatPagePluginWidgetBuilder( | 
					
						
							|  |  |  |         bloc: _viewInfoBloc, | 
					
						
							|  |  |  |         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(); | 
					
						
							|  |  |  |     notifier.dispose(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-06-03 14:27:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AIChatPagePluginWidgetBuilder extends PluginWidgetBuilder | 
					
						
							|  |  |  |     with NavigationItem { | 
					
						
							|  |  |  |   AIChatPagePluginWidgetBuilder({ | 
					
						
							|  |  |  |     required this.bloc, | 
					
						
							|  |  |  |     required this.notifier, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   final ViewInfoBloc bloc; | 
					
						
							|  |  |  |   final ViewPluginNotifier notifier; | 
					
						
							|  |  |  |   int? deletedViewIndex; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   Widget get leftBarItem => | 
					
						
							|  |  |  |       ViewTitleBar(key: ValueKey(notifier.view.id), view: notifier.view); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   Widget tabBarItem(String pluginId) => ViewTabBarItem(view: notifier.view); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   Widget buildWidget({ | 
					
						
							|  |  |  |     required PluginContext context, | 
					
						
							|  |  |  |     required bool shrinkWrap, | 
					
						
							|  |  |  |   }) { | 
					
						
							|  |  |  |     notifier.isDeleted.addListener(() { | 
					
						
							|  |  |  |       final deletedView = notifier.isDeleted.value; | 
					
						
							|  |  |  |       if (deletedView != null && deletedView.hasIndex()) { | 
					
						
							|  |  |  |         deletedViewIndex = deletedView.index; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return BlocProvider<ViewInfoBloc>.value( | 
					
						
							|  |  |  |       value: bloc, | 
					
						
							|  |  |  |       child: AIChatPage( | 
					
						
							|  |  |  |         userProfile: context.userProfile!, | 
					
						
							|  |  |  |         key: ValueKey(notifier.view.id), | 
					
						
							|  |  |  |         view: notifier.view, | 
					
						
							|  |  |  |         onDeleted: () => | 
					
						
							|  |  |  |             context.onDeleted?.call(notifier.view, deletedViewIndex), | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   List<NavigationItem> get navigationItems => [this]; | 
					
						
							| 
									
										
										
										
											2024-06-28 16:54:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   EdgeInsets get contentPadding => EdgeInsets.zero; | 
					
						
							| 
									
										
										
										
											2024-06-03 14:27:28 +08:00
										 |  |  | } |