Commit 1d5f209

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2024-05-16 15:32:33
implement new device selection UI
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 64c27a1
Changed files (5)
app
lib
test
ui
components
bluetooth_input
app/lib/components/bluetooth_input/device_selection.dart
@@ -0,0 +1,40 @@
+import 'package:blood_pressure_app/components/bluetooth_input/input_card.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter_blue_plus/flutter_blue_plus.dart';
+import 'package:flutter_gen/gen_l10n/app_localizations.dart';
+
+/// A pairing dialoge with a single bluetooth device.
+class DeviceSelection extends StatelessWidget {
+  /// Create a pairing dialoge with a single bluetooth device.
+  const DeviceSelection({super.key,
+    required this.scanResults,
+    required this.onAccepted,
+  });
+
+  /// The name of the device trying to connect.
+  final List<ScanResult> scanResults;
+
+  /// Called when the user accepts the device.
+  final void Function(BluetoothDevice) onAccepted;
+
+  Widget _buildDeviceTile(BuildContext context, ScanResult dev) => ListTile(
+    title: Text(dev.device.platformName),
+    trailing: FilledButton(
+      onPressed: () => onAccepted(dev.device),
+      child: Text(AppLocalizations.of(context)!.connect),
+    ),
+    onTap: () => onAccepted(dev.device),
+  );
+
+  @override
+  Widget build(BuildContext context) => InputCard(
+    //title: Text('Available devices:'),
+    child: ListView(
+      children: [
+        for (final dev in scanResults)
+          _buildDeviceTile(context, dev),
+      ]
+    ),
+  );
+
+}
app/lib/components/bluetooth_input/input_card.dart
@@ -41,11 +41,11 @@ class InputCard extends StatelessWidget {
   );
 
   Widget _buildBody() => Padding( // content
-    padding: const EdgeInsets.only(
-      top: 42,
-      bottom: 20,
-      left: 20,
-      right: 20,
+    padding: EdgeInsets.only(
+      top: (title == null) ? 12 : 42,
+      bottom: 12,
+      left: 8,
+      right: 8,
     ),
     child: Center(
       child: child,
app/lib/components/bluetooth_input.dart
@@ -4,6 +4,7 @@ import 'package:blood_pressure_app/bluetooth/ble_read_cubit.dart';
 import 'package:blood_pressure_app/bluetooth/bluetooth_cubit.dart';
 import 'package:blood_pressure_app/bluetooth/device_scan_cubit.dart';
 import 'package:blood_pressure_app/components/bluetooth_input/closed_bluetooth_input.dart';
+import 'package:blood_pressure_app/components/bluetooth_input/device_selection.dart';
 import 'package:blood_pressure_app/components/bluetooth_input/input_card.dart';
 import 'package:blood_pressure_app/model/storage/storage.dart';
 import 'package:flutter/material.dart';
@@ -62,26 +63,15 @@ class _BluetoothInputState extends State<BluetoothInput> {
         DeviceListLoading() => _buildMainCard(context,
           title: Text(AppLocalizations.of(context)!.scanningForDevices),
           child: const CircularProgressIndicator()),
-        DeviceListAvailable() => _buildMainCard(context,
-          title: Text(AppLocalizations.of(context)!.selectDevice),
-          child: ListView(
-            children: [
-              for (final d in state.devices)
-                ListTile(
-                  title: Text(d.device.platformName),
-                  onTap: () => _deviceScanCubit!.acceptDevice(d.device),
-                ),
-            ],
-          ),
-        ),
-        SingleDeviceAvailable() => _buildMainCard(context,
-          title: Text(AppLocalizations.of(context)!
-              .connectTo(state.device.device.platformName)),
-          child: FilledButton(
-            child: Text(AppLocalizations.of(context)!.connect),
-            onPressed: () => _deviceScanCubit!.acceptDevice(state.device.device),
-          ),
+        DeviceListAvailable() => DeviceSelection(
+          scanResults: state.devices,
+          onAccepted: (dev) => _deviceScanCubit!.acceptDevice(dev),
         ),
+        SingleDeviceAvailable() => DeviceSelection(
+          scanResults: [ state.device ],
+          onAccepted: (dev) => _deviceScanCubit!.acceptDevice(dev),
+        ), // TODO: remove DeviceListAvailable and SingleDeviceAvailable
+          // distinction
         DeviceSelected() => BlocBuilder<BleReadCubit, BleReadState>(
           bloc: BleReadCubit(state.device),
           builder: (BuildContext context, BleReadState state) => switch (state) {
app/lib/l10n/app_en.arb
@@ -509,17 +509,6 @@
   "@errBleNoPerms": {},
   "bluetoothDisabled": "Bluetooth disabled",
   "@bluetoothDisabled": {},
-  "selectDevice": "Select device",
-  "@selectDevice": {},
-  "connectTo": "Connect to {deviceName}?",
-  "@connectTo": {
-    "placeholders": {
-      "deviceName": {
-        "type": "String",
-        "example": "boso medicus CE6674"
-      }
-    }
-  },
   "errMeasurementRead": "Error reading measurement",
   "@errBluetooth": {},
   "measurementSuccess": "Measurement taken successfully",
app/test/ui/components/bluetooth_input/input_card.test.dart → app/test/ui/components/bluetooth_input/input_card_test.dart
File renamed without changes