132 lines
5.2 KiB
Dart
132 lines
5.2 KiB
Dart
// lib/features/auth/screens/sms_verification_helper.dart
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:kmobile/api/services/send_sms_service.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
class SmsVerificationHelper {
|
|
final SmsService _smsService = SmsService();
|
|
|
|
Future<void> _showRestrictedSmsDialog(BuildContext context) async {
|
|
await showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text("SMS Permission Restricted"),
|
|
content: const SingleChildScrollView(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text("It seems your device is restricting this app from sending SMS messages, which is required for verification. Please follow these steps to enable it:\n"),
|
|
Text("1. Open your device Settings.", style: TextStyle(fontWeight: FontWeight.bold)),
|
|
Text("2. Go to 'Apps' or 'Apps & notifications'."),
|
|
Text("3. Find and tap on this app ('KMobile')."),
|
|
Text("4. Tap on the three dots (⋮) in the top right corner."),
|
|
Text("5. Select 'Allow restricted settings' and confirm. This is crucial to allow SMS permission."),
|
|
Text("6. Now you have two options to allow SMS permission:"),
|
|
Text(" a. Tap on 'Permissions', then find 'SMS' is set to 'Allow'."),
|
|
Text(" b. Alternatively, you can return to the KMobile app, and the SMS permission pop-up should appear again, allowing you to grant it directly."),
|
|
Text("\nSome devices have an additional setting for 'Premium SMS'. If the above doesn't work, look for a 'Premium SMS access' setting (you can search for it in your Settings app) and set it to 'Always Allow' for this app.\n"),
|
|
Text("After you've enabled the permission, please come back to the app."),
|
|
],
|
|
),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
child: const Text("I've Enabled It"),
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
),
|
|
TextButton(
|
|
child: const Text("Open Settings"),
|
|
onPressed: () {
|
|
openAppSettings();
|
|
Navigator.of(context).pop();
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showSnackBar(BuildContext context, String message) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(message),
|
|
duration: const Duration(seconds: 3),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> initiateSmsSequence({
|
|
required BuildContext context,
|
|
}) async {
|
|
bool hasPermission = false;
|
|
|
|
// --- PERMISSION LOOP ---
|
|
while (!hasPermission) {
|
|
final status = await _smsService.handleSmsPermission();
|
|
|
|
switch (status) {
|
|
case PermissionStatusResult.granted:
|
|
_showSnackBar(context, "Permissions Granted! Proceeding...");
|
|
hasPermission = true; // This will break the loop
|
|
break;
|
|
case PermissionStatusResult.denied:
|
|
_showSnackBar(context, "SMS and Phone permissions are required. Please try again.");
|
|
await Future.delayed(const Duration(seconds: 3));
|
|
break;
|
|
case PermissionStatusResult.permanentlyDenied:
|
|
await showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text("Permission Required"),
|
|
content: const Text("SMS and Phone permissions are required for device verification. Please enable them in your app settings to continue."),
|
|
actions: [
|
|
TextButton(
|
|
child: const Text("Cancel"),
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
),
|
|
TextButton(
|
|
child: const Text("Open Settings"),
|
|
onPressed: () {
|
|
openAppSettings(); // Opens the phone's settings screen for this app
|
|
Navigator.of(context).pop();
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
// Wait for user to return from settings
|
|
await Future.delayed(const Duration(seconds: 5));
|
|
break;
|
|
case PermissionStatusResult.restricted:
|
|
await _showRestrictedSmsDialog(context);
|
|
// Wait for user to return from settings
|
|
await Future.delayed(const Duration(seconds: 10));
|
|
break;
|
|
}
|
|
}
|
|
|
|
// --- SMS SENDING LOOP ---
|
|
bool isSmsSent = false;
|
|
while (!isSmsSent) {
|
|
var uuid = const Uuid();
|
|
String uniqueId = uuid.v4();
|
|
String smsMessage = uniqueId;
|
|
_showSnackBar(context, "Attempting to send verification SMS...");
|
|
isSmsSent = await _smsService.sendVerificationSms(
|
|
context: context,
|
|
destinationNumber: '9580079717', // Replace with your number
|
|
message: smsMessage,
|
|
);
|
|
if (isSmsSent) {
|
|
_showSnackBar(context, "SMS sent successfully! Proceeding to login.");
|
|
break;
|
|
} else {
|
|
_showSnackBar(context, "SMS failed to send. Retrying in 5 seconds...");
|
|
await Future.delayed(const Duration(seconds: 5));
|
|
}
|
|
}
|
|
}
|
|
}
|