Skip to content

Commit

Permalink
Check for all statuses at once and also ask for location for android'
Browse files Browse the repository at this point in the history
  • Loading branch information
jhsu committed Apr 28, 2024
1 parent 0101dc8 commit f2895e4
Showing 1 changed file with 84 additions and 60 deletions.
144 changes: 84 additions & 60 deletions apps/AppWithWearable/lib/onboarding/welcome/welcome_widget.dart
Expand Up @@ -13,9 +13,9 @@ import 'welcome_model.dart';
import 'package:lottie/lottie.dart';
import 'dart:math';
import 'dart:ui';
import 'dart:io' show Platform;
import 'package:permission_handler/permission_handler.dart';


class WelcomeWidget extends StatefulWidget {
const WelcomeWidget({super.key});

Expand Down Expand Up @@ -150,72 +150,96 @@ class _WelcomeWidgetState extends State<WelcomeWidget> with SingleTickerProvider
highlightColor: Colors.transparent,
mouseCursor: SystemMouseCursors.click,
onTap: () async {
// Check if Bluetooth permission is granted
PermissionStatus bluetoothStatus =
await Permission.bluetooth.status;
if (bluetoothStatus.isGranted) {
// Bluetooth permission is already granted
// Request notification permission
PermissionStatus notificationStatus =
await Permission.notification.request();
Map<Permission, PermissionStatus> statuses;
List<Permission> requestStatuses;

// Navigate to the 'scanDevices' screen
context.goNamed('findDevices');
if (Platform.isAndroid) {
statuses = {
Permission.bluetooth:
PermissionStatus.denied,
Permission.notification:
PermissionStatus.denied,
Permission.location:
PermissionStatus.denied,
};
} else {
// Bluetooth permission is not granted
if (await Permission.bluetooth
.request()
.isGranted) {
// Bluetooth permission is granted now
// Request notification permission
PermissionStatus notificationStatus =
await Permission.notification.request();
// iOS-specific code
statuses = {
Permission.bluetooth:
PermissionStatus.denied,
Permission.notification:
PermissionStatus.denied,
Permission.location:
PermissionStatus.denied,
};
}
requestStatuses = statuses.keys.toList();

// Navigate to the 'scanDevices' screen
context.goNamed('findDevices');
} else {
// Bluetooth permission is denied
// Show a dialog to inform the user and provide an action to open app settings
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: Colors.grey[900],
title: Text(
'Bluetooth Required',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
bool granted = false;
// check if all statuses are 'granted
bool allStatusesGranted(
Map<Permission, PermissionStatus>
statuses) {
return statuses.values
.every((status) => status.isGranted);
}

int attempts = 0;
int maxAttempts = 3;
while (attempts < maxAttempts &&
!allStatusesGranted(statuses)) {
// Request permissions
statuses = await requestStatuses.request();
if (allStatusesGranted(statuses)) {
granted = true;
break;
}
attempts += 1;
}
if (granted) {
context.goNamed('findDevices');
} else {
// Bluetooth permission is denied
// Show a dialog to inform the user and provide an action to open app settings
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: Colors.grey[900],
title: Text(
'Bluetooth Required',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
content: Text(
'This app needs Bluetooth to function properly. Please enable it in the settings.',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
),
content: Text(
'This app needs Bluetooth to function properly. Please enable it in the settings.',
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
openAppSettings();
},
child: Text(
'OK',
style: TextStyle(
color: Colors.blue,
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
openAppSettings();
},
child: Text(
'OK',
style: TextStyle(
color: Colors.blue,
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
],
);
},
);
}
),
],
);
},
);
}
},
child: ShaderMask(
Expand Down

0 comments on commit f2895e4

Please sign in to comment.