-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_list_route.dart
249 lines (234 loc) · 7.6 KB
/
device_list_route.dart
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import 'dart:async';
//import 'dart:developer' as dev;
import 'package:espmui/device_widgets.dart';
import 'package:flutter/material.dart';
// import 'package:flutter_ble_lib/flutter_ble_lib.dart';
//import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
import 'package:page_transition/page_transition.dart';
import 'ble.dart';
import 'device_route.dart';
import 'device.dart';
import 'device_list.dart';
import 'scanner.dart';
//import 'preferences.dart';
import 'util.dart';
import 'debug.dart';
class DeviceListRoute extends StatefulWidget {
const DeviceListRoute({super.key});
@override
DeviceListRouteState createState() => DeviceListRouteState();
}
class DeviceListRouteState extends State<DeviceListRoute> with Debug {
final String defaultTitle = "Devices";
final _key = GlobalKey<DeviceListRouteState>();
Scanner get scanner => Scanner();
DeviceListRouteState() {
logD("construct");
}
@override
void initState() {
super.initState();
logD("initState()");
}
@override
void dispose() {
logD("dispose()");
//scanner.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: BleAdapterCheck(
_appBarTitle(),
ifNotReady: (state) => BleNotReady(state),
),
),
body: Container(
margin: const EdgeInsets.all(6),
child: _list(),
),
);
}
Widget _appBarTitle() {
return Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start, // Align left
children: [
Row(children: [
Text(defaultTitle),
]),
Row(
children: [
_status(),
],
),
],
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.end, // Align right
children: [_scanButton()],
)
],
);
}
Widget _status() {
return StreamBuilder<bool>(
stream: scanner.scanningStream,
initialData: scanner.scanning,
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
String status = "";
if (snapshot.hasData) {
status = snapshot.data! ? "Scanning..." : "${scanner.devices.length} device${scanner.devices.length == 1 ? "" : "s"}";
}
return Text(
status,
style: const TextStyle(fontSize: 10),
);
},
);
}
Widget _scanButton() {
return StreamBuilder<bool>(
stream: scanner.scanningStream,
initialData: scanner.scanning,
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
bool scanning = snapshot.hasData ? snapshot.data! : false;
return EspmuiElevatedButton(onPressed: scanning ? null : scanner.startScan, child: const Text("Scan"));
},
);
}
Widget _list() {
var devices = DeviceList().devices;
return StreamBuilder<Map<String, Device>>(
stream: DeviceList().stream,
initialData: devices,
builder: (BuildContext context, AsyncSnapshot<Map<String, Device>> snapshot) {
//logD("_list() rebuilding");
List<Widget> items = [];
if (devices.isEmpty) {
items.add(const Center(child: Text("No devices")));
} else {
List<Device> sorted = devices.values.toList(growable: false);
sorted.sort((a, b) {
// if (a.autoConnect.value == b.autoConnect.value) return a.name?.compareTo(b.name ?? "") ?? -1;
// return a.autoConnect.value ? 1 : -1;
return a.name.compareTo(b.name);
});
for (var device in sorted) {
//logD("_list() adding ${device.runtimeType} ${device.name} ${device.lastScanRssi}");
items.add(_listItem(device));
}
}
return RefreshIndicator(
key: _key,
onRefresh: () {
scanner.startScan();
return Future.value(null);
},
child: ListView(
physics: const AlwaysScrollableScrollPhysics(),
children: items,
),
);
},
);
}
Widget _listItem(Device device) {
return InkWell(
onTap: () {
openDevice(device);
},
child: Container(
padding: const EdgeInsets.all(10),
margin: const EdgeInsets.fromLTRB(0, 0, 0, 6),
decoration: const BoxDecoration(
color: Colors.black38,
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(device.iconData),
Text(
device.name.isNotEmpty ? device.name : 'Unnamed device',
style: const TextStyle(fontSize: 18),
),
Expanded(
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 10, 0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
ValueListenableBuilder(
valueListenable: device.remember,
builder: (context, bool value, _) {
return FavoriteIcon(active: value);
}),
ValueListenableBuilder(
valueListenable: device.autoConnect,
builder: (context, bool value, _) {
return AutoConnectIcon(active: value);
}),
StreamBuilder(
stream: device.stateStream,
initialData: device.lastConnectionState,
builder: (context, snapshot) {
return ConnectionStateIcon(state: snapshot.data);
/*
if (snapshot.hasData) {
return ConnectionStateIcon(state: snapshot.data);
}
return Text(
" ",
style: TextStyle(fontSize: 10),
);
*/
}),
],
),
),
),
],
),
Text(
0 < device.lastScanRssi ? "rssi: ${device.lastScanRssi}" : " ",
style: const TextStyle(fontSize: 10),
),
],
),
),
ElevatedButton(
onPressed: () {
openDevice(device);
},
child: const Icon(Icons.arrow_forward),
),
],
),
),
);
}
void openDevice(Device device) {
if (scanner.scanning) scanner.stopScan();
if (!device.connected) device.connect();
Navigator.push(
context,
PageTransition(
type: PageTransitionType.rightToLeft,
child: DeviceRoute(device),
),
);
}
}