summaryrefslogtreecommitdiff
path: root/lib/ui.dart
blob: 38ace9af0de4588613afc3ac3564f109ecccd243 (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
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'data.dart';

class UI extends StatelessWidget {
  const UI({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 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  Consumer<AppState>(
      builder: (BuildContext context, AppState state, Widget? child) {
        Icon icon;
        Text text;
        if (state.brokerConnected) {
          icon = const Icon(Icons.cloud, color: Colors.green);
          text = const Text('Connected');
        } else {
          icon = const Icon(Icons.cloud_off, color: Colors.red);
          text = const Text('Disconnected');
        }

        return ListTile(leading: icon, title: text);
      },
    );
  }
}