Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package org.siloserver.silo.common.ui

import org.siloserver.silo.model.catalog.CastMember
import org.siloserver.silo.model.catalog.CrewMember
import org.siloserver.silo.model.catalog.ItemDetail

/** Which part of the Cast & Crew row a card belongs to. */
enum class CastCrewGroup { Lead, Writers, Cast }

/**
* One card in the detail page's Cast & Crew row. Phone and TV render every
* credit with the same card, so crew and cast stay the same size.
*/
data class CastCrewCredit(
val group: CastCrewGroup,
val name: String,
/** Caption under the name: the role for crew, the character for cast. */
val caption: String?,
val personId: String?,
val photoUrl: String?,
val photoThumbhash: String?,
/**
* Label for the thin divider drawn before this card, or null when no
* divider belongs here. Only the first card of a group after the first
* shown group carries one.
*/
val dividerLabel: String? = null,
)

internal const val CAST_CREW_MAX_LEADS = 2
internal const val CAST_CREW_MAX_WRITERS = 3
internal const val CAST_CREW_MAX_CAST = 12

/**
* Builds the Cast & Crew row for a detail page, matching the Silo web layout:
* up to two directors (captioned "Creator" on series, since Silo stores series
* creators as Director credits), then up to three writers, then up to twelve
* cast members by billing order.
*
* Only movies and series get the grouped row. Any other type (episodes in
* particular) keeps the plain cast row exactly as the server sent it.
*/
fun castCrewCredits(detail: ItemDetail): List<CastCrewCredit> {
val type = detail.type.trim().lowercase()
return when (type) {
"movie" -> castCrewCredits(detail.cast, detail.crew, isSeries = false)
"series" -> castCrewCredits(detail.cast, detail.crew, isSeries = true)
else -> detail.cast.map { it.toCredit(dividerLabel = null) }
}
}

fun castCrewCredits(
cast: List<CastMember>,
crew: List<CrewMember>,
isSeries: Boolean,
): List<CastCrewCredit> {
val directors = crew
.filter { it.hasJob("Director") }
.distinctCrew()
// Exclude every director from the writers, not just the ones shown, so a
// writer-director past the lead cap doesn't reappear as a writer.
val leadKeys = directors.mapTo(HashSet()) { it.creditKey() }
val leads = directors.take(CAST_CREW_MAX_LEADS)
val writers = crew
.filter { it.hasJob("Writer") || it.hasJob("Screenplay") }
.distinctCrew()
.filterNot { it.creditKey() in leadKeys }
.take(CAST_CREW_MAX_WRITERS)
// sortedBy is stable, so equal billing orders keep the server's order.
val billedCast = cast
.filter { it.name.isNotBlank() }
.sortedBy { it.order }
.take(CAST_CREW_MAX_CAST)

val leadCaption = if (isSeries) "Creator" else "Director"
val out = ArrayList<CastCrewCredit>(leads.size + writers.size + billedCast.size)
leads.forEach { out += it.toCredit(CastCrewGroup.Lead, leadCaption, dividerLabel = null) }
writers.forEachIndexed { index, member ->
out += member.toCredit(
CastCrewGroup.Writers,
caption = "Writer",
dividerLabel = "Writers".takeIf { index == 0 && out.isNotEmpty() },
)
}
billedCast.forEachIndexed { index, member ->
out += member.toCredit(dividerLabel = "Cast".takeIf { index == 0 && out.isNotEmpty() })
}
return out
}

/** Up to two initials for a headshot placeholder, e.g. "Greta Gerwig" -> "GG". */
fun personInitials(name: String): String {
val words = name.trim().split(Regex("\\s+")).filter { it.isNotEmpty() }
return when {
words.isEmpty() -> ""
words.size == 1 -> words[0].take(1).uppercase()
else -> (words.first().take(1) + words.last().take(1)).uppercase()
}
}

private fun CrewMember.hasJob(job: String): Boolean =
this.job?.trim().equals(job, ignoreCase = true)

private fun CrewMember.creditKey(): String =
personId?.trim()?.takeIf { it.isNotEmpty() } ?: "name:${name.trim().lowercase()}"

private fun List<CrewMember>.distinctCrew(): List<CrewMember> =
filter { it.name.isNotBlank() }.distinctBy { it.creditKey() }

private fun CrewMember.toCredit(
group: CastCrewGroup,
caption: String,
dividerLabel: String?,
) = CastCrewCredit(
group = group,
name = name.trim(),
caption = caption,
personId = personId,
photoUrl = photoUrl,
photoThumbhash = photoThumbhash,
dividerLabel = dividerLabel,
)

private fun CastMember.toCredit(dividerLabel: String?) = CastCrewCredit(
group = CastCrewGroup.Cast,
name = name,
caption = character,
personId = personId,
photoUrl = photoUrl,
photoThumbhash = photoThumbhash,
dividerLabel = dividerLabel,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.siloserver.silo.common.ui.components

import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.layout.layout
import androidx.compose.ui.unit.Constraints

/**
* Turns horizontal text into a bottom-to-top vertical label. `rotate` alone
* only changes drawing, so the layout would still reserve the text's full
* horizontal width; this swaps the measured width and height first.
*/
fun Modifier.verticalText(): Modifier = this
.layout { measurable, _ ->
val placeable = measurable.measure(Constraints())
layout(placeable.height, placeable.width) {
placeable.place(
x = (placeable.height - placeable.width) / 2,
y = (placeable.width - placeable.height) / 2,
)
}
}
.rotate(-90f)
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package org.siloserver.silo.common.ui

import org.siloserver.silo.model.catalog.CastMember
import org.siloserver.silo.model.catalog.CrewMember
import org.siloserver.silo.model.catalog.ItemDetail
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue

class CastCrewCreditsTest {
@Test
fun groupsDirectorsThenWritersThenCastWithDividers() {
val credits = castCrewCredits(
cast = listOf(
CastMember(name = "Second", character = "B", order = 1),
CastMember(name = "First", character = "A", order = 0),
),
crew = listOf(
CrewMember(name = "Writer One", job = "Writer", personId = "20"),
CrewMember(name = "Producer", job = "Producer", personId = "30"),
CrewMember(name = "Dir", job = "Director", personId = "10"),
CrewMember(name = "Script", job = "Screenplay", personId = "21"),
),
isSeries = false,
)

assertEquals(
listOf("Dir", "Writer One", "Script", "First", "Second"),
credits.map { it.name },
)
assertEquals(
listOf("Director", "Writer", "Writer", "A", "B"),
credits.map { it.caption },
)
assertEquals(
listOf(null, "Writers", null, "Cast", null),
credits.map { it.dividerLabel },
)
assertEquals(
listOf(
CastCrewGroup.Lead,
CastCrewGroup.Writers,
CastCrewGroup.Writers,
CastCrewGroup.Cast,
CastCrewGroup.Cast,
),
credits.map { it.group },
)
}

@Test
fun capsEachGroup() {
val credits = castCrewCredits(
cast = (0 until 20).map { CastMember(name = "Actor $it", order = it) },
crew = (0 until 4).map { CrewMember(name = "Dir $it", job = "Director", personId = "d$it") } +
(0 until 5).map { CrewMember(name = "Writer $it", job = "Writer", personId = "w$it") },
isSeries = false,
)

assertEquals(listOf("Dir 0", "Dir 1"), credits.namesIn(CastCrewGroup.Lead))
assertEquals(listOf("Writer 0", "Writer 1", "Writer 2"), credits.namesIn(CastCrewGroup.Writers))
assertEquals((0 until 12).map { "Actor $it" }, credits.namesIn(CastCrewGroup.Cast))
}

@Test
fun deduplicatesByPersonIdThenNameAndOnlyMatchesExactJobs() {
val credits = castCrewCredits(
cast = emptyList(),
crew = listOf(
CrewMember(name = "Alice", job = " director ", personId = "1"),
CrewMember(name = "Alice A.", job = "Director", personId = "1"),
CrewMember(name = "Camera", job = "Director of Photography", personId = "2"),
CrewMember(name = "Bob", job = "Writer"),
CrewMember(name = " bob ", job = "Screenplay"),
CrewMember(name = "Story Person", job = "Story", personId = "3"),
CrewMember(name = "", job = "Writer"),
),
isSeries = false,
)

assertEquals(listOf("Alice"), credits.namesIn(CastCrewGroup.Lead))
assertEquals(listOf("Bob"), credits.namesIn(CastCrewGroup.Writers))
}

@Test
fun writerDirectorOnlyAppearsInTheLeadGroup() {
val credits = castCrewCredits(
cast = listOf(CastMember(name = "Star", order = 0)),
crew = listOf(
CrewMember(name = "Auteur", job = "Writer", personId = "1"),
CrewMember(name = "Auteur", job = "Director", personId = "1"),
CrewMember(name = "Nameonly", job = "Director"),
CrewMember(name = "Nameonly", job = "Screenplay"),
CrewMember(name = "Co-writer", job = "Writer", personId = "2"),
),
isSeries = false,
)

assertEquals(listOf("Auteur", "Nameonly"), credits.namesIn(CastCrewGroup.Lead))
assertEquals(listOf("Co-writer"), credits.namesIn(CastCrewGroup.Writers))
}

@Test
fun directorPastTheLeadCapIsNotShownAsAWriter() {
val credits = castCrewCredits(
cast = emptyList(),
crew = listOf(
CrewMember(name = "Director A", job = "Director", personId = "1"),
CrewMember(name = "Director B", job = "Director", personId = "2"),
CrewMember(name = "Director C", job = "Director", personId = "3"),
CrewMember(name = "Director C", job = "Writer", personId = "3"),
CrewMember(name = "Writer", job = "Writer", personId = "4"),
),
isSeries = false,
)

assertEquals(listOf("Director A", "Director B"), credits.namesIn(CastCrewGroup.Lead))
assertEquals(listOf("Writer"), credits.namesIn(CastCrewGroup.Writers))
}

@Test
fun seriesCaptionsDirectorCreditsAsCreator() {
val detail = ItemDetail(
contentId = "series-1",
type = "series",
title = "Series",
crew = listOf(CrewMember(name = "Showrunner", job = "Director", personId = "1")),
)

val credits = castCrewCredits(detail)

assertEquals(listOf("Creator"), credits.map { it.caption })
assertNull(credits.single().dividerLabel)
}

@Test
fun castOnlyFallbackHasNoDividers() {
val credits = castCrewCredits(
cast = listOf(
CastMember(name = "B", order = 1),
CastMember(name = "A", order = 0),
),
crew = listOf(CrewMember(name = "Producer", job = "Producer")),
isSeries = false,
)

assertEquals(listOf("A", "B"), credits.map { it.name })
assertTrue(credits.all { it.dividerLabel == null })
}

@Test
fun firstShownGroupNeverHasADivider() {
val credits = castCrewCredits(
cast = listOf(CastMember(name = "Star", order = 0)),
crew = listOf(CrewMember(name = "Writer", job = "Writer")),
isSeries = false,
)

assertEquals(listOf(null, "Cast"), credits.map { it.dividerLabel })
}

@Test
fun nonMovieOrSeriesKeepsThePlainCastRow() {
val cast = (0 until 15).map { CastMember(name = "Actor $it", character = "C$it", order = 15 - it) }
val detail = ItemDetail(
contentId = "episode-1",
type = "episode",
title = "Episode",
cast = cast,
crew = listOf(CrewMember(name = "Dir", job = "Director")),
)

val credits = castCrewCredits(detail)

assertEquals(cast.map { it.name }, credits.map { it.name })
assertTrue(credits.all { it.group == CastCrewGroup.Cast && it.dividerLabel == null })
}

@Test
fun initialsUseFirstAndLastWords() {
assertEquals("GG", personInitials("Greta Gerwig"))
assertEquals("JT", personInitials(" John Ronald Tolkien "))
assertEquals("C", personInitials("cher"))
assertEquals("", personInitials(" "))
}

private fun List<CastCrewCredit>.namesIn(group: CastCrewGroup) =
filter { it.group == group }.map { it.name }
}
Loading
Loading