summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
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),
),
),
),