Files
tm_app/lib/view_models/reset_password_view_model.dart
amirrezaghabeli cee5e6c6d6 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.
2025-10-06 15:39:08 +03:30

95 lines
2.6 KiB
Dart

import 'package:flutter/material.dart';
import '../core/utils/error_utils.dart';
import '../core/utils/result.dart';
import '../data/repositories/auth_repository.dart';
import '../data/services/model/reset_password_response/reset_password_response.dart';
class ResetPasswordViewModel with ChangeNotifier {
final AuthRepository _authRepository;
ResetPasswordViewModel({required AuthRepository authRepository})
: _authRepository = authRepository {
newPasswordController.addListener(_onTextChanged);
}
final TextEditingController newPasswordController = TextEditingController();
final FocusNode newPasswordFocus = FocusNode();
bool _iLoading = false;
String? _errorMessage;
bool _successMessage = false;
bool _obscureNewPassword = true;
bool get obscureNewPassword => _obscureNewPassword;
bool get isLoading => _iLoading;
String? get errorMessage => _errorMessage;
bool get successMessage => _successMessage;
Future<void> resetPassword({required String resetToken}) async {
_iLoading = true;
_errorMessage = null;
_successMessage = false;
notifyListeners();
final result = await _authRepository.resetPassword(
newPassword: newPasswordController.text.trim(),
token: resetToken,
);
switch (result) {
case Ok<ResetPasswordResponse>():
_successMessage = true;
break;
case Error<ResetPasswordResponse>():
_errorMessage = ErrorUtils.getErrorMessage(result.error);
_successMessage = false;
break;
}
_iLoading = false;
_errorMessage = null;
notifyListeners();
}
void clearResetPasswordSuccess() {
_successMessage = false;
notifyListeners();
}
void clearResetFlow() {
_errorMessage = null;
_successMessage = false;
_iLoading = false;
// Intentionally do not clear resetToken
newPasswordController.clear();
notifyListeners();
}
bool get isNewPasswordValid {
final password = newPasswordController.text.trim();
final hasMinLength = password.length >= 5;
final hasUppercase = RegExp(r'[A-Z]').hasMatch(password);
final hasLowercase = RegExp(r'[a-z]').hasMatch(password);
final hasSpecialChar = RegExp(r'[!@#\$%&\*\^]').hasMatch(password);
return hasMinLength && hasUppercase && hasLowercase && hasSpecialChar;
}
void toggleForgotPasswordVisibility() {
_obscureNewPassword = !_obscureNewPassword;
notifyListeners();
}
void _onTextChanged() {
notifyListeners();
}
@override
void dispose() {
newPasswordController.dispose();
newPasswordFocus.dispose();
super.dispose();
}
}