mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-27 19:04:25 +01:00
This commit introduces the TaskViewModel, which manages the business logic for tasks. The ViewModel interacts with a mock data source, providing functionalities like fetching tasks, selecting a task, creating, and deleting tasks. Additionally, comprehensive tests for TaskViewModel have been added to ensure its behavior aligns with expectations. The mock data source has also been updated to support the new functionalities. Key Features: - Task management in TaskViewModel. - Tests for each major functionality in TaskViewModel. - Mock data source to simulate data interactions.
69 lines
2.0 KiB
Dart
69 lines
2.0 KiB
Dart
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';
|
|
|
|
// TODO: Update whole class once we have created TaskService
|
|
class TaskViewModel with ChangeNotifier {
|
|
List<Task> _tasks = [];
|
|
Task? _selectedTask; // This will store the currently selected task
|
|
|
|
/// 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);
|
|
|
|
// Add to data source
|
|
addTask(newTask);
|
|
|
|
// Update local tasks list and notify listeners
|
|
_tasks.add(newTask);
|
|
notifyListeners();
|
|
}
|
|
|
|
/// Deletes a task.
|
|
void deleteTask(int id) {
|
|
// Remove from data source
|
|
removeTask(id);
|
|
|
|
// Update local tasks list and notify listeners
|
|
_tasks.removeWhere((task) => task.id == id);
|
|
notifyListeners();
|
|
}
|
|
|
|
/// Fetches tasks from the data source.
|
|
void fetchTasks() {
|
|
try {
|
|
_tasks = mockTasks;
|
|
notifyListeners(); // Notify listeners to rebuild UI
|
|
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) {
|
|
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);
|
|
}
|
|
}
|
|
}
|