package eu.friedcutlet.yokan.services.notifications import eu.friedcutlet.yokan.domain.ProfileId /** Default adopts existing OS notifications. Extra owners use full string identities, not hashes. */ internal data class ProfileNotificationNamespace(val id: ProfileId) { private fun owned(channel: NotificationChannel, tag: String?): String = "yokan.profile.${id.value}.${channel.name.lowercase()}.${tag.orEmpty().length}:${tag.orEmpty()}" fun androidTag(channel: NotificationChannel, tag: String?): String? = if (id == ProfileId.Default) null else owned(channel, tag) fun androidId(legacyChannelId: String, tag: String?): Int = if (id == ProfileId.Default) (legacyChannelId + tag.orEmpty()).hashCode() else 0 fun iosIdentifier(channel: NotificationChannel, tag: String?): String = if (id == ProfileId.Default) "yokan.${channel.name.lowercase()}.${tag.orEmpty()}" else owned(channel, tag) } /** * Captured stale references cannot post, cancel or request permissions after owner retirement. * [hideContent] (Security → hide notification content) swaps titles for generic text on every platform. */ internal class ProfileNotificationService( private val id: ProfileId, private val delegate: NotificationService, private val hideContent: () -> Boolean = { false }, private val isOpen: () -> Boolean, ) : NotificationService { override fun supported(): Boolean = isOpen() && delegate.supported() override fun requestPermission() { if (isOpen()) delegate.requestPermission() } override fun notify(payload: NotificationPayload) { if (!isOpen()) return val shown = if (hideContent()) payload.redacted() else payload delegate.notify(shown.copy(profileId = id)) } override fun cancel(channel: NotificationChannel, tag: String?) { if (isOpen()) delegate.cancel(channel, tag) } } /** Keeps the channel and tag (so updates/cancels still match) but drops titles and details. */ internal fun NotificationPayload.redacted(): NotificationPayload = copy( title = "Yokan", message = when (channel) { NotificationChannel.Downloads -> "Download activity" NotificationChannel.LibraryUpdates -> "Library update" NotificationChannel.Backup -> "Backup activity" NotificationChannel.Sync -> "Sync activity" }, )