~captainepoch/husky

94292c30180cb3db5de5bc67834752c745d1a0c2 — Adolfo Santiago 2 months ago 1c90213
LoginViewModel and verify URL at the LoginFragment
M husky/app/src/main/java/com/keylesspalace/tusky/refactor_features/login/view/fragments/LoginFragment.kt => husky/app/src/main/java/com/keylesspalace/tusky/refactor_features/login/view/fragments/LoginFragment.kt +25 -0
@@ 4,12 4,14 @@ import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.core.extensions.addHttpsProtocolUrl
import com.keylesspalace.tusky.core.extensions.dialogWithLink
import com.keylesspalace.tusky.core.extensions.viewBinding
import com.keylesspalace.tusky.core.extensions.viewObserve
import com.keylesspalace.tusky.core.ui.fragment.BaseFragment
import com.keylesspalace.tusky.databinding.ActivityLoginBinding
import com.keylesspalace.tusky.refactor_features.login.view.viewmodel.LoginViewModel
import com.keylesspalace.tusky.util.afterTextChanged
import com.zhuinden.simplestackextensions.fragmentsktx.lookup

class LoginFragment : BaseFragment(R.layout.activity_login) {


@@ 31,6 33,16 @@ class LoginFragment : BaseFragment(R.layout.activity_login) {
    }

    private fun initListeners() {
        binding.domainEditText.afterTextChanged {
            if(it.isEmpty()) {
                setDomainUrlError(false)
            }
        }

        binding.loginButton.setOnClickListener {
            viewModel.verifyUrl(binding.domainEditText.text.toString().addHttpsProtocolUrl())
        }

        binding.whatsAnInstanceTextView.setOnClickListener {
            AlertDialog.Builder(requireActivity()).dialogWithLink(
                getString(R.string.dialog_whats_an_instance),


@@ 40,5 52,18 @@ class LoginFragment : BaseFragment(R.layout.activity_login) {
    }

    private fun handleVerifyUrl(isValid: Boolean?) {
        isValid?.let { valid ->
            if(valid == true) {
            }
            setDomainUrlError(isValid)
        }
    }

    private fun setDomainUrlError(isValid: Boolean) {
        binding.domainTextInputLayout.error = if(isValid) {
            null
        } else {
            getString(R.string.error_invalid_domain)
        }
    }
}

M husky/app/src/main/java/com/keylesspalace/tusky/refactor_features/login/view/viewmodel/LoginViewModel.kt => husky/app/src/main/java/com/keylesspalace/tusky/refactor_features/login/view/viewmodel/LoginViewModel.kt +20 -17
@@ 1,36 1,39 @@
package com.keylesspalace.tusky.refactor_features.login.view.viewmodel

import android.util.Patterns
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.keylesspalace.tusky.core.ui.viewmodel.BaseViewModel
import com.keylesspalace.tusky.core.extensions.orEmpty
import com.keylesspalace.tusky.core.ui.viewmodel.RegisteredViewModel
import com.zhuinden.statebundle.StateBundle
import timber.log.Timber

class LoginViewModel : BaseViewModel() {
class LoginViewModel : RegisteredViewModel() {

    private val mVerifyUrl = MutableLiveData<Boolean>()
    val verifyUrl: LiveData<Boolean>
        get() = mVerifyUrl

    override fun toBundle(): StateBundle {
        val stateBundle = StateBundle()

        stateBundle.putBoolean(LoginViewModelKeys.Bundle.VERIFY_URL, mVerifyUrl.value ?: false)

        Timber.d("TO BUNDLE")

        return stateBundle
    override fun toBundle(): StateBundle = StateBundle().apply {
        putBoolean(
            LoginViewModelKeys.Bundle.VERIFY_URL,
            mVerifyUrl.value.orEmpty()
        )
    }

    override fun fromBundle(bundle: StateBundle?) {
        bundle?.let {
            mVerifyUrl.value = it.getBoolean(LoginViewModelKeys.Bundle.VERIFY_URL)

            Timber.d("FROM BUNDLE")
        bundle?.run {
            mVerifyUrl.value = getBoolean(LoginViewModelKeys.Bundle.VERIFY_URL)
        }
    }

    fun verifyUrl(url: String?) {
        mVerifyUrl.value = false
    override fun onServiceRegistered() {
    }

    override fun onServiceUnregistered() {
        super.onServiceUnregistered()
    }

    fun verifyUrl(url: String) {
        mVerifyUrl.value = Patterns.WEB_URL.matcher(url).matches()
    }
}