feat(audio-rooms): RoomTheme projection from kind-30312 (T3 #1)
Materialised view of the kind-30312 theme tags into a small
renderer-friendly struct. Pure data + a `from(event)` projection
function — the Compose `AudioRoomThemedScope` wrapper consumes it
later.
RoomTheme — packs colors as `0xAARRGGBB` Long (full alpha)
so commons stays free of the Android Color type;
the Compose renderer recovers via `Color(argb)`.
`null` per field means "use the platform default" so
partial themes (background-only, primary-only, etc.)
fall back per-field.
RoomTheme.Empty — sentinel for un-themed rooms; renderer can pass
it unconditionally without an extra null branch.
RoomTheme.from(event) — picks the FIRST color per target (palette
fallbacks deferred to a later phase),
drops typo'd hex via Quartz's strict
ColorTag parser (returns null per field
rather than crashing), maps the
BackgroundTag mode to the renderer enum
(unknown wire modes fall back to COVER).
Tests:
* Empty event → empty theme
* Three color targets project to opaque ARGB Longs
* First-per-target wins (extra colors ignored in v1)
* Typo'd hex leaves null per field; OTHER colors still project
* Background URL + tile mode round-trip
* Unknown bg mode (a future "blur") → COVER fallback
* hexToOpaqueArgb always sets alpha=0xFF (no inadvertent
transparency for #000000)
Compose renderer + AudioRoomThemedScope wrapper come next.
This commit is contained in:
+91
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.viewmodels
|
||||
|
||||
import androidx.compose.runtime.Immutable
|
||||
import com.vitorpamplona.quartz.nip53LiveActivities.meetingSpaces.MeetingSpaceEvent
|
||||
import com.vitorpamplona.quartz.nip53LiveActivities.meetingSpaces.tags.BackgroundTag
|
||||
import com.vitorpamplona.quartz.nip53LiveActivities.meetingSpaces.tags.ColorTag
|
||||
|
||||
/**
|
||||
* Materialised room theme — the small surface the renderer
|
||||
* actually consumes. Each color is `0xAARRGGBB` packed into a
|
||||
* Long so commons stays free of the Android Color type;
|
||||
* `androidx.compose.ui.graphics.Color(value)` recovers the
|
||||
* platform type at the call site.
|
||||
*
|
||||
* `null` fields mean "use the platform default" — clients without
|
||||
* theming support pass a null [RoomTheme] entirely; clients with
|
||||
* partial support fall back per-field.
|
||||
*/
|
||||
@Immutable
|
||||
data class RoomTheme(
|
||||
val backgroundArgb: Long?,
|
||||
val textArgb: Long?,
|
||||
val primaryArgb: Long?,
|
||||
val backgroundImageUrl: String?,
|
||||
val backgroundMode: BackgroundMode,
|
||||
) {
|
||||
enum class BackgroundMode { COVER, TILE }
|
||||
|
||||
companion object {
|
||||
val Empty = RoomTheme(null, null, null, null, BackgroundMode.COVER)
|
||||
|
||||
/**
|
||||
* Project the theme tags off a kind-30312 event. Returns
|
||||
* [Empty] when the event has no theme tags so the renderer
|
||||
* can pass it unconditionally without an extra null branch.
|
||||
*/
|
||||
fun from(event: MeetingSpaceEvent): RoomTheme {
|
||||
val colors = event.colors()
|
||||
val bg = event.background()
|
||||
|
||||
fun pickHex(target: ColorTag.Target): Long? =
|
||||
colors
|
||||
.firstOrNull { it.target == target }
|
||||
?.hex
|
||||
?.let { hexToOpaqueArgb(it) }
|
||||
|
||||
return RoomTheme(
|
||||
backgroundArgb = pickHex(ColorTag.Target.BACKGROUND),
|
||||
textArgb = pickHex(ColorTag.Target.TEXT),
|
||||
primaryArgb = pickHex(ColorTag.Target.PRIMARY),
|
||||
backgroundImageUrl = bg?.url,
|
||||
backgroundMode =
|
||||
when (bg?.mode) {
|
||||
BackgroundTag.Mode.TILE -> BackgroundMode.TILE
|
||||
else -> BackgroundMode.COVER
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack a 6-char uppercase hex (validated by [ColorTag]) into
|
||||
* a fully-opaque ARGB Long: `0xFFRRGGBB`.
|
||||
*/
|
||||
internal fun hexToOpaqueArgb(hex: String): Long {
|
||||
// ColorTag's parser already validated the input — this
|
||||
// is a straight string-to-int.
|
||||
val rgb = hex.toLong(radix = 16)
|
||||
return 0xFF000000L or rgb
|
||||
}
|
||||
}
|
||||
}
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.viewmodels
|
||||
|
||||
import com.vitorpamplona.quartz.nip53LiveActivities.meetingSpaces.MeetingSpaceEvent
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNull
|
||||
|
||||
class RoomThemeTest {
|
||||
private fun event(extra: List<Array<String>>): MeetingSpaceEvent =
|
||||
MeetingSpaceEvent(
|
||||
id = "0".repeat(64),
|
||||
pubKey = "a".repeat(64),
|
||||
createdAt = 1L,
|
||||
tags =
|
||||
(
|
||||
listOf(
|
||||
arrayOf("d", "rt"),
|
||||
arrayOf("room", "Room"),
|
||||
arrayOf("status", "open"),
|
||||
arrayOf("service", "https://moq"),
|
||||
) + extra
|
||||
).toTypedArray(),
|
||||
content = "",
|
||||
sig = "0".repeat(128),
|
||||
)
|
||||
|
||||
@Test
|
||||
fun emptyEventProducesEmptyTheme() {
|
||||
val theme = RoomTheme.from(event(emptyList()))
|
||||
assertNull(theme.backgroundArgb)
|
||||
assertNull(theme.textArgb)
|
||||
assertNull(theme.primaryArgb)
|
||||
assertNull(theme.backgroundImageUrl)
|
||||
assertEquals(RoomTheme.BackgroundMode.COVER, theme.backgroundMode)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun projectsAllThreeColorTargets() {
|
||||
val theme =
|
||||
RoomTheme.from(
|
||||
event(
|
||||
listOf(
|
||||
arrayOf("c", "#FF8800", "background"),
|
||||
arrayOf("c", "#101010", "text"),
|
||||
arrayOf("c", "#1E88E5", "primary"),
|
||||
),
|
||||
),
|
||||
)
|
||||
assertEquals(0xFFFF8800L, theme.backgroundArgb)
|
||||
assertEquals(0xFF101010L, theme.textArgb)
|
||||
assertEquals(0xFF1E88E5L, theme.primaryArgb)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun firstColorPerTargetWins() {
|
||||
val theme =
|
||||
RoomTheme.from(
|
||||
event(
|
||||
listOf(
|
||||
arrayOf("c", "#FF0000", "background"),
|
||||
// A second background color is ignored — palette
|
||||
// fallbacks aren't supported in v1.
|
||||
arrayOf("c", "#00FF00", "background"),
|
||||
),
|
||||
),
|
||||
)
|
||||
assertEquals(0xFFFF0000L, theme.backgroundArgb)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun typoedHexFallsBackToNullField() {
|
||||
val theme =
|
||||
RoomTheme.from(
|
||||
event(
|
||||
listOf(
|
||||
arrayOf("c", "red", "background"), // ColorTag rejects, RoomTheme leaves null
|
||||
arrayOf("c", "#101010", "text"),
|
||||
),
|
||||
),
|
||||
)
|
||||
assertNull(theme.backgroundArgb)
|
||||
assertEquals(0xFF101010L, theme.textArgb)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun backgroundImageWithTileMode() {
|
||||
val theme =
|
||||
RoomTheme.from(
|
||||
event(listOf(arrayOf("bg", "https://i/p.png", "tile"))),
|
||||
)
|
||||
assertEquals("https://i/p.png", theme.backgroundImageUrl)
|
||||
assertEquals(RoomTheme.BackgroundMode.TILE, theme.backgroundMode)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun unknownBackgroundModeFallsBackToCover() {
|
||||
val theme =
|
||||
RoomTheme.from(
|
||||
event(listOf(arrayOf("bg", "https://i/p.png", "blur"))),
|
||||
)
|
||||
assertEquals(RoomTheme.BackgroundMode.COVER, theme.backgroundMode)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun packedArgbIsAlwaysOpaque() {
|
||||
// Even for low-luminance hex like #000000, the alpha channel
|
||||
// must be 0xFF — themes don't expose transparency in v1.
|
||||
assertEquals(0xFF000000L, RoomTheme.hexToOpaqueArgb("000000"))
|
||||
assertEquals(0xFFFFFFFFL, RoomTheme.hexToOpaqueArgb("FFFFFF"))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user