-
Notifications
You must be signed in to change notification settings - Fork 195
/
devices_list_view.dart
176 lines (157 loc) · 4.77 KB
/
devices_list_view.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
import 'dart:async';
import 'package:fimber/fimber.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ble_lib_example/model/ble_device.dart';
import 'devices_bloc.dart';
import 'devices_bloc_provider.dart';
import 'hex_painter.dart';
typedef DeviceTapListener = void Function();
class DevicesListScreen extends StatefulWidget {
@override
State<DevicesListScreen> createState() => DeviceListScreenState();
}
class DeviceListScreenState extends State<DevicesListScreen> {
DevicesBloc _devicesBloc;
StreamSubscription _appStateSubscription;
@override
void didUpdateWidget(DevicesListScreen oldWidget) {
super.didUpdateWidget(oldWidget);
Fimber.d("didUpdateWidget");
}
void _onPause() {
Fimber.d("onPause");
_appStateSubscription.cancel();
_devicesBloc.dispose();
}
void _onResume() {
Fimber.d("onResume");
_devicesBloc.init();
_appStateSubscription = _devicesBloc.pickedDevice.listen((bleDevice) async {
Fimber.d("navigate to details");
_onPause();
await Navigator.pushNamed(context, "/details");
_shouldRunOnResume = true;
Fimber.d("back from details");
});
}
bool _shouldRunOnResume = true;
@override
void didChangeDependencies() {
super.didChangeDependencies();
Fimber.d("DeviceListScreenState didChangeDependencies");
if (_devicesBloc == null) {
_devicesBloc = DevicesBlocProvider.of(context);
if (_shouldRunOnResume) {
_shouldRunOnResume = false;
_onResume();
}
}
}
@override
Widget build(BuildContext context) {
Fimber.d("build DeviceListScreenState");
if (_shouldRunOnResume) {
_shouldRunOnResume = false;
_onResume();
}
return Scaffold(
appBar: AppBar(
title: Text('Bluetooth devices'),
),
body: StreamBuilder<List<BleDevice>>(
initialData: _devicesBloc.visibleDevices.value,
stream: _devicesBloc.visibleDevices,
builder: (context, snapshot) => RefreshIndicator(
onRefresh: _devicesBloc.refresh,
child: DevicesList(_devicesBloc, snapshot.data),
),
),
);
}
@override
void dispose() {
Fimber.d("Dispose DeviceListScreenState");
_onPause();
super.dispose();
}
@override
void deactivate() {
print("deactivate");
super.deactivate();
}
@override
void reassemble() {
Fimber.d("reassemble");
super.reassemble();
}
}
class DevicesList extends ListView {
DevicesList(DevicesBloc devicesBloc, List<BleDevice> devices)
: super.separated(
separatorBuilder: (context, index) => Divider(
color: Colors.grey[300],
height: 0,
indent: 0,
),
itemCount: devices.length,
itemBuilder: (context, i) {
Fimber.d("Build row for $i");
return _buildRow(context, devices[i],
_createTapListener(devicesBloc, devices[i]));
});
static DeviceTapListener _createTapListener(
DevicesBloc devicesBloc, BleDevice bleDevice) {
return () {
Fimber.d("clicked device: ${bleDevice.name}");
devicesBloc.devicePicker.add(bleDevice);
};
}
static Widget _buildAvatar(BuildContext context, BleDevice device) {
switch (device.category) {
case DeviceCategory.sensorTag:
return CircleAvatar(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Image.asset('assets/ti_logo.png'),
),
backgroundColor: Theme.of(context).accentColor);
case DeviceCategory.hex:
return CircleAvatar(
child: CustomPaint(painter: HexPainter(), size: Size(20, 24)),
backgroundColor: Colors.black);
case DeviceCategory.other:
default:
return CircleAvatar(
child: Icon(Icons.bluetooth),
backgroundColor: Theme.of(context).primaryColor,
foregroundColor: Colors.white);
}
}
static Widget _buildRow(BuildContext context, BleDevice device,
DeviceTapListener deviceTapListener) {
return ListTile(
leading: Padding(
padding: const EdgeInsets.only(top: 8),
child: _buildAvatar(context, device),
),
title: Text(device.name),
trailing: Padding(
padding: const EdgeInsets.only(top: 16),
child: Icon(Icons.chevron_right, color: Colors.grey),
),
subtitle: Column(
children: <Widget>[
Text(
device.id.toString(),
style: TextStyle(fontSize: 10),
overflow: TextOverflow.ellipsis,
maxLines: 1,
)
],
crossAxisAlignment: CrossAxisAlignment.start,
),
onTap: deviceTapListener,
contentPadding: EdgeInsets.fromLTRB(16, 0, 16, 12),
);
}
}