mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-28 11:24:30 +01:00
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.
75 lines
2.4 KiB
Dart
75 lines
2.4 KiB
Dart
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';
|
|
import 'package:auto_gpt_flutter_client/models/task_request_body.dart';
|
|
|
|
class TaskViewModel with ChangeNotifier {
|
|
final TaskService _taskService;
|
|
List<Task> _tasks = [];
|
|
Task? _selectedTask; // This will store the currently selected task
|
|
|
|
TaskViewModel(this._taskService);
|
|
|
|
/// Returns the list of tasks.
|
|
List<Task> get tasks => _tasks;
|
|
|
|
/// Returns the currently selected task.
|
|
Task? get selectedTask => _selectedTask;
|
|
|
|
/// Adds a task and returns its ID.
|
|
Future<String> createTask(String title) async {
|
|
final newTask = TaskRequestBody(input: title);
|
|
// Add to data source
|
|
final createdTask = await _taskService.createTask(newTask);
|
|
// Create a Task object from the created task response
|
|
final newTaskObject =
|
|
Task(id: createdTask['task_id'], title: createdTask['input']);
|
|
|
|
// Update local tasks list and notify listeners
|
|
_tasks.add(newTaskObject);
|
|
notifyListeners();
|
|
|
|
return newTaskObject.id; // Return the ID of the new task
|
|
}
|
|
|
|
/// Deletes a task.
|
|
void deleteTask(String id) async {
|
|
// TODO: Protocol doesn't support deleting tasks, we need to manually manage this list
|
|
|
|
// Update local tasks list and notify listeners
|
|
_tasks.removeWhere((task) => task.id == id);
|
|
notifyListeners();
|
|
}
|
|
|
|
/// Fetches tasks from the data source.
|
|
void fetchTasks() async {
|
|
try {
|
|
final TaskResponse tasksResponse = await _taskService.listAllTasks();
|
|
_tasks = tasksResponse.tasks;
|
|
|
|
notifyListeners();
|
|
print("Tasks fetched successfully!");
|
|
} catch (error) {
|
|
print("Error fetching tasks: $error");
|
|
}
|
|
}
|
|
|
|
/// Handles the selection of a task by its ID.
|
|
void selectTask(String id) {
|
|
final task = _tasks.firstWhereOrNull((t) => t.id == id);
|
|
|
|
if (task != null) {
|
|
_selectedTask = task;
|
|
print("Selected task with ID: ${task.id} and Title: ${task.title}");
|
|
notifyListeners(); // Notify listeners to rebuild UI
|
|
} else {
|
|
final errorMessage =
|
|
"Error: Attempted to select a task with ID: $id that does not exist in the data source.";
|
|
print(errorMessage);
|
|
throw ArgumentError(errorMessage);
|
|
}
|
|
}
|
|
}
|