⚡ Kotlin Coroutines binding APIs for Android UI widgets from the platform and support libraries. Supports Flow, ReceiveChannel and Actor.
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.
Please consider giving this repository a star ⭐ if you like the project.
Platform bindings:
dependencies {
implementation(platform("ru.ldralighieri.corbind:corbind-bom:2024.01.00"))
implementation("ru.ldralighieri.corbind:corbind")
}
AndroidX library bindings:
dependencies {
implementation(platform("ru.ldralighieri.corbind:corbind-bom:2024.01.00"))
implementation("ru.ldralighieri.corbind:corbind-activity")
implementation("ru.ldralighieri.corbind:corbind-appcompat")
implementation("ru.ldralighieri.corbind:corbind-core")
implementation("ru.ldralighieri.corbind:corbind-drawerlayout")
implementation("ru.ldralighieri.corbind:corbind-fragment")
implementation("ru.ldralighieri.corbind:corbind-leanback")
implementation("ru.ldralighieri.corbind:corbind-lifecycle")
implementation("ru.ldralighieri.corbind:corbind-navigation")
implementation("ru.ldralighieri.corbind:corbind-recyclerview")
implementation("ru.ldralighieri.corbind:corbind-slidingpanelayout")
implementation("ru.ldralighieri.corbind:corbind-swiperefreshlayout")
implementation("ru.ldralighieri.corbind:corbind-viewpager")
implementation("ru.ldralighieri.corbind:corbind-viewpager2")
}
Google 'material' library bindings:
dependencies {
implementation(platform("ru.ldralighieri.corbind:corbind-bom:2024.01.00"))
implementation("ru.ldralighieri.corbind:corbind-material")
}
repositories {
maven("https://oss.sonatype.org/content/repositories/snapshots/")
}
dependencies {
implementation(platform("ru.ldralighieri.corbind:corbind-bom:2024.02.00-SNAPSHOT"))
implementation("ru.ldralighieri.corbind:{module}")
}
You can find a list of extensions in the description of each module:
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.etName)
.textChanges() // Flow<CharSequence>
.onEach { /* handle text change events */ }
.flowWithLifecycle(lifecycle)
.launchIn(lifecycleScope) // lifecycle-runtime-ktx
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.vpSlides)
.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.btConfirm)
.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(
etEmail.textChanges() // Flow<CharSequence>
.map { Patterns.EMAIL_ADDRESS.matcher(it).matches() },
etPassword.textChanges() // Flow<CharSequence>
.map { it.length > 7 },
transform = { email, password -> email && password }
)
.onEach { btLogin.isEnabled = it }
.flowWithLifecycle(lifecycle)
.launchIn(lifecycleScope) // lifecycle-runtime-ktx
More examples in module descriptions and in source code
If I forgot something or you have any ideas what can be added or corrected, please create an issue or contact me directly.
Jake Wharton. This project is inspired by RxBinding.
Copyright 2019-2023 Vladimir Raupov
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.