Created
October 2, 2025 17:44
-
-
Save odbol/dc2bcf67533d2fb5b41429808185813d to your computer and use it in GitHub Desktop.
Shares events across activities, services, etc. So much simpler than the Java version! But still useful.
This file contains hidden or 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
| package com.odbol.utils | |
| import kotlinx.coroutines.flow.MutableSharedFlow | |
| import kotlinx.coroutines.flow.MutableStateFlow | |
| /** Shares events across activities, services, etc */ | |
| object EventBus { | |
| val events = MutableStateFlow<Event>(Event.None) | |
| } | |
| sealed class Event { | |
| data object None : Event() | |
| data class OnError(val error: String) : Event() | |
| // Add your own events here... | |
| } |
This file contains hidden or 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
| // Example of how to use it in an Activity or wherever | |
| fun onCreate() { | |
| lifecycleScope.launch(Dispatchers.IO) { | |
| EventBus.events.collect { event -> | |
| when (event) { | |
| is Event.None -> Log.v(TAG, "Subscribing to event bus") | |
| is Event.OnError -> Log.e(TAG, "Error: ${event.error}") | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment