Commit dee1bd6

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2024-08-01 03:22:39
fix automatic bluetooth permission requests
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 9929539
Changed files (3)
app
lib
test
features
app/lib/features/bluetooth/logic/bluetooth_cubit.dart
@@ -50,7 +50,6 @@ class BluetoothCubit extends Cubit<BluetoothState> {
       case BluetoothAdapterState.turningOff:
       case BluetoothAdapterState.turningOn:
         emit(BluetoothDisabled());
-        await enableBluetooth();
       case BluetoothAdapterState.unknown:
         emit(BluetoothInitial());
     }
app/lib/features/bluetooth/bluetooth_input.dart
@@ -4,7 +4,6 @@ import 'package:blood_pressure_app/features/bluetooth/logic/ble_read_cubit.dart'
 import 'package:blood_pressure_app/features/bluetooth/logic/bluetooth_cubit.dart';
 import 'package:blood_pressure_app/features/bluetooth/logic/characteristics/ble_measurement_data.dart';
 import 'package:blood_pressure_app/features/bluetooth/logic/device_scan_cubit.dart';
-import 'package:blood_pressure_app/features/bluetooth/logic/flutter_blue_plus_mockable.dart';
 import 'package:blood_pressure_app/features/bluetooth/ui/closed_bluetooth_input.dart';
 import 'package:blood_pressure_app/features/bluetooth/ui/device_selection.dart';
 import 'package:blood_pressure_app/features/bluetooth/ui/input_card.dart';
@@ -26,7 +25,6 @@ class BluetoothInput extends StatefulWidget {
     this.bluetoothCubit,
     this.deviceScanCubit,
     this.bleReadCubit,
-    this.flutterBluePlus,
   });
 
   /// Called when a measurement was received through bluetooth.
@@ -41,8 +39,6 @@ class BluetoothInput extends StatefulWidget {
   /// Function to customize [BleReadCubit] creation.
   final BleReadCubit Function(BluetoothDevice dev)? bleReadCubit;
 
-  final FlutterBluePlusMockable? flutterBluePlus;
-
   @override
   State<BluetoothInput> createState() => _BluetoothInputState();
 }
@@ -65,8 +61,7 @@ class _BluetoothInputState extends State<BluetoothInput> {
   @override
   void initState() {
     super.initState();
-    _bluetoothCubit = widget.bluetoothCubit?.call()
-      ?? BluetoothCubit(flutterBluePlus: widget.flutterBluePlus);
+    _bluetoothCubit = widget.bluetoothCubit?.call() ?? BluetoothCubit();
   }
 
   @override
@@ -108,13 +103,12 @@ class _BluetoothInputState extends State<BluetoothInput> {
     _deviceScanCubit ??= widget.deviceScanCubit?.call() ?? DeviceScanCubit(
       service: serviceUUID,
       settings: settings,
-      flutterBluePlus: widget.flutterBluePlus,
     );
     return BlocBuilder<DeviceScanCubit, DeviceScanState>(
       bloc: _deviceScanCubit,
       builder: (context, DeviceScanState state) {
         Log.trace('BluetoothInput _BluetoothInputState _deviceScanCubit: $state');
-        SizeChangedLayoutNotification().dispatch(context);
+        const SizeChangedLayoutNotification().dispatch(context);
         return switch(state) {
           DeviceListLoading() => _buildMainCard(context,
             title: Text(AppLocalizations.of(context)!.scanningForDevices),
@@ -158,7 +152,7 @@ class _BluetoothInputState extends State<BluetoothInput> {
             },
             builder: (BuildContext context, BleReadState state) {
               Log.trace('_BluetoothInputState BleReadCubit: $state');
-              SizeChangedLayoutNotification().dispatch(context);
+              const SizeChangedLayoutNotification().dispatch(context);
               return switch (state) {
                 BleReadInProgress() => _buildMainCard(context,
                   child: const CircularProgressIndicator(),
@@ -180,7 +174,7 @@ class _BluetoothInputState extends State<BluetoothInput> {
 
   @override
   Widget build(BuildContext context) {
-    SizeChangedLayoutNotification().dispatch(context);
+    const SizeChangedLayoutNotification().dispatch(context);
     if (_finishedData != null) {
       return MeasurementSuccess(
         onTap: _returnToIdle,
@@ -190,7 +184,9 @@ class _BluetoothInputState extends State<BluetoothInput> {
     if (_isActive) return _buildActive(context);
     return ClosedBluetoothInput(
       bluetoothCubit: _bluetoothCubit,
-      onStarted: () => setState(() =>_isActive = true),
+      onStarted: () async {
+        setState(() =>_isActive = true);
+      },
       inputInfo: () async {
         if (context.mounted) {
           await showDialog(
app/test/features/bluetooth/bluetooth_input_test.dart
@@ -19,6 +19,13 @@ class _MockDeviceScanCubit extends MockCubit<DeviceScanState>
     implements DeviceScanCubit {}
 class _MockBleReadCubit extends MockCubit<BleReadState>
     implements BleReadCubit {}
+class _MockBluetoothCubitFailingEnable extends MockCubit<BluetoothState>
+    implements BluetoothCubit {
+  @override
+  Future<bool> enableBluetooth() async {
+    throw 'enableBluetooth called';
+  }
+}
 
 void main() {
   testWidgets('propagates successful read', (WidgetTester tester) async {
@@ -61,7 +68,6 @@ void main() {
     expect(reads.first.dia?.mmHg, 45);
     expect(reads.first.pul, null);
   });
-
   testWidgets('allows closing after successful read', (WidgetTester tester) async {
     final bluetoothCubit = _MockBluetoothCubit();
     whenListen(bluetoothCubit, Stream<BluetoothState>.fromIterable([BluetoothReady()]),
@@ -102,4 +108,14 @@ void main() {
     await tester.pumpAndSettle();
     expect(find.byType(ClosedBluetoothInput), findsOneWidget);
   });
+  testWidgets("doesn't attempt to turn on bluetooth before interaction", (tester) async {
+    final bluetoothCubit = _MockBluetoothCubitFailingEnable();
+    whenListen(bluetoothCubit, Stream<BluetoothState>.fromIterable([BluetoothDisabled()]),
+      initialState: BluetoothReady());
+    await tester.pumpWidget(materialApp(BluetoothInput(
+      onMeasurement: (_) {},
+      bluetoothCubit: () => bluetoothCubit,
+    )));
+    expect(tester.takeException(), isNull);
+  });
 }