Series: a finished video continues its own series

Auto-advance follows the series instead of picking at random.

- next video = the next episode of the same folder, in numeric order
- at the end of a series: the first episode of another folder (random folder),
  so the child never lands in the middle of an unfamiliar series
- a library with a single folder wraps to its own first episode; a single video
  simply stops
- Series.nextToPlay is pure and unit tested with an injectable folder picker:
  middle of a series, end of a series, wrap in a one-folder library, single video
  (16 tests, 0 failures)

Not deployed: the child is still watching, so the APK was only built here.
This commit is contained in:
lotherk committed 2026-10-28 18:47:52 +00:00
1 parent ee50fb9288
commit 382bb2653c
4 files changed
+76 -3

No files matched your search

+3 -1
View File
@@ -83,7 +83,9 @@ Mandatory: from API 29 scoped storage forbids directory listing (the app uid get
tile in its cell. Tiles are sized from the **home grid** height, one value
shared by both lists.
- `ui/PlayerActivity` — VideoView, immersive, chrome auto-hides; four suggestions,
heart to like, and a finished video rolls into a random one.
heart to like. When a video ends, `Series.nextToPlay` continues the same folder
with the next episode; if the series is finished it starts another folder at
its first episode.
- `stats/StatsDb` — SQLite: `sessions` (plays, watched ms, per day) and `likes`.
Mirrored to `getExternalFilesDir()/stats.json` for another process to pull.
@@ -36,6 +36,29 @@ object Series {
return previous to next
}
/**
* What plays when the current video ends: the next episode of the same
* series (folder), and when that series is finished, the first episode of
* another series. A series is a folder.
*/
fun <T : SeriesItem> nextToPlay(
all: List<T>,
current: T,
pickFolder: (List<String>) -> String = { it.random() }
): T? {
val series = all.filter { it.folder == current.folder }.sortedWith(naturalOrder)
val index = series.indexOfFirst { it.key == current.key }
if (index >= 0 && index < series.size - 1) return series[index + 1]
val byFolder = all.filter { it.folder != current.folder }.groupBy { it.folder }
if (byFolder.isNotEmpty()) {
val folder = pickFolder(byFolder.keys.toList())
return byFolder[folder]?.sortedWith(naturalOrder)?.firstOrNull()
}
// Only one series in the library: start it over.
return series.firstOrNull { it.key != current.key }
}
fun <T : SeriesItem> suggestionsFor(all: List<T>, current: T, size: Int = 4): List<T> {
val (previous, next) = neighbours(all, current)
@@ -39,6 +39,7 @@ class PlayerActivity : AppCompatActivity() {
private val executor = Executors.newSingleThreadExecutor()
private lateinit var suggestions: SuggestionAdapter
private var pool: List<MediaItem> = emptyList()
private var library: List<MediaItem> = emptyList()
private lateinit var stats: StatsDb
private var item: MediaItem? = null
private var segmentStart = 0L
@@ -114,8 +115,13 @@ class PlayerActivity : AppCompatActivity() {
binding.video.setOnCompletionListener {
commitWatch()
recordSegment()
val next = pool.firstOrNull { it.uri != item?.uri }
if (next != null) play(next) else finish()
// Next episode of the same series; when that is finished, the first
// episode of another series.
val current = item
val next = if (current != null) Series.nextToPlay(library, current) else null
val fallback = pool.firstOrNull { it.uri != current?.uri }
val target = next ?: fallback
if (target != null) play(target) else finish()
}
binding.video.setOnErrorListener { _, _, _ ->
@@ -160,6 +166,7 @@ class PlayerActivity : AppCompatActivity() {
executor.execute {
val scan = Library.scan(this)
val others = Series.suggestionsFor(scan, current, 4)
library = scan
pool = others
runOnUiThread {
suggestions.submit(others)
@@ -121,6 +121,47 @@ class SeriesTest {
}
}
@Test
fun nextToPlay_middleOfSeries_isTheNextEpisode() {
val all = series("Sandmann", 6) + outsiders(3)
val next = Series.nextToPlay(all, all[2])
assertEquals("004 - Folge 4", next?.title)
}
@Test
fun nextToPlay_lastOfSeries_startsAnotherSeriesAtItsFirstEpisode() {
val all = series("Sandmann", 4) + outsiders(3, "Pingu")
val last = all.filter { it.folder == "Sandmann" }.maxByOrNull { it.title }
val next = Series.nextToPlay(all, last!!) { "Pingu" }
assertEquals("Pingu", next?.folder)
assertEquals("001 - Tier 1", next?.title)
}
@Test
fun nextToPlay_neverPicksTheCurrentVideoWhenStartingASeries() {
val all = series("Sandmann", 3) + outsiders(2, "Pingu")
val last = all.filter { it.folder == "Sandmann" }.maxByOrNull { it.title }!!
repeat(20) {
val next = Series.nextToPlay(all, last)
assertNotEquals(last.key, next?.key)
assertNotEquals("Sandmann", next?.folder)
}
}
@Test
fun nextToPlay_singleFolderWrapsToItsFirstEpisode() {
val all = series("Sandmann", 4)
val last = all.last()
val next = Series.nextToPlay(all, last)
assertEquals("001 - Folge 1", next?.title)
}
@Test
fun nextToPlay_lastVideoOfLibrary_returnsNull() {
val all = series("Sandmann", 1)
assertNull(Series.nextToPlay(all, all[0]))
}
@Test
fun suggestions_handlesShortLibrary() {
val all = series("Sandmann", 2)