From 2df6c5e3340922cf867c48d7be5d5d27db7af08a Mon Sep 17 00:00:00 2001 From: hunteraraujo Date: Thu, 31 Aug 2023 15:45:55 -0700 Subject: [PATCH] Implement onSendPressed Functionality in ChatView This commit enhances the ChatView class by implementing the onSendPressed functionality, which is triggered when the user sends a message through the ChatInputField: - When onSendPressed is triggered, it checks if a task ID is currently selected (currentTaskId in ChatViewModel). - If a task ID is selected, the message is sent as a chat message for that task using sendChatMessage from ChatViewModel. - If no task ID is selected, a new task is created using createTask from TaskViewModel, and then the message is sent for that new task. This change provides a complete workflow for sending chat messages, either within an existing task or by creating a new task. --- lib/views/chat/chat_view.dart | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/views/chat/chat_view.dart b/lib/views/chat/chat_view.dart index 4efb819e..cee0746f 100644 --- a/lib/views/chat/chat_view.dart +++ b/lib/views/chat/chat_view.dart @@ -1,9 +1,13 @@ import 'package:auto_gpt_flutter_client/models/message_type.dart'; +import 'package:auto_gpt_flutter_client/viewmodels/task_viewmodel.dart'; import 'package:auto_gpt_flutter_client/views/chat/agent_message_tile.dart'; import 'package:auto_gpt_flutter_client/views/chat/chat_input_field.dart'; import 'package:auto_gpt_flutter_client/views/chat/user_message_tile.dart'; import 'package:flutter/material.dart'; import 'package:auto_gpt_flutter_client/viewmodels/chat_viewmodel.dart'; +import 'package:provider/provider.dart'; + +// TODO: Implement artifacts class ChatView extends StatefulWidget { final ChatViewModel viewModel; @@ -21,13 +25,14 @@ class _ChatViewState extends State { // Schedule the fetchTasks call for after the initial build WidgetsBinding.instance.addPostFrameCallback((_) { - // TODO: Update to actual task id - widget.viewModel.fetchChatsForTask(1); + widget.viewModel.fetchChatsForTask(); }); } @override Widget build(BuildContext context) { + // TODO: Do we want to have a reference to task view model in this class? + final taskViewModel = Provider.of(context, listen: false); return Scaffold( body: Column( children: [ @@ -49,8 +54,14 @@ class _ChatViewState extends State { Padding( padding: const EdgeInsets.all(8.0), child: ChatInputField( - onSendPressed: () { - // TODO: Implement passing the message back up + onSendPressed: (message) async { + if (widget.viewModel.currentTaskId != null) { + widget.viewModel.sendChatMessage(message); + } else { + String newTaskId = await taskViewModel.createTask(message); + widget.viewModel.setCurrentTaskId(newTaskId); + widget.viewModel.sendChatMessage(message); + } }, ), ),