louiscad
11/05/2018, 9:36 AMselect
clause to select between two user inputs (two button clicks). Should I implement the select clause myself in an extension function, or should I transform the button clicks to a channel, and then use the already existing select clause implementations?gildor
11/05/2018, 9:39 AMgroostav
11/05/2018, 9:40 AMDominaezzz
11/05/2018, 9:40 AMlouiscad
11/05/2018, 9:41 AMgildor
11/05/2018, 9:42 AMlouiscad
11/05/2018, 9:43 AMgildor
11/05/2018, 9:43 AMlouiscad
11/05/2018, 9:44 AMUserAction
sealed class hierarchy that I want to return from a suspend function implemented in the UI codegildor
11/05/2018, 9:45 AMfun onClick(v1: View, v2: View, onClick: (View) -> Unit) {
v1.setOnClickListener {
onClick(it)
}
v2.setOnClickListener {
onClick(it)
}
}
suspendCoroutine
instead of onClick lambdalouiscad
11/05/2018, 9:48 AMsuspendCoroutine
may be enough for my use case, I was over-complicating things. Thanks for your insight!Dominaezzz
11/05/2018, 9:48 AMgildor
11/05/2018, 9:49 AMsuspend fun awaitClick(v1: View, v2: View): View {
return suspendCancellableCoroutine { cont ->
val onClick: (View) -> Unit = {
cont.resume(it)
}
// Not sure about this part, but probably make sense
cont.invokeOnCancellation {
v1.setOnClickListener(null)
v2.setOnClickListener(null)
}
v1.setOnClickListener(onClick)
v2.setOnClickListener(onClick)
}
}
suspend fun List<View>.awaitClick(): View {
return suspendCancellableCoroutine { cont ->
val onClick: (View) -> Unit = {
cont.resume(it)
}
// Not sure about this part, but probably make sense
cont.invokeOnCancellation {
forEach { view ->
view.setOnClickListener(null)
}
}
forEach { view ->
view.setOnClickListener(onClick)
}
}
}
louiscad
11/05/2018, 9:51 AMgildor
11/05/2018, 9:52 AMlouiscad
11/05/2018, 9:52 AMgildor
11/05/2018, 9:53 AMList<View>
louiscad
11/05/2018, 9:53 AMZach Klippenstein (he/him) [MOD]
11/05/2018, 5:55 PMlouiscad
11/05/2018, 6:44 PM