SMS integrated with new ui
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:kmobile/app.dart';
|
||||
import 'package:kmobile/features/auth/screens/mpin_screen.dart';
|
||||
import 'package:kmobile/features/auth/screens/set_password_screen.dart';
|
||||
import 'package:kmobile/features/auth/screens/tnc_required_screen.dart';
|
||||
import 'package:kmobile/features/auth/screens/verification_screen.dart';
|
||||
import 'package:kmobile/widgets/tnc_dialog.dart';
|
||||
import '../../../l10n/app_localizations.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -30,12 +31,23 @@ class LoginScreenState extends State<LoginScreen>
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _submitForm() {
|
||||
void _submitForm() async {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
context.read<AuthCubit>().login(
|
||||
_customerNumberController.text.trim(),
|
||||
_passwordController.text,
|
||||
);
|
||||
final bool? verificationSuccess = await Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => VerificationScreen(
|
||||
customerNo: _customerNumberController.text.trim(),
|
||||
password: _passwordController.text,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
if (verificationSuccess == true && mounted) {
|
||||
context.read<AuthCubit>().login(
|
||||
_customerNumberController.text.trim(),
|
||||
_passwordController.text,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
169
lib/features/auth/screens/sms_verification_helper.dart
Normal file
169
lib/features/auth/screens/sms_verification_helper.dart
Normal file
@@ -0,0 +1,169 @@
|
||||
|
||||
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> _showPermanentlyDeniedDialog(BuildContext context) async {
|
||||
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();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
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(),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showSnackBar(BuildContext context, String message) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(message),
|
||||
duration: const Duration(seconds: 3),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<String?> initiateSmsSequence({
|
||||
required BuildContext context,
|
||||
}) async {
|
||||
bool hasPermission = false;
|
||||
|
||||
// --- PERMISSION LOOP ---
|
||||
while (!hasPermission) {
|
||||
// handleSmsPermission will check the status and request if not granted.
|
||||
final status = await _smsService.handleSmsPermission();
|
||||
|
||||
switch (status) {
|
||||
case PermissionStatusResult.granted:
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text("Permissions Granted! Proceeding..."), duration: Duration(seconds: 2)),
|
||||
);
|
||||
hasPermission = true; // This will break the loop
|
||||
break;
|
||||
case PermissionStatusResult.denied:
|
||||
// The user denied the permission. We show a dialog to explain why we need it
|
||||
// and give them a chance to cancel or let the loop try again.
|
||||
final tryAgain = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text("Permission Required"),
|
||||
content: const Text("This app requires SMS and Phone permissions to verify your device. Please grant the permissions to continue."),
|
||||
actions: [
|
||||
TextButton(
|
||||
child: const Text("Cancel"),
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
),
|
||||
TextButton(
|
||||
child: const Text("Try Again"),
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (tryAgain != true) {
|
||||
return null; // User chose to cancel.
|
||||
}
|
||||
// If they chose "Try Again", the loop will repeat.
|
||||
break;
|
||||
case PermissionStatusResult.permanentlyDenied:
|
||||
await _showPermanentlyDeniedDialog(context);
|
||||
// Give user time to come back from settings
|
||||
await Future.delayed(const Duration(seconds: 5));
|
||||
// The loop will repeat and re-check the status.
|
||||
break;
|
||||
case PermissionStatusResult.restricted:
|
||||
await _showRestrictedSmsDialog(context);
|
||||
// Give user time to come back from settings
|
||||
await Future.delayed(const Duration(seconds: 5));
|
||||
// The loop will repeat and re-check the status.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// --- SMS SENDING LOOP ---
|
||||
// This part will only be reached if hasPermission is true.
|
||||
int retries = 3;
|
||||
while (retries > 0) {
|
||||
var uuid = const Uuid();
|
||||
String uniqueId = uuid.v4();
|
||||
String smsMessage = uniqueId;
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text("Attempting to send verification SMS... (${4 - retries})"), duration: const Duration(seconds: 2)),
|
||||
);
|
||||
|
||||
bool isSmsSent = await _smsService.sendVerificationSms(
|
||||
context: context,
|
||||
destinationNumber: '9580079717', // Replace with your number
|
||||
message: smsMessage,
|
||||
);
|
||||
|
||||
if (isSmsSent) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text("SMS sent successfully!"), duration: Duration(seconds: 2)),
|
||||
);
|
||||
return uniqueId;
|
||||
} else {
|
||||
retries--;
|
||||
if (retries > 0) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text("SMS failed to send. Retrying in 5 seconds..."), duration: Duration(seconds: 4)),
|
||||
);
|
||||
await Future.delayed(const Duration(seconds: 5));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If all retries fail
|
||||
return null;
|
||||
}
|
||||
}
|
||||
166
lib/features/auth/screens/verification_screen.dart
Normal file
166
lib/features/auth/screens/verification_screen.dart
Normal file
@@ -0,0 +1,166 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:kmobile/api/services/auth_service.dart';
|
||||
import 'package:kmobile/di/injection.dart';
|
||||
import 'package:kmobile/features/auth/screens/sms_verification_helper.dart';
|
||||
|
||||
class VerificationScreen extends StatefulWidget {
|
||||
final String customerNo;
|
||||
final String password;
|
||||
|
||||
const VerificationScreen({
|
||||
super.key,
|
||||
required this.customerNo,
|
||||
required this.password,
|
||||
});
|
||||
|
||||
@override
|
||||
State<VerificationScreen> createState() => _VerificationScreenState();
|
||||
}
|
||||
|
||||
class _VerificationScreenState extends State<VerificationScreen> {
|
||||
String _statusMessage = "Starting verification...";
|
||||
Timer? _timer;
|
||||
int _countdown = 120;
|
||||
bool _isVerifying = false;
|
||||
bool _verificationFailed = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_startVerificationProcess();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_timer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _startTimer() {
|
||||
_timer?.cancel(); // Cancel any existing timer
|
||||
_countdown = 120;
|
||||
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
||||
if (_countdown > 0) {
|
||||
setState(() {
|
||||
_countdown--;
|
||||
});
|
||||
} else {
|
||||
_timer?.cancel();
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_statusMessage = "Verification timed out. Please try again.";
|
||||
_isVerifying = false;
|
||||
_verificationFailed = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _startVerificationProcess() async {
|
||||
setState(() {
|
||||
_isVerifying = true;
|
||||
_verificationFailed = false;
|
||||
_statusMessage = "Starting verification...";
|
||||
});
|
||||
_startTimer();
|
||||
|
||||
// 1. Send SMS
|
||||
setState(() {
|
||||
_statusMessage = "SMS sending...";
|
||||
});
|
||||
final smsHelper = SmsVerificationHelper();
|
||||
final uuid = await smsHelper.initiateSmsSequence(context: context);
|
||||
|
||||
if (uuid != null && mounted) {
|
||||
// SMS sending was successful, now wait before verifying.
|
||||
setState(() {
|
||||
_statusMessage = "SMS sent. Waiting for network delivery...";
|
||||
});
|
||||
|
||||
// Adding a 10-second delay to account for SMS network latency.
|
||||
await Future.delayed(const Duration(seconds: 10));
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
// 2. Verify SIM
|
||||
setState(() {
|
||||
_statusMessage = "Verifying with server...";
|
||||
});
|
||||
|
||||
final authService = getIt<AuthService>();
|
||||
try {
|
||||
await authService.simVerify(uuid, widget.customerNo);
|
||||
|
||||
setState(() {
|
||||
_statusMessage = "Verification successful!";
|
||||
_isVerifying = false;
|
||||
});
|
||||
_timer?.cancel();
|
||||
|
||||
// Pop with success result
|
||||
Navigator.of(context).pop(true);
|
||||
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_statusMessage = " $e SIM verification failed. Please try again.";
|
||||
_isVerifying = false;
|
||||
_verificationFailed = true;
|
||||
});
|
||||
}
|
||||
} else if (mounted) {
|
||||
setState(() {
|
||||
_statusMessage = "SMS sending failed. Please check permissions and try again.";
|
||||
_isVerifying = false;
|
||||
_verificationFailed = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text("Device Verification"),
|
||||
automaticallyImplyLeading: !_isVerifying,
|
||||
),
|
||||
body: Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24.0),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
if (_isVerifying)
|
||||
const CircularProgressIndicator(),
|
||||
if (!_isVerifying && _verificationFailed)
|
||||
const Icon(Icons.error_outline, color: Colors.red, size: 50),
|
||||
if (!_isVerifying && !_verificationFailed)
|
||||
const Icon(Icons.check_circle_outline, color: Colors.green, size: 50),
|
||||
|
||||
const SizedBox(height: 32),
|
||||
Text(
|
||||
_statusMessage,
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(fontSize: 18),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
if (_isVerifying)
|
||||
Text(
|
||||
"Time remaining: $_countdown seconds",
|
||||
style: const TextStyle(fontSize: 16, color: Colors.grey),
|
||||
),
|
||||
if (_verificationFailed && !_isVerifying) ...[
|
||||
const SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: _startVerificationProcess,
|
||||
child: const Text('Retry'),
|
||||
),
|
||||
]
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user