mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-19 06:54:22 +01:00
Enhance SettingsViewModel with State Persistence and Sign Out
This commit enriches the `SettingsViewModel` by integrating state persistence functionality and a sign-out method, thus ensuring a more robust and user-friendly settings feature. Key Enhancements: 1. **State Persistence**: - Leveraging the `shared_preferences` package, the app now stores and retrieves user preferences related to app settings, ensuring consistency across app restarts. - Preferences like Dark Mode, Developer Mode, Base URL, and Continuous Mode Steps are persistently stored and loaded when the ViewModel is initialized. 2. **Sign Out Method**: - A `signOut` method has been introduced in the ViewModel, utilizing the `AuthService` to facilitate user sign-out processes. - This addition allows for seamless integration of sign-out functionality within the settings interface, granting users the ability to easily terminate sessions. 3. **SettingsView Enhancement**: - The `SettingsView` has been adapted to incorporate a UI element invoking the `signOut` method, enhancing user interaction within the settings environment. This enhancement not only bolsters the resilience and usability of the app’s settings but also lays down a structured approach for potential future additions to user preferences and settings-related functionalities.
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
|
import 'package:auto_gpt_flutter_client/services/auth_service.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
|
||||||
@@ -15,32 +17,48 @@ class SettingsViewModel extends ChangeNotifier {
|
|||||||
String get baseURL => _baseURL;
|
String get baseURL => _baseURL;
|
||||||
int get continuousModeSteps => _continuousModeSteps;
|
int get continuousModeSteps => _continuousModeSteps;
|
||||||
|
|
||||||
|
final AuthService _authService = AuthService();
|
||||||
|
|
||||||
|
SettingsViewModel() {
|
||||||
|
_loadPreferences(); // Load stored preferences when the view model is created
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method to load stored preferences
|
||||||
|
Future<void> _loadPreferences() async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
_isDarkModeEnabled = prefs.getBool('isDarkModeEnabled') ?? false;
|
||||||
|
_isDeveloperModeEnabled = prefs.getBool('isDeveloperModeEnabled') ?? false;
|
||||||
|
_baseURL = prefs.getString('baseURL') ?? '';
|
||||||
|
_continuousModeSteps = prefs.getInt('continuousModeSteps') ?? 10;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
/// Toggles the state of Dark Mode and notifies listeners.
|
/// Toggles the state of Dark Mode and notifies listeners.
|
||||||
void toggleDarkMode(bool value) {
|
void toggleDarkMode(bool value) {
|
||||||
_isDarkModeEnabled = value;
|
_isDarkModeEnabled = value;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
// TODO: Save to local storage or sync with the server
|
_saveBoolPreference('isDarkModeEnabled', value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Toggles the state of Developer Mode and notifies listeners.
|
/// Toggles the state of Developer Mode and notifies listeners.
|
||||||
void toggleDeveloperMode(bool value) {
|
void toggleDeveloperMode(bool value) {
|
||||||
_isDeveloperModeEnabled = value;
|
_isDeveloperModeEnabled = value;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
// TODO: Save to local storage or sync with the server
|
_saveBoolPreference('isDeveloperModeEnabled', value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Updates the state of Base URL and notifies listeners.
|
/// Updates the state of Base URL and notifies listeners.
|
||||||
void updateBaseURL(String value) {
|
void updateBaseURL(String value) {
|
||||||
_baseURL = value;
|
_baseURL = value;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
// TODO: Save to local storage or sync with the server
|
_saveStringPreference('baseURL', value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Increments the number of Continuous Mode Steps and notifies listeners.
|
/// Increments the number of Continuous Mode Steps and notifies listeners.
|
||||||
void incrementContinuousModeSteps() {
|
void incrementContinuousModeSteps() {
|
||||||
_continuousModeSteps += 1;
|
_continuousModeSteps += 1;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
// TODO: Save to local storage or sync with the server
|
_saveIntPreference('continuousModeSteps', _continuousModeSteps);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decrements the number of Continuous Mode Steps and notifies listeners.
|
/// Decrements the number of Continuous Mode Steps and notifies listeners.
|
||||||
@@ -49,7 +67,29 @@ class SettingsViewModel extends ChangeNotifier {
|
|||||||
// 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();
|
||||||
// TODO: Save to local storage or sync with the server
|
_saveIntPreference('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
|
||||||
|
Future<void> signOut() async {
|
||||||
|
await _authService.signOut();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,6 +62,13 @@ class SettingsView extends StatelessWidget {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
ListTile(
|
||||||
|
title: Text('Sign Out'),
|
||||||
|
onTap: () {
|
||||||
|
viewModel.signOut();
|
||||||
|
// Optionally, navigate to a different view or show a message
|
||||||
|
},
|
||||||
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user