Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LoginViewModel(private val emailValidator: EmailValidator) : KoinBaseViewModel() {
- var termsAccepted: MutableLiveData<Boolean>? = null
- get() {
- if (field == null) {
- field = MutableLiveData()
- }
- return field
- }
- var email: MutableLiveData<String>? = null
- get() {
- if (field == null) {
- field = MutableLiveData()
- }
- return field
- }
- var password: MutableLiveData<String>? = null
- get() {
- if (field == null) {
- field = MutableLiveData()
- }
- if (field?.value.isNullOrEmpty()) {
- passwordValid?.value = false
- createPasswordEnabled?.value = false
- }
- return field
- }
- var confirmPassword: MutableLiveData<String>? = null
- get() {
- if (field == null) {
- field = MutableLiveData()
- }
- return field
- }
- var createPasswordEnabled: MutableLiveData<Boolean>? = null
- get() {
- if (field == null) {
- field = MutableLiveData()
- }
- return field
- }
- var emailContinueEnabled: MutableLiveData<Boolean>? = null
- get() {
- if (field == null) {
- field = MutableLiveData()
- }
- return field
- }
- var validationCode: MutableLiveData<String>? = null
- get() {
- if (field == null) {
- field = MutableLiveData()
- }
- return field
- }
- var backNavUiVisibility: MutableLiveData<Int>? = null
- get() {
- if (field == null) {
- field = MutableLiveData()
- }
- return field
- }
- var displayValidationError: MutableLiveData<Boolean>? = null
- get() {
- if (field == null) {
- field = MutableLiveData()
- }
- return field
- }
- var passwordValid: MutableLiveData<Boolean>? = null
- get() {
- if (field == null) {
- field = MutableLiveData()
- }
- return field
- }
- // Email, ShowMovesTab
- var goToHomeActivity: SingleLiveEvent<Pair<String, Boolean>> = SingleLiveEvent()
- init {
- termsAccepted?.value = false
- emailContinueEnabled?.value = false
- createPasswordEnabled?.value = false
- backNavUiVisibility?.value = View.GONE
- }
- fun login(@Suppress("UNUSED_PARAMETER") view: View) {
- password?.value?.let { password ->
- // Email has to be not-null to reach this screen
- apiService.login(LoginRequest(email?.value!!, password))
- .compose(schedulerProvider.ioToMainSingleScheduler())
- .compose(uiHandlerProvider.uiHandlerSingle(this, true))
- .subscribe(
- {
- if (it.response?.addressLookupApiKey.isNullOrEmpty()) {
- snackbarMessage.value = SnackbarContent().apply {
- copyRes = R.string.login_forbidden
- }
- } else {
- handleLoginResponse(it)
- }
- },
- {
- if (it is HttpException) {
- snackbarMessage.value = SnackbarContent().apply {
- copyRes = R.string.incorrect_password_copy
- }
- }
- }
- ).apply {
- compositeDisposable.add(this)
- }
- }
- }
- fun forgotPassword(view: View) {
- navigator?.get()?.navigateTo(Uri.parse(BuildConfig.BASE_URL + BuildConfig.RESET_PASSWORD_PATH))
- }
- fun createPassword(view: View) {
- password?.value?.let { password ->
- compositeDisposable.add(
- apiService.createPassword(
- CreatePasswordRequest(email?.value!!, password)
- )
- .compose(schedulerProvider.ioToMainSingleScheduler())
- .compose(uiHandlerProvider.uiHandlerSingle(this, true))
- .subscribe { _, t ->
- if (t != null) return@subscribe
- AnalyticsHelper.logEvent(view.context, com.gobuzzvault.core.R.string.ga_user_create_password)
- navigator?.get()?.navigate(R.id.action_passwordCreateFragment_to_codeVerifyFragment)
- }
- )
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement