summaryrefslogtreecommitdiff
path: root/lib/ui.dart
blob: a191f5f2563dda4606289dbfbe4aa006cc3ad3f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'data.dart';

class Sia extends StatelessWidget {
  const Sia({super.key});

  @override
  Widget build(BuildContext context) {
    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 StatelessWidget {
  const ConnectionPage({super.key});

  @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(
              decoration: const InputDecoration(
                labelText: "Server name",
                hintText: "iot.example.org",
                border: OutlineInputBorder(),
              ),
              onChanged: (String value) {
                state.fqdn = value;
              },
            ),
          ),
          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});

  @override
  Widget build(BuildContext context) {
    return  Consumer<AppState>(
      builder: (BuildContext context, AppState state, Widget? child) {
        return ListView.builder(
          itemCount: state.contacts.length,
          itemBuilder: (BuildContext context, int index) {
            MapEntry<String, bool> data = state.contacts.entries.elementAt(index);
            String address = data.key;
            bool isOpen = data.value;
            return ListTile(
              leading: Icon(
                isOpen ? Icons.meeting_room : Icons.door_front_door,
                color: isOpen ? Colors.red : Colors.green,
              ),
              title: Text(address),
            );
          },
        );
      },
    );
  }
}

class ConnectionStatus extends StatelessWidget {
  const ConnectionStatus({super.key});

  @override
  Widget build(BuildContext context) {
    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');

          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');
          }

          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),
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}