From 7966b81e964b17191d4c5915dda9aa6940a8d1bb Mon Sep 17 00:00:00 2001 From: hunteraraujo Date: Sat, 2 Sep 2023 15:42:54 -0700 Subject: [PATCH] Update ViewModel classes for Chat and Task to handle structured responses This commit updates the ViewModel classes for both Chat and Task to accommodate the new structured responses received from the API. These changes are essential for the correct functioning of the application as they deal with the new TaskResponse and Step objects. Updates in ChatViewModel: - Modified `fetchChatsForTask` to handle structured step data - Updated how steps are retrieved and processed Updates in TaskViewModel: - Updated `fetchTasks` to use the new TaskResponse object - Refactored task fetching and selection logic These updates ensure that the ViewModel classes are aligned with the new structured API responses, improving data handling and UI updates. --- lib/viewmodels/chat_viewmodel.dart | 5 ++++- lib/viewmodels/task_viewmodel.dart | 11 ++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/viewmodels/chat_viewmodel.dart b/lib/viewmodels/chat_viewmodel.dart index e2a0127c..6e12423a 100644 --- a/lib/viewmodels/chat_viewmodel.dart +++ b/lib/viewmodels/chat_viewmodel.dart @@ -38,9 +38,12 @@ class ChatViewModel with ChangeNotifier { } try { // Fetch task steps from the data source - final List stepsJsonList = + final Map stepsResponse = await _chatService.listTaskSteps(_currentTaskId!); + // Extract steps from the response + final List stepsJsonList = stepsResponse['steps'] ?? []; + // Convert each map into a Step object List steps = stepsJsonList.map((stepMap) => Step.fromMap(stepMap)).toList(); diff --git a/lib/viewmodels/task_viewmodel.dart b/lib/viewmodels/task_viewmodel.dart index e4041c63..d4085d87 100644 --- a/lib/viewmodels/task_viewmodel.dart +++ b/lib/viewmodels/task_viewmodel.dart @@ -1,4 +1,5 @@ import 'package:auto_gpt_flutter_client/models/task.dart'; +import 'package:auto_gpt_flutter_client/models/task_response.dart'; import 'package:flutter/foundation.dart'; import 'package:collection/collection.dart'; import 'package:auto_gpt_flutter_client/services/task_service.dart'; @@ -45,13 +46,9 @@ class TaskViewModel with ChangeNotifier { /// Fetches tasks from the data source. void fetchTasks() async { try { - final fetchedTasks = await _taskService.listAllTasks(); - _tasks = fetchedTasks.map((taskMap) { - return Task( - id: taskMap['task_id'].toString(), - title: taskMap['output'].toString(), - ); - }).toList(); + final TaskResponse tasksResponse = await _taskService.listAllTasks(); + _tasks = tasksResponse.tasks; + notifyListeners(); print("Tasks fetched successfully!"); } catch (error) {