110 lines
3.6 KiB
Dart
110 lines
3.6 KiB
Dart
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';
|
|
static const _passwordKey = 'localiserd_password';
|
|
static const _hostKey = 'localiserd_host';
|
|
static const _portKey = 'localiserd_port';
|
|
static const _mqttHostKey = 'mqtt_host';
|
|
static const _mqttPortKey = 'mqtt_port';
|
|
static const _wifiCredentialsKey = 'wifi_credentials';
|
|
|
|
final _storage = const FlutterSecureStorage();
|
|
|
|
Future<void> save(Credentials credentials) => Future.wait([
|
|
_storage.write(key: _usernameKey, value: credentials.username),
|
|
_storage.write(key: _passwordKey, value: credentials.password),
|
|
]).then((_) {});
|
|
|
|
Future<Credentials?> load() async {
|
|
final results = await Future.wait([
|
|
_storage.read(key: _usernameKey),
|
|
_storage.read(key: _passwordKey),
|
|
]);
|
|
final username = results[0];
|
|
final password = results[1];
|
|
if (username == null || password == null) return null;
|
|
return (username: username, password: password);
|
|
}
|
|
|
|
Future<void> saveServer(ServerConfig config) => Future.wait([
|
|
_storage.write(key: _hostKey, value: config.host),
|
|
_storage.write(key: _portKey, value: config.port.toString()),
|
|
]).then((_) {});
|
|
|
|
Future<ServerConfig?> loadServer() async {
|
|
final results = await Future.wait([
|
|
_storage.read(key: _hostKey),
|
|
_storage.read(key: _portKey),
|
|
]);
|
|
final host = results[0];
|
|
final portStr = results[1];
|
|
if (host == null || portStr == null) return null;
|
|
final port = int.tryParse(portStr);
|
|
if (port == null) return null;
|
|
return ServerConfig(host: host, port: port);
|
|
}
|
|
|
|
Future<void> saveMqttBroker(String host, int port) => Future.wait([
|
|
_storage.write(key: _mqttHostKey, value: host),
|
|
_storage.write(key: _mqttPortKey, value: port.toString()),
|
|
]).then((_) {});
|
|
|
|
Future<({String host, int port})?> loadMqttBroker() async {
|
|
final results = await Future.wait([
|
|
_storage.read(key: _mqttHostKey),
|
|
_storage.read(key: _mqttPortKey),
|
|
]);
|
|
final host = results[0];
|
|
final portStr = results[1];
|
|
if (host == null || portStr == null) return null;
|
|
final port = int.tryParse(portStr);
|
|
if (port == null) return null;
|
|
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),
|
|
_storage.delete(key: _hostKey),
|
|
_storage.delete(key: _portKey),
|
|
]).then((_) {});
|
|
}
|