diff --git a/AGENTS.md b/AGENTS.md index 5255e10..c89f0b0 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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. diff --git a/app/src/main/java/dev/lotherk/kidstube/data/Series.kt b/app/src/main/java/dev/lotherk/kidstube/data/Series.kt index 08a6c8f..741f448 100644 --- a/app/src/main/java/dev/lotherk/kidstube/data/Series.kt +++ b/app/src/main/java/dev/lotherk/kidstube/data/Series.kt @@ -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 nextToPlay( + all: List, + current: T, + pickFolder: (List) -> 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 suggestionsFor(all: List, current: T, size: Int = 4): List { val (previous, next) = neighbours(all, current) diff --git a/app/src/main/java/dev/lotherk/kidstube/ui/PlayerActivity.kt b/app/src/main/java/dev/lotherk/kidstube/ui/PlayerActivity.kt index 9d85361..135adab 100644 --- a/app/src/main/java/dev/lotherk/kidstube/ui/PlayerActivity.kt +++ b/app/src/main/java/dev/lotherk/kidstube/ui/PlayerActivity.kt @@ -39,6 +39,7 @@ class PlayerActivity : AppCompatActivity() { private val executor = Executors.newSingleThreadExecutor() private lateinit var suggestions: SuggestionAdapter private var pool: List = emptyList() + private var library: List = 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) diff --git a/app/src/test/java/dev/lotherk/kidstube/data/SeriesTest.kt b/app/src/test/java/dev/lotherk/kidstube/data/SeriesTest.kt index 67567f1..6640dac 100644 --- a/app/src/test/java/dev/lotherk/kidstube/data/SeriesTest.kt +++ b/app/src/test/java/dev/lotherk/kidstube/data/SeriesTest.kt @@ -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)