-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbluetooth-device-selection-bottom-sheet.tsx
More file actions
335 lines (301 loc) · 14.7 KB
/
Copy pathbluetooth-device-selection-bottom-sheet.tsx
File metadata and controls
335 lines (301 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { BluetoothIcon, RefreshCwIcon, WifiIcon } from 'lucide-react-native';
import React, { useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Alert, useWindowDimensions } from 'react-native';
import { showMessage } from 'react-native-flash-message';
import { Box } from '@/components/ui/box';
import { Button, ButtonIcon, ButtonText } from '@/components/ui/button';
import { FlatList } from '@/components/ui/flat-list';
import { Heading } from '@/components/ui/heading';
import { HStack } from '@/components/ui/hstack';
import { Pressable } from '@/components/ui/pressable';
import { Spinner } from '@/components/ui/spinner';
import { Text } from '@/components/ui/text';
import { VStack } from '@/components/ui/vstack';
import { usePreferredBluetoothDevice } from '@/lib/hooks/use-preferred-bluetooth-device';
import { logger } from '@/lib/logging';
import { bluetoothAudioService } from '@/services/bluetooth-audio.service';
import { type BluetoothAudioDevice, State, useBluetoothAudioStore } from '@/stores/app/bluetooth-audio-store';
import { CustomBottomSheet } from '../ui/bottom-sheet';
interface BluetoothDeviceSelectionBottomSheetProps {
isOpen: boolean;
onClose: () => void;
}
export function BluetoothDeviceSelectionBottomSheet({ isOpen, onClose }: BluetoothDeviceSelectionBottomSheetProps) {
const { t } = useTranslation();
const { width, height } = useWindowDimensions();
const isLandscape = width > height;
const { preferredDevice, setPreferredDevice } = usePreferredBluetoothDevice();
const { availableDevices, isScanning, bluetoothState, connectedDevice, connectionError } = useBluetoothAudioStore();
const [hasScanned, setHasScanned] = useState(false);
const [connectingDeviceId, setConnectingDeviceId] = useState<string | null>(null);
// Start scanning when sheet opens
useEffect(() => {
if (isOpen && !hasScanned) {
startScan();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isOpen, hasScanned]);
const startScan = React.useCallback(async () => {
try {
setHasScanned(true);
await bluetoothAudioService.startScanning(10000); // 10 second scan
} catch (error) {
setHasScanned(false); // Reset scan state on error
logger.error({
message: 'Failed to start Bluetooth scan',
context: { error },
});
Alert.alert(t('bluetooth.scan_error_title'), error instanceof Error ? error.message : t('bluetooth.scan_error_message'), [{ text: t('common.ok') }]);
}
}, [t]);
const handleDeviceSelect = React.useCallback(
async (device: BluetoothAudioDevice) => {
try {
// Disconnect from any currently connected device first?
// The service connectToDevice usually handles this or we do it here.
// Previous code did disconnect manually.
/*
if (connectedDevice) {
... disconnect ...
}
*/
// User wants "When attempting to connect... display loading".
// User wants "If error... don't save device".
// 1. Set connecting state
// We can resolve this by using local state for the specific device being connected to,
// or rely on store's global isConnecting.
// Let's use the store's setIsConnecting to be consistent if the UI uses it.
useBluetoothAudioStore.getState().setIsConnecting(true);
setConnectingDeviceId(device.id);
// 2. Clear existing preferred temporarily? Or just wait?
// User said "don't save the save device, only do that when connection is successful".
// So we shouldn't touch preferredDevice yet.
logger.info({
message: 'Attempting to connect to Bluetooth device',
context: { deviceId: device.id, deviceName: device.name },
});
// 3. Connect
await bluetoothAudioService.connectToDevice(device.id);
// 4. Success handling
logger.info({
message: 'Successfully connected to new Bluetooth device',
context: { deviceId: device.id },
});
// Set as preferred only on success
const selectedDevice = {
id: device.id,
name: device.name || t('bluetooth.unknown_device'),
};
await setPreferredDevice(selectedDevice);
// Sound is handled by service or we can ensure it here:
// await audioService.playConnectedToAudioRoomSound(); // If service doesn't do it.
// Checking previous logs, service seems to play "connectedDevice".
onClose();
} catch (error) {
logger.warn({
message: 'Failed to connect to device',
context: { error, deviceId: device.id },
});
// 5. Error handling
const errorMessage = error instanceof Error ? error.message : String(error);
showMessage({
message: t('bluetooth.connection_error_title') || 'Connection Failed',
description: errorMessage === 'Device disconnected' ? t('bluetooth.device_disconnected') : errorMessage || t('bluetooth.connection_error_message') || 'Could not connect to device',
type: 'danger',
duration: 4000,
});
// Keep sheet open (don't call onClose)
} finally {
useBluetoothAudioStore.getState().setIsConnecting(false);
setConnectingDeviceId(null);
}
},
[setPreferredDevice, onClose, t]
);
const handleClearSelection = React.useCallback(async () => {
try {
await bluetoothAudioService.reset();
await setPreferredDevice(null);
onClose();
} catch (error) {
logger.error({
message: 'Failed to clear preferred Bluetooth device',
context: { error },
});
}
}, [setPreferredDevice, onClose]);
const stopScan = React.useCallback(() => {
bluetoothAudioService.stopScanning();
}, []);
// Stop scanning when component unmounts or dialog closes
useEffect(() => {
if (!isOpen && isScanning) {
stopScan();
}
}, [isOpen, isScanning, stopScan]);
const renderDeviceItem = useCallback(
({ item }: { item: BluetoothAudioDevice }) => {
const isSelected = preferredDevice?.id === item.id;
const isConnected = connectedDevice?.id === item.id;
const isConnectingToThisDevice = connectingDeviceId === item.id;
return (
<Pressable
onPress={() => handleDeviceSelect(item)}
disabled={!!connectingDeviceId}
className={`mb-2 rounded-lg border p-4 ${isSelected ? 'border-primary-500 bg-primary-50 dark:bg-primary-950' : 'border-neutral-200 bg-white dark:border-neutral-700 dark:bg-neutral-800'} ${!!connectingDeviceId ? 'opacity-70' : ''}`}
>
<HStack className="items-center justify-between">
<VStack className="flex-1">
<HStack className="items-center">
<BluetoothIcon size={16} className="mr-2 text-primary-600" />
<Text className={`font-medium ${isSelected ? 'text-primary-700 dark:text-primary-300' : 'text-neutral-900 dark:text-neutral-100'}`}>{item.name || t('bluetooth.unknown_device')}</Text>
{isConnected && <WifiIcon size={14} className="ml-2 text-green-600" />}
</HStack>
<HStack className="mt-1 items-center">
{item.rssi && <Text className="text-xs text-neutral-600 dark:text-neutral-400">RSSI: {item.rssi}dBm</Text>}
{item.supportsMicrophoneControl && <Text className="ml-2 text-xs text-blue-600 dark:text-blue-400">{t('bluetooth.supports_mic_control')}</Text>}
{item.hasAudioCapability && <Text className="ml-2 text-xs text-green-600 dark:text-green-400">{t('bluetooth.audio_capable')}</Text>}
</HStack>
</VStack>
{isConnectingToThisDevice ? (
<Spinner size="small" className="text-primary-600" color="#2563EB" />
) : isSelected ? (
<VStack className="items-end">
<Text className="text-sm font-medium text-primary-600 dark:text-primary-400">{t('bluetooth.selected')}</Text>
{isConnected && <Text className="text-xs text-green-600 dark:text-green-400">{t('bluetooth.connected')}</Text>}
</VStack>
) : null}
</HStack>
</Pressable>
);
},
[preferredDevice, connectedDevice, handleDeviceSelect, t, connectingDeviceId]
);
const renderEmptyState = useCallback(() => {
if (isScanning) {
return (
<VStack className="items-center py-8">
<Spinner size="large" />
<Text className="mt-4 text-center text-neutral-600 dark:text-neutral-400">{t('bluetooth.scanning')}</Text>
</VStack>
);
}
return (
<VStack className="items-center py-8">
<BluetoothIcon size={48} className="text-neutral-400" />
<Text className="mt-4 text-center text-neutral-600 dark:text-neutral-400">{hasScanned ? t('bluetooth.no_devices_found') : t('bluetooth.tap_scan_to_find_devices')}</Text>
<Button onPress={startScan} className="mt-4" variant="outline">
<ButtonIcon as={RefreshCwIcon} />
<ButtonText>{t('bluetooth.scan_again')}</ButtonText>
</Button>
</VStack>
);
}, [isScanning, hasScanned, startScan, t]);
return (
<CustomBottomSheet isOpen={isOpen} onClose={onClose}>
<VStack className="flex-1 p-4">
<Heading className="mb-4 text-lg">{t('bluetooth.select_device')}</Heading>
{/* Current Selection */}
{preferredDevice && (
<Box className="mb-4 rounded-lg border border-neutral-200 bg-neutral-50 p-3 dark:border-neutral-700 dark:bg-neutral-800">
<HStack className="items-center justify-between">
<VStack>
<Text className="text-sm font-medium text-neutral-900 dark:text-neutral-100">{t('bluetooth.current_selection')}</Text>
<Text className="text-sm text-neutral-600 dark:text-neutral-400">{preferredDevice.name}</Text>
</VStack>
<Button onPress={handleClearSelection} size={isLandscape ? 'sm' : 'xs'} variant="outline">
<ButtonText className={isLandscape ? '' : 'text-2xs'}>{t('bluetooth.clear')}</ButtonText>
</Button>
</HStack>
</Box>
)}
{/* System Audio Option */}
<Box className="mb-4">
<Text className="mb-2 text-sm text-neutral-600 dark:text-neutral-400">{t('bluetooth.audio_output')}</Text>
<Pressable
onPress={async () => {
try {
useBluetoothAudioStore.getState().setIsConnecting(true);
// We use a dummy ID for loading state tracking if needed, or just rely on global loading
setConnectingDeviceId('system-audio');
await bluetoothAudioService.connectToSystemAudio();
// Update preferred device manually here to ensure UI reflects it immediately
// preventing race conditions with store updates
await setPreferredDevice({ id: 'system-audio', name: 'System Audio' });
onClose();
} catch (error) {
logger.error({ message: 'Failed to select System Audio', context: { error } });
showMessage({
message: t('bluetooth.connection_error_title') || 'Selection Failed',
description: t('bluetooth.system_audio_error') || 'Could not switch to System Audio',
type: 'danger',
});
} finally {
useBluetoothAudioStore.getState().setIsConnecting(false);
setConnectingDeviceId(null);
}
}}
disabled={!!connectingDeviceId}
className={`rounded-lg border p-4 ${preferredDevice?.id === 'system-audio' ? 'border-primary-500 bg-primary-50 dark:bg-primary-950' : 'border-neutral-200 bg-white dark:border-neutral-700 dark:bg-neutral-800'} ${!!connectingDeviceId ? 'opacity-70' : ''}`}
>
<HStack className="items-center justify-between">
<HStack className="items-center">
<BluetoothIcon size={16} className="mr-2 text-primary-600" />
<VStack>
<Text className={`font-medium ${preferredDevice?.id === 'system-audio' ? 'text-primary-700 dark:text-primary-300' : 'text-neutral-900 dark:text-neutral-100'}`}>
System Audio
</Text>
<Text className="text-xs text-neutral-500">AirPods, Car, Wired Headset</Text>
</VStack>
</HStack>
{preferredDevice?.id === 'system-audio' && (
<VStack className="items-end">
<Text className="text-sm font-medium text-primary-600 dark:text-primary-400">{t('bluetooth.selected')}</Text>
</VStack>
)}
</HStack>
</Pressable>
</Box>
{/* Scan Button */}
<HStack className="mb-4 w-full items-center justify-between">
<Text className="text-sm text-neutral-600 dark:text-neutral-400">{t('bluetooth.available_devices')}</Text>
<Button onPress={startScan} disabled={isScanning} size={isLandscape ? 'sm' : 'xs'} variant="outline">
<ButtonIcon as={RefreshCwIcon} />
<ButtonText className={isLandscape ? '' : 'text-2xs'}>{isScanning ? t('bluetooth.scanning') : t('bluetooth.scan')}</ButtonText>
</Button>
</HStack>
{/* Device List */}
<Box className="flex-1">
<FlatList
data={availableDevices}
renderItem={renderDeviceItem}
keyExtractor={(item) => item.id}
ListEmptyComponent={renderEmptyState}
showsVerticalScrollIndicator={false}
estimatedItemSize={60}
extraData={connectingDeviceId}
/>
</Box>
{/* Bluetooth State Info */}
{bluetoothState !== State.PoweredOn && (
<Box className="mt-4 rounded-lg border border-yellow-200 bg-yellow-50 p-3 dark:border-yellow-700 dark:bg-yellow-900">
<Text className="text-sm text-yellow-800 dark:text-yellow-200">
{bluetoothState === State.PoweredOff
? t('bluetooth.bluetooth_disabled')
: bluetoothState === State.Unauthorized
? t('bluetooth.bluetooth_unauthorized')
: t('bluetooth.bluetooth_not_ready', { state: bluetoothState })}
</Text>
</Box>
)}
{/* Connection Error Display */}
{connectionError && (
<Box className="mt-4 rounded-lg border border-red-200 bg-red-50 p-3 dark:border-red-700 dark:bg-red-900">
<Text className="text-sm text-red-800 dark:text-red-200">{connectionError}</Text>
</Box>
)}
</VStack>
</CustomBottomSheet>
);
}