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 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(): _successMessage = true; break; case Error(): _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(); } }