204 lines
5.1 KiB
Dart
204 lines
5.1 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:google_sign_in/google_sign_in.dart';
|
|
import 'package:stacked/stacked.dart';
|
|
import 'package:stacked_services/stacked_services.dart';
|
|
import 'package:yimaru_app/app/app.locator.dart';
|
|
import 'package:yimaru_app/app/app.router.dart';
|
|
import 'package:yimaru_app/models/user_model.dart';
|
|
|
|
import '../../../services/api_service.dart';
|
|
import '../../../services/authentication_service.dart';
|
|
import '../../../services/google_auth_service.dart';
|
|
import '../../../services/image_downloader_service.dart';
|
|
import '../../../services/status_checker_service.dart';
|
|
import '../../common/enmus.dart';
|
|
import '../../common/ui_helpers.dart';
|
|
import '../home/home_view.dart';
|
|
|
|
class LoginViewModel extends FormViewModel {
|
|
final _apiService = locator<ApiService>();
|
|
|
|
final _statusChecker = locator<StatusCheckerService>();
|
|
|
|
final _navigationService = locator<NavigationService>();
|
|
|
|
final _googleAuthService = locator<GoogleAuthService>();
|
|
|
|
final _authenticationService = locator<AuthenticationService>();
|
|
|
|
// In-app navigation
|
|
int _currentIndex = 0;
|
|
|
|
int get currentIndex => _currentIndex;
|
|
|
|
// Email
|
|
bool _focusEmail = false;
|
|
|
|
bool get focusEmail => _focusEmail;
|
|
|
|
// Password
|
|
bool _focusPassword = false;
|
|
|
|
bool get focusPassword => _focusPassword;
|
|
|
|
bool _obscurePassword = true;
|
|
|
|
bool get obscurePassword => _obscurePassword;
|
|
|
|
// Phone number
|
|
bool _focusPhoneNumber = false;
|
|
|
|
bool get focusPhoneNumber => _focusPhoneNumber;
|
|
|
|
// Focus otp
|
|
bool _focusOtp = false;
|
|
|
|
bool get focusOtp => _focusOtp;
|
|
|
|
// Focus node
|
|
final FocusNode _focusNode = FocusNode();
|
|
|
|
FocusNode get focusNode => _focusNode;
|
|
|
|
// Resend button state
|
|
bool _buttonActive = false;
|
|
|
|
bool get buttonActive => _buttonActive;
|
|
|
|
// User data
|
|
final Map<String, dynamic> _userData = {};
|
|
|
|
Map<String, dynamic> get userData => _userData;
|
|
|
|
// Email
|
|
void setEmailFocus() {
|
|
_focusEmail = true;
|
|
rebuildUi();
|
|
}
|
|
|
|
// Password
|
|
void setPasswordFocus() {
|
|
_focusPassword = true;
|
|
rebuildUi();
|
|
}
|
|
|
|
void setObscurePassword() {
|
|
_obscurePassword = !_obscurePassword;
|
|
rebuildUi();
|
|
}
|
|
|
|
// Phone number
|
|
void setPhoneNumberFocus() {
|
|
_focusPhoneNumber = true;
|
|
rebuildUi();
|
|
}
|
|
|
|
// Otp
|
|
void setOtpFocus() {
|
|
_focusOtp = true;
|
|
rebuildUi();
|
|
}
|
|
|
|
// Validate otp
|
|
Future<void> validateOtp(String otp) async {}
|
|
|
|
void setResendButton() {
|
|
_buttonActive = true;
|
|
rebuildUi();
|
|
}
|
|
|
|
// Add user data
|
|
void addUserData(Map<String, dynamic> data) {
|
|
_userData.addAll(data);
|
|
}
|
|
|
|
void clearUserData() {
|
|
_userData.clear();
|
|
}
|
|
|
|
// In app navigation
|
|
void goTo(int page) {
|
|
_currentIndex = page;
|
|
rebuildUi();
|
|
}
|
|
|
|
void goBack() {
|
|
if (_currentIndex == 1) {
|
|
_currentIndex = 0;
|
|
rebuildUi();
|
|
} else if (_currentIndex == 2) {
|
|
_currentIndex = 1;
|
|
rebuildUi();
|
|
} else {
|
|
_navigationService.back();
|
|
}
|
|
}
|
|
|
|
// Navigation
|
|
Future<void> navigateToRegister() async =>
|
|
await _navigationService.navigateToRegisterView();
|
|
|
|
Future<void> navigateToForgetPassword() async =>
|
|
await _navigationService.navigateToForgetPasswordView();
|
|
|
|
Future<void> replaceWithHome() async =>
|
|
await _navigationService.clearStackAndShowView(const HomeView());
|
|
|
|
// Remote api calls
|
|
|
|
// Login with email
|
|
Future<void> emailLogin() async => await runBusyFuture(_emailLogin(),
|
|
busyObject: StateObjects.loginWithEmail);
|
|
|
|
Future<void> _emailLogin() async {
|
|
if (await _statusChecker.checkConnection()) {
|
|
Map<String, dynamic> response = await _apiService.emailLogin(_userData);
|
|
if (response['status'] == ResponseStatus.success) {
|
|
UserModel user = response['data'] as UserModel;
|
|
Map<String, dynamic> data = {
|
|
'userId': user.userId,
|
|
'accessToken': user.accessToken,
|
|
'refreshToken': user.refreshToken
|
|
};
|
|
|
|
await _authenticationService.saveUserCredential(data);
|
|
clearUserData();
|
|
await replaceWithHome();
|
|
showSuccessToast(response['message']);
|
|
} else {
|
|
showErrorToast(response['message']);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> signInWithGoogle() async => await runBusyFuture(_signInWithGoogle(),
|
|
busyObject: StateObjects.loginWithGoogle);
|
|
|
|
Future<void> _signInWithGoogle() async {
|
|
if (await _statusChecker.checkConnection()) {
|
|
GoogleSignInAccount? googleUser = await _googleAuthService.googleAuth();
|
|
|
|
Map<String, dynamic> data = {
|
|
'id_token': googleUser?.authentication.idToken ?? '',
|
|
};
|
|
|
|
Map<String, dynamic> response = await _apiService.googleAuth(data);
|
|
|
|
if (response['status'] == ResponseStatus.success) {
|
|
UserModel user = response['data'] as UserModel;
|
|
Map<String, dynamic> data = {
|
|
'userId': user.userId,
|
|
'accessToken': user.accessToken,
|
|
'refreshToken': user.refreshToken
|
|
};
|
|
await _authenticationService.saveUserCredential(data);
|
|
clearUserData();
|
|
await replaceWithHome();
|
|
showSuccessToast(response['message']);
|
|
} else {
|
|
showErrorToast(response['message']);
|
|
}
|
|
}
|
|
}
|
|
}
|