1
1
import 'dart:convert' ;
2
2
import 'package:auto_gpt_flutter_client/models/task.dart' ;
3
3
import 'package:auto_gpt_flutter_client/models/test_suite.dart' ;
4
+ import 'package:auto_gpt_flutter_client/services/shared_preferences_service.dart' ;
4
5
import 'package:flutter/foundation.dart' ;
5
6
import 'package:collection/collection.dart' ;
6
7
import 'package:auto_gpt_flutter_client/services/task_service.dart' ;
@@ -10,6 +11,7 @@ import 'package:shared_preferences/shared_preferences.dart';
10
11
// TODO: How will all these functions work with test suites?
11
12
class TaskViewModel with ChangeNotifier {
12
13
final TaskService _taskService;
14
+ final SharedPreferencesService _prefsService;
13
15
14
16
List <Task > _tasks = [];
15
17
List <TestSuite > _testSuites = [];
@@ -19,7 +21,7 @@ class TaskViewModel with ChangeNotifier {
19
21
Task ? _selectedTask;
20
22
TestSuite ? _selectedTestSuite;
21
23
22
- TaskViewModel (this ._taskService);
24
+ TaskViewModel (this ._taskService, this ._prefsService );
23
25
24
26
/// Returns the currently selected task.
25
27
Task ? get selectedTask => _selectedTask;
@@ -107,10 +109,9 @@ class TaskViewModel with ChangeNotifier {
107
109
108
110
// Helper method to save test suites to SharedPreferences
109
111
Future <void > _saveTestSuitesToPrefs () async {
110
- final prefs = await SharedPreferences .getInstance ();
111
112
final testSuitesToStore =
112
113
_testSuites.map ((testSuite) => jsonEncode (testSuite.toJson ())).toList ();
113
- prefs .setStringList ('testSuites' , testSuitesToStore);
114
+ await _prefsService .setStringList ('testSuites' , testSuitesToStore);
114
115
}
115
116
116
117
// Adds a new test suite and saves it to SharedPreferences
@@ -123,8 +124,8 @@ class TaskViewModel with ChangeNotifier {
123
124
124
125
// Fetch test suites from SharedPreferences
125
126
Future <void > fetchTestSuites () async {
126
- final prefs = await SharedPreferences . getInstance ();
127
- final storedTestSuites = prefs .getStringList ('testSuites' ) ?? [];
127
+ final storedTestSuites =
128
+ await _prefsService .getStringList ('testSuites' ) ?? [];
128
129
_testSuites = storedTestSuites
129
130
.map ((testSuiteMap) => TestSuite .fromJson (jsonDecode (testSuiteMap)))
130
131
.toList ();
0 commit comments