From d75eccdc1745e59cb9cdb978cbdc12219f3ffc91 Mon Sep 17 00:00:00 2001 From: lotherk Date: Wed, 28 Oct 2026 18:55:55 +0000 Subject: [PATCH] Suggestions: swipable strip with a random tail MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The carousel under the player now scrolls sideways; the four visible slots keep their meaning. - Series.suggestionsFor takes a size: the first four slots stay [outsider, previous, next, outsider], everything after them is a shuffled tail of up to 24 entries, so swiping left and right keeps revealing random videos - the tail is only appended when both end slots really carry a video from outside the running series. Without that guard a series entry slid into position four as soon as the tail existed, which is exactly what must not happen - tests: long tail with the visible four intact, tail that mixes the series back in, exhausted library, and the two edge cases that must stay short 19 Series tests + 5 store tests, 0 failures; assembleDebug and lintDebug green. Built only — the tablet still runs the deployed build. --- AGENTS.md | 9 +++--- .../java/dev/lotherk/kidstube/data/Series.kt | 25 +++++++++------ .../dev/lotherk/kidstube/ui/PlayerActivity.kt | 2 +- .../dev/lotherk/kidstube/data/SeriesTest.kt | 31 +++++++++++++++++++ 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 2aa1bd9..a39e78c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -69,10 +69,11 @@ 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 `[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. + tested: the visible four are `[outsider, previous of the series, next of the + series, outsider]`, followed by a shuffled tail so the strip can be swiped + sideways. Slots 1 and 4 always come from another folder; the tail is only + appended when both of them are really filled, otherwise the promise would break + as a series entry slides into position four. - `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. 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 741f448..1446f8e 100644 --- a/app/src/main/java/dev/lotherk/kidstube/data/Series.kt +++ b/app/src/main/java/dev/lotherk/kidstube/data/Series.kt @@ -71,26 +71,31 @@ object Series { val used = hashSetOf(current.key) val out = ArrayList(size) - fun takeOutsider() { - while (outsiders.hasNext() && out.size < size) { + fun takeOutsider(): Boolean { + while (outsiders.hasNext()) { val candidate = outsiders.next() if (used.add(candidate.key)) { out.add(candidate) - return + return true } } + return false } - takeOutsider() + val firstFilled = 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() + val fourthFilled = 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) + // The swiping tail is only appended when both end slots really carry a + // video from outside the series. Otherwise the promise would break the + // moment a series entry slides into position four. + if (firstFilled && fourthFilled && out.size < size) { + val rest = all.filter { it.key !in used }.shuffled() + for (candidate in rest) { + if (out.size >= size) break + if (used.add(candidate.key)) out.add(candidate) + } } return out } 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 166cc6d..9136671 100644 --- a/app/src/main/java/dev/lotherk/kidstube/ui/PlayerActivity.kt +++ b/app/src/main/java/dev/lotherk/kidstube/ui/PlayerActivity.kt @@ -165,7 +165,7 @@ class PlayerActivity : AppCompatActivity() { private fun loadSuggestions(current: MediaItem) { executor.execute { val scan = Library.scan(this) - val others = Series.suggestionsFor(scan, current, 4) + val others = Series.suggestionsFor(scan, current, 24) library = scan pool = others runOnUiThread { 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 6640dac..a8569d4 100644 --- a/app/src/test/java/dev/lotherk/kidstube/data/SeriesTest.kt +++ b/app/src/test/java/dev/lotherk/kidstube/data/SeriesTest.kt @@ -162,6 +162,37 @@ class SeriesTest { assertNull(Series.nextToPlay(all, all[0])) } + @Test + fun suggestions_tailKeepsGoingForSwiping() { + val all = series("Sandmann", 10) + outsiders(6) + val out = Series.suggestionsFor(all, all[4], size = 12) + assertEquals(12, out.size) + assertEquals(12, out.map { it.key }.distinct().size) + // the visible four keep their rule even with a long tail + assertNotEquals("Sandmann", out[0].folder) + assertNotEquals("Sandmann", out[3].folder) + assertEquals("004 - Folge 4", out[1].title) + assertEquals("006 - Folge 6", out[2].title) + } + + @Test + fun suggestions_tailMayContainTheSeries() { + val all = series("Sandmann", 10) + outsiders(2) + val out = Series.suggestionsFor(all, all[4], size = 10) + assertEquals(10, out.size) + // only two outsiders exist, so slots after the fourth must come from the + // series — the tail is not restricted + assertTrue(out.drop(5).any { it.folder == "Sandmann" }) + } + + @Test + fun suggestions_stopsWhenTheLibraryIsExhausted() { + val all = series("Sandmann", 3) + outsiders(1) + val out = Series.suggestionsFor(all, all[1], size = 24) + assertEquals(3, out.size) // current + all four others, nothing repeats + assertEquals(3, out.map { it.key }.distinct().size) + } + @Test fun suggestions_handlesShortLibrary() { val all = series("Sandmann", 2)