mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-17 14:04:27 +01:00
Refactor SettingsViewModel to use SharedPreferencesService
This commit is contained in:
@@ -59,11 +59,14 @@ void main() async {
|
|||||||
update: (context, restApiUtility, leaderboardService) =>
|
update: (context, restApiUtility, leaderboardService) =>
|
||||||
LeaderboardService(restApiUtility),
|
LeaderboardService(restApiUtility),
|
||||||
),
|
),
|
||||||
ChangeNotifierProxyProvider<RestApiUtility, SettingsViewModel>(
|
ChangeNotifierProxyProvider2<RestApiUtility, SharedPreferencesService,
|
||||||
|
SettingsViewModel>(
|
||||||
create: (context) => SettingsViewModel(
|
create: (context) => SettingsViewModel(
|
||||||
Provider.of<RestApiUtility>(context, listen: false)),
|
Provider.of<RestApiUtility>(context, listen: false),
|
||||||
update: (context, restApiUtility, settingsViewModel) =>
|
Provider.of<SharedPreferencesService>(context, listen: false),
|
||||||
SettingsViewModel(restApiUtility),
|
),
|
||||||
|
update: (context, restApiUtility, prefsService, settingsViewModel) =>
|
||||||
|
SettingsViewModel(restApiUtility, prefsService),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
child: MyApp(),
|
child: MyApp(),
|
||||||
|
|||||||
@@ -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/models/task_response.dart';
|
||||||
import 'package:auto_gpt_flutter_client/services/shared_preferences_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:auto_gpt_flutter_client/utils/rest_api_utility.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
|
|
||||||
/// Service class for performing task-related operations.
|
/// Service class for performing task-related operations.
|
||||||
class TaskService {
|
class TaskService {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import 'package:auto_gpt_flutter_client/services/auth_service.dart';
|
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:auto_gpt_flutter_client/utils/rest_api_utility.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
|
|
||||||
/// [SettingsViewModel] is responsible for managing the state and logic
|
/// [SettingsViewModel] is responsible for managing the state and logic
|
||||||
/// for the [SettingsView]. It extends [ChangeNotifier] to provide
|
/// for the [SettingsView]. It extends [ChangeNotifier] to provide
|
||||||
@@ -13,6 +13,7 @@ class SettingsViewModel extends ChangeNotifier {
|
|||||||
int _continuousModeSteps = 1; // State for Continuous Mode Steps
|
int _continuousModeSteps = 1; // State for Continuous Mode Steps
|
||||||
|
|
||||||
final RestApiUtility _restApiUtility;
|
final RestApiUtility _restApiUtility;
|
||||||
|
final SharedPreferencesService _prefsService;
|
||||||
|
|
||||||
// Getters to access the private state variables
|
// Getters to access the private state variables
|
||||||
bool get isDarkModeEnabled => _isDarkModeEnabled;
|
bool get isDarkModeEnabled => _isDarkModeEnabled;
|
||||||
@@ -22,77 +23,63 @@ class SettingsViewModel extends ChangeNotifier {
|
|||||||
|
|
||||||
final AuthService _authService = AuthService();
|
final AuthService _authService = AuthService();
|
||||||
|
|
||||||
SettingsViewModel(this._restApiUtility) {
|
SettingsViewModel(this._restApiUtility, this._prefsService) {
|
||||||
_loadPreferences();
|
_loadPreferences();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method to load stored preferences
|
// Method to load stored preferences
|
||||||
Future<void> _loadPreferences() async {
|
Future<void> _loadPreferences() async {
|
||||||
final prefs = await SharedPreferences.getInstance();
|
_isDarkModeEnabled =
|
||||||
_isDarkModeEnabled = prefs.getBool('isDarkModeEnabled') ?? false;
|
await _prefsService.getBool('isDarkModeEnabled') ?? false;
|
||||||
_isDeveloperModeEnabled = prefs.getBool('isDeveloperModeEnabled') ?? true;
|
_isDeveloperModeEnabled =
|
||||||
_baseURL = prefs.getString('baseURL') ?? 'http://127.0.0.1:8000/ap/v1';
|
await _prefsService.getBool('isDeveloperModeEnabled') ?? true;
|
||||||
|
_baseURL = await _prefsService.getString('baseURL') ??
|
||||||
|
'http://127.0.0.1:8000/ap/v1';
|
||||||
_restApiUtility.updateBaseURL(_baseURL);
|
_restApiUtility.updateBaseURL(_baseURL);
|
||||||
_continuousModeSteps = prefs.getInt('continuousModeSteps') ?? 10;
|
_continuousModeSteps =
|
||||||
|
await _prefsService.getInt('continuousModeSteps') ?? 10;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Toggles the state of Dark Mode and notifies listeners.
|
/// Toggles the state of Dark Mode and notifies listeners.
|
||||||
void toggleDarkMode(bool value) {
|
Future<void> toggleDarkMode(bool value) async {
|
||||||
_isDarkModeEnabled = value;
|
_isDarkModeEnabled = value;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
_saveBoolPreference('isDarkModeEnabled', value);
|
await _prefsService.setBool('isDarkModeEnabled', value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Toggles the state of Developer Mode and notifies listeners.
|
/// Toggles the state of Developer Mode and notifies listeners.
|
||||||
void toggleDeveloperMode(bool value) {
|
Future<void> toggleDeveloperMode(bool value) async {
|
||||||
_isDeveloperModeEnabled = value;
|
_isDeveloperModeEnabled = value;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
_saveBoolPreference('isDeveloperModeEnabled', value);
|
await _prefsService.setBool('isDeveloperModeEnabled', value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Updates the state of Base URL, notifies listeners, and updates the RestApiUtility baseURL.
|
/// 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;
|
_baseURL = value;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
_saveStringPreference('baseURL', value);
|
await _prefsService.setString('baseURL', value);
|
||||||
_restApiUtility.updateBaseURL(value);
|
_restApiUtility.updateBaseURL(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Increments the number of Continuous Mode Steps and notifies listeners.
|
/// Increments the number of Continuous Mode Steps and notifies listeners.
|
||||||
void incrementContinuousModeSteps() {
|
Future<void> incrementContinuousModeSteps() async {
|
||||||
_continuousModeSteps += 1;
|
_continuousModeSteps += 1;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
_saveIntPreference('continuousModeSteps', _continuousModeSteps);
|
await _prefsService.setInt('continuousModeSteps', _continuousModeSteps);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decrements the number of Continuous Mode Steps and notifies listeners.
|
/// Decrements the number of Continuous Mode Steps and notifies listeners.
|
||||||
void decrementContinuousModeSteps() {
|
Future<void> decrementContinuousModeSteps() async {
|
||||||
if (_continuousModeSteps > 1) {
|
if (_continuousModeSteps > 1) {
|
||||||
// Ensure that the number of steps is at least 1
|
// Ensure that the number of steps is at least 1
|
||||||
_continuousModeSteps -= 1;
|
_continuousModeSteps -= 1;
|
||||||
notifyListeners();
|
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 out
|
// Method to sign out
|
||||||
Future<void> signOut() async {
|
Future<void> signOut() async {
|
||||||
await _authService.signOut();
|
await _authService.signOut();
|
||||||
|
|||||||
Reference in New Issue
Block a user