Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions app/lib/pages/onboarding/find_device/found_devices.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ class _FoundDevicesState extends State<FoundDevices> {

if (!recordingStarted) {
await _showMicrophonePermissionPage(connection);
if (!mounted) return;
} else {
await _completeAppleWatchOnboarding(device, provider);
if (!mounted) return;
}
} catch (e) {
debugPrint('Error handling Apple Watch onboarding: $e');
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error connecting to Apple Watch: $e'),
Expand All @@ -82,36 +85,45 @@ class _FoundDevicesState extends State<FoundDevices> {

/// Show bottom sheet when Apple Watch is not reachable
Future<void> _showWatchNotReachableBottomSheet(String deviceId) async {
final provider = Provider.of<OnboardingProvider>(context, listen: false);
final device = provider.deviceList.firstWhereOrNull((d) => d.id == deviceId);
if (device == null) {
debugPrint('Device with id $deviceId not found in provider list.');
return;
}

await showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
isScrollControlled: true,
builder: (context) => AppleWatchSetupBottomSheet(
deviceId: deviceId,
onConnected: () async {
// Retry the connection flow when user says they've connected
final device =
Provider.of<OnboardingProvider>(context, listen: false).deviceList.firstWhere((d) => d.id == deviceId);
final provider = Provider.of<OnboardingProvider>(context, listen: false);
await _handleAppleWatchOnboarding(device, provider);
},
),
);
}

Future<void> _showMicrophonePermissionPage(AppleWatchDeviceConnection connection) async {
final provider = Provider.of<OnboardingProvider>(context, listen: false);
final device = provider.deviceList.firstWhereOrNull((d) => d.id == connection.device.id);
if (device == null) {
debugPrint('Device with id ${connection.device.id} not found in provider list.');
return;
}

await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => AppleWatchPermissionPage(
connection: connection,
onPermissionGranted: () async {
final provider = Provider.of<OnboardingProvider>(context, listen: false);
final device = provider.deviceList.firstWhere((d) => d.id == connection.device.id);
await _completeAppleWatchOnboarding(device, provider);
},
),
),
);
if (!mounted) return;
}

Future<void> _completeAppleWatchOnboarding(BtDevice device, OnboardingProvider provider) async {
Expand All @@ -124,13 +136,17 @@ class _FoundDevicesState extends State<FoundDevices> {

await provider.deviceProvider?.scanAndConnectToDevice();

if (!mounted) return;

// Show firmware warning if needed
await _showFirmwareWarningIfNeeded(device);

if (!mounted) return;

if (widget.isFromOnboarding) {
widget.goNext();
} else {
Navigator.pop(context);
if (mounted) Navigator.pop(context);
}
} catch (e) {
debugPrint('Error completing Apple Watch onboarding: $e');
Expand Down Expand Up @@ -196,7 +212,7 @@ class _FoundDevicesState extends State<FoundDevices> {
// ),
// (route) => false,
// );
Navigator.pop(context);
if (mounted) Navigator.pop(context);
} else {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(info),
Expand Down Expand Up @@ -279,6 +295,8 @@ class _FoundDevicesState extends State<FoundDevices> {
goNext: widget.goNext,
);

if (!mounted) return;

// Show firmware warning after successful connection
if (provider.isConnected) {
await _showFirmwareWarningIfNeeded(device);
Expand Down