feat: persist MQTT broker settings in store
This commit is contained in:
@@ -9,6 +9,8 @@ class CredentialStore {
|
|||||||
static const _passwordKey = 'localiserd_password';
|
static const _passwordKey = 'localiserd_password';
|
||||||
static const _hostKey = 'localiserd_host';
|
static const _hostKey = 'localiserd_host';
|
||||||
static const _portKey = 'localiserd_port';
|
static const _portKey = 'localiserd_port';
|
||||||
|
static const _mqttHostKey = 'mqtt_host';
|
||||||
|
static const _mqttPortKey = 'mqtt_port';
|
||||||
|
|
||||||
final _storage = const FlutterSecureStorage();
|
final _storage = const FlutterSecureStorage();
|
||||||
|
|
||||||
@@ -46,6 +48,24 @@ class CredentialStore {
|
|||||||
return ServerConfig(host: host, port: port);
|
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([
|
Future<void> clear() => Future.wait([
|
||||||
_storage.delete(key: _usernameKey),
|
_storage.delete(key: _usernameKey),
|
||||||
_storage.delete(key: _passwordKey),
|
_storage.delete(key: _passwordKey),
|
||||||
|
|||||||
@@ -44,6 +44,12 @@ final credentialStoreProvider = Provider<CredentialStore>((ref) {
|
|||||||
return CredentialStore();
|
return CredentialStore();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/// MQTT broker settings stored locally on the device.
|
||||||
|
/// Null if not yet configured. Set via [CredentialStore.saveMqttBroker].
|
||||||
|
final mqttBrokerProvider = FutureProvider<({String host, int port})?>(
|
||||||
|
(ref) => ref.watch(credentialStoreProvider).loadMqttBroker(),
|
||||||
|
);
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Feature clients — throw if required state is missing
|
// Feature clients — throw if required state is missing
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user