From ef8877ccffe0ebcc8f9f1c0bb6db372fa0886c2a Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 8 Mar 2026 16:49:16 +0100 Subject: Add manual disconnect This allows going back to the connection page and to choose a different server. Furthermore it might be useful to force a new connection attempt while auto-reconnect might be in a delay state. --- lib/data.dart | 13 ++++++++++++- lib/ui.dart | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/data.dart b/lib/data.dart index 89d8760..415e61e 100644 --- a/lib/data.dart +++ b/lib/data.dart @@ -21,11 +21,14 @@ class AppState with ChangeNotifier { MachineState.init: { MachineEvent.connect: MachineState.disconnected, }, + MachineState.disconnected: { + MachineEvent.disconnect: MachineState.init, + }, }; MachineState state = MachineState.init; Map contacts = {}; - late final MqttServerClient _client; + late MqttServerClient _client; bool _brokerConnected = false; bool get brokerConnected => _brokerConnected; bool _serverConnected = false; @@ -54,6 +57,10 @@ class AppState with ChangeNotifier { _initMqtt(); } + if (nextState == MachineState.init) { + _deinitMqtt(); + } + state = nextState; notifyListeners(); } @@ -91,6 +98,10 @@ class AppState with ChangeNotifier { _client.updates?.listen(_onMessage); } + void _deinitMqtt() { + _client.disconnect(); + } + void _onConnected() { _client.subscribe('$topicPrefix/contact/+/state', MqttQos.exactlyOnce); _client.subscribe('$topicPrefix/server/health', MqttQos.exactlyOnce); diff --git a/lib/ui.dart b/lib/ui.dart index 1d2ad53..e14895b 100644 --- a/lib/ui.dart +++ b/lib/ui.dart @@ -113,7 +113,7 @@ class ConnectionStatus extends StatelessWidget { } MachineEvent event = MachineEvent.disconnect; - String action = '(disabled)'; + String action = 'disconnect'; if (state.state == MachineState.init) { event = MachineEvent.connect; action = 'connect'; -- cgit v1.3