AmrJyniat
06/25/2022, 5:01 AMvar property: String? = null
.......
propertyFlow.collect{
property = it
}
.......
textView.text = property // reflect this value when its get ready
Something like that, any idea?Erick Sumargo
06/25/2022, 7:19 AMAmrJyniat
06/25/2022, 7:27 AMproperty
many times in multiple functions within the fragment, it's difficult for me to put all these uses inside collect
.DALDEI
06/26/2022, 8:38 PMtypealias Observe<T> = (T)-> Unit
typealias Observers<T> = MutableList<Observe<T>>
fun <T> observers() = mutableListOf<Observe<T>>()
fun <T> Observers<T>.invoke(v: T) = forEach { it(v) }
...
val observers = observers<String>()
...
// Anywhere its useful to be notified of a new value
// simply add a new 'observer'\ callback to the list
observers += {
textview.value = it
}
Wherever the value changes .. could be in collect()
wherevaluechanged(v: T ) { observers(v) } // notify all observers
This has worked well for me as long as I consistantly guaretnee that the call to observers() is on the correct thread (main/UI thread for android) -- that can be baked into the observers(T) call if needed