Change TPIn #4
This commit is contained in:
@@ -1,164 +1,146 @@
|
||||
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';
|
||||
import '../../../api/services/change_password_service.dart';
|
||||
import 'dart:convert';
|
||||
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';
|
||||
import '../../../api/services/change_password_service.dart';
|
||||
|
||||
class ChangeTpinScreen extends StatefulWidget {
|
||||
final String mobileNumber;
|
||||
const ChangeTpinScreen({super.key, required this.mobileNumber});
|
||||
class ChangeTpinScreen extends StatefulWidget {
|
||||
final String mobileNumber;
|
||||
const ChangeTpinScreen({super.key, required this.mobileNumber});
|
||||
|
||||
@override
|
||||
State<ChangeTpinScreen> createState() => _ChangeTpinScreenState();
|
||||
}
|
||||
@override
|
||||
State<ChangeTpinScreen> createState() => _ChangeTpinScreenState();
|
||||
}
|
||||
|
||||
class _ChangeTpinScreenState extends State<ChangeTpinScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _oldTpinController = TextEditingController();
|
||||
final _newTpinController = TextEditingController();
|
||||
final _confirmTpinController = TextEditingController();
|
||||
final ChangePasswordService _changePasswordService = getIt<ChangePasswordService>();
|
||||
bool _isLoading = false;
|
||||
class _ChangeTpinScreenState extends State<ChangeTpinScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _oldTpinController = TextEditingController();
|
||||
final _newTpinController = TextEditingController();
|
||||
final _confirmTpinController = TextEditingController();
|
||||
final ChangePasswordService _changePasswordService =
|
||||
getIt<ChangePasswordService>();
|
||||
bool _isLoading = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_oldTpinController.dispose();
|
||||
_newTpinController.dispose();
|
||||
_confirmTpinController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
@override
|
||||
void dispose() {
|
||||
_oldTpinController.dispose();
|
||||
_newTpinController.dispose();
|
||||
_confirmTpinController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _handleChangeTpin() async {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
try {
|
||||
// 1. Validate the current TPIN and new TPIN
|
||||
final responseString = await _changePasswordService.validateChangeTpin(
|
||||
oldTpin: _oldTpinController.text,
|
||||
newTpin: _newTpinController.text,
|
||||
);
|
||||
void _handleChangeTpin() async {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
try {
|
||||
// 1. Get OTP for TPIN change.
|
||||
await _changePasswordService.getOtpTpin(mobileNumber: '8981274001');
|
||||
|
||||
// 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. Navigate to the OTP screen on success.
|
||||
if (mounted) {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => ChangeTpinOtpScreen(
|
||||
oldTpin: _oldTpinController.text,
|
||||
newTpin: _newTpinController.text,
|
||||
mobileNumber: '8981274001',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Failed to send OTP: $e')),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Change TPIN'),
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text('Current TPIN'),
|
||||
const SizedBox(height: 8),
|
||||
PinInputField(
|
||||
controller: _oldTpinController,
|
||||
validator: (value) {
|
||||
if (value == null || value.length != 6) {
|
||||
return 'Please enter your 6-digit old TPIN';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
const Text('New TPIN'),
|
||||
const SizedBox(height: 8),
|
||||
PinInputField(
|
||||
controller: _newTpinController,
|
||||
validator: (value) {
|
||||
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;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
const Text('Confirm New TPIN'),
|
||||
const SizedBox(height: 8),
|
||||
PinInputField(
|
||||
controller: _confirmTpinController,
|
||||
validator: (value) {
|
||||
if (value == null || value.length != 6) {
|
||||
return 'Please confirm your new TPIN';
|
||||
}
|
||||
if (value != _newTpinController.text) {
|
||||
return 'TPINs do not match';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 32),
|
||||
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'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Change TPIN'),
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text('Current TPIN'),
|
||||
const SizedBox(height: 8),
|
||||
PinInputField(
|
||||
controller: _oldTpinController,
|
||||
validator: (value) {
|
||||
if (value == null || value.length != 6) {
|
||||
return 'Please enter your 6-digit old TPIN';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
const Text('New TPIN'),
|
||||
const SizedBox(height: 8),
|
||||
PinInputField(
|
||||
controller: _newTpinController,
|
||||
validator: (value) {
|
||||
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;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
const Text('Confirm New TPIN'),
|
||||
const SizedBox(height: 8),
|
||||
PinInputField(
|
||||
controller: _confirmTpinController,
|
||||
validator: (value) {
|
||||
if (value == null || value.length != 6) {
|
||||
return 'Please confirm your new TPIN';
|
||||
}
|
||||
if (value != _newTpinController.text) {
|
||||
return 'TPINs do not match';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 32),
|
||||
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'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user