Created
November 7, 2022 06:09
-
-
Save asissuthar/862cbc1c3fecf2e06ebea81c3cfef054 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.util.Patterns | |
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 com.asissuthar.validation.formfields.disable | |
import com.asissuthar.validation.formfields.enable | |
import com.asissuthar.validation.formfields.validate | |
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 | |
} | |
} | |
) | |
} | |
private val fieldEmail by lazy { | |
FormFieldText( | |
scope = lifecycleScope, | |
textInputLayout = binding.tilEmail, | |
textInputEditText = binding.etEmail, | |
validation = { value -> | |
when { | |
value.isNullOrBlank() -> "Email is required." | |
!Patterns.EMAIL_ADDRESS.toRegex().matches(value) -> "Invalid email." | |
else -> null | |
} | |
} | |
) | |
} | |
private val fieldPhoneNumber by lazy { | |
FormFieldText( | |
scope = lifecycleScope, | |
textInputLayout = binding.tilPhoneNumber, | |
textInputEditText = binding.etPhoneNumber, | |
validation = { value -> | |
when { | |
value.isNullOrBlank() -> "Phone number is required." | |
value.length != 10 -> "Invalid phone number." | |
else -> null | |
} | |
} | |
) | |
} | |
private val fieldPassword by lazy { | |
FormFieldText( | |
scope = lifecycleScope, | |
textInputLayout = binding.tilPassword, | |
textInputEditText = binding.etPassword, | |
validation = { value -> | |
when { | |
value.isNullOrBlank() -> "Password is required." | |
value.length <= 6 -> "Password length must be above 6 characters." | |
else -> null | |
} | |
} | |
) | |
} | |
private val fieldConfirmPassword by lazy { | |
FormFieldText( | |
scope = lifecycleScope, | |
textInputLayout = binding.tilConfirmPassword, | |
textInputEditText = binding.etConfirmPassword, | |
validation = { value -> | |
when { | |
value.isNullOrBlank() -> "Confirm Password is required." | |
value != fieldPassword.value -> "Confirm Password must be same as Password." | |
else -> null | |
} | |
} | |
) | |
} | |
private val formFields by lazy { | |
listOf( | |
fieldUsername, | |
fieldEmail, | |
fieldPhoneNumber, | |
fieldPassword, | |
fieldConfirmPassword, | |
) | |
} | |
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 | |
formFields.disable() | |
if (formFields.validate()) { | |
// use field data here | |
fieldUsername.value | |
fieldEmail.value | |
fieldPhoneNumber.value | |
fieldPassword.value | |
showToast("Submit successful!") | |
} | |
formFields.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