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.
Refactor SettingsViewModel to use SharedPreferencesService
hunteraraujo8972132eb0a4
diff --git a/frontend/lib/main.dart b/frontend/lib/main.dartindex 1e666bca46..2bf6346933 100644--- a/frontend/lib/main.dart+++ b/frontend/lib/main.dart@@ -59,11 +59,14 @@ void main() async {update: (context, restApiUtility, leaderboardService) =>LeaderboardService(restApiUtility),),- ChangeNotifierProxyProvider<RestApiUtility, SettingsViewModel>(+ ChangeNotifierProxyProvider2<RestApiUtility, SharedPreferencesService,+ SettingsViewModel>(create: (context) => SettingsViewModel(- Provider.of<RestApiUtility>(context, listen: false)),- update: (context, restApiUtility, settingsViewModel) =>- SettingsViewModel(restApiUtility),+ Provider.of<RestApiUtility>(context, listen: false),+ Provider.of<SharedPreferencesService>(context, listen: false),+ ),+ update: (context, restApiUtility, prefsService, settingsViewModel) =>+ SettingsViewModel(restApiUtility, prefsService),),],child: MyApp(),diff --git a/frontend/lib/services/task_service.dart b/frontend/lib/services/task_service.dartindex 5e921f38ea..bb55834865 100644--- a/frontend/lib/services/task_service.dart+++ b/frontend/lib/services/task_service.dart@@ -3,7 +3,6 @@ import 'package:auto_gpt_flutter_client/models/task_request_body.dart';import 'package:auto_gpt_flutter_client/models/task_response.dart';import 'package:auto_gpt_flutter_client/services/shared_preferences_service.dart';import 'package:auto_gpt_flutter_client/utils/rest_api_utility.dart';-import 'package:shared_preferences/shared_preferences.dart';/// Service class for performing task-related operations.class TaskService {diff --git a/frontend/lib/viewmodels/settings_viewmodel.dart b/frontend/lib/viewmodels/settings_viewmodel.dartindex 74b55f7e6b..eb7f246a34 100644--- a/frontend/lib/viewmodels/settings_viewmodel.dart+++ b/frontend/lib/viewmodels/settings_viewmodel.dart@@ -1,7 +1,7 @@import 'package:auto_gpt_flutter_client/services/auth_service.dart';+import 'package:auto_gpt_flutter_client/services/shared_preferences_service.dart';import 'package:auto_gpt_flutter_client/utils/rest_api_utility.dart';import 'package:flutter/material.dart';-import 'package:shared_preferences/shared_preferences.dart';/// [SettingsViewModel] is responsible for managing the state and logic/// for the [SettingsView]. It extends [ChangeNotifier] to provide@@ -13,6 +13,7 @@ class SettingsViewModel extends ChangeNotifier {int _continuousModeSteps = 1; // State for Continuous Mode Stepsfinal RestApiUtility _restApiUtility;+ final SharedPreferencesService _prefsService;// Getters to access the private state variablesbool get isDarkModeEnabled => _isDarkModeEnabled;@@ -22,77 +23,63 @@ class SettingsViewModel extends ChangeNotifier {final AuthService _authService = AuthService();- SettingsViewModel(this._restApiUtility) {+ SettingsViewModel(this._restApiUtility, this._prefsService) {_loadPreferences();}// Method to load stored preferencesFuture<void> _loadPreferences() async {- final prefs = await SharedPreferences.getInstance();- _isDarkModeEnabled = prefs.getBool('isDarkModeEnabled') ?? false;- _isDeveloperModeEnabled = prefs.getBool('isDeveloperModeEnabled') ?? true;- _baseURL = prefs.getString('baseURL') ?? 'http://127.0.0.1:8000/ap/v1';+ _isDarkModeEnabled =+ await _prefsService.getBool('isDarkModeEnabled') ?? false;+ _isDeveloperModeEnabled =+ await _prefsService.getBool('isDeveloperModeEnabled') ?? true;+ _baseURL = await _prefsService.getString('baseURL') ??+ 'http://127.0.0.1:8000/ap/v1';_restApiUtility.updateBaseURL(_baseURL);- _continuousModeSteps = prefs.getInt('continuousModeSteps') ?? 10;+ _continuousModeSteps =+ await _prefsService.getInt('continuousModeSteps') ?? 10;notifyListeners();}/// Toggles the state of Dark Mode and notifies listeners.- void toggleDarkMode(bool value) {+ Future<void> toggleDarkMode(bool value) async {_isDarkModeEnabled = value;notifyListeners();- _saveBoolPreference('isDarkModeEnabled', value);+ await _prefsService.setBool('isDarkModeEnabled', value);}/// Toggles the state of Developer Mode and notifies listeners.- void toggleDeveloperMode(bool value) {+ Future<void> toggleDeveloperMode(bool value) async {_isDeveloperModeEnabled = value;notifyListeners();- _saveBoolPreference('isDeveloperModeEnabled', value);+ await _prefsService.setBool('isDeveloperModeEnabled', value);}/// Updates the state of Base URL, notifies listeners, and updates the RestApiUtility baseURL.- void updateBaseURL(String value) {+ Future<void> updateBaseURL(String value) async {_baseURL = value;notifyListeners();- _saveStringPreference('baseURL', value);+ await _prefsService.setString('baseURL', value);_restApiUtility.updateBaseURL(value);}/// Increments the number of Continuous Mode Steps and notifies listeners.- void incrementContinuousModeSteps() {+ Future<void> incrementContinuousModeSteps() async {_continuousModeSteps += 1;notifyListeners();- _saveIntPreference('continuousModeSteps', _continuousModeSteps);+ await _prefsService.setInt('continuousModeSteps', _continuousModeSteps);}/// Decrements the number of Continuous Mode Steps and notifies listeners.- void decrementContinuousModeSteps() {+ Future<void> decrementContinuousModeSteps() async {if (_continuousModeSteps > 1) {// Ensure that the number of steps is at least 1_continuousModeSteps -= 1;notifyListeners();- _saveIntPreference('continuousModeSteps', _continuousModeSteps);+ await _prefsService.setInt('continuousModeSteps', _continuousModeSteps);}}- // TODO: Create a service for interacting with shared preferences- // Helper methods to save preferences- Future<void> _saveBoolPreference(String key, bool value) async {- final prefs = await SharedPreferences.getInstance();- prefs.setBool(key, value);- }-- Future<void> _saveStringPreference(String key, String value) async {- final prefs = await SharedPreferences.getInstance();- prefs.setString(key, value);- }-- Future<void> _saveIntPreference(String key, int value) async {- final prefs = await SharedPreferences.getInstance();- prefs.setInt(key, value);- }-// Method to sign outFuture<void> signOut() async {await _authService.signOut();