Gordon
12/14/2022, 9:05 AMhfhbd
12/14/2022, 9:11 AMfilterIsInstance
, mapNotNull
+ when
or filter
+ when
, but I would not use filterIsInstance
with sealed classes to not forget to update the check when adding new cases.Gordon
12/14/2022, 9:14 AMGordon
12/14/2022, 9:17 AMfun <Event: AppEventData> subscribeForSpecificEvent(scope: CoroutineScope, block: suspend (AppEventData) -> Unit) =
_eventFlow.mapNotNull { if (it is Event) it else null}
Gordon
12/14/2022, 9:17 AMgildor
12/14/2022, 9:20 AMGordon
12/14/2022, 9:22 AMsealed class AppEvent {
data class Event1(val name: String) : AppEvent()
data class OtherEvent(val id: Int, val data: Long) : AppEvent()
}
class AppEventBus {
private val _eventFlow = MutableSharedFlow<AppEvent>()
fun subscribe(scope: CoroutineScope, block: suspend (AppEvent) -> Unit) = _eventFlow.onEach(block).launchIn(scope)
suspend fun emit(appEvent: AppEvent) = _eventFlow.emit(appEvent)
}
Let's say i have something like thisGordon
12/14/2022, 9:24 AMgildor
12/14/2022, 9:28 AMgildor
12/14/2022, 9:29 AMGordon
12/14/2022, 9:30 AMgildor
12/14/2022, 9:30 AMGordon
12/14/2022, 9:30 AMgildor
12/14/2022, 9:30 AMI Am using FlowWell, no, you hide it behind far inferior callback based API
gildor
12/14/2022, 9:31 AMGordon
12/14/2022, 9:32 AMgildor
12/14/2022, 9:33 AMgildor
12/14/2022, 9:33 AMGordon
12/14/2022, 9:33 AMgildor
12/14/2022, 9:35 AMhfhbd
12/14/2022, 9:41 AMgildor
12/14/2022, 9:42 AMGordon
12/14/2022, 9:48 AMgildor
12/14/2022, 9:49 AMinline fun <reified Event : AppEvent> subscribeForEvent(
scope: CoroutineScope,
noinline block: suspend (AppEvent) -> Unit
) = subscribe(scope) { if (it is Event) block(it) }
hfhbd
12/14/2022, 9:49 AMgildor
12/14/2022, 9:50 AMgildor
12/14/2022, 9:50 AMhfhbd
12/14/2022, 9:51 AMGordon
12/14/2022, 9:51 AMGordon
12/14/2022, 9:51 AMgildor
12/14/2022, 9:51 AMgildor
12/14/2022, 9:51 AMGordon
12/14/2022, 9:54 AMgildor
12/14/2022, 9:55 AMGordon
12/14/2022, 9:55 AMgildor
12/14/2022, 9:55 AMgildor
12/14/2022, 9:55 AMgildor
12/14/2022, 9:58 AMfun <Event : AppEvent> subscribeForEvent(
scope: CoroutineScope,
eventType: Class<Event>,
block: suspend (Event) -> Unit
) = subscribe(scope) {
if (eventType.isInstance(it)) {
@Suppress("UNCHECKED_CAST")
block(it as Event)
}
}
gildor
12/14/2022, 9:59 AMinline fun <reified Event : AppEvent> subscribeForEvent(
scope: CoroutineScope,
noinline block: suspend (Event) -> Unit
) = subscribeForEvent(scope, Event::class.java, block)
gildor
12/14/2022, 10:01 AMinline fun <reified Event : AppEvent> subscribeForEvent(
scope: CoroutineScope,
noinline block: suspend (Event) -> Unit
) = subscribe(scope) { if (it is Event) block(it) }
gildor
12/14/2022, 10:02 AMThe only last problem: I Hate it 🙂
It looks like over engineering.
Do you have a good example of how I can pass events from various classes to other classesWhat is over engineering?
gildor
12/14/2022, 10:03 AMGordon
12/14/2022, 10:03 AMgildor
12/14/2022, 10:06 AMgildor
12/14/2022, 10:06 AMGordon
12/14/2022, 10:06 AMGordon
12/14/2022, 10:06 AMGordon
12/14/2022, 10:07 AMgildor
12/14/2022, 10:07 AMgildor
12/14/2022, 10:07 AM> Do you have a good example of how I can pass events from various classes to other classes
I feel that this question not about rified types, right? But about EventBus
gildor
12/14/2022, 10:09 AMGordon
12/14/2022, 10:09 AMgildor
12/14/2022, 10:10 AMpass events from various classes to other classesYes, create a class with Flow (which can be implemented in any kind of flow), inject it where you need events
gildor
12/14/2022, 10:10 AMGordon
12/14/2022, 10:11 AMgildor
12/14/2022, 10:11 AMgildor
12/14/2022, 10:12 AM