package eu.friedcutlet.yokan.services.notifications import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.asSharedFlow import eu.friedcutlet.yokan.domain.ProfileId /** A message surfaced as an in-app snackbar/toast. */ data class InAppMessage( val title: String, val message: String, val channel: NotificationChannel? = null, val profileId: ProfileId? = null, ) { fun visibleTo(id: ProfileId): Boolean = profileId == null || profileId == id } /** * App-wide bus for transient in-app messages. The root composable collects * [messages] and shows them through a Material 3 SnackbarHost, so toasts render * inside the app instead of relying on OS-level popups (which on Linux showed * stale/duplicate system-tray balloons). * * It lives in the service layer so background, non-UI code (notification * services, download/update workers) can post without depending on Compose. */ object InAppNotifications { // A small replay-less buffer so emits from background threads aren't dropped // before the UI starts collecting. private val _messages = MutableSharedFlow(extraBufferCapacity = 32) val messages: SharedFlow = _messages.asSharedFlow() fun post(message: InAppMessage) { _messages.tryEmit(message) } fun post(title: String, message: String = "", channel: NotificationChannel? = null) { _messages.tryEmit(InAppMessage(title, message, channel)) } fun post(payload: NotificationPayload) { _messages.tryEmit(InAppMessage(payload.title, payload.message, payload.channel, payload.profileId)) } }