Update TaskViewModel to Use TaskService and Task API Integration

This commit includes a significant overhaul of the TaskViewModel to use the newly created TaskService and integrate it with the task API. Specifically:

- Removed dependency on mock data for tasks.
- Added real API calls through the TaskService for task operations like creating and fetching tasks.
- Updated createTask to return the ID of the newly created task.
- Updated fetchTasks method to fetch tasks from the API and update the local list.
- Updated selectTask to handle selection based on string IDs.

These changes make the TaskViewModel ready for real-world usage and remove dependencies on mock data.
This commit is contained in:
hunteraraujo
2023-08-31 15:15:24 -07:00
parent 651e112e3d
commit 5ae17d009b

View File

@@ -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 TaskService
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.
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) {