Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement binding for the low-level CompoundButton interface (Checkable + Listener) #162

Closed
kramlex opened this issue Oct 27, 2021 · 1 comment
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@kramlex
Copy link
Contributor

kramlex commented Oct 27, 2021

There is such a binding:

fun MutableLiveData<Boolean>.bindTwoWayToCheckBoxChecked(
    lifecycleOwner: LifecycleOwner,
    checkBox: CheckBox
): Closeable {
    val readCloseable = bindNotNull(lifecycleOwner) { value ->
        if (checkBox.isChecked == value) return@bindNotNull

        checkBox.isChecked = value
    }

    val checkListener = CompoundButton.OnCheckedChangeListener { _, isChecked ->
        if (value == isChecked) return@OnCheckedChangeListener

        value = isChecked
    }
    checkBox.setOnCheckedChangeListener(checkListener)

    val writeCloseable = Closeable {
        checkBox.setOnCheckedChangeListener(null)
    }

    return readCloseable + writeCloseable
}

Need:

fun MutableLiveData<Boolean>.bindTwoWayToCheckable(
    lifecycleOwner: LifecycleOwner,
    switch: CompoundButton
): Closeable {
    val readCloseable = bindNotNull(lifecycleOwner) { value ->
        if (switch.isChecked == value) return@bindNotNull

        switch.isChecked = value
    }

    val checkListener = CompoundButton.OnCheckedChangeListener { _, isChecked ->
        if (value == isChecked) return@OnCheckedChangeListener

        value = isChecked
    }


    switch.setOnCheckedChangeListener(checkListener)

    val writeCloseable = Closeable {
        switch.setOnCheckedChangeListener(null)
    }

    return readCloseable + writeCloseable
}
@Alex009 Alex009 added the enhancement New feature or request label Feb 11, 2022
@Alex009 Alex009 added this to the 0.13.0 milestone Apr 16, 2022
@Alex009 Alex009 self-assigned this Apr 16, 2022
@Alex009
Copy link
Member

Alex009 commented Apr 30, 2022

available in 0.13.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants