Series: first and fourth suggestion never continue the series

The player's suggestion strip must offer something else at both ends, so the
row cannot read as "more of the same series".

- slots are now [outsider, previous, next, outsider]; an outsider is a video from
  another folder than the running one
- when there is no second outsider the fourth slot stays empty instead of falling
  back to the series
- neighbours keep their middle slots, so a series still continues in the middle
- tests cover 20 random runs for the end slots, the single-outsider case, a
  single-folder library, and no-repeat of the current video (11 tests total)

Not deployed: the child is watching, so the APK was only built and tested here.
This commit is contained in:
lotherk committed 2026-10-28 18:44:42 +00:00
1 parent cab9c9b1e2
commit ee50fb9288
3 files changed
+76 -46

No files matched your search

+4 -1
View File
@@ -69,7 +69,10 @@ Mandatory: from API 29 scoped storage forbids directory listing (the app uid get
- `data/Library` — SAF tree else MediaStore; `MediaItem`; sorted by folder then
`Series.naturalOrder` (leading `001`, `002` … numeric, not lexical).
- `data/Series` — ordering plus the player's suggestion rule, pure and unit
tested: slots are `[random, previous of the series, next of the series, random]`.
tested: slots are `[outsider, previous of the series, next of the series,
outsider]`. Slots 1 and 4 are guaranteed to come from a different folder than
the running video; if there is no such video the slot stays empty instead of
falling back to the same series.
- `media/ThumbCache` — decode via `MediaMetadataRetriever`, take the brightest of
three offsets (openings often fade from black), write JPEGs to
`filesDir/thumbs`, `LruCache` in memory, 3 threads, tag-guarded callback.
@@ -38,30 +38,36 @@ object Series {
fun <T : SeriesItem> suggestionsFor(all: List<T>, current: T, size: Int = 4): List<T> {
val (previous, next) = neighbours(all, current)
val used = hashSetOf(current.key)
previous?.let { used.add(it.key) }
next?.let { used.add(it.key) }
val rest = all.filter { it.key !in used }.shuffled().iterator()
// Slot 1 and slot 4 must never continue the running series: the child has
// to see something different at both ends of the strip, otherwise the row
// reads as "more of the same" and he just keeps the series running.
val outsiders = all.filter { it.folder != current.folder && it.key != current.key }
.shuffled()
.iterator()
val used = hashSetOf(current.key)
val out = ArrayList<T>(size)
fun takeRandom() {
while (rest.hasNext() && out.size < size) {
val candidate = rest.next()
if (out.none { it.key == candidate.key }) {
fun takeOutsider() {
while (outsiders.hasNext() && out.size < size) {
val candidate = outsiders.next()
if (used.add(candidate.key)) {
out.add(candidate)
return
}
}
}
takeRandom()
previous?.let { if (out.size < size) out.add(it) }
next?.let { if (out.size < size) out.add(it) }
takeRandom()
while (out.size < size && rest.hasNext()) {
val candidate = rest.next()
if (out.none { it.key == candidate.key }) out.add(candidate)
takeOutsider()
previous?.let { if (used.add(it.key) && out.size < size) out.add(it) }
next?.let { if (used.add(it.key) && out.size < size) out.add(it) }
takeOutsider()
// Fill the middle only from outside the series; a series entry never lands
// in a slot that is not one of the two explicit neighbour slots.
while (out.size < size && outsiders.hasNext()) {
val candidate = outsiders.next()
if (used.add(candidate.key)) out.add(candidate)
}
return out
}
@@ -1,6 +1,7 @@
package dev.lotherk.kidstube.data
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
@@ -19,6 +20,9 @@ class SeriesTest {
private fun series(folder: String, count: Int) =
(1..count).map { item(folder, String.format("%03d - Folge %d", it, it)) }
private fun outsiders(count: Int, folder: String = "Tiere") =
(1..count).map { item(folder, String.format("%03d - Tier %d", it, it)) }
@Test
fun naturalOrder_sortsByNumericPrefix() {
val shuffled = listOf(
@@ -58,37 +62,63 @@ class SeriesTest {
}
@Test
fun suggestions_middleOfSeries_keepsFixedSlotOrder() {
val all = series("Sandmann", 10)
fun suggestions_firstAndLastSlotNeverContinueTheSeries() {
val all = series("Sandmann", 10) + outsiders(4)
val current = all[4]
val out = Series.suggestionsFor(all, current)
assertEquals(4, out.size)
// slot 1 random, slot 2 previous, slot 3 next, slot 4 random
assertTrue(out[0].key != current.key)
assertEquals("004 - Folge 4", out[1].title)
assertEquals("006 - Folge 6", out[2].title)
assertTrue(out[3].key != current.key)
assertEquals(4, out.map { it.key }.distinct().size)
repeat(20) {
val out = Series.suggestionsFor(all, current)
assertEquals(4, out.size)
assertNotEquals("first slot must leave the series", "Sandmann", out[0].folder)
assertNotEquals("fourth slot must leave the series", "Sandmann", out[3].folder)
}
}
@Test
fun suggestions_firstOfSeries_hasNoPrevious() {
val all = series("Sandmann", 6)
fun suggestions_keepsNeighboursInTheMiddleSlots() {
val all = series("Sandmann", 10) + outsiders(4)
val current = all[4]
repeat(20) {
val out = Series.suggestionsFor(all, current)
assertEquals("004 - Folge 4", out[1].title)
assertEquals("006 - Folge 6", out[2].title)
}
}
@Test
fun suggestions_firstOfSeries_hasNoPreviousButStillFillsEnds() {
val all = series("Sandmann", 6) + outsiders(3)
val out = Series.suggestionsFor(all, all.first())
assertEquals(4, out.size)
assertEquals("002 - Folge 2", out[1].title) // next moves up one slot
assertNotEquals("Sandmann", out[0].folder)
assertNotEquals("Sandmann", out[3].folder)
assertEquals(4, out.map { it.key }.distinct().size)
}
@Test
fun suggestions_prefersNeighboursOverOtherFolders() {
val sandmann = series("Sandmann", 4)
val other = (1..6).map { item("Tiere", String.format("%03d - Tier %d", it, it)) }
val all = (sandmann + other).shuffled()
val current = sandmann[1]
val out = Series.suggestionsFor(all, current)
assertEquals("001 - Folge 1", out[1].title)
assertEquals("003 - Folge 3", out[2].title)
fun suggestions_withOneOutsider_leavesTheFourthSlotEmpty() {
val all = series("Sandmann", 6) + outsiders(1)
val out = Series.suggestionsFor(all, all[1])
assertEquals(3, out.size)
assertEquals("Tiere", out[0].folder)
assertNotEquals("Tiere", out[1].folder)
}
@Test
fun suggestions_singleFolderLibrary_returnsOnlyNeighbours() {
val all = series("Sandmann", 4)
val out = Series.suggestionsFor(all, all[1])
assertEquals(2, out.size)
assertEquals("001 - Folge 1", out[0].title)
assertEquals("003 - Folge 3", out[1].title)
}
@Test
fun suggestions_neverRepeatsTheCurrentVideo() {
val all = series("Sandmann", 3) + outsiders(3)
repeat(30) {
val out = Series.suggestionsFor(all, all[1])
assertTrue(out.none { it.key == all[1].key })
}
}
@Test
@@ -98,13 +128,4 @@ class SeriesTest {
assertEquals(1, out.size)
assertEquals("002 - Folge 2", out[0].title)
}
@Test
fun suggestions_neverRepeatsTheCurrentVideo() {
val all = series("Sandmann", 3)
repeat(20) {
val out = Series.suggestionsFor(all, all[1])
assertTrue(out.none { it.key == all[1].key })
}
}
}