AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
Update TaskViewModel to Use TaskService and Task API Integration
hunteraraujo5ae17d009b83
diff --git a/lib/viewmodels/task_viewmodel.dart b/lib/viewmodels/task_viewmodel.dartindex 96c68cbf25..e4041c6366 100644--- a/lib/viewmodels/task_viewmodel.dart+++ b/lib/viewmodels/task_viewmodel.dart@@ -1,37 +1,41 @@import 'package:auto_gpt_flutter_client/models/task.dart';-import 'package:auto_gpt_flutter_client/viewmodels/mock_data.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';-// TODO: Update whole class once we have created TaskServiceclass 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.- void createTask(String title) {- // Generate an ID (This is a simplistic approach for mock data)- final id = _tasks.length + 1;- final newTask = Task(id: id, title: title);-+ /// Adds a task and returns its ID.+ Future<String> createTask(String title) async {+ final newTask = TaskRequestBody(input: title);// Add to data source- addTask(newTask);+ 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(newTask);+ _tasks.add(newTaskObject);notifyListeners();++ return newTaskObject.id; // Return the ID of the new task}/// Deletes a task.- void deleteTask(int id) {- // Remove from data source- removeTask(id);+ 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);@@ -39,19 +43,24 @@ class TaskViewModel with ChangeNotifier {}/// Fetches tasks from the data source.- void fetchTasks() {+ void fetchTasks() async {try {- _tasks = mockTasks;- notifyListeners(); // Notify listeners to rebuild UI+ final fetchedTasks = await _taskService.listAllTasks();+ _tasks = fetchedTasks.map((taskMap) {+ return Task(+ id: taskMap['task_id'].toString(),+ title: taskMap['output'].toString(),+ );+ }).toList();+ notifyListeners();print("Tasks fetched successfully!");} catch (error) {print("Error fetching tasks: $error");- // TODO: Handle additional error scenarios or log them as required}}/// Handles the selection of a task by its ID.- void selectTask(int id) {+ void selectTask(String id) {final task = _tasks.firstWhereOrNull((t) => t.id == id);if (task != null) {