diff --git a/lib/data/sources/local/credential_store.dart b/lib/data/sources/local/credential_store.dart index 47030ff..a6ba176 100644 --- a/lib/data/sources/local/credential_store.dart +++ b/lib/data/sources/local/credential_store.dart @@ -9,6 +9,8 @@ class CredentialStore { 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(); @@ -46,6 +48,24 @@ class CredentialStore { return ServerConfig(host: host, port: port); } + Future 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 clear() => Future.wait([ _storage.delete(key: _usernameKey), _storage.delete(key: _passwordKey), diff --git a/lib/providers.dart b/lib/providers.dart index 48b3ac6..512aa4b 100644 --- a/lib/providers.dart +++ b/lib/providers.dart @@ -44,6 +44,12 @@ final credentialStoreProvider = Provider((ref) { 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 // ---------------------------------------------------------------------------