|  |  | @ -1,4 +1,5 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | import 'package:flutter/material.dart'; |  |  |  | import 'package:flutter/material.dart'; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | import 'package:pi_dashboard/logger.dart'; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | import 'settings_service.dart'; |  |  |  | import 'settings_service.dart'; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
	
		
		
			
				
					|  |  | @ -8,50 +9,48 @@ import 'settings_service.dart'; | 
			
		
	
		
		
			
				
					
					|  |  |  | /// Controllers glue Data Services to Flutter Widgets. The SettingsController |  |  |  | /// Controllers glue Data Services to Flutter Widgets. The SettingsController | 
			
		
	
		
		
			
				
					
					|  |  |  | /// uses the SettingsService to store and retrieve user settings. |  |  |  | /// uses the SettingsService to store and retrieve user settings. | 
			
		
	
		
		
			
				
					
					|  |  |  | class SettingsController with ChangeNotifier { |  |  |  | class SettingsController with ChangeNotifier { | 
			
		
	
		
		
			
				
					
					|  |  |  |   SettingsController( |  |  |  |   SettingsController(this._settingsService); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     this._settingsService, |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   )   : _stringOpts = {}, |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         _boolOpts = {}, |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         _intOpts = {}; |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   final SettingsService _settingsService; |  |  |  |   final SettingsService _settingsService; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   Future<void> loadSettings() async { |  |  |  |   Future<void> loadSettings() async { | 
			
		
	
		
		
			
				
					
					|  |  |  |     _themeMode = await _settingsService.themeMode(); |  |  |  |     _themeMode = await _settingsService.themeMode(); | 
			
		
	
		
		
			
				
					
					|  |  |  |     _stringOpts["protocol"] = await _settingsService.get<String>("protocol"); |  |  |  |     _hostname = await _settingsService.keyStr("hostname"); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     _stringOpts["hostname"] = await _settingsService.get("hostname"); |  |  |  |     _username = await _settingsService.keyStr("username"); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     _intOpts["port"] = await _settingsService.get("port"); |  |  |  |     _password = await _settingsService.keyStr("password"); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     _stringOpts["username"] = await _settingsService.get("username"); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     _stringOpts["password"] = await _settingsService.get("password"); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     _boolOpts["lockApp"] = await _settingsService.get("lockApp"); |  |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |     // Important! Inform listeners a change has occurred. |  |  |  |     // Important! Inform listeners a change has occurred. | 
			
		
	
		
		
			
				
					
					|  |  |  |     notifyListeners(); |  |  |  |     notifyListeners(); | 
			
		
	
		
		
			
				
					
					|  |  |  |   } |  |  |  |   } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   late ThemeMode _themeMode; |  |  |  |   late ThemeMode _themeMode; | 
			
		
	
		
		
			
				
					
					|  |  |  |   ThemeMode get themeMode => _themeMode; |  |  |  |   ThemeMode get themeMode => _themeMode; | 
			
		
	
		
		
			
				
					
					|  |  |  |   final Map<String, String?> _stringOpts; |  |  |  |   late String _hostname; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |   final Map<String, bool?> _boolOpts; |  |  |  |   String get hostname => _hostname; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |   final Map<String, int?> _intOpts; |  |  |  |   late String _username; | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   String get username => _username; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   late String _password; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   String get password => _password; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   Future<void> set<T>(String key, T? value) async { |  |  |  |   Future<void> _setKey(String key, String value) async { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     if (value == null) return; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     await _settingsService.set(key, value); |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |     notifyListeners(); |  |  |  |     notifyListeners(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     await _settingsService.setKeyStr(key, value); | 
			
		
	
		
		
			
				
					
					|  |  |  |   } |  |  |  |   } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   T get<T>(String key, T default_) { |  |  |  |   Future<void> setHostname(String newHostname) async { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     if (T is String) { |  |  |  |     _hostname = newHostname; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       return _stringOpts.containsKey(key) ? _stringOpts[key] as T : default_; |  |  |  |     await _setKey("hostname", hostname); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     } else if (T is bool) { |  |  |  |     Log().debug("Hostname update: $hostname"); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       return _boolOpts.containsKey(key) ? _boolOpts[key] as T : default_; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     } else if (T is int) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       return _intOpts.containsKey(key) ? _intOpts[key] as T : default_; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     } else { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       throw Exception("this type is not implemented"); |  |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |   } |  |  |  |   } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   Future<void> setUsername(String newUsername) async { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     _username = newUsername; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     await _setKey("username", username); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     Log().debug("username update: $username"); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   Future<void> setPassword(String newPassword) async { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     _password = newPassword; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     await _setKey("password", password); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     Log().debug("password update"); | 
			
		
	
		
		
			
				
					
					|  |  |  |   } |  |  |  |   } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   /// Update and persist the ThemeMode based on the user's selection. |  |  |  |   /// Update and persist the ThemeMode based on the user's selection. | 
			
		
	
		
		
			
				
					
					|  |  |  |   Future<void> updateThemeMode(ThemeMode? newThemeMode) async { |  |  |  |   Future<void> updateThemeMode(ThemeMode? newThemeMode) async { | 
			
		
	
		
		
			
				
					
					|  |  |  |     if (newThemeMode == null) return; |  |  |  |     if (newThemeMode == null) return; | 
			
		
	
	
		
		
			
				
					|  |  | 
 |