Compare commits

..

1 Commits

Author SHA1 Message Date
Felix Bruns 0adde37cf1 Prevent VSCode alert
5 months ago

@ -0,0 +1,3 @@
{
"cmake.ignoreCMakeListsMissing": true
}

@ -44,27 +44,10 @@ class _ProxmoxListerState extends State<ProxmoxListerView> {
Future<ProxmoxNodeMap> getVms() async { Future<ProxmoxNodeMap> getVms() async {
await settings.loadSettings(); await settings.loadSettings();
final protocol = settings.get("protocol", 'https://');
final hostname = settings.get("hostname", "");
final username = settings.get("username", "");
final password = settings.get("password", "");
var port = settings.get("port", 0);
if (hostname.isEmpty) return {};
if (port == 0) {
if (settings.get("protocol", 'https://') == 'https://') {
port = 443;
} else {
port = 80;
}
}
_service = ProxmoxWebService( _service = ProxmoxWebService(
protocol: protocol, hostname: settings.hostname,
hostname: hostname, username: settings.username,
port: port, password: settings.password,
username: username,
password: password,
); );
final success = await _service.authenticate(); final success = await _service.authenticate();
if (!success) { if (!success) {

@ -6,16 +6,12 @@ import 'model.dart';
class ProxmoxWebService { class ProxmoxWebService {
ProxmoxWebService({ ProxmoxWebService({
required this.protocol, this.hostname = "",
required this.hostname, this.username = "",
required this.port, this.password = "",
required this.username,
required this.password,
}); });
final String protocol;
final String hostname; final String hostname;
final int port;
final String username; final String username;
final String password; final String password;

@ -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;

@ -22,30 +22,14 @@ class SettingsService {
prefs.setInt("theme", theme.index); prefs.setInt("theme", theme.index);
} }
Future<T?> get<T>(String key) async { Future<String> keyStr(String key) async {
final SharedPreferences prefs = await SharedPreferences.getInstance(); final SharedPreferences prefs = await SharedPreferences.getInstance();
T? res; final res = prefs.getString(key);
if (res == null) return "";
if (T == String) {
res = prefs.getString(key) as T?;
} else if (T == bool) {
res = prefs.getBool(key) as T?;
} else if (T == int) {
res = prefs.getInt(key) as T?;
} else {
res = null as T?;
}
return res; return res;
} }
Future<void> setKeyStr(String key, String value) async {
Future<void> set<T>(String k, T v) async {
final SharedPreferences prefs = await SharedPreferences.getInstance(); final SharedPreferences prefs = await SharedPreferences.getInstance();
if (T is String) { await prefs.setString(key, value);
await prefs.setString(k, v as String);
} else if (T is bool) {
await prefs.setBool(k, v as bool);
} else if (T is int) {
await prefs.setInt(k, v as int);
}
} }
} }

@ -23,17 +23,7 @@ class SettingsView extends StatelessWidget {
padding: const EdgeInsets.all(16), padding: const EdgeInsets.all(16),
child: Column( child: Column(
children: [ children: [
themeSelector(), DropdownButton<ThemeMode>(
...hostSelector(),
...loginForm(),
],
),
),
);
}
Widget themeSelector() {
return DropdownButton<ThemeMode>(
value: controller.themeMode, value: controller.themeMode,
onChanged: controller.updateThemeMode, onChanged: controller.updateThemeMode,
items: const [ items: const [
@ -50,54 +40,22 @@ class SettingsView extends StatelessWidget {
child: Text('Dark Theme'), child: Text('Dark Theme'),
) )
], ],
);
}
List<Widget> hostSelector() {
return [
DropdownButton<String?>(
value: controller.get('protocol', 'https://'),
onChanged: (v) => controller.set('protocol', v),
items: const [
DropdownMenuItem(
value: 'https://',
child: Text('https://'),
),
DropdownMenuItem(
value: 'http://',
child: Text('http://'),
)
],
), ),
TextFormField( TextFormField(
decoration: const InputDecoration( decoration: const InputDecoration(
border: UnderlineInputBorder(), border: UnderlineInputBorder(),
labelText: "Hostname", labelText: "Hostname",
), ),
initialValue: controller.get('hostname', ''), initialValue: controller.hostname,
onChanged: (v) => controller.set('hostname', v), onChanged: controller.setHostname,
), ),
TextFormField(
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: "Port",
),
keyboardType: TextInputType.number,
initialValue: controller.get("port", 0).toString(),
onChanged: (v) => controller.set('port', int.parse(v)),
),
];
}
List<Widget> loginForm() {
return [
TextFormField( TextFormField(
decoration: const InputDecoration( decoration: const InputDecoration(
border: UnderlineInputBorder(), border: UnderlineInputBorder(),
labelText: "Username", labelText: "Username",
), ),
initialValue: controller.get('username', ''), initialValue: controller.username,
onChanged: (v) => controller.set('username', v), onChanged: controller.setUsername,
), ),
TextFormField( TextFormField(
decoration: const InputDecoration( decoration: const InputDecoration(
@ -107,9 +65,12 @@ class SettingsView extends StatelessWidget {
obscureText: true, obscureText: true,
enableSuggestions: false, enableSuggestions: false,
autocorrect: false, autocorrect: false,
initialValue: controller.get('password', ''), initialValue: controller.password,
onChanged: (v) => controller.set('password', v), onChanged: controller.setPassword,
),
],
),
), ),
]; );
} }
} }

Loading…
Cancel
Save