DEV Community

Vladimir Raupov
Vladimir Raupov

Posted on

2

Corbind

Kotlin Coroutine binding APIs for Android UI widgets from the platform and support libraries. Supports Flow, ReceiveChannel and Actor.

Description

This library is for Android applications only. Help you to transform Android UI events into cold Flow, hot ReceiveChannel or just perform an action through an Actor.

Github link

https://github.com/LDRAlighieri/Corbind

Dependencies

allprojects {

    repositories {
        mavenCentral()
    }

}

Platform bindings:

dependencies {
    implementation 'ru.ldralighieri.corbind:corbind:1.0.0-RC'
}

AndroidX library bindings:

dependencies {
    implementation 'ru.ldralighieri.corbind:corbind-core:1.0.0-RC'
    implementation 'ru.ldralighieri.corbind:corbind-appcompat:1.0.0-RC'
    implementation 'ru.ldralighieri.corbind:corbind-drawerlayout:1.0.0-RC'
    implementation 'ru.ldralighieri.corbind:corbind-leanback:1.0.0-RC'
    implementation 'ru.ldralighieri.corbind:corbind-recyclerview:1.0.0-RC'
    implementation 'ru.ldralighieri.corbind:corbind-slidingpanelayout:1.0.0-RC'
    implementation 'ru.ldralighieri.corbind:corbind-swiperefreshlayout:1.0.0-RC'
    implementation 'ru.ldralighieri.corbind:corbind-viewpager:1.0.0-RC'
}

Google 'material' library bindings:

dependencies {
    implementation 'ru.ldralighieri.corbind:corbind-material:1.0.0-RC'
}

How to use it?

If you need to get a text change events of EditText widget, simple use case with cold Flow will look something like this:

findViewById<EditText>(R.id.et_name)
    .textChanges() // Flow<CharSequence>
    .onEach { /* handle text change events */ }
    .launchIn(scope)

If you prefer hot ReceiveChannel and you need to get a ViewPager page selection events, then the use case will transform in something like this:

launch {
    findViewById<ViewPager>(R.id.vp_slides)
        .pageSelections(scope) // ReceiveChannel<Int>
        .consumeEach {
            /* handle ViewPager events */
        }
}

And if you just need to perform an action on button click, the easiest way will be:

launch {
    findViewById<AppCompatButton>(R.id.bt_confirm)
        .clicks {
            /* perform an action on View click events */
        }
}

Just one more traditional example of login button enabling/disabling by email and password field validation:

combine(
    et_email.textChanges()
        .map { Patterns.EMAIL_ADDRESS.matcher(it).matches() },

    et_password.textChanges()
        .map { it.length > 7 },

    transform = { email, password -> email && password }
) 
    .onEach { bt_login.isEnabled = it }
    .launchIn(scope)

Sentry mobile image

Mobile Vitals: A first step to Faster Apps

Slow startup times, UI hangs, and frozen frames frustrate users—but they’re also fixable. Mobile Vitals help you measure and understand these performance issues so you can optimize your app’s speed and responsiveness. Learn how to use them to reduce friction and improve user experience.

Read the guide →

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay