Skip to content

Commit f465d6b

Browse files
Replace capability checks with new hasCapability/requireCapability API
Co-authored-by: DennisMoschina <45356478+DennisMoschina@users.noreply.github.com>
1 parent b5934e0 commit f465d6b

11 files changed

Lines changed: 72 additions & 72 deletions

File tree

open_wearable/lib/apps/widgets/apps_page.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ List<AppInfo> _apps = [
3232
widget: SelectEarableView(startApp: (wearable, sensorConfigProvider) {
3333
return PostureTrackerView(
3434
EarableAttitudeTracker(
35-
wearable as SensorManager,
35+
wearable.requireCapability<SensorManager>(),
3636
sensorConfigProvider,
3737
wearable.name.endsWith("L"),
3838
),
@@ -45,9 +45,9 @@ List<AppInfo> _apps = [
4545
description: "Track your heart rate and other vitals",
4646
widget: SelectEarableView(
4747
startApp: (wearable, _) {
48-
if (wearable is SensorManager) {
48+
if (wearable.hasCapability<SensorManager>()) {
4949
//TODO: show alert if no ppg sensor is found
50-
Sensor ppgSensor = (wearable as SensorManager).sensors.firstWhere(
50+
Sensor ppgSensor = wearable.requireCapability<SensorManager>().sensors.firstWhere(
5151
(s) => s.sensorName.toLowerCase() == "photoplethysmography".toLowerCase(),
5252
);
5353

open_wearable/lib/view_models/sensor_recorder_provider.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ class SensorRecorderProvider with ChangeNotifier {
7474
notifyListeners();
7575
});
7676

77-
if (wearable is SensorManager) {
78-
for (Sensor sensor in (wearable as SensorManager).sensors) {
77+
if (wearable.hasCapability<SensorManager>()) {
78+
for (Sensor sensor in wearable.requireCapability<SensorManager>().sensors) {
7979
if (!_recorders[wearable]!.containsKey(sensor)) {
8080
_recorders[wearable]![sensor] = Recorder(columns: sensor.axisNames);
8181
}

open_wearable/lib/view_models/wearables_provider.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,18 @@ class WearablesProvider with ChangeNotifier {
194194

195195
// 2) Slow/async work: run in microtasks so it doesn't block the add
196196
// Stereo pairing (if applicable)
197-
if (wearable is StereoDevice) {
198-
_scheduleMicrotask(() => _maybeAutoPairStereoAsync(wearable as StereoDevice));
197+
if (wearable.hasCapability<StereoDevice>()) {
198+
_scheduleMicrotask(() => _maybeAutoPairStereoAsync(wearable.requireCapability<StereoDevice>()));
199199
}
200200

201201
// Firmware support check (if applicable)
202-
if (wearable is DeviceFirmwareVersion) {
203-
_scheduleMicrotask(() => _maybeEmitUnsupportedFirmwareAsync(wearable as DeviceFirmwareVersion));
202+
if (wearable.hasCapability<DeviceFirmwareVersion>()) {
203+
_scheduleMicrotask(() => _maybeEmitUnsupportedFirmwareAsync(wearable.requireCapability<DeviceFirmwareVersion>()));
204204
}
205205

206206
// Check for newer firmware (if applicable)
207-
if (wearable is DeviceFirmwareVersion) {
208-
_scheduleMicrotask(() => _checkForNewerFirmwareAsync(wearable as DeviceFirmwareVersion));
207+
if (wearable.hasCapability<DeviceFirmwareVersion>()) {
208+
_scheduleMicrotask(() => _checkForNewerFirmwareAsync(wearable.requireCapability<DeviceFirmwareVersion>()));
209209
}
210210
}
211211

@@ -214,7 +214,7 @@ class WearablesProvider with ChangeNotifier {
214214
void _ensureSensorConfigProvider(Wearable wearable) {
215215
if (!_sensorConfigurationProviders.containsKey(wearable)) {
216216
_sensorConfigurationProviders[wearable] = SensorConfigurationProvider(
217-
sensorConfigurationManager: wearable as SensorConfigurationManager,
217+
sensorConfigurationManager: wearable.requireCapability<SensorConfigurationManager>(),
218218
);
219219
}
220220
}

open_wearable/lib/widgets/devices/battery_state.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class BatteryStateView extends StatelessWidget {
1313
return Row(
1414
mainAxisSize: MainAxisSize.min,
1515
children: [
16-
if (_device is BatteryLevelStatus)
16+
if (_device.hasCapability<BatteryLevelStatus>())
1717
StreamBuilder(
18-
stream: (_device as BatteryLevelStatus).batteryPercentageStream,
18+
stream: _device.requireCapability<BatteryLevelStatus>().batteryPercentageStream,
1919
builder: (context, snapshot) {
2020
if (snapshot.hasData) {
2121
return PlatformText("${snapshot.data}%");
@@ -24,9 +24,9 @@ class BatteryStateView extends StatelessWidget {
2424
}
2525
},
2626
),
27-
if (_device is BatteryLevelStatusService)
27+
if (_device.hasCapability<BatteryLevelStatusService>())
2828
StreamBuilder(
29-
stream: (_device as BatteryLevelStatusService).powerStatusStream,
29+
stream: _device.requireCapability<BatteryLevelStatusService>().powerStatusStream,
3030
builder: (context, snapshot) {
3131
if (snapshot.hasData) {
3232
if (!snapshot.data!.batteryPresent) {
@@ -52,9 +52,9 @@ class BatteryStateView extends StatelessWidget {
5252
}
5353
},
5454
)
55-
else if (_device is BatteryLevelStatus)
55+
else if (_device.hasCapability<BatteryLevelStatus>())
5656
StreamBuilder(
57-
stream: (_device as BatteryLevelStatus).batteryPercentageStream,
57+
stream: _device.requireCapability<BatteryLevelStatus>().batteryPercentageStream,
5858
builder: (context, snapshot) {
5959
if (snapshot.hasData) {
6060
return Icon(getBatteryIcon(snapshot.data!));

open_wearable/lib/widgets/devices/device_detail/device_detail_page.dart

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class _DeviceDetailPageState extends State<DeviceDetailPage> {
3636
}
3737

3838
Future<void> _initSelectedMicrophone() async {
39-
if (widget.device is MicrophoneManager) {
40-
final mic = await (widget.device as MicrophoneManager).getMicrophone();
39+
if (widget.device.hasCapability<MicrophoneManager>()) {
40+
final mic = await widget.device.requireCapability<MicrophoneManager>().getMicrophone();
4141
setState(() {
4242
selectedMicrophone = mic;
4343
});
@@ -67,10 +67,10 @@ class _DeviceDetailPageState extends State<DeviceDetailPage> {
6767
mainAxisAlignment: MainAxisAlignment.center,
6868
children: [
6969
BatteryStateView(device: widget.device),
70-
if (widget.device is StereoDevice)
70+
if (widget.device.hasCapability<StereoDevice>())
7171
Padding(
7272
padding: const EdgeInsets.only(left: 8.0),
73-
child: StereoPosLabel(device: widget.device as StereoDevice),
73+
child: StereoPosLabel(device: widget.device.requireCapability<StereoDevice>()),
7474
),
7575
],
7676
),
@@ -79,7 +79,7 @@ class _DeviceDetailPageState extends State<DeviceDetailPage> {
7979
Row(
8080
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
8181
children: [
82-
if (widget.device is SystemDevice && (widget.device as SystemDevice).isConnectedViaSystem)
82+
if (widget.device.hasCapability<SystemDevice>() && widget.device.requireCapability<SystemDevice>().isConnectedViaSystem)
8383
PlatformElevatedButton(
8484
child: PlatformText("Forget Device"),
8585
onPressed: () {
@@ -112,12 +112,12 @@ class _DeviceDetailPageState extends State<DeviceDetailPage> {
112112
],
113113
),
114114
// MARK: Audio Mode
115-
if (widget.device is AudioModeManager)
116-
AudioModeWidget(device: widget.device as AudioModeManager),
115+
if (widget.device.hasCapability<AudioModeManager>())
116+
AudioModeWidget(device: widget.device.requireCapability<AudioModeManager>()),
117117
// MARK: Microphone Control
118-
if (widget.device is MicrophoneManager)
118+
if (widget.device.hasCapability<MicrophoneManager>())
119119
MicrophoneSelectionWidget(
120-
device: widget.device as MicrophoneManager,
120+
device: widget.device.requireCapability<MicrophoneManager>(),
121121
),
122122
// MARK: Device info
123123
PlatformText("Device Info", style: Theme.of(context).textTheme.titleSmall),
@@ -129,14 +129,14 @@ class _DeviceDetailPageState extends State<DeviceDetailPage> {
129129
subtitle: PlatformText(widget.device.deviceId),
130130
),
131131
// MARK: Device Identifier
132-
if (widget.device is DeviceIdentifier)
132+
if (widget.device.hasCapability<DeviceIdentifier>())
133133
PlatformListTile(
134134
title: PlatformText(
135135
"Device Identifier",
136136
style: Theme.of(context).textTheme.bodyLarge,
137137
),
138138
subtitle: FutureBuilder(
139-
future: (widget.device as DeviceIdentifier)
139+
future: widget.device.requireCapability<DeviceIdentifier>()
140140
.readDeviceIdentifier(),
141141
builder: (context, snapshot) {
142142
if (snapshot.connectionState == ConnectionState.done) {
@@ -155,15 +155,15 @@ class _DeviceDetailPageState extends State<DeviceDetailPage> {
155155
),
156156
),
157157
// MARK: Device Firmware Version
158-
if (widget.device is DeviceFirmwareVersion)
158+
if (widget.device.hasCapability<DeviceFirmwareVersion>())
159159
PlatformListTile(
160160
title: PlatformText(
161161
"Firmware Version",
162162
style: Theme.of(context).textTheme.bodyLarge,
163163
),
164164
subtitle: Row(children: [
165165
FutureBuilder(
166-
future: (widget.device as DeviceFirmwareVersion)
166+
future: widget.device.requireCapability<DeviceFirmwareVersion>()
167167
.readDeviceFirmwareVersion(),
168168
builder: (context, snapshot) {
169169
if (snapshot.connectionState == ConnectionState.done) {
@@ -181,7 +181,7 @@ class _DeviceDetailPageState extends State<DeviceDetailPage> {
181181
},
182182
),
183183
FutureBuilder(
184-
future: (widget.device as DeviceFirmwareVersion)
184+
future: widget.device.requireCapability<DeviceFirmwareVersion>()
185185
.checkFirmwareSupport(),
186186
builder: (context, snapshot) {
187187
if (snapshot.connectionState == ConnectionState.done) {
@@ -222,14 +222,14 @@ class _DeviceDetailPageState extends State<DeviceDetailPage> {
222222
),
223223
),
224224
// MARK: Device Hardware Version
225-
if (widget.device is DeviceHardwareVersion)
225+
if (widget.device.hasCapability<DeviceHardwareVersion>())
226226
PlatformListTile(
227227
title: PlatformText(
228228
"Hardware Version",
229229
style: Theme.of(context).textTheme.bodyLarge,
230230
),
231231
subtitle: FutureBuilder(
232-
future: (widget.device as DeviceHardwareVersion)
232+
future: widget.device.requireCapability<DeviceHardwareVersion>()
233233
.readDeviceHardwareVersion(),
234234
builder: (context, snapshot) {
235235
if (snapshot.connectionState == ConnectionState.done) {
@@ -249,17 +249,17 @@ class _DeviceDetailPageState extends State<DeviceDetailPage> {
249249
),
250250

251251
// MARK: Status LED control
252-
if (widget.device is StatusLed) ...[
252+
if (widget.device.hasCapability<StatusLed>()) ...[
253253
PlatformText(
254254
"Control Status LED",
255255
style: Theme.of(context).textTheme.titleSmall,
256256
),
257257
StatusLEDControlWidget(
258-
statusLED: widget.device as StatusLed,
259-
rgbLed: widget.device as RgbLed,
258+
statusLED: widget.device.requireCapability<StatusLed>(),
259+
rgbLed: widget.device.requireCapability<RgbLed>(),
260260
),
261-
] else if (widget.device is RgbLed &&
262-
widget.device is! StatusLed) ...[
261+
] else if (widget.device.hasCapability<RgbLed>() &&
262+
!widget.device.hasCapability<StatusLed>()) ...[
263263
PlatformText(
264264
"Control RGB LED",
265265
style: Theme.of(context).textTheme.titleSmall,
@@ -269,18 +269,18 @@ class _DeviceDetailPageState extends State<DeviceDetailPage> {
269269
"LED Color",
270270
style: Theme.of(context).textTheme.bodyLarge,
271271
),
272-
trailing: RgbControlView(rgbLed: widget.device as RgbLed),
272+
trailing: RgbControlView(rgbLed: widget.device.requireCapability<RgbLed>()),
273273
),
274274
],
275275

276276
// MARK: Device Battery State
277-
if (widget.device is BatteryEnergyStatusService) ...[
277+
if (widget.device.hasCapability<BatteryEnergyStatusService>()) ...[
278278
PlatformText(
279279
"Battery Energy Status",
280280
style: Theme.of(context).textTheme.titleSmall,
281281
),
282282
StreamBuilder<BatteryEnergyStatus>(
283-
stream: (widget.device as BatteryEnergyStatusService)
283+
stream: widget.device.requireCapability<BatteryEnergyStatusService>()
284284
.energyStatusStream,
285285
builder: (context, snapshot) {
286286
if (snapshot.connectionState == ConnectionState.waiting) {
@@ -326,13 +326,13 @@ class _DeviceDetailPageState extends State<DeviceDetailPage> {
326326
],
327327

328328
// MARK: Battery Health
329-
if (widget.device is BatteryHealthStatusService) ...[
329+
if (widget.device.hasCapability<BatteryHealthStatusService>()) ...[
330330
PlatformText(
331331
"Battery Health Status",
332332
style: Theme.of(context).textTheme.titleSmall,
333333
),
334334
StreamBuilder<BatteryHealthStatus>(
335-
stream: (widget.device as BatteryHealthStatusService)
335+
stream: widget.device.requireCapability<BatteryHealthStatusService>()
336336
.healthStatusStream,
337337
builder: (context, snapshot) {
338338
if (snapshot.connectionState == ConnectionState.waiting) {

open_wearable/lib/widgets/devices/devices_page.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,20 +220,20 @@ class DeviceRow extends StatelessWidget {
220220
),
221221
Row(children: [
222222
BatteryStateView(device: _device),
223-
if (_device is StereoDevice)
223+
if (_device.hasCapability<StereoDevice>())
224224
Padding(
225225
padding: EdgeInsets.only(left: 8.0),
226-
child: StereoPosLabel(device: _device as StereoDevice),
226+
child: StereoPosLabel(device: _device.requireCapability<StereoDevice>()),
227227
),
228228
],
229229
),
230230
],
231231
),
232232
Spacer(),
233-
if (_device is DeviceIdentifier)
233+
if (_device.hasCapability<DeviceIdentifier>())
234234
FutureBuilder(
235235
future:
236-
(_device as DeviceIdentifier).readDeviceIdentifier(),
236+
_device.requireCapability<DeviceIdentifier>().readDeviceIdentifier(),
237237
builder: (context, snapshot) {
238238
if (snapshot.connectionState ==
239239
ConnectionState.waiting) {
@@ -249,12 +249,12 @@ class DeviceRow extends StatelessWidget {
249249
PlatformText(_device.deviceId),
250250
],
251251
),
252-
if (_device is DeviceFirmwareVersion)
252+
if (_device.hasCapability<DeviceFirmwareVersion>())
253253
Row(
254254
children: [
255255
PlatformText("Firmware Version: "),
256256
FutureBuilder(
257-
future: (_device as DeviceFirmwareVersion)
257+
future: _device.requireCapability<DeviceFirmwareVersion>()
258258
.readDeviceFirmwareVersion(),
259259
builder: (context, snapshot) {
260260
if (snapshot.connectionState ==
@@ -268,7 +268,7 @@ class DeviceRow extends StatelessWidget {
268268
},
269269
),
270270
FutureBuilder(
271-
future: (_device as DeviceFirmwareVersion)
271+
future: _device.requireCapability<DeviceFirmwareVersion>()
272272
.checkFirmwareSupport(),
273273
builder: (context, snapshot) {
274274
if (snapshot.connectionState ==
@@ -300,12 +300,12 @@ class DeviceRow extends StatelessWidget {
300300
),
301301
],
302302
),
303-
if (_device is DeviceHardwareVersion)
303+
if (_device.hasCapability<DeviceHardwareVersion>())
304304
Row(
305305
children: [
306306
PlatformText("Hardware Version: "),
307307
FutureBuilder(
308-
future: (_device as DeviceHardwareVersion)
308+
future: _device.requireCapability<DeviceHardwareVersion>()
309309
.readDeviceHardwareVersion(),
310310
builder: (context, snapshot) {
311311
if (snapshot.connectionState ==

open_wearable/lib/widgets/fota/firmware_select/firmware_list.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class _FirmwareListState extends State<FirmwareList> {
3636
final wearable =
3737
Provider.of<FirmwareUpdateRequestProvider>(context, listen: false)
3838
.selectedWearable;
39-
if (wearable is DeviceFirmwareVersion) {
39+
if (wearable != null && wearable.hasCapability<DeviceFirmwareVersion>()) {
4040
final version =
41-
await (wearable as DeviceFirmwareVersion).readDeviceFirmwareVersion();
41+
await wearable.requireCapability<DeviceFirmwareVersion>().readDeviceFirmwareVersion();
4242
setState(() {
4343
firmwareVersion = version;
4444
});

open_wearable/lib/widgets/fota/fota_warning_page.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class _FotaWarningPageState extends State<FotaWarningPage> {
3333
);
3434
final device = updateProvider.selectedWearable;
3535

36-
if (device != null && device is BatteryLevelStatus) {
36+
if (device != null && device.hasCapability<BatteryLevelStatus>()) {
3737
// Get the current battery level from the stream
38-
final batteryLevel = await (device as BatteryLevelStatus)
38+
final batteryLevel = await device.requireCapability<BatteryLevelStatus>()
3939
.batteryPercentageStream
4040
.first
4141
.timeout(

0 commit comments

Comments
 (0)