Skip to content

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.

Extend TaskViewModel to Support Test Suites and Combined Data Sources

hunteraraujo93094c72238b
diff --git a/frontend/lib/viewmodels/task_viewmodel.dart b/frontend/lib/viewmodels/task_viewmodel.dart
index 42abbab3a0..bd3511042d 100644
--- a/frontend/lib/viewmodels/task_viewmodel.dart
+++ b/frontend/lib/viewmodels/task_viewmodel.dart
@@ -1,22 +1,29 @@
+import 'dart:convert';
import 'package:auto_gpt_flutter_client/models/task.dart';
import 'package:auto_gpt_flutter_client/models/task_response.dart';
+import 'package:auto_gpt_flutter_client/models/test_suite.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';
+import 'package:shared_preferences/shared_preferences.dart';
+// TODO: How will all these functions work with test suites?
class TaskViewModel with ChangeNotifier {
final TaskService _taskService;
+
List<Task> _tasks = [];
- Task? _selectedTask; // This will store the currently selected task
+ List<TestSuite> _testSuites = [];
+ List<dynamic> combinedDataSource = [];
- TaskViewModel(this._taskService);
+ Task? _selectedTask;
+ TestSuite? _selectedTestSuite;
- /// Returns the list of tasks.
- List<Task> get tasks => _tasks;
+ TaskViewModel(this._taskService);
/// Returns the currently selected task.
Task? get selectedTask => _selectedTask;
+ TestSuite? get selectedTestSuite => _selectedTestSuite;
/// Adds a task and returns its ID.
Future<String> createTask(String title) async {
@@ -37,13 +44,13 @@ class TaskViewModel with ChangeNotifier {
/// Deletes a task.
void deleteTask(String taskId) {
_taskService.saveDeletedTask(taskId);
- tasks.removeWhere((task) => task.id == taskId);
+ _tasks.removeWhere((task) => task.id == taskId);
notifyListeners();
print("Task $taskId deleted successfully!");
}
/// Fetches tasks from the data source.
- void fetchTasks() async {
+ Future<void> fetchTasks() async {
try {
final TaskResponse tasksResponse = await _taskService.listAllTasks();
final tasksFromApi = tasksResponse.tasks;
@@ -80,4 +87,106 @@ class TaskViewModel with ChangeNotifier {
print("Deselected the current task.");
notifyListeners(); // Notify listeners to rebuild UI
}
+
+ void selectTestSuite(TestSuite testSuite) {
+ _selectedTestSuite = testSuite;
+ notifyListeners();
+ }
+
+ void deselectTestSuite() {
+ _selectedTestSuite = null;
+ notifyListeners();
+ }
+
+ // Helper method to save test suites to SharedPreferences
+ Future<void> _saveTestSuitesToPrefs() async {
+ final prefs = await SharedPreferences.getInstance();
+ final testSuitesToStore =
+ _testSuites.map((testSuite) => jsonEncode(testSuite.toJson())).toList();
+ prefs.setStringList('testSuites', testSuitesToStore);
+ }
+
+ // Adds a new test suite and saves it to SharedPreferences
+ void addTestSuite(TestSuite testSuite) async {
+ _testSuites.add(testSuite);
+ await _saveTestSuitesToPrefs();
+ notifyListeners();
+ print("Test suite successfully added!");
+ }
+
+ // Fetch test suites from SharedPreferences
+ Future<void> fetchTestSuites() async {
+ final prefs = await SharedPreferences.getInstance();
+ final storedTestSuites = prefs.getStringList('testSuites') ?? [];
+ _testSuites = storedTestSuites
+ .map((testSuiteMap) => TestSuite.fromJson(jsonDecode(testSuiteMap)))
+ .toList();
+ notifyListeners();
+ }
+
+ // The fetchAndCombineData method performs several tasks:
+ // 1. It fetches the tasks and filters out deleted ones.
+ // 2. It fetches the test suites from SharedPreferences.
+ // 3. It combines both the tasks and test suites into a single data source according to specified logic.
+ Future<void> fetchAndCombineData() async {
+ // Step 1: Fetch tasks from the data source
+ // This will populate the _tasks list with tasks fetched from the backend.
+ await fetchTasks();
+
+ // Step 2: Fetch test suites from SharedPreferences
+ // This will populate the _testSuites list with test suites fetched from SharedPreferences.
+ await fetchTestSuites();
+
+ // Step 3: Combine into a shared data source
+ // Create a map to hold test suites by their timestamp.
+ Map<String, TestSuite> testSuiteMap = {};
+
+ // Clear the existing combined data source to start fresh.
+ combinedDataSource.clear();
+
+ // Iterate through each task to check if it's contained in any of the test suites.
+ for (var task in _tasks) {
+ bool found = false;
+
+ // Iterate through each test suite.
+ for (var testSuite in _testSuites) {
+ // Check if the current task is contained in the current test suite.
+ if (testSuite.tests.contains(task)) {
+ found = true;
+
+ // If this test suite is already in the map, add this task to its list of tasks.
+ if (testSuiteMap.containsKey(testSuite.timestamp)) {
+ testSuiteMap[testSuite.timestamp]!.tests.add(task);
+
+ // Find and replace the test suite in the combined data source.
+ final index = combinedDataSource.indexWhere((item) =>
+ item is TestSuite && item.timestamp == testSuite.timestamp);
+ if (index != -1) {
+ combinedDataSource[index] = testSuiteMap[testSuite.timestamp]!;
+ }
+ }
+ // If this test suite is not in the map, add it to the map and to the combined data source.
+ else {
+ final newTestSuite = TestSuite(
+ timestamp: testSuite.timestamp,
+ tests: [task],
+ );
+ testSuiteMap[testSuite.timestamp] = newTestSuite;
+ combinedDataSource.add(
+ newTestSuite); // Add the new test suite to the combined data source.
+ }
+ break; // Exit the loop as the task is found in a test suite.
+ }
+ }
+
+ // If the task was not found in any test suite, add it to the combined data source.
+ if (!found) {
+ combinedDataSource.add(task);
+ }
+ }
+
+ // After processing all tasks, call notifyListeners to rebuild the widgets that depend on this data.
+ notifyListeners();
+ print("Combined tasks and test suites successfully!");
+ }
}