76 lines
2.6 KiB
Dart
76 lines
2.6 KiB
Dart
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
|
import '../../../domain/models/server_config.dart';
|
|
|
|
typedef Credentials = ({String username, 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';
|
|
|
|
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<void> clear() => Future.wait([
|
|
_storage.delete(key: _usernameKey),
|
|
_storage.delete(key: _passwordKey),
|
|
_storage.delete(key: _hostKey),
|
|
_storage.delete(key: _portKey),
|
|
]).then((_) {});
|
|
}
|