main
1import 'package:app_settings/app_settings.dart';
2import 'package:blood_pressure_app/features/bluetooth/logic/bluetooth_cubit.dart';
3import 'package:blood_pressure_app/logging.dart';
4import 'package:flutter/material.dart';
5import 'package:flutter_bloc/flutter_bloc.dart';
6import 'package:blood_pressure_app/l10n/app_localizations.dart';
7
8/// A closed ble input that shows the adapter state and allows to start the input.
9class ClosedBluetoothInput extends StatelessWidget with TypeLogger {
10 /// Show adapter state and allow starting inputs
11 const ClosedBluetoothInput({super.key,
12 required this.bluetoothCubit,
13 required this.onStarted,
14 this.inputInfo,
15 });
16
17 /// State update provider and interaction with the device.
18 final BluetoothCubit bluetoothCubit;
19
20 /// Called when the user taps on an active start button.
21 final void Function() onStarted;
22
23 /// Callback called when the user wants to know more about this input.
24 ///
25 /// The info icon is not shown when this is null.
26 final void Function()? inputInfo;
27
28 Widget _buildTile({
29 required String text,
30 required IconData icon,
31 required void Function() onTap,
32 }) => ListTile(
33 title: Text(text),
34 leading: Icon(icon),
35 onTap: onTap,
36 trailing: inputInfo == null ? null : IconButton(
37 icon: const Icon(Icons.info_outline),
38 onPressed: inputInfo!,
39 ),
40 );
41
42 @override
43 Widget build(BuildContext context) {
44 final localizations = AppLocalizations.of(context)!;
45 return BlocBuilder<BluetoothCubit, BluetoothState>(
46 bloc: bluetoothCubit,
47 builder: (context, BluetoothState state) {
48 logger.finer('Called with state: $state');
49 return switch(state) {
50 BluetoothStateInitial() => const SizedBox.shrink(),
51 BluetoothStateUnfeasible() => const SizedBox.shrink(),
52 BluetoothStateUnauthorized() => _buildTile(
53 text: localizations.errBleNoPerms,
54 icon: Icons.bluetooth_disabled,
55 onTap: () async {
56 await AppSettings.openAppSettings();
57 bluetoothCubit.forceRefresh();
58 },
59 ),
60 BluetoothStateDisabled() => _buildTile(
61 text: localizations.bluetoothDisabled,
62 icon: Icons.bluetooth_disabled,
63 onTap: () async {
64 final bluetoothOn = await bluetoothCubit.enableBluetooth();
65 if (bluetoothOn == false) await AppSettings.openAppSettings(type: AppSettingsType.bluetooth);
66 bluetoothCubit.forceRefresh();
67 },
68 ),
69 BluetoothStateReady() => _buildTile(
70 text: localizations.bluetoothInput,
71 icon: Icons.bluetooth,
72 onTap: onStarted,
73 )
74 };
75 },
76 );
77 }
78
79}