Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.95 KB | None | 0 0
  1. class LoginViewModel(private val emailValidator: EmailValidator) : KoinBaseViewModel() {
  2.  
  3. var termsAccepted: MutableLiveData<Boolean>? = null
  4.         get() {
  5.             if (field == null) {
  6.                 field = MutableLiveData()
  7.             }
  8.  
  9.             return field
  10.         }
  11.  
  12.     var email: MutableLiveData<String>? = null
  13.         get() {
  14.             if (field == null) {
  15.                 field = MutableLiveData()
  16.             }
  17.  
  18.             return field
  19.         }
  20.  
  21.     var password: MutableLiveData<String>? = null
  22.         get() {
  23.             if (field == null) {
  24.                 field = MutableLiveData()
  25.             }
  26.             if (field?.value.isNullOrEmpty()) {
  27.                 passwordValid?.value = false
  28.                 createPasswordEnabled?.value = false
  29.             }
  30.             return field
  31.         }
  32.  
  33.     var confirmPassword: MutableLiveData<String>? = null
  34.         get() {
  35.             if (field == null) {
  36.                 field = MutableLiveData()
  37.             }
  38.  
  39.             return field
  40.         }
  41.  
  42.     var createPasswordEnabled: MutableLiveData<Boolean>? = null
  43.         get() {
  44.             if (field == null) {
  45.                 field = MutableLiveData()
  46.             }
  47.  
  48.             return field
  49.         }
  50.  
  51.     var emailContinueEnabled: MutableLiveData<Boolean>? = null
  52.         get() {
  53.             if (field == null) {
  54.                 field = MutableLiveData()
  55.             }
  56.  
  57.             return field
  58.         }
  59.  
  60.     var validationCode: MutableLiveData<String>? = null
  61.         get() {
  62.             if (field == null) {
  63.                 field = MutableLiveData()
  64.             }
  65.  
  66.             return field
  67.         }
  68.  
  69.     var backNavUiVisibility: MutableLiveData<Int>? = null
  70.         get() {
  71.             if (field == null) {
  72.                 field = MutableLiveData()
  73.             }
  74.  
  75.             return field
  76.         }
  77.  
  78.     var displayValidationError: MutableLiveData<Boolean>? = null
  79.         get() {
  80.             if (field == null) {
  81.                 field = MutableLiveData()
  82.             }
  83.  
  84.             return field
  85.         }
  86.  
  87.     var passwordValid: MutableLiveData<Boolean>? = null
  88.         get() {
  89.             if (field == null) {
  90.                 field = MutableLiveData()
  91.             }
  92.  
  93.             return field
  94.         }
  95.  
  96.     // Email, ShowMovesTab
  97.     var goToHomeActivity: SingleLiveEvent<Pair<String, Boolean>> = SingleLiveEvent()
  98.  
  99.     init {
  100.         termsAccepted?.value = false
  101.         emailContinueEnabled?.value = false
  102.         createPasswordEnabled?.value = false
  103.         backNavUiVisibility?.value = View.GONE
  104.     }
  105.  
  106.     fun login(@Suppress("UNUSED_PARAMETER") view: View) {
  107.         password?.value?.let { password ->
  108.             // Email has to be not-null to reach this screen
  109.             apiService.login(LoginRequest(email?.value!!, password))
  110.                     .compose(schedulerProvider.ioToMainSingleScheduler())
  111.                     .compose(uiHandlerProvider.uiHandlerSingle(this, true))
  112.                     .subscribe(
  113.                             {
  114.                                 if (it.response?.addressLookupApiKey.isNullOrEmpty()) {
  115.                                     snackbarMessage.value = SnackbarContent().apply {
  116.                                         copyRes = R.string.login_forbidden
  117.                                     }
  118.                                 } else {
  119.                                     handleLoginResponse(it)
  120.                                 }
  121.                             },
  122.                             {
  123.                                 if (it is HttpException) {
  124.                                     snackbarMessage.value = SnackbarContent().apply {
  125.                                         copyRes = R.string.incorrect_password_copy
  126.                                     }
  127.                                 }
  128.                             }
  129.                     ).apply {
  130.                         compositeDisposable.add(this)
  131.                     }
  132.         }
  133.     }
  134.  
  135. fun forgotPassword(view: View) {
  136.         navigator?.get()?.navigateTo(Uri.parse(BuildConfig.BASE_URL + BuildConfig.RESET_PASSWORD_PATH))
  137.     }
  138.  
  139. fun createPassword(view: View) {
  140.         password?.value?.let { password ->
  141.             compositeDisposable.add(
  142.                     apiService.createPassword(
  143.                             CreatePasswordRequest(email?.value!!, password)
  144.                     )
  145.                             .compose(schedulerProvider.ioToMainSingleScheduler())
  146.                             .compose(uiHandlerProvider.uiHandlerSingle(this, true))
  147.                             .subscribe { _, t ->
  148.                                 if (t != null) return@subscribe
  149.  
  150.                                 AnalyticsHelper.logEvent(view.context, com.gobuzzvault.core.R.string.ga_user_create_password)
  151.  
  152.                                 navigator?.get()?.navigate(R.id.action_passwordCreateFragment_to_codeVerifyFragment)
  153.                             }
  154.             )
  155.         }
  156.     }
  157.  
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement