SMS integrated with new ui
This commit is contained in:
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