InAppNotifications.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/notifications/InAppNotifications.kt
1package eu.friedcutlet.yokan.services.notifications23import kotlinx.coroutines.flow.MutableSharedFlow4import kotlinx.coroutines.flow.SharedFlow5import kotlinx.coroutines.flow.asSharedFlow6import eu.friedcutlet.yokan.domain.ProfileId78/** A message surfaced as an in-app snackbar/toast. */9data class InAppMessage(10 val title: String,11 val message: String,12 val channel: NotificationChannel? = null,13 val profileId: ProfileId? = null,14) {15 fun visibleTo(id: ProfileId): Boolean = profileId == null || profileId == id16}1718/**19 * App-wide bus for transient in-app messages. The root composable collects20 * [messages] and shows them through a Material 3 SnackbarHost, so toasts render21 * inside the app instead of relying on OS-level popups (which on Linux showed22 * stale/duplicate system-tray balloons).23 *24 * It lives in the service layer so background, non-UI code (notification25 * services, download/update workers) can post without depending on Compose.26 */27object InAppNotifications {28 // A small replay-less buffer so emits from background threads aren't dropped29 // before the UI starts collecting.30 private val _messages = MutableSharedFlow<InAppMessage>(extraBufferCapacity = 32)31 val messages: SharedFlow<InAppMessage> = _messages.asSharedFlow()3233 fun post(message: InAppMessage) {34 _messages.tryEmit(message)35 }3637 fun post(title: String, message: String = "", channel: NotificationChannel? = null) {38 _messages.tryEmit(InAppMessage(title, message, channel))39 }4041 fun post(payload: NotificationPayload) {42 _messages.tryEmit(InAppMessage(payload.title, payload.message, payload.channel, payload.profileId))43 }44}45