Advertisement
Guest User

Untitled

a guest
May 25th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.85 KB | None | 0 0
  1. package com.remek.rosettapos.ui.authentication
  2.  
  3. import android.app.Application
  4. import androidx.lifecycle.AndroidViewModel
  5. import androidx.lifecycle.LiveData
  6. import androidx.lifecycle.MutableLiveData
  7. import com.remek.rosettapos.R
  8. import com.remek.rosettapos.communication.api.Api
  9. import com.remek.rosettapos.communication.api.ApiCallback
  10. import com.remek.rosettapos.communication.api.CommunicationException
  11. import com.remek.rosettapos.communication.types.DeviceConfiguration
  12. import com.remek.rosettapos.communication.types.LoginRequest
  13. import com.remek.rosettapos.communication.types.Tokens
  14. import com.remek.rosettapos.configuration.ConfigurationStorageImpl
  15. import com.remek.rosettapos.token.TokenStorageImpl
  16. import retrofit2.Retrofit
  17. import retrofit2.converter.gson.GsonConverterFactory
  18.  
  19. /**
  20.  * View model for the login screen.
  21.  */
  22. class LoginScreenViewModel(application: Application) : AndroidViewModel(application) {
  23.  
  24.     enum class State {
  25.         PROCESS,
  26.         ERROR,
  27.         LOGIN
  28.     }
  29.  
  30.  
  31.     private val configurationStorage = ConfigurationStorageImpl(application)
  32.  
  33.     private val tokenStorage = TokenStorageImpl(application)
  34.  
  35.     private val api = Retrofit.Builder()
  36.             .baseUrl(application.getString(R.string.api_url))
  37.             .addConverterFactory(GsonConverterFactory.create())
  38.             .build()
  39.             .create(Api::class.java)
  40.  
  41.     private var configurationCheckStarted = false
  42.  
  43.     private val communicationInProgress: MutableLiveData<Boolean> = MutableLiveData()
  44.  
  45.     private val processTextVisible: MutableLiveData<Boolean> = MutableLiveData()
  46.  
  47.     private val errorTextVisible: MutableLiveData<Boolean> = MutableLiveData()
  48.  
  49.     private val errorText: MutableLiveData<String> = MutableLiveData()
  50.  
  51.     private val loginFormVisible: MutableLiveData<Boolean> = MutableLiveData()
  52.  
  53.     private val loginErrorTextVisible: MutableLiveData<Boolean> = MutableLiveData()
  54.  
  55.     private val loginErrorText: MutableLiveData<String> = MutableLiveData()
  56.  
  57.     private val errorTextMissingDeviceId = application.getString(R.string.login_screen_error_device_id_missing)
  58.  
  59.     private val errorTextUnknown = application.getString(R.string.login_screen_error_unknown)
  60.  
  61.     private val errorTextInvalidLoginData =
  62.             application.getString(R.string.login_screen_error_invalid_username_or_password)
  63.  
  64.     private val loginSucceeded: MutableLiveData<Boolean> = MutableLiveData()
  65.  
  66.     init {
  67.         communicationInProgress.value = false
  68.         processTextVisible.value = true
  69.         errorTextVisible.value = false
  70.         loginFormVisible.value = false
  71.         loginSucceeded.value = false
  72.     }
  73.  
  74.     fun hasConfigurationCheckStarted(): Boolean {
  75.         return configurationCheckStarted
  76.     }
  77.  
  78.     fun checkConfiguration() {
  79.         configurationCheckStarted = true
  80.         communicationInProgress.postValue(true)
  81.         configurationStorage.retrieve()?.id?.let { id ->
  82.             api.getDeviceConfig(id).enqueue(deviceConfigurationCallback())
  83.         } ?: run {
  84.             api.createDeviceConfig().enqueue(deviceConfigurationCallback())
  85.         }
  86.     }
  87.  
  88.     fun deviceConfigurationCallback(): ApiCallback<DeviceConfiguration> {
  89.         return object : ApiCallback<DeviceConfiguration> {
  90.             override fun onSuccess(response: DeviceConfiguration) {
  91.                 communicationInProgress.postValue(false)
  92.                 configurationStorage.store(response)
  93.                 checkTokens()
  94.             }
  95.  
  96.             override fun onError(error: CommunicationException) {
  97.                 communicationInProgress.postValue(false)
  98.                 error.printStackTrace()
  99.                 errorText.postValue(errorTextUnknown)
  100.                 setState(State.ERROR)
  101.             }
  102.         }
  103.     }
  104.  
  105.     fun checkTokens() {
  106.         tokenStorage.retrieve()?.let {
  107.             communicationInProgress.postValue(true)
  108.             api.refreshTokens(it).enqueue(object : ApiCallback<Tokens> {
  109.                 override fun onSuccess(response: Tokens) {
  110.                     communicationInProgress.postValue(false)
  111.                     loginSucceeded.postValue(true)
  112.                 }
  113.  
  114.                 override fun onError(error: CommunicationException) {
  115.                     communicationInProgress.postValue(false)
  116.                     error.printStackTrace()
  117.                     setState(State.LOGIN)
  118.                 }
  119.             })
  120.  
  121.         } ?: run {
  122.             setState(State.LOGIN)
  123.         }
  124.     }
  125.  
  126.     fun communicationInProgress(): LiveData<Boolean> {
  127.         return communicationInProgress
  128.     }
  129.  
  130.     fun loginSucceeded(): LiveData<Boolean> {
  131.         return loginSucceeded
  132.     }
  133.  
  134.     fun processTextVisible(): LiveData<Boolean> {
  135.         return processTextVisible
  136.     }
  137.  
  138.     fun errorTextVisible(): LiveData<Boolean> {
  139.         return errorTextVisible
  140.     }
  141.  
  142.     fun errorText(): LiveData<String> {
  143.         return errorText
  144.     }
  145.  
  146.     fun loginFormVisible(): LiveData<Boolean> {
  147.         return loginFormVisible
  148.     }
  149.  
  150.     fun loginErrorTextVisible(): LiveData<Boolean> {
  151.         return loginErrorTextVisible
  152.     }
  153.  
  154.     fun loginErrorText(): LiveData<String> {
  155.         return loginErrorText
  156.     }
  157.  
  158.     fun setState(newState: State) {
  159.         processTextVisible.postValue(State.PROCESS == newState)
  160.         errorTextVisible.postValue(State.ERROR == newState)
  161.         loginFormVisible.postValue(State.LOGIN == newState)
  162.     }
  163.  
  164.     fun startLogin(username: String, password: String) {
  165.         loginErrorTextVisible.postValue(false)
  166.         configurationStorage.retrieve()?.id?.let { deviceId ->
  167.             communicationInProgress.postValue(true)
  168.             api.login(
  169.                     LoginRequest(
  170.                             deviceId,
  171.                             username,
  172.                             password)
  173.             ).enqueue(
  174.                     object : ApiCallback<Tokens> {
  175.                         override fun onSuccess(response: Tokens) {
  176.                             communicationInProgress.postValue(false)
  177.                             tokenStorage.store(response)
  178.                             loginSucceeded.postValue(true)
  179.                         }
  180.  
  181.                         override fun onError(error: CommunicationException) {
  182.                             communicationInProgress.postValue(false)
  183.                             error.printStackTrace()
  184.                             when (error) {
  185.                                 is CommunicationException.NotAuthenticatedException -> loginErrorText.postValue(
  186.                                         errorTextInvalidLoginData)
  187.                                 is CommunicationException.UnknownCommunicationException -> loginErrorText.postValue(
  188.                                         errorTextUnknown)
  189.                             }
  190.  
  191.                             loginErrorTextVisible.postValue(true)
  192.                         }
  193.  
  194.                     }
  195.             )
  196.         } ?: run {
  197.             errorText.postValue(errorTextMissingDeviceId)
  198.             setState(State.ERROR)
  199.         }
  200.  
  201.     }
  202.  
  203.  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement