From 2e15dd323b7c3198e2af10f35f1186d91cc6cfaf Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 7 Mar 2026 13:03:24 +0100 Subject: Label button with '(dis)connect' This does not yet work but the correct text based on the page is displayed. This is based on a state machine based implementation suitable to implement the state handling cleanly. --- lib/data.dart | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) (limited to 'lib/data.dart') diff --git a/lib/data.dart b/lib/data.dart index e660689..53c3d01 100644 --- a/lib/data.dart +++ b/lib/data.dart @@ -8,8 +8,27 @@ const String brokerHostname = 'sia.xengineering.eu'; const int brokerPort = 1883; const String topicPrefix = 'sia'; +enum MachineState { + init, // user does not want connection MQTT not connected + disconnected, // user wants connection but MQTT not (yet) connected +} + +enum MachineEvent { + connect, // user wants to connect + disconnect, // user does not want an connection anymore +} + class AppState with ChangeNotifier { - bool onConnectionPage = true; + static const Map> machine = > { + MachineState.init: { + MachineEvent.connect: MachineState.disconnected, + }, + MachineState.disconnected: { + MachineEvent.disconnect: MachineState.init, + }, + }; + MachineState state = MachineState.init; + Map contacts = {}; late final MqttServerClient _client; bool _brokerConnected = false; @@ -21,6 +40,21 @@ class AppState with ChangeNotifier { _initMqtt(); } + void process(MachineEvent event) { + Map? ruleset = machine[state]; + if (ruleset == null) { + return; + } + + MachineState? nextMachineState = ruleset[event]; + if (nextMachineState == null) { + return; + } + + state = nextMachineState; + notifyListeners(); + } + @override void dispose() { _client.disconnect(); @@ -123,9 +157,4 @@ class AppState with ChangeNotifier { return null; } } - - void togglePage() { - onConnectionPage = !onConnectionPage; - notifyListeners(); - } } -- cgit v1.3