|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_web_bluetooth/flutter_web_bluetooth.dart'; |
| 5 | + |
| 6 | +abstract class Actions { |
| 7 | + Future<void> readValue(); |
| 8 | + |
| 9 | + Future<void> toggleNotify(); |
| 10 | + |
| 11 | + bool get isNotifying; |
| 12 | + |
| 13 | + Future<void> writeValue(BuildContext? context); |
| 14 | +} |
| 15 | + |
| 16 | +class CharacteristicActions extends Actions { |
| 17 | + CharacteristicActions(BluetoothCharacteristic characteristic) |
| 18 | + : _characteristic = characteristic; |
| 19 | + |
| 20 | + final BluetoothCharacteristic _characteristic; |
| 21 | + |
| 22 | + @override |
| 23 | + bool get isNotifying => _characteristic.isNotifying; |
| 24 | + |
| 25 | + @override |
| 26 | + Future<void> readValue() { |
| 27 | + return _characteristic.readValue(); |
| 28 | + } |
| 29 | + |
| 30 | + @override |
| 31 | + Future<void> toggleNotify() { |
| 32 | + if (isNotifying) { |
| 33 | + return _characteristic.stopNotifications(); |
| 34 | + } else { |
| 35 | + return _characteristic.startNotifications(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @override |
| 40 | + Future<void> writeValue(final BuildContext? context) async { |
| 41 | + if (context != null) { |
| 42 | + ScaffoldMessenger.maybeOf(context)?.showSnackBar(const SnackBar( |
| 43 | + content: Text('Write is supported just not implemented in the example'), |
| 44 | + )); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/// |
| 50 | +/// A widget to handle the different actions for a characteristic. |
| 51 | +/// |
| 52 | +/// It reads from the [properties] to see if the actions is supported and |
| 53 | +/// then adds it to the list, or it leaves it out. |
| 54 | +/// |
| 55 | +class ActionsWidget extends StatefulWidget { |
| 56 | + const ActionsWidget(this.properties, this.actions, {super.key}); |
| 57 | + |
| 58 | + final Actions actions; |
| 59 | + final BluetoothCharacteristicProperties properties; |
| 60 | + |
| 61 | + @override |
| 62 | + State<StatefulWidget> createState() { |
| 63 | + return ActionsSate(); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +class ActionsSate extends State<ActionsWidget> { |
| 68 | + Color? _getErrorColor() { |
| 69 | + if (!mounted) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + final theme = Theme.of(context); |
| 73 | + return theme.colorScheme.error; |
| 74 | + } |
| 75 | + |
| 76 | + Widget? getReadValueAction() { |
| 77 | + if (!widget.properties.hasProperties || widget.properties.read) { |
| 78 | + return OutlinedButton( |
| 79 | + onPressed: handlePressed(widget.actions.readValue), |
| 80 | + child: const Text('Read value')); |
| 81 | + } |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + Widget? getNotifyAction() { |
| 86 | + if (!widget.properties.hasProperties || widget.properties.notify) { |
| 87 | + return OutlinedButton( |
| 88 | + onPressed: handlePressed(widget.actions.toggleNotify), |
| 89 | + child: Text(widget.actions.isNotifying |
| 90 | + ? 'Stop notifying' |
| 91 | + : 'Start notifying')); |
| 92 | + } |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + Widget? getWriteAction() { |
| 97 | + if (!widget.properties.hasProperties || widget.properties.write) { |
| 98 | + return OutlinedButton( |
| 99 | + onPressed: handlePressedWithContext(widget.actions.writeValue), |
| 100 | + child: const Text('Write value')); |
| 101 | + } |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + FutureOr<void> Function() handlePressed(FutureOr<void> Function() method) { |
| 106 | + return () async { |
| 107 | + try { |
| 108 | + await method.call(); |
| 109 | + } catch (e, s) { |
| 110 | + debugPrint("$e\n$s"); |
| 111 | + if (mounted) { |
| 112 | + ScaffoldMessenger.maybeOf(context)?.showSnackBar(SnackBar( |
| 113 | + content: Text('Something went wrong: $e'), |
| 114 | + backgroundColor: _getErrorColor(), |
| 115 | + )); |
| 116 | + } |
| 117 | + } |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + FutureOr<void> Function() handlePressedWithContext( |
| 122 | + FutureOr<void> Function(BuildContext?) method) { |
| 123 | + return () async { |
| 124 | + try { |
| 125 | + if (mounted) { |
| 126 | + await method.call(context); |
| 127 | + } else { |
| 128 | + await method.call(null); |
| 129 | + } |
| 130 | + } catch (e, s) { |
| 131 | + debugPrint("$e\n$s"); |
| 132 | + if (mounted) { |
| 133 | + ScaffoldMessenger.maybeOf(context)?.showSnackBar(SnackBar( |
| 134 | + content: Text('Something went wrong: $e'), |
| 135 | + backgroundColor: _getErrorColor(), |
| 136 | + )); |
| 137 | + } |
| 138 | + } |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + bool _hasValueSet<T>([List<T?> args = const []]) { |
| 143 | + for (final arg in args) { |
| 144 | + if (arg != null) { |
| 145 | + return true; |
| 146 | + } |
| 147 | + } |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + @override |
| 152 | + Widget build(final BuildContext context) { |
| 153 | + final actionReadValue = getReadValueAction(); |
| 154 | + final actionNotify = getNotifyAction(); |
| 155 | + final actionWrite = getWriteAction(); |
| 156 | + |
| 157 | + final hasAction = |
| 158 | + _hasValueSet([actionReadValue, actionNotify, actionWrite]); |
| 159 | + |
| 160 | + if (hasAction) { |
| 161 | + return Row( |
| 162 | + children: [ |
| 163 | + if (actionReadValue != null) actionReadValue, |
| 164 | + if (actionNotify != null) actionNotify, |
| 165 | + if (actionWrite != null) actionWrite, |
| 166 | + ], |
| 167 | + ); |
| 168 | + } else { |
| 169 | + return Row( |
| 170 | + children: const [ |
| 171 | + Text("No actions for this characteristic"), |
| 172 | + ], |
| 173 | + ); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments