refactor(android): one plural() says every count, so none reads "1 tracks" (DRY B1)

The playlist card, the playlist header and the playlist a mix was
started from said "${n} tracks" whatever n was. Seven count labels
were each written by hand; they now go through shared/plural(), which
also formats the number for the listener's locale, as the web's counts
do. PluralTest names any hand-written copy (checked against the tree
before this change: it flags all seven).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 14:04:22 -05:00
co-authored by Claude Opus 5.5
parent b46725e387
commit a5d94754bf
10 changed files with 76 additions and 12 deletions
@@ -48,6 +48,7 @@ import com.fabledsword.minstrel.models.TrackRef
import com.fabledsword.minstrel.nav.AlbumDetail
import com.fabledsword.minstrel.nav.ArtistDetail
import com.fabledsword.minstrel.shared.formatDuration
import com.fabledsword.minstrel.shared.plural
import com.fabledsword.minstrel.shared.widgets.CoverTile
import com.fabledsword.minstrel.shared.widgets.ShellContentWindowInsets
import com.fabledsword.minstrel.shared.widgets.TrackRow
@@ -280,7 +281,7 @@ private fun AlbumCover(album: AlbumRef) {
}
private fun trackCountLine(album: AlbumRef): String {
val tracks = if (album.trackCount == 1) "1 track" else "${album.trackCount} tracks"
val tracks = plural(album.trackCount, "track", "tracks")
return if (album.year != null) "$tracks · ${album.year}" else tracks
}
@@ -56,6 +56,7 @@ import com.fabledsword.minstrel.models.albumCoverPath
import com.fabledsword.minstrel.nav.AlbumDetail
import com.fabledsword.minstrel.nav.ArtistDetail
import com.fabledsword.minstrel.shared.formatDuration
import com.fabledsword.minstrel.shared.plural
import com.fabledsword.minstrel.shared.widgets.ShellContentWindowInsets
import com.fabledsword.minstrel.shared.widgets.ErrorRetry
import com.fabledsword.minstrel.shared.widgets.HorizontalScrollRow
@@ -341,7 +342,7 @@ private fun ArtistAvatar(artist: ArtistRef, fallbackAlbumId: String? = null) {
}
private fun albumCountLine(artist: ArtistRef): String =
if (artist.albumCount == 1) "1 album" else "${artist.albumCount} albums"
plural(artist.albumCount, "album", "albums")
@Composable
private fun EmptyAlbumsHint() {
@@ -37,6 +37,7 @@ import com.composables.icons.lucide.ArrowLeft
import com.composables.icons.lucide.Lucide
import com.composables.icons.lucide.Trash2
import com.fabledsword.minstrel.models.TrackRef
import com.fabledsword.minstrel.shared.plural
import com.fabledsword.minstrel.shared.widgets.EmptyState
import kotlinx.coroutines.launch
@@ -186,8 +187,7 @@ private fun queueSummary(tracks: List<TrackRef>): String {
} else {
"$minutes min"
}
val noun = if (tracks.size == 1) "track" else "tracks"
return "${tracks.size} $noun · $length"
return "${plural(tracks.size, "track", "tracks")} · $length"
}
private const val SECONDS_PER_MINUTE = 60
@@ -1,6 +1,7 @@
package com.fabledsword.minstrel.playlists
import com.fabledsword.minstrel.models.PlaylistRef
import com.fabledsword.minstrel.shared.plural
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
@@ -103,9 +104,6 @@ val byShelfRank: Comparator<PlaylistRef> =
*/
fun songsLikeArtist(p: PlaylistRef): String = p.name.replace(SONGS_LIKE_PREFIX, "")
/** "{n} song" / "{n} songs". */
fun plural(n: Int, one: String, many: String): String = "$n ${if (n == 1) one else many}"
/** An sRGB colour as 0–255 channels; kept free of Compose so it unit-tests. */
data class Rgb(val r: Int, val g: Int, val b: Int)
@@ -84,6 +84,7 @@ import com.fabledsword.minstrel.playlists.data.trackOrder
import com.fabledsword.minstrel.playlists.data.withEntryMoved
import com.fabledsword.minstrel.playlists.data.withoutEntry
import com.fabledsword.minstrel.shared.formatDuration
import com.fabledsword.minstrel.shared.plural
import com.fabledsword.minstrel.shared.widgets.ConfirmDestructiveDialog
import com.fabledsword.minstrel.shared.widgets.CoverTile
import com.fabledsword.minstrel.shared.widgets.NameDialog
@@ -628,7 +629,7 @@ private fun PlaylistHeader(
}
Spacer(Modifier.height(4.dp))
Text(
text = "${playlist.trackCount} tracks",
text = plural(playlist.trackCount, "track", "tracks"),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
@@ -840,7 +841,7 @@ private fun SeededPlaylistLoading(seed: PlaylistRef) {
}
Spacer(Modifier.height(4.dp))
Text(
text = "${seed.trackCount} tracks",
text = plural(seed.trackCount, "track", "tracks"),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
@@ -23,6 +23,7 @@ import com.composables.icons.lucide.ListMusic
import com.composables.icons.lucide.Lucide
import com.fabledsword.minstrel.models.PlaylistRef
import com.fabledsword.minstrel.nav.LocalDetailSeedCache
import com.fabledsword.minstrel.shared.plural
import com.fabledsword.minstrel.shared.widgets.CoverTile
import com.fabledsword.minstrel.shared.widgets.PlayCircleButton
import com.fabledsword.minstrel.theme.FabledSwordFlatTokens
@@ -156,7 +157,7 @@ private fun VariantPill(label: String, modifier: Modifier = Modifier) {
private const val PILL_BG_ALPHA = 0.85f
private fun subtitleFor(playlist: PlaylistRef): String = when {
playlist.trackCount > 0 -> "${playlist.trackCount} tracks"
playlist.trackCount > 0 -> plural(playlist.trackCount, "track", "tracks")
playlist.isSystem -> systemLabelFor(playlist.systemVariant)
else -> " "
}
@@ -43,9 +43,9 @@ import com.fabledsword.minstrel.models.albumCoverPath
import com.fabledsword.minstrel.nav.LocalDetailSeedCache
import com.fabledsword.minstrel.player.ui.rememberDominantColor
import com.fabledsword.minstrel.playlists.Rgb
import com.fabledsword.minstrel.playlists.plural
import com.fabledsword.minstrel.playlists.songsLikeArtist
import com.fabledsword.minstrel.playlists.washTone
import com.fabledsword.minstrel.shared.plural
import com.fabledsword.minstrel.shared.widgets.CoverTile
import com.fabledsword.minstrel.shared.widgets.PlayCircleButton
import com.fabledsword.minstrel.shared.widgets.ServerImage
@@ -0,0 +1,11 @@
package com.fabledsword.minstrel.shared
import java.text.NumberFormat
/**
* A count with its noun: "1 track", "26,171 tracks". The number is formatted
* for the listener's locale, as the web app's counts are. The app's one way to
* say a count; PluralTest names any hand-written copy.
*/
fun plural(n: Int, one: String, many: String): String =
"${NumberFormat.getIntegerInstance().format(n)} ${if (n == 1) one else many}"
@@ -32,6 +32,7 @@ import com.composables.icons.lucide.Lucide
import com.composables.icons.lucide.Plus
import com.fabledsword.minstrel.models.PlaylistRef
import com.fabledsword.minstrel.shared.UiState
import com.fabledsword.minstrel.shared.plural
import com.fabledsword.minstrel.shared.widgets.NameDialog
/**
@@ -138,7 +139,7 @@ private fun PlaylistPickRow(playlist: PlaylistRef, onClick: () -> Unit) {
overflow = TextOverflow.Ellipsis,
)
Text(
text = "${playlist.trackCount} ${if (playlist.trackCount == 1) "track" else "tracks"}",
text = plural(playlist.trackCount, "track", "tracks"),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
@@ -0,0 +1,50 @@
package com.fabledsword.minstrel.shared
import org.junit.jupiter.api.Test
import java.io.File
import java.util.Locale
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class PluralTest {
@Test
fun `says a count with the noun that agrees with it, in the listener's number format`() {
val saved = Locale.getDefault()
Locale.setDefault(Locale.US)
try {
assertEquals("1 track", plural(1, "track", "tracks"))
assertEquals("0 tracks", plural(0, "track", "tracks"))
assertEquals("26,171 tracks", plural(26_171, "track", "tracks"))
} finally {
Locale.setDefault(saved)
}
}
// One way to say a count, so every count agrees on wording and the
// thousands separator. A hand-written copy has a tell this guard looks for:
// - a branch that spells the one: `if (n == 1) "1 album" else ...`;
// - the number beside a choice of noun: `"${n} ${if (n == 1) ...}"`;
// - a choice of noun alone: `if (n == 1) "track" else "tracks"`;
// - a count with a fixed plural, which reads "1 tracks": `"${x.trackCount} tracks"`.
private val handRolled = listOf(
Regex("""== 1\) "1 """),
Regex("""\$\{?([\w.]+)\}? \$\{if \(\1 == 1\)"""),
Regex("""== 1\) "[a-z]+" else "[a-z]+s""""),
Regex("""\$\{[\w.]+(?:Count|\.size)\} (?:tracks|albums|songs|artists|playlists|files|users)\b"""),
)
/** Comments describe the forbidden shape; strip them before asserting its absence. */
private fun stripComments(s: String) = s.replace(Regex("""^\s*(//|\*|/\*).*$""", RegexOption.MULTILINE), "")
@Test
fun `every count in the app is said by plural()`() {
val root = File("src/main/java")
val sources = root.walkTopDown().filter { it.extension == "kt" }.toList()
assertTrue(sources.size > 100, "expected the app's sources under ${root.absolutePath}")
val offenders = sources.filter { f ->
val text = stripComments(f.readText())
handRolled.any { it.containsMatchIn(text) }
}.map { it.relativeTo(root).path }
assertEquals(emptyList(), offenders)
}
}