Skip to content

Commit

Permalink
Change KAE to ViewBinding
Browse files Browse the repository at this point in the history
  • Loading branch information
arifaizin committed Nov 5, 2020
1 parent 577cd63 commit dec307f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
12 changes: 8 additions & 4 deletions MyReactiveForm/MyReactiveForm (starter)/app/build.gradle
@@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
plugins {
id 'com.android.application'
id 'kotlin-android'
}

android {
compileSdkVersion 30
Expand Down Expand Up @@ -29,10 +30,13 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}

buildFeatures {
viewBinding = true
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.core:core-ktx:1.3.2'
Expand Down
Expand Up @@ -6,21 +6,24 @@ import android.text.TextWatcher
import android.util.Patterns
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*
import com.dicoding.myreactiveform.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private var emailValid = false
private var passwordValid = false
private var passwordConfirmationValid = false

lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

validateButton()

ed_email.addTextChangedListener(object : TextWatcher {
binding.edEmail.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}

Expand All @@ -32,7 +35,7 @@ class MainActivity : AppCompatActivity() {
}
})

ed_password.addTextChangedListener(object : TextWatcher {
binding.edPassword.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}

Expand All @@ -44,7 +47,7 @@ class MainActivity : AppCompatActivity() {
}
})

ed_confirm_password.addTextChangedListener(object : TextWatcher {
binding.edConfirmPassword.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}

Expand All @@ -59,7 +62,7 @@ class MainActivity : AppCompatActivity() {

fun validateEmail() {
// jika password tidak valid tampilkan peringatan
val input = ed_email.text.toString()
val input = binding.edEmail.text.toString()
if (!Patterns.EMAIL_ADDRESS.matcher(input).matches()) {
emailValid = false
showEmailExistAlert(true)
Expand All @@ -72,7 +75,7 @@ class MainActivity : AppCompatActivity() {

fun validatePassword() {
// jika password < 6 karakter tampilkan peringatan
val input = ed_password.text.toString()
val input = binding.edPassword.text.toString()
if (input.length < 6) {
passwordValid = false
showPasswordMinimalAlert(true)
Expand All @@ -85,8 +88,8 @@ class MainActivity : AppCompatActivity() {

fun validatePasswordConfirmation() {
// jika konfirmasi password tidak sesuai tampilkan peringatan
val input = ed_confirm_password.text.toString()
if (input != ed_password.text.toString()) {
val input = binding.edConfirmPassword.text.toString()
if (input != binding.edPassword.text.toString()) {
passwordConfirmationValid = false
showPasswordConfirmationAlert(true)
} else {
Expand All @@ -99,23 +102,23 @@ class MainActivity : AppCompatActivity() {
private fun validateButton() {
// jika semua field sudah terisi, enable button submit
if (emailValid && passwordValid && passwordConfirmationValid) {
btn_register.isEnabled = true
btn_register.setBackgroundColor(ContextCompat.getColor(this, R.color.teal_200))
binding.btnRegister.isEnabled = true
binding.btnRegister.setBackgroundColor(ContextCompat.getColor(this, R.color.purple_500))
} else {
btn_register.isEnabled = false
btn_register.setBackgroundColor(ContextCompat.getColor(this, android.R.color.darker_gray))
binding.btnRegister.isEnabled = false
binding.btnRegister.setBackgroundColor(ContextCompat.getColor(this, android.R.color.darker_gray))
}
}

private fun showEmailExistAlert(isValid: Boolean) {
ed_email.error = if (isValid) getString(R.string.email_not_valid) else null
private fun showEmailExistAlert(isNotValid: Boolean) {
binding.edEmail.error = if (isNotValid) getString(R.string.email_not_valid) else null
}

private fun showPasswordMinimalAlert(isValid: Boolean) {
ed_password.error = if (isValid) getString(R.string.password_not_valid) else null
private fun showPasswordMinimalAlert(isNotValid: Boolean) {
binding.edPassword.error = if (isNotValid) getString(R.string.password_not_valid) else null
}

private fun showPasswordConfirmationAlert(isValid: Boolean) {
ed_confirm_password.error = if (isValid) getString(R.string.password_not_same) else null
private fun showPasswordConfirmationAlert(isNotValid: Boolean) {
binding.edConfirmPassword.error = if (isNotValid) getString(R.string.password_not_same) else null
}
}

0 comments on commit dec307f

Please sign in to comment.