Last active
January 17, 2024 09:44
-
-
Save ryancfogarty/613af06243e61bf482523c10d49e5f95 to your computer and use it in GitHub Desktop.
Communicator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PerActivity | |
class BottomSheetDialogCommunicator @Inject constructor() { | |
val flow = MutableSharedFlow<BottomSheetDialogMessage>() | |
} | |
sealed interface BottomSheetDialogMessage | |
sealed interface ReminderMessage : BottomSheetDialogMessage { | |
data class Test(val text: String) : ReminderMessage | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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