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(); final _statusChecker = locator(); final _navigationService = locator(); final _googleAuthService = locator(); final _authenticationService = locator(); // 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 _userData = {}; Map 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 validateOtp(String otp) async {} void setResendButton() { _buttonActive = true; rebuildUi(); } // Add user data void addUserData(Map 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 navigateToRegister() async => await _navigationService.navigateToRegisterView(); Future navigateToForgetPassword() async => await _navigationService.navigateToForgetPasswordView(); Future replaceWithHome() async => await _navigationService.clearStackAndShowView(const HomeView()); // Remote api calls // Login with email Future emailLogin() async => await runBusyFuture(_emailLogin(), busyObject: StateObjects.loginWithEmail); Future _emailLogin() async { if (await _statusChecker.checkConnection()) { Map response = await _apiService.emailLogin(_userData); if (response['status'] == ResponseStatus.success) { UserModel user = response['data'] as UserModel; Map 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 signInWithGoogle() async => await runBusyFuture(_signInWithGoogle(), busyObject: StateObjects.loginWithGoogle); Future _signInWithGoogle() async { if (await _statusChecker.checkConnection()) { GoogleSignInAccount? googleUser = await _googleAuthService.googleAuth(); Map data = { 'id_token': googleUser?.authentication.idToken ?? '', }; Map response = await _apiService.googleAuth(data); if (response['status'] == ResponseStatus.success) { UserModel user = response['data'] as UserModel; Map data = { 'userId': user.userId, 'accessToken': user.accessToken, 'refreshToken': user.refreshToken }; await _authenticationService.saveUserCredential(data); clearUserData(); await replaceWithHome(); showSuccessToast(response['message']); } else { showErrorToast(response['message']); } } } }