Skip to content

Instantly share code, notes, and snippets.

@rfogar2
Last active January 17, 2024 09:44
Show Gist options
  • Save rfogar2/613af06243e61bf482523c10d49e5f95 to your computer and use it in GitHub Desktop.
Save rfogar2/613af06243e61bf482523c10d49e5f95 to your computer and use it in GitHub Desktop.
Communicator
@PerActivity
class BottomSheetDialogCommunicator @Inject constructor() {
val flow = MutableSharedFlow<BottomSheetDialogMessage>()
}
sealed interface BottomSheetDialogMessage
sealed interface ReminderMessage : BottomSheetDialogMessage {
data class Test(val text: String) : ReminderMessage
}
@Inject lateinit var communicator: BottomSheetDialogCommunicator
override fun onCreate(...) {
...
lifecycleScope.launch {
communicator.flow
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
.collectType { message: ReminderMessage.Type
Log.i("EditorActivity", message.text)
}
}
}
private suspend inline fun <T, reified U : T> Flow<T>.collectType(crossinline collector: (U) -> Unit) =
collect {
if (it is U) collector(it)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment