43 lines
1.0 KiB
Dart
43 lines
1.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../core/utils/result.dart';
|
|
import '../data/models/user_model.dart';
|
|
import '../data/repositories/auth_repository.dart';
|
|
|
|
class AuthViewModel with ChangeNotifier {
|
|
final AuthRepository _authRepository;
|
|
|
|
AuthViewModel({required AuthRepository authRepository})
|
|
: _authRepository = authRepository;
|
|
|
|
bool _isLoading = false;
|
|
String? _errorMessage;
|
|
UserModel? _loggedInUser;
|
|
|
|
bool get isLoading => _isLoading;
|
|
String? get errorMessage => _errorMessage;
|
|
UserModel? get loggedInUser => _loggedInUser;
|
|
|
|
Future<void> login(String email, String password) async {
|
|
_isLoading = true;
|
|
_errorMessage = null;
|
|
notifyListeners();
|
|
|
|
final result = await _authRepository.login(email, password);
|
|
|
|
switch (result) {
|
|
case Ok<UserModel>():
|
|
_loggedInUser = result.value;
|
|
// موفقیتآمیز
|
|
break;
|
|
case Error<UserModel>():
|
|
_errorMessage = result.error.toString();
|
|
// خطا
|
|
break;
|
|
}
|
|
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
}
|
|
}
|