summaryrefslogtreecommitdiff
path: root/lib/ui.dart
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2026-03-10 17:56:51 +0100
committerxengineering <me@xengineering.eu>2026-03-10 17:56:51 +0100
commit1809a88c679fcd17f29c13ddd47732bb65db96b2 (patch)
tree70bf3b6fa5b5709c583756e2bca8e3844e47bf67 /lib/ui.dart
parentec394bd20b3c9325130596d63c5268587b159027 (diff)
parent0f32a2c2aab78b882fff10a6b81ddd271bcb17b0 (diff)
downloadsia-app-1809a88c679fcd17f29c13ddd47732bb65db96b2.tar
sia-app-1809a88c679fcd17f29c13ddd47732bb65db96b2.tar.zst
sia-app-1809a88c679fcd17f29c13ddd47732bb65db96b2.zip
Merge MQTT broker selectionHEADmain
This allows the user to select the MQTT broker and thus Sia server instance which should be used.
Diffstat (limited to 'lib/ui.dart')
-rw-r--r--lib/ui.dart153
1 files changed, 125 insertions, 28 deletions
diff --git a/lib/ui.dart b/lib/ui.dart
index b9f82d0..9603562 100644
--- a/lib/ui.dart
+++ b/lib/ui.dart
@@ -3,27 +3,96 @@ import 'package:provider/provider.dart';
import 'data.dart';
-class UI extends StatelessWidget {
- const UI({super.key});
+class Sia extends StatelessWidget {
+ const Sia({super.key});
@override
Widget build(BuildContext context) {
- return MaterialApp(
- home: Scaffold(
- appBar: AppBar(title: const Text("Contacts")),
- body: const Column(
- children: <Widget>[
- Expanded(child: ContactList()),
- ],
- ),
- bottomNavigationBar: const SafeArea(
- child: ConnectionStatus(),
+ return ChangeNotifierProvider<AppState>(
+ create: (BuildContext context) => AppState(),
+ child: MaterialApp(
+ home: Consumer<AppState>(
+ builder: (_, AppState provider, _) {
+ if (provider.state == MachineState.init) {
+ return const ConnectionPage();
+ }
+ return const DevicesPage();
+ }
),
),
);
}
}
+class ConnectionPage extends StatefulWidget {
+ const ConnectionPage({super.key});
+
+ @override
+ State<ConnectionPage> createState() => _ConnectionPageState();
+}
+
+class _ConnectionPageState extends State<ConnectionPage> {
+ late TextEditingController controller;
+
+ @override
+ void initState() {
+ super.initState();
+
+ final AppState provider = context.read<AppState>();
+ controller = TextEditingController(text: provider.fqdn);
+
+ controller.addListener(() {
+ provider.fqdn = controller.text;
+ });
+ }
+
+ @override
+ void dispose() {
+ controller.dispose();
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return Consumer<AppState>(
+ builder: (_, AppState state, _) {
+ return Scaffold(
+ appBar: AppBar(title: const Text("Connection")),
+ body: Padding(
+ padding: const EdgeInsets.all(16.0),
+ child: TextField(
+ controller: controller,
+ decoration: const InputDecoration(
+ labelText: "Server name",
+ hintText: "iot.example.org",
+ border: OutlineInputBorder(),
+ ),
+ ),
+ ),
+ bottomNavigationBar: const ConnectionStatus(),
+ );
+ }
+ );
+ }
+}
+
+class DevicesPage extends StatelessWidget {
+ const DevicesPage({super.key});
+
+ @override
+ Widget build(BuildContext context) {
+ return Scaffold(
+ appBar: AppBar(title: const Text("Contacts")),
+ body: const Column(
+ children: <Widget>[
+ Expanded(child: ContactList()),
+ ],
+ ),
+ bottomNavigationBar: const ConnectionStatus(),
+ );
+ }
+}
+
class ContactList extends StatelessWidget {
const ContactList({super.key});
@@ -56,24 +125,52 @@ class ConnectionStatus extends StatelessWidget {
@override
Widget build(BuildContext context) {
- return Consumer<AppState>(
- builder: (BuildContext context, AppState state, Widget? child) {
- Icon icon;
- Text text;
+ return SafeArea(
+ child: Consumer<AppState>(
+ builder: (BuildContext context, AppState state, Widget? child) {
+ Icon icon = const Icon(Icons.cloud_off, color: Colors.grey);
+ Text text = const Text('Disconnected');
- if (state.brokerConnected && state.serverConnected) {
- icon = const Icon(Icons.cloud, color: Colors.green);
- text = const Text('Connected');
- } else if (state.brokerConnected && !state.serverConnected) {
- icon = const Icon(Icons.cloud_off, color: Colors.orange);
- text = const Text('Connection issue');
- } else {
- icon = const Icon(Icons.cloud_off, color: Colors.red);
- text = const Text('Disconnected');
- }
+ switch (state.state) {
+ case MachineState.init:
+ icon = const Icon(Icons.cloud_off, color: Colors.grey);
+ text = const Text('Off');
+ case MachineState.disconnected:
+ icon = const Icon(Icons.cloud_off, color: Colors.red);
+ text = const Text('Disconnected');
+ case MachineState.unreachable:
+ icon = const Icon(Icons.cloud_off, color: Colors.orange);
+ text = const Text('Unreachable');
+ case MachineState.reachable:
+ icon = const Icon(Icons.cloud, color: Colors.green);
+ text = const Text('Connected');
+ }
- return ListTile(leading: icon, title: text);
- },
+ MachineEvent event = MachineEvent.disconnect;
+ String action = 'disconnect';
+ if (state.state == MachineState.init) {
+ event = MachineEvent.connect;
+ action = 'connect';
+ }
+
+ return Row(
+ children: <Widget>[
+ Expanded(
+ child: ListTile(
+ leading: icon,
+ title: text,
+ trailing: ElevatedButton(
+ onPressed: () {
+ state.process(event);
+ },
+ child: Text(action),
+ ),
+ ),
+ ),
+ ],
+ );
+ },
+ ),
);
}
}