Change TPIn #3

This commit is contained in:
2025-11-08 16:56:54 +05:30
parent 87fd36b748
commit c26cc507a1
4 changed files with 178 additions and 25 deletions

View File

@@ -1,9 +1,13 @@
import 'package:flutter/material.dart';
import 'package:kmobile/di/injection.dart';
import 'package:kmobile/features/profile/tpin/change_tpin_otp_screen.dart';
import 'package:kmobile/widgets/pin_input_field.dart'; // Use the new widget
import 'package:kmobile/widgets/pin_input_field.dart';
import '../../../api/services/change_password_service.dart';
import 'dart:convert';
class ChangeTpinScreen extends StatefulWidget {
const ChangeTpinScreen({super.key});
final String mobileNumber;
const ChangeTpinScreen({super.key, required this.mobileNumber});
@override
State<ChangeTpinScreen> createState() => _ChangeTpinScreenState();
@@ -14,6 +18,8 @@
final _oldTpinController = TextEditingController();
final _newTpinController = TextEditingController();
final _confirmTpinController = TextEditingController();
final ChangePasswordService _changePasswordService = getIt<ChangePasswordService>();
bool _isLoading = false;
@override
void dispose() {
@@ -23,16 +29,58 @@
super.dispose();
}
void _handleChangeTpin() {
void _handleChangeTpin() async {
if (_formKey.currentState!.validate()) {
// TODO: Add API call to request OTP for TPIN change
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ChangeTpinOtpScreen(
newTpin: _newTpinController.text,
),
),
);
setState(() {
_isLoading = true;
});
try {
// 1. Validate the current TPIN and new TPIN
final responseString = await _changePasswordService.validateChangeTpin(
oldTpin: _oldTpinController.text,
newTpin: _newTpinController.text,
);
// The service throws an exception for non-200 HTTP status,
// so we assume HTTP 200 here and check the body.
final response = jsonDecode(responseString);
// 2. Check the status code from the response body
if (response.statusCode == 200) {
// 3. Get OTP for TPIN change
await _changePasswordService.getOtpTpin(mobileNumber: '8981274001');
// 4. Navigate to OTP screen
if (mounted) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ChangeTpinOtpScreen(
newTpin: _newTpinController.text,
mobileNumber: '8981274001', // Pass mobile number
),
),
);
}
} else {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(response['message'] ?? 'Invalid TPIN details')
));
}
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('An error occurred: $e')),
);
}
} finally {
if (mounted) {
setState(() {
_isLoading = false;
});
}
}
}
}
@@ -69,6 +117,9 @@
if (value == null || value.length != 6) {
return 'Please enter a 6-digit new TPIN';
}
if (value == _oldTpinController.text) {
return 'New TPIN must be different from the old one.';
}
return null;
},
),
@@ -88,9 +139,21 @@
},
),
const SizedBox(height: 32),
ElevatedButton(
onPressed: _handleChangeTpin,
child: const Text('Proceed'),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _isLoading ? null : _handleChangeTpin,
child: _isLoading
? const SizedBox(
height: 24,
width: 24,
child: CircularProgressIndicator(
color: Colors.white,
strokeWidth: 2.5,
),
)
: const Text('Proceed'),
),
),
],
),