ProfileNotifications.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/notifications/ProfileNotifications.kt
1package eu.friedcutlet.yokan.services.notifications23import eu.friedcutlet.yokan.domain.ProfileId45/** Default adopts existing OS notifications. Extra owners use full string identities, not hashes. */6internal data class ProfileNotificationNamespace(val id: ProfileId) {7 private fun owned(channel: NotificationChannel, tag: String?): String =8 "yokan.profile.${id.value}.${channel.name.lowercase()}.${tag.orEmpty().length}:${tag.orEmpty()}"910 fun androidTag(channel: NotificationChannel, tag: String?): String? =11 if (id == ProfileId.Default) null else owned(channel, tag)1213 fun androidId(legacyChannelId: String, tag: String?): Int =14 if (id == ProfileId.Default) (legacyChannelId + tag.orEmpty()).hashCode() else 01516 fun iosIdentifier(channel: NotificationChannel, tag: String?): String =17 if (id == ProfileId.Default) "yokan.${channel.name.lowercase()}.${tag.orEmpty()}" else owned(channel, tag)18}1920/**21 * Captured stale references cannot post, cancel or request permissions after owner retirement.22 * [hideContent] (Security → hide notification content) swaps titles for generic text on every platform.23 */24internal class ProfileNotificationService(25 private val id: ProfileId,26 private val delegate: NotificationService,27 private val hideContent: () -> Boolean = { false },28 private val isOpen: () -> Boolean,29) : NotificationService {30 override fun supported(): Boolean = isOpen() && delegate.supported()31 override fun requestPermission() { if (isOpen()) delegate.requestPermission() }32 override fun notify(payload: NotificationPayload) {33 if (!isOpen()) return34 val shown = if (hideContent()) payload.redacted() else payload35 delegate.notify(shown.copy(profileId = id))36 }37 override fun cancel(channel: NotificationChannel, tag: String?) {38 if (isOpen()) delegate.cancel(channel, tag)39 }40}4142/** Keeps the channel and tag (so updates/cancels still match) but drops titles and details. */43internal fun NotificationPayload.redacted(): NotificationPayload = copy(44 title = "Yokan",45 message = when (channel) {46 NotificationChannel.Downloads -> "Download activity"47 NotificationChannel.LibraryUpdates -> "Library update"48 NotificationChannel.Backup -> "Backup activity"49 NotificationChannel.Sync -> "Sync activity"50 },51)52