Dataflow async for Android™
Library ID dataflow-async-android
Latest version 1.0.0

Listening attributes on views

Like what we do for fields, we'll also want to listen for changes in attributes. Recalling that an attribute can be seen as a field of AttributeData<T>, we can now see the listen() extension function available on Views. Like its field counterpart, the callback is called on the UI thread.

Example:

val tv = TextView(context) val attr = attributeOf("hello") tv.listen(attr) { //Will be called on the UI thread tv.setText(it) }
TextView tv = new TextView(getContext()); Attribute<String> attr = Attribute.of("hello"); AttributeListenUtils.listen(tv, attr, str -> { //Will be called on the UI thread tv.setText(str); });

If you want your listener to be called asynchronously, you can use the asAsyncLifecycleOwner() function:

val tv = TextView(context) val attr = attributeOf("hello") tv.asAsyncLifecycleOwner().listen(attr) { //Will be called on a background thread tv.setText(it) }
TextView tv = new TextView(getContext()); Attribute<String> attr = Attribute.of("hello"); AttributeListenUtils.asAsyncLifecycleOwner(tv).listen(attr, str -> { //Will be called on a background thread tv.setText(str); });