Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.remek.rosettapos.ui.authentication
- import android.app.Application
- import androidx.lifecycle.AndroidViewModel
- import androidx.lifecycle.LiveData
- import androidx.lifecycle.MutableLiveData
- import com.remek.rosettapos.R
- import com.remek.rosettapos.communication.api.Api
- import com.remek.rosettapos.communication.api.ApiCallback
- import com.remek.rosettapos.communication.api.CommunicationException
- import com.remek.rosettapos.communication.types.DeviceConfiguration
- import com.remek.rosettapos.communication.types.LoginRequest
- import com.remek.rosettapos.communication.types.Tokens
- import com.remek.rosettapos.configuration.ConfigurationStorageImpl
- import com.remek.rosettapos.token.TokenStorageImpl
- import retrofit2.Retrofit
- import retrofit2.converter.gson.GsonConverterFactory
- /**
- * View model for the login screen.
- */
- class LoginScreenViewModel(application: Application) : AndroidViewModel(application) {
- enum class State {
- PROCESS,
- ERROR,
- LOGIN
- }
- private val configurationStorage = ConfigurationStorageImpl(application)
- private val tokenStorage = TokenStorageImpl(application)
- private val api = Retrofit.Builder()
- .baseUrl(application.getString(R.string.api_url))
- .addConverterFactory(GsonConverterFactory.create())
- .build()
- .create(Api::class.java)
- private var configurationCheckStarted = false
- private val communicationInProgress: MutableLiveData<Boolean> = MutableLiveData()
- private val processTextVisible: MutableLiveData<Boolean> = MutableLiveData()
- private val errorTextVisible: MutableLiveData<Boolean> = MutableLiveData()
- private val errorText: MutableLiveData<String> = MutableLiveData()
- private val loginFormVisible: MutableLiveData<Boolean> = MutableLiveData()
- private val loginErrorTextVisible: MutableLiveData<Boolean> = MutableLiveData()
- private val loginErrorText: MutableLiveData<String> = MutableLiveData()
- private val errorTextMissingDeviceId = application.getString(R.string.login_screen_error_device_id_missing)
- private val errorTextUnknown = application.getString(R.string.login_screen_error_unknown)
- private val errorTextInvalidLoginData =
- application.getString(R.string.login_screen_error_invalid_username_or_password)
- private val loginSucceeded: MutableLiveData<Boolean> = MutableLiveData()
- init {
- communicationInProgress.value = false
- processTextVisible.value = true
- errorTextVisible.value = false
- loginFormVisible.value = false
- loginSucceeded.value = false
- }
- fun hasConfigurationCheckStarted(): Boolean {
- return configurationCheckStarted
- }
- fun checkConfiguration() {
- configurationCheckStarted = true
- communicationInProgress.postValue(true)
- configurationStorage.retrieve()?.id?.let { id ->
- api.getDeviceConfig(id).enqueue(deviceConfigurationCallback())
- } ?: run {
- api.createDeviceConfig().enqueue(deviceConfigurationCallback())
- }
- }
- fun deviceConfigurationCallback(): ApiCallback<DeviceConfiguration> {
- return object : ApiCallback<DeviceConfiguration> {
- override fun onSuccess(response: DeviceConfiguration) {
- communicationInProgress.postValue(false)
- configurationStorage.store(response)
- checkTokens()
- }
- override fun onError(error: CommunicationException) {
- communicationInProgress.postValue(false)
- error.printStackTrace()
- errorText.postValue(errorTextUnknown)
- setState(State.ERROR)
- }
- }
- }
- fun checkTokens() {
- tokenStorage.retrieve()?.let {
- communicationInProgress.postValue(true)
- api.refreshTokens(it).enqueue(object : ApiCallback<Tokens> {
- override fun onSuccess(response: Tokens) {
- communicationInProgress.postValue(false)
- loginSucceeded.postValue(true)
- }
- override fun onError(error: CommunicationException) {
- communicationInProgress.postValue(false)
- error.printStackTrace()
- setState(State.LOGIN)
- }
- })
- } ?: run {
- setState(State.LOGIN)
- }
- }
- fun communicationInProgress(): LiveData<Boolean> {
- return communicationInProgress
- }
- fun loginSucceeded(): LiveData<Boolean> {
- return loginSucceeded
- }
- fun processTextVisible(): LiveData<Boolean> {
- return processTextVisible
- }
- fun errorTextVisible(): LiveData<Boolean> {
- return errorTextVisible
- }
- fun errorText(): LiveData<String> {
- return errorText
- }
- fun loginFormVisible(): LiveData<Boolean> {
- return loginFormVisible
- }
- fun loginErrorTextVisible(): LiveData<Boolean> {
- return loginErrorTextVisible
- }
- fun loginErrorText(): LiveData<String> {
- return loginErrorText
- }
- fun setState(newState: State) {
- processTextVisible.postValue(State.PROCESS == newState)
- errorTextVisible.postValue(State.ERROR == newState)
- loginFormVisible.postValue(State.LOGIN == newState)
- }
- fun startLogin(username: String, password: String) {
- loginErrorTextVisible.postValue(false)
- configurationStorage.retrieve()?.id?.let { deviceId ->
- communicationInProgress.postValue(true)
- api.login(
- LoginRequest(
- deviceId,
- username,
- password)
- ).enqueue(
- object : ApiCallback<Tokens> {
- override fun onSuccess(response: Tokens) {
- communicationInProgress.postValue(false)
- tokenStorage.store(response)
- loginSucceeded.postValue(true)
- }
- override fun onError(error: CommunicationException) {
- communicationInProgress.postValue(false)
- error.printStackTrace()
- when (error) {
- is CommunicationException.NotAuthenticatedException -> loginErrorText.postValue(
- errorTextInvalidLoginData)
- is CommunicationException.UnknownCommunicationException -> loginErrorText.postValue(
- errorTextUnknown)
- }
- loginErrorTextVisible.postValue(true)
- }
- }
- )
- } ?: run {
- errorText.postValue(errorTextMissingDeviceId)
- setState(State.ERROR)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement