Orhan Tozan
03/28/2020, 4:27 PMval textField: Flow<String>
that depends on a val textInputEvents: Channel<TextInputEvent>
and a val clearTextFieldEvents: Channel<ClearTextInputEvent>
. I'm trying to initialize the textField flow by combining the textInputEvents and clearTextFieldEvents channels and decide the value of the text field based of those two event streams, only problem is that just
val textField: Flow<String> = textInputEvents
.consumeAsFlow()
.combine(clearTextFieldEvents.consumeAsFlow()) { textInputEvent, clearTextFieldEvent ->
// Which one of the two just emitted?
// return textInputEvent.text or ""
}
wouldn't do it, since I have to know which one of the channels emitted value is the most recent one, which isnt possible.
So my question kinda is: is there a combine operator that also tells you which flow emitted the most recent value?
EDIT: perhaps this is a usecase for the select expression (https://kotlinlang.org/docs/reference/coroutines/select-expression.html) ?Dennis
03/28/2020, 5:40 PMOrhan Tozan
03/28/2020, 7:25 PMDennis
03/28/2020, 7:40 PMTextInputEvent("")
.val textField: Flow<String> = textInputEvents
.consumeAsFlow()
.merge(clearTextFieldEvents.map { TextInputEvent("") }.consumeAsFlow()) { event ->
}
so we just map one to the other and mergemerge
(takes whatever happens on any of them). For merge, emitted objects have to be of same type.
Depending on what you're doing inside the collector, you can also transform both to a string right away (if you only need the new string)Orhan Tozan
03/28/2020, 10:26 PM