0

I have a model

data class RegisterPostDataWithPwdCheck(
    var phone_number: String?,
    var name: String?,
    var password: String?,
    var secondPassword: String?)

And a ViewModel

class SignUpViewModel(application: Application) : BaseViewModel(application){
        val registerPostData = MutableLiveData<RegisterPostDataWithPwdCheck>...
        fun checkPassword(){}...}

I also have a View that has this code inside

viewModel.registerPostData.observe(viewLifecycleOwner, Observer {
    viewModel.checkPassword()
    })

In the XML there are two fields of interest

<EditText
android:id="@+id/edittext_sign_up_password"
android:text="@={view_model.registerPostData.password}" />
<EditText
android:id="@+id/edittext_sign_up_second_pw"
android:text="@={view_model.registerPostData.secondPassword}" />

What I understood so far is that the .observe will be called only when the entire RegisterPostDataWithPwdCheck object changes and I don't want that. I want it to be triggered when any of the parameters changes so I can call the fun checkPassword(){} in order to see if the two fields match. Is this possible?

Giorgos Neokleous
  • 1,709
  • 1
  • 14
  • 25
slavko
  • 103
  • 1
  • 10
  • 1
    check this https://stackoverflow.com/questions/48020377/livedata-update-on-object-field-change – mahdi shahbazi Feb 17 '20 at 12:21
  • 1
    also you can use edit text onChangeText listener and check password and save it for that you can use this link. https://stackoverflow.com/questions/33798426/how-to-databind-to-ontextchanged-for-an-edittext-on-android/55701091#55701091 – mahdi shahbazi Feb 17 '20 at 12:24

1 Answers1

0

Using @mahdi-shahbazi comment I've managed to work this out in Kotlin. My Model is now:

data class RegisterPostDataWithPwdCheck(
@SerializedName(value = "phone_number")
private var phoneNumber: String?,
private var name: String?,
private var password: String?,
private var secondPassword: String?
) : BaseObservable() {

@Bindable
fun getPhoneNumber(): String? {
    return phoneNumber
}

fun setPhoneNumber(value: String) {
    if (value != phoneNumber) {
        phoneNumber = value
        notifyPropertyChanged(BR.phoneNumber)
    }
}

@Bindable
fun getName(): String? {
    return name
}

fun setName(value: String?) {
    if (value != name) {
        name = value
        notifyPropertyChanged(BR.name)
    }
}

@Bindable
fun getPassword(): String? {
    return password
}

fun setPassword(value: String?) {
    if (value != password) {
        password = value
        notifyPropertyChanged(BR.password)
    }
}

@Bindable
fun getSecondPassword(): String? {
    return secondPassword
}

fun setSecondPassword(value: String?) {
    if (value != secondPassword) {
        secondPassword = value
        notifyPropertyChanged(BR.secondPassword)
    }
}
}

And creating custom LiveData class:

class PropertyAwareMutableLiveData<T : BaseObservable> : MutableLiveData<T>() 
{

private val callback = object : Observable.OnPropertyChangedCallback() {
    override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
        value = value
    }
}

override fun setValue(value: T?) {
    super.setValue(value)
    value?.addOnPropertyChangedCallback(callback)
}
}

What I still don't know if there is a way to automate this @Binding process which is terribly slow and boring and also forces some changes (turning parameters to private).

slavko
  • 103
  • 1
  • 10