changed logoRepository to authRepository
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../core/utils/result.dart';
|
||||
import '../data/models/user_model.dart';
|
||||
import '../data/repositories/auth_repository.dart';
|
||||
@@ -8,8 +7,25 @@ class AuthViewModel with ChangeNotifier {
|
||||
final AuthRepository _authRepository;
|
||||
|
||||
AuthViewModel({required AuthRepository authRepository})
|
||||
: _authRepository = authRepository;
|
||||
: _authRepository = authRepository {
|
||||
usernameController.addListener(_onTextChanged);
|
||||
passwordController.addListener(_onTextChanged);
|
||||
usernameFocus.addListener(_onTextChanged);
|
||||
passwordFocus.addListener(_onTextChanged);
|
||||
}
|
||||
|
||||
final TextEditingController usernameController = TextEditingController();
|
||||
final TextEditingController passwordController = TextEditingController();
|
||||
final FocusNode usernameFocus = FocusNode();
|
||||
final FocusNode passwordFocus = FocusNode();
|
||||
|
||||
bool _obscurePassword = true;
|
||||
bool get obscurePassword => _obscurePassword;
|
||||
bool get isLoginEnabled =>
|
||||
usernameController.text.isNotEmpty &&
|
||||
passwordController.text.isNotEmpty;
|
||||
|
||||
// Auth state
|
||||
bool _isLoading = false;
|
||||
String? _errorMessage;
|
||||
UserModel? _loggedInUser;
|
||||
@@ -18,25 +34,44 @@ class AuthViewModel with ChangeNotifier {
|
||||
String? get errorMessage => _errorMessage;
|
||||
UserModel? get loggedInUser => _loggedInUser;
|
||||
|
||||
Future<void> login(String email, String password) async {
|
||||
void togglePasswordVisibility() {
|
||||
_obscurePassword = !_obscurePassword;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void _onTextChanged() {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> login() async {
|
||||
_isLoading = true;
|
||||
_errorMessage = null;
|
||||
notifyListeners();
|
||||
|
||||
final result = await _authRepository.login(email, password);
|
||||
final result = await _authRepository.login(
|
||||
usernameController.text.trim(),
|
||||
passwordController.text.trim(),
|
||||
);
|
||||
|
||||
switch (result) {
|
||||
case Ok<UserModel>():
|
||||
_loggedInUser = result.value;
|
||||
// موفقیتآمیز
|
||||
break;
|
||||
case Error<UserModel>():
|
||||
_errorMessage = result.error.toString();
|
||||
// خطا
|
||||
break;
|
||||
}
|
||||
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
usernameController.dispose();
|
||||
passwordController.dispose();
|
||||
usernameFocus.dispose();
|
||||
passwordFocus.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
// lib/view_models/login_view_model.dart
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LoginViewModel with ChangeNotifier {
|
||||
final TextEditingController usernameController = TextEditingController();
|
||||
final TextEditingController passwordController = TextEditingController();
|
||||
|
||||
final FocusNode usernameFocus = FocusNode();
|
||||
final FocusNode passwordFocus = FocusNode();
|
||||
|
||||
bool _obscurePassword = true;
|
||||
|
||||
LoginViewModel() {
|
||||
usernameController.addListener(_onTextChanged);
|
||||
passwordController.addListener(_onTextChanged);
|
||||
usernameFocus.addListener(_onTextChanged);
|
||||
passwordFocus.addListener(_onTextChanged);
|
||||
}
|
||||
|
||||
bool get obscurePassword => _obscurePassword;
|
||||
bool get isLoginEnabled =>
|
||||
usernameController.text.isNotEmpty &&
|
||||
passwordController.text.isNotEmpty;
|
||||
|
||||
void togglePasswordVisibility() {
|
||||
_obscurePassword = !_obscurePassword;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void _onTextChanged() {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
usernameController.dispose();
|
||||
passwordController.dispose();
|
||||
usernameFocus.dispose();
|
||||
passwordFocus.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user