summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2026-03-07 13:03:24 +0100
committerxengineering <me@xengineering.eu>2026-03-07 13:03:24 +0100
commit2e15dd323b7c3198e2af10f35f1186d91cc6cfaf (patch)
treecd632240cb4313db5a304f6493ffc08c9cc269c7 /lib
parenta46cb6e94688a65a31248bc4585bccfbc22242f0 (diff)
downloadsia-app-2e15dd323b7c3198e2af10f35f1186d91cc6cfaf.tar
sia-app-2e15dd323b7c3198e2af10f35f1186d91cc6cfaf.tar.zst
sia-app-2e15dd323b7c3198e2af10f35f1186d91cc6cfaf.zip
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.
Diffstat (limited to 'lib')
-rw-r--r--lib/data.dart41
-rw-r--r--lib/ui.dart15
2 files changed, 47 insertions, 9 deletions
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<MachineState, Map<MachineEvent, MachineState>> machine = <MachineState, Map<MachineEvent, MachineState>> {
+ MachineState.init: <MachineEvent, MachineState> {
+ MachineEvent.connect: MachineState.disconnected,
+ },
+ MachineState.disconnected: <MachineEvent, MachineState> {
+ MachineEvent.disconnect: MachineState.init,
+ },
+ };
+ MachineState state = MachineState.init;
+
Map<String, bool> contacts = <String, bool>{};
late final MqttServerClient _client;
bool _brokerConnected = false;
@@ -21,6 +40,21 @@ class AppState with ChangeNotifier {
_initMqtt();
}
+ void process(MachineEvent event) {
+ Map<MachineEvent, MachineState>? 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();
- }
}
diff --git a/lib/ui.dart b/lib/ui.dart
index 73e532a..e14895b 100644
--- a/lib/ui.dart
+++ b/lib/ui.dart
@@ -13,7 +13,7 @@ class Sia extends StatelessWidget {
child: MaterialApp(
home: Consumer<AppState>(
builder: (_, AppState provider, _) {
- if (provider.onConnectionPage) {
+ if (provider.state == MachineState.init) {
return const ConnectionPage();
}
return const DevicesPage();
@@ -112,6 +112,13 @@ class ConnectionStatus extends StatelessWidget {
text = const Text('Disconnected');
}
+ MachineEvent event = MachineEvent.disconnect;
+ String action = 'disconnect';
+ if (state.state == MachineState.init) {
+ event = MachineEvent.connect;
+ action = 'connect';
+ }
+
return Row(
children: <Widget>[
Expanded(
@@ -119,8 +126,10 @@ class ConnectionStatus extends StatelessWidget {
leading: icon,
title: text,
trailing: ElevatedButton(
- onPressed: state.togglePage,
- child: const Text('switch view'),
+ onPressed: () {
+ state.process(event);
+ },
+ child: Text(action),
),
),
),