All Projects → ReactiveCircus → Flowbinding

ReactiveCircus / Flowbinding

Licence: apache-2.0
Kotlin Coroutines Flow binding APIs for Android's platform and unbundled UI widgets, inspired by RxBinding.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Flowbinding

Photos
No description or website provided.
Stars: ✭ 74 (-87.89%)
Mutual labels:  material-components, kotlin-coroutines
Light
🍭 The usual Snackbar, but elegant
Stars: ✭ 542 (-11.29%)
Mutual labels:  material-components
Quasar
Quasar Framework - Build high-performance VueJS user interfaces in record time
Stars: ✭ 20,090 (+3188.05%)
Mutual labels:  material-components
Kroto Plus
gRPC Kotlin Coroutines, Protobuf DSL, Scripting for Protoc
Stars: ✭ 400 (-34.53%)
Mutual labels:  kotlin-coroutines
Elm Mdc
Elm port of the Material Components for the Web CSS/JS library
Stars: ✭ 338 (-44.68%)
Mutual labels:  material-components
Material Components Ios
[In maintenance mode] Modular and customizable Material Design UI components for iOS
Stars: ✭ 4,484 (+633.88%)
Mutual labels:  material-components
Fritz2
Easily build reactive web-apps in Kotlin based on flows and coroutines.
Stars: ✭ 308 (-49.59%)
Mutual labels:  kotlin-coroutines
Kotlin Coroutines Android Examples
Learn Kotlin Coroutines for Android by Examples. Learn how to use Kotlin Coroutines for Android App Development.
Stars: ✭ 572 (-6.38%)
Mutual labels:  kotlin-coroutines
Preact Material Components
preact wrapper for "Material Components for the web"
Stars: ✭ 535 (-12.44%)
Mutual labels:  material-components
Android Mdc Theming
Sample code used for a series of Medium posts on Material Components for Android
Stars: ✭ 388 (-36.5%)
Mutual labels:  material-components
Svelte Materialify
A Material UI Design Component library for Svelte heavily inspired by vuetify.
Stars: ✭ 351 (-42.55%)
Mutual labels:  material-components
Blegattcoroutines
Functional Bluetooth GATT for Android (all the meanings)
Stars: ✭ 343 (-43.86%)
Mutual labels:  kotlin-coroutines
Android Showcase
💎 Android application following best practices: Kotlin, Coroutines, JetPack, Clean Architecture, Feature Modules, Tests, MVVM, DI, Static Analysis...
Stars: ✭ 5,214 (+753.36%)
Mutual labels:  kotlin-coroutines
Expenso
📊 A Minimal Expense Tracker App built to demonstrate the use of modern android architecture component with MVVM Architecture
Stars: ✭ 325 (-46.81%)
Mutual labels:  kotlin-coroutines
Angular Mdc Web
Angular wrapper for Material Design (Web) Components
Stars: ✭ 542 (-11.29%)
Mutual labels:  material-components
Norris
Showcase for Unidirectional Data Flow architecture for Android, powered by Kotlin Coroutines
Stars: ✭ 315 (-48.45%)
Mutual labels:  kotlin-coroutines
Corbind
Kotlin Coroutines binding APIs for Android UI widgets from the platform and support libraries
Stars: ✭ 357 (-41.57%)
Mutual labels:  kotlin-coroutines
Uniflow Kt
Uniflow 🦄 - Simple Unidirectional Data Flow for Android & Kotlin, using Kotlin coroutines and open to functional programming
Stars: ✭ 414 (-32.24%)
Mutual labels:  kotlin-coroutines
Fluid
📖 Library for QtQuick apps with Material Design
Stars: ✭ 601 (-1.64%)
Mutual labels:  material-components
Taptargetview
An implementation of tap targets from the Material Design guidelines for feature discovery.
Stars: ✭ 5,114 (+736.99%)
Mutual labels:  material-components

FlowBinding

CI Maven Central Android API License

Kotlin Flow binding APIs for Android's platform and unbundled UI widgets, inspired by RxBinding.

Flow is (conceptually) a reactive streams implementation provided by the kotlinx-coroutines-core artifact.

FlowBinding offers an extensive set of extension functions that turn traditional callbacks / listeners on Android UI widgets into the Flow type.

Article

Download

Dependencies are hosted on Maven Central.

Latest version:

def flowbinding_version = "1.0.0"

Platform Bindings

implementation "io.github.reactivecircus.flowbinding:flowbinding-android:${flowbinding_version}"

AndroidX Bindings

implementation "io.github.reactivecircus.flowbinding:flowbinding-activity:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-appcompat:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-core:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-drawerlayout:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-lifecycle:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-navigation:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-preference:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-recyclerview:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-swiperefreshlayout:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-viewpager:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-viewpager2:${flowbinding_version}"

Material Components Bindings

implementation "io.github.reactivecircus.flowbinding:flowbinding-material:${flowbinding_version}"

Snapshots of the development version are available in Sonatype's snapshots repository.

Usage

Binding UI Events

To observe click events on an Android View:

findViewById<Button>(R.id.button)
    .clicks() // binding API available in flowbinding-android
    .onEach {
        // handle button clicked
    }
    .launchIn(uiScope)

Binding Scope

launchIn(scope) is a shorthand for scope.launch { flow.collect() } provided by the kotlinx-coroutines-core library.

This uiScope in the example above is a CoroutineScope that defines the lifecycle of this Flow. The binding implementation will respect this scope by unregistering the callback / listener automatically when the scope is cancelled.

In the context of Android lifecycle this means the uiScope passed in here should be a scope that's bound to the Lifecycle of the view the widget lives in.

androidx.lifecycle:lifecycle-runtime-ktx:2.2.0 introduced an extension property LifecycleOwner.lifecycleScope: LifecycleCoroutineScope which will be cancelled when the Lifecycle is destroyed.

In an Activity it might look something like this:

class ExampleActivity : AppCompatActivity() {
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_example)

        findViewById<ViewPager2>(R.id.viewPager)
            .pageSelections() // binding API available in flowbinding-viewpager2
            .onEach { pagePosition ->
                // handle pagePosition
            }
            .launchIn(lifecycleScope) // provided by lifecycle-runtime-ktx
    }
}

In a Fragment:

class ExampleFragment : Fragment(R.layout.example_fragment_layout) {
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val binding = ExampleFragmentLayoutBinding.bind(view)
        binding.viewPager
            .pageSelections() // binding API available in flowbinding-viewpager2
            .onEach { pagePosition ->
                // handle pagePosition
            }
            .launchIn(viewLifecycleOwner.lifecycleScope) // provided by lifecycle-runtime-ktx
    }
}

Note that with FlowBinding you no longer need to unregister / remove listeners or callbacks in onDestroy() as this is done automatically for you.

Binding UI Events with Additional Information

Some UI widgets might hold a state internally which you might want to observe with a Flow.

For example with a TabLayout you might want to observe and react to the Tab selection events. In this case the binding API returns a Flow of custom TabLayoutSelectionEvent type which contains the currently selected Tab:

tabLayout.tabSelectionEvents()
    .filterIsInstance<TabLayoutSelectionEvent.TabSelected>() // only care about TabSelected events
    .onEach { event ->
        // sync selected tab title to some other UI element
        selectedTabTitle.text = event.tab.text
    }
    .launchIn(uiScope)

Skipping Initial Value

Bindings which emit a stream of state changes return the InitialValueFlow.

An InitialValueFlow emits the current value (state) of the widget immediately upon collection of the Flow.

In some cases you might want to skip the initial emission of the current value. This can be done by calling the skipInitialValue() on the InitialValueFlow:

slider.valueChanges()
    .skipInitialValue()
    .onEach { value ->
        // handle value
    }
    .launchIn(uiScope) // current value won't be emitted immediately

Additional Samples

All binding APIs are documented with Example of usage.

All bindings are covered by instrumented tests which you may want to refer to.

Roadmap

Our goal is to provide most of the bindings provided by RxBinding, while shifting our focus to supporting more modern AndroidX APIs such as ViewPager2 and the new components in Material Components as they become available.

List of all bindings available:

Please feel free to create an issue if you think a useful binding is missing or you want a new binding added to the library.

Credits

This library is inspired by RxBinding which provides RxJava binding APIs for Android's UI widgets.

Many thanks to RxBinding's author Jake Wharton and its contributors.

License

Copyright 2019 Yang Chen

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].