Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for all statuses at once and also ask for location for android' #94

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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