content: package com.gero.newpass.viewmodel;
  
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.util.Log;
  
  import androidx.annotation.NonNull;
  import androidx.biometric.BiometricManager;
  
  import androidx.biometric.BiometricPrompt;
  import androidx.core.content.ContextCompat;
  import androidx.fragment.app.FragmentActivity;
  import androidx.lifecycle.LiveData;
  import androidx.lifecycle.MutableLiveData;
  import androidx.lifecycle.ViewModel;
  import androidx.security.crypto.EncryptedSharedPreferences;
  
  import com.gero.newpass.R;
  import com.gero.newpass.encryption.HashUtils;
  import com.gero.newpass.repository.ResourceRepository;
  
  import java.security.NoSuchAlgorithmException;
  import java.security.spec.InvalidKeySpecException;
  import java.util.concurrent.Executor;
  
  public class LoginViewModel extends ViewModel {
  
      private final MutableLiveData<String> loginMessageLiveData = new MutableLiveData<>();
      private final MutableLiveData<Boolean> loginSuccessLiveData = new MutableLiveData<>();
      private final ResourceRepository resourceRepository;
  
  
      public LoginViewModel(ResourceRepository resourceRepository) {
          this.resourceRepository =  resourceRepository;
      }
  
  
      public LiveData<String> getLoginMessageLiveData() {
          return loginMessageLiveData;
      }
      public LiveData<Boolean> getLoginSuccessLiveData() {
          return loginSuccessLiveData;
      }
  
  
      public void createUser(String password, EncryptedSharedPreferences sharedPreferences) throws NoSuchAlgorithmException, InvalidKeySpecException {
  
          if (password.length() >= 4) {
              SharedPreferences.Editor editor = sharedPreferences.edit();
  
              String hashedPassword = HashUtils.hashPassword(password);
              editor.putString("password", hashedPassword);
              editor.apply();
  
              loginSuccessLiveData.setValue(true);
              loginMessageLiveData.setValue(resourceRepository.getString(R.string.user_created_successfully));
          } else {
              loginSuccessLiveData.setValue(false);
              loginMessageLiveData.setValue(resourceRepository.getString(R.string.password_must_be_at_least_4_characters_long));
          }
      }
  
      public void loginUserWithPassword(String password, EncryptedSharedPreferences sharedPreferences) throws NoSuchAlgorithmException, InvalidKeySpecException {
  
          String hashedPassword = sharedPreferences.getString("password", "");
  
          if (HashUtils.verifyPassword(password, hashedPassword)) {
              loginSuccessLiveData.setValue(true);
              loginMessageLiveData.setValue(resourceRepository.getString(R.string.login_done));
          } else {
              loginSuccessLiveData.setValue(false);
              loginMessageLiveData.setValue(resourceRepository.getString(R.string.access_denied));
          }
      }
  
      public void loginUserWithBiometricAuth(Context context) {
          Executor executor = ContextCompat.getMainExecutor(context);
          BiometricPrompt biometricPrompt = new BiometricPrompt((FragmentActivity) context, executor, new BiometricPrompt.AuthenticationCallback() {
              @Override
              public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
                  super.onAuthenticationError(errorCode, errString);
                  loginSuccessLiveData.postValue(false);
                  loginMessageLiveData.postValue(errString.toString());
              }
  
              @Override
              public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
                  super.onAuthenticationSucceeded(result);
                  loginSuccessLiveData.postValue(true);
                  loginMessageLiveData.postValue(resourceRepository.getString(R.string.login_done));
              }
  
              @Override
              public void onAuthenticationFailed() {
                  super.onAuthenticationFailed();
                  loginSuccessLiveData.postValue(false);
                  loginMessageLiveData.postValue(resourceRepository.getString(R.string.access_denied));
              }
          });
  
          BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
                  .setTitle(context.getString(R.string.login))
                  .setSubtitle(context.getString(R.string.use_your_biometric_or_device_credentials))
                  .setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_WEAK | BiometricManager.Authenticators.DEVICE_CREDENTIAL)
                  .build();
  
          biometricPrompt.authenticate(promptInfo);
      }
  }
  