Add forgot password, reset password, and verify OTP providers and view models
- Implemented `ForgotPasswordViewModel`, `ResetPasswordViewModel`, and `VerifyOtpViewModel` for handling respective functionalities. - Created provider functions for lazy loading of these view models in the app. - Updated routing to utilize the new providers for managing state in the forgot password flow. - Refactored UI components to integrate with the new view models, ensuring proper state management and user feedback during operations.
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:tm_app/core/utils/result.dart';
|
||||
|
||||
import '../data/repositories/auth_repository.dart';
|
||||
import '../data/services/model/verify_otp_response/verify_otp_response_model.dart';
|
||||
|
||||
class VerifyOtpViewModel with ChangeNotifier {
|
||||
final AuthRepository _authRepository;
|
||||
|
||||
VerifyOtpViewModel({required AuthRepository authRepository})
|
||||
: _authRepository = authRepository {
|
||||
otpController.addListener(_onTextChanged);
|
||||
}
|
||||
|
||||
final TextEditingController otpController = TextEditingController();
|
||||
|
||||
final FocusNode otpFocus = FocusNode();
|
||||
|
||||
bool _isLoading = false;
|
||||
String? _errorMessage;
|
||||
String? _successMessage;
|
||||
bool _verifyOtpSuccess = false;
|
||||
String? _resetToken;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
String? get errorMessage => _errorMessage;
|
||||
String? get successMessage => _successMessage;
|
||||
bool get verifyOtpSuccess => _verifyOtpSuccess;
|
||||
String? get resetToken => _resetToken;
|
||||
|
||||
Future<void> verifyOtp({required String email}) async {
|
||||
_isLoading = true;
|
||||
_errorMessage = null;
|
||||
_verifyOtpSuccess = false;
|
||||
notifyListeners();
|
||||
|
||||
final result = await _authRepository.verifyOtp(
|
||||
code: otpController.text.trim(),
|
||||
email: email,
|
||||
);
|
||||
|
||||
switch (result) {
|
||||
case Ok<VerifyOtpResponseModel>():
|
||||
_isLoading = false;
|
||||
_resetToken = result.value.data.token;
|
||||
_successMessage = result.value.message;
|
||||
_verifyOtpSuccess = true;
|
||||
notifyListeners();
|
||||
|
||||
break;
|
||||
|
||||
case Error<VerifyOtpResponseModel>():
|
||||
_isLoading = false;
|
||||
_errorMessage = result.error.toString();
|
||||
_verifyOtpSuccess = false;
|
||||
notifyListeners();
|
||||
break;
|
||||
}
|
||||
|
||||
_errorMessage = null;
|
||||
_successMessage = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void clearOtpFlow() {
|
||||
_errorMessage = null;
|
||||
_successMessage = null;
|
||||
_isLoading = false;
|
||||
_verifyOtpSuccess = false;
|
||||
// Intentionally do not clear resetToken
|
||||
otpController.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void _onTextChanged() {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
bool get isOtpValid {
|
||||
final otp = otpController.text.trim();
|
||||
return otp.length == 6;
|
||||
}
|
||||
|
||||
bool get canSubmitOtpPage {
|
||||
return isOtpValid;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
otpController.dispose();
|
||||
otpFocus.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user