feat: save and offer Wi-Fi credentials when adding new sensors
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
|
||||
import '../../../domain/models/server_config.dart';
|
||||
|
||||
typedef Credentials = ({String username, String password});
|
||||
typedef WifiCredential = ({String ssid, String password});
|
||||
|
||||
class CredentialStore {
|
||||
static const _usernameKey = 'localiserd_username';
|
||||
@@ -11,6 +14,7 @@ class CredentialStore {
|
||||
static const _portKey = 'localiserd_port';
|
||||
static const _mqttHostKey = 'mqtt_host';
|
||||
static const _mqttPortKey = 'mqtt_port';
|
||||
static const _wifiCredentialsKey = 'wifi_credentials';
|
||||
|
||||
final _storage = const FlutterSecureStorage();
|
||||
|
||||
@@ -66,6 +70,36 @@ class CredentialStore {
|
||||
return (host: host, port: port);
|
||||
}
|
||||
|
||||
Future<List<WifiCredential>> loadWifiCredentials() async {
|
||||
final raw = await _storage.read(key: _wifiCredentialsKey);
|
||||
if (raw == null) return [];
|
||||
try {
|
||||
final list = jsonDecode(raw) as List<dynamic>;
|
||||
return list
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map((m) => (
|
||||
ssid: m['ssid'] as String? ?? '',
|
||||
password: m['password'] as String? ?? '',
|
||||
))
|
||||
.where((c) => c.ssid.isNotEmpty)
|
||||
.toList();
|
||||
} catch (_) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> saveWifiCredential(WifiCredential credential) async {
|
||||
final existing = await loadWifiCredentials();
|
||||
final updated = [
|
||||
credential,
|
||||
...existing.where((c) => c.ssid != credential.ssid),
|
||||
];
|
||||
final json = jsonEncode(
|
||||
updated.map((c) => {'ssid': c.ssid, 'password': c.password}).toList(),
|
||||
);
|
||||
await _storage.write(key: _wifiCredentialsKey, value: json);
|
||||
}
|
||||
|
||||
Future<void> clear() => Future.wait([
|
||||
_storage.delete(key: _usernameKey),
|
||||
_storage.delete(key: _passwordKey),
|
||||
|
||||
Reference in New Issue
Block a user