Created
November 6, 2022 16:08
-
-
Save asissuthar/4554a78518689a5825c13a0283c3dfca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.asissuthar.validation | |
import android.os.Bundle | |
import android.widget.Toast | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.lifecycle.lifecycleScope | |
import com.asissuthar.validation.databinding.ActivityMainBinding | |
import com.asissuthar.validation.formfields.FormFieldText | |
import kotlinx.coroutines.flow.launchIn | |
import kotlinx.coroutines.flow.onEach | |
import kotlinx.coroutines.launch | |
import reactivecircus.flowbinding.android.view.clicks | |
class MainActivity : AppCompatActivity() { | |
private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) } | |
private val fieldUsername by lazy { | |
FormFieldText( | |
scope = lifecycleScope, | |
textInputLayout = binding.tilUsername, | |
textInputEditText = binding.etUsername, | |
validation = { value -> | |
when { | |
value.isNullOrBlank() -> "Username is required." | |
else -> null | |
} | |
} | |
) | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(binding.root) | |
binding.btnSubmit.clicks().onEach { | |
submit() | |
}.launchIn(lifecycleScope) | |
} | |
private fun submit() = lifecycleScope.launch { | |
binding.btnSubmit.isEnabled = false | |
fieldUsername.disable() | |
if (fieldUsername.validate()) { | |
// use field data here | |
fieldUsername.value | |
showToast("Submit successful!") | |
} | |
fieldUsername.enable() | |
binding.btnSubmit.isEnabled = true | |
} | |
private fun showToast(message: String) { | |
Toast.makeText(this, message, Toast.LENGTH_LONG).show() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment