Repository navigation
Replies: 6 comments 1 reply
|
Any ideas? |
|
As far as I know, there isn't a direct API to query by starting letter, so there's no way to tell which letters are in a given library without searching the entire library. So there isn't a great way to filter out unused letters. The letters shown are actually determined by the UI language, not the library. But this is complicated because, while a library can be associated with a particular Country/Region, that doesn't necessarily mean the titles will be only in that country's implicit language (not to mention countries where multiple languages are common!). So I'm open to ideas or suggestions for improving this! |
|
@damontecres — coming back to this with something concrete: the API can already answer "which letters
A letter is present in the library iff Cost: one pass of Bonus: Sketch below — mechanism only, not compiled here, and the one-line pass-through of the new Two things worth knowing before this is worth merging:
Separately, one small thing I noticed while reading diff--- a/app/src/main/java/com/github/damontecres/wholphin/ui/components/CollectionFolderView.kt
+++ b/app/src/main/java/com/github/damontecres/wholphin/ui/components/CollectionFolderView.kt
@@ -78,6 +78,9 @@
import dagger.assisted.AssistedInject
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
+import kotlinx.coroutines.async
+import kotlinx.coroutines.awaitAll
+import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
@@ -469,7 +472,42 @@
* The count must come from the same endpoint that [createPager] pages through, otherwise
* the index refers to a different result set and lands past the end of the list.
*/
- suspend fun positionOfLetter(letter: Char): Int? =
+ suspend fun positionOfLetter(letter: Char): Int? = countBefore(letter.toString())
+
+ /**
+ * The subset of [letters] that actually has items, derived from the same counts the
+ * alphabet jump already uses: a letter is present iff more items sort before the next
+ * letter than before it.
+ *
+ * One pass over the alphabet, every request `limit=0`, so the server answers with a
+ * COUNT and no rows. Returns null when counting is not supported for the current filter,
+ * in which case the caller keeps the full alphabet.
+ */
+ suspend fun presentLetters(letters: String): String? =
+ withContext(WholphinDispatchers.IO) {
+ val total = countBefore(null) ?: return@withContext null
+ val boundaries =
+ coroutineScope {
+ letters
+ .drop(1)
+ .chunked(COUNT_CONCURRENCY)
+ .flatMap { chunk ->
+ chunk.map { async { countBefore(it.toString()) } }.awaitAll()
+ }
+ }
+ if (boundaries.any { it == null }) return@withContext null
+ // The server's collation decides what `nameLessThan` counts, not the order of
+ // the resource string, so keep the offsets monotonic before comparing them.
+ val offsets =
+ (listOf(0) + boundaries.filterNotNull() + listOf(total))
+ .runningReduce(::maxOf)
+ letters
+ .filterIndexed { index, _ -> offsets[index + 1] > offsets[index] }
+ .ifEmpty { letters }
+ }
+
+ /** Number of items sorting before [name], or the total number of items when null. */
+ private suspend fun countBefore(name: String?): Int? =
withContext(WholphinDispatchers.IO) {
val filter = state.value.filter
when (filter.override) {
@@ -480,7 +518,7 @@
createGetArtistsRequest(filter).copy(
enableImageTypes = null,
fields = null,
- nameLessThan = letter.toString(),
+ nameLessThan = name,
limit = 0,
enableTotalRecordCount = true,
enableUserData = false,
@@ -505,7 +543,7 @@
).copy(
enableImageTypes = null,
fields = null,
- nameLessThan = letter.toString(),
+ nameLessThan = name,
limit = 0,
enableTotalRecordCount = true,
enableUserData = false,
@@ -952,3 +990,6 @@
)
}
}
+
+/** How many letter counts are requested at once when probing the alphabet. */
+private const val COUNT_CONCURRENCY = 8
--- a/app/src/main/java/com/github/damontecres/wholphin/ui/detail/CardGrid.kt
+++ b/app/src/main/java/com/github/damontecres/wholphin/ui/detail/CardGrid.kt
@@ -110,6 +110,8 @@
gridFocusRequester: FocusRequester,
showJumpButtons: Boolean,
showLetterButtons: Boolean,
+ /** Narrows the alphabet to the letters present; null keeps every letter. */
+ presentLetters: (suspend (String) -> String?)? = null,
modifier: Modifier = Modifier,
initialPosition: Int = 0,
positionCallback: ((columns: Int, position: Int) -> Unit)? = null,
@@ -426,19 +428,25 @@
}
}
}
- val letters = stringResource(R.string.jump_letters)
+ val allLetters = stringResource(R.string.jump_letters)
+ var letters by remember(allLetters) { mutableStateOf(allLetters) }
+ LaunchedEffect(allLetters, showLetterButtons) {
+ if (showLetterButtons) {
+ letters = presentLetters?.invoke(allLetters) ?: allLetters
+ }
+ }
// Letters
val currentLetter =
- remember(focusedIndex) {
+ remember(focusedIndex, letters) {
pager
.getOrNull(focusedIndex)
?.sortName
?.firstOrNull()
?.uppercaseChar()
?.let {
- when (it) {
- in '0'..'9' -> '#'
- in 'A'..'Z' -> it
+ when {
+ it in '0'..'9' -> '#'
+ it in letters -> it
else -> null
}
} |
|
I'll take a look at your idea, but just wanted to linked this discussion to another about the letter scrolling: #1856 |
|
Thanks for the link — #1856 is useful context, and I think the two are the same problem The grid layout there exists because the bar is long. The bar is long because it carries Two more things I found while reading the server side, both relevant to #968. 1. The counts assume ascending order, the bar does not. The alphabet pass fixes this for free: once 2. The server compares lowercased dbQuery = dbQuery.Where(e => e.SortName!.ToLower().CompareTo(lessThanLower) < 0);while the ordering for That is the shape of the report in #968: a contiguous block of letters off by exactly one or This also bounds what the presence check can promise: it inherits the same comparison, so a |
|
@damontecres — following up since it's been a couple of weeks on the offset-pass sketch above — happy to Separately, the same per-letter offsets turned out to explain #968 as well (posted the |
Uh oh!
There was an error while loading. Please reload this page.
Description
Currently, the alphabetical jump/fast-scroll bar in library views displays the complete alphabet (e.g., full A-Z followed by full Cyrillic А-Я) regardless of whether there are any media items starting with those letters.
When browsing libraries—especially bilingual ones—this results in a long list of 60+ characters, many of which contain no items. Navigating this list with a TV remote controller requires excessive scrolling through empty letters, creating unnecessary friction.
Solution:
Dynamic generation of the alphabet indexer bar:
Leaving the full static list makes fast-scrolling inefficient on TV devices, especially when combined with dual-language alphabets (Latin + Cyrillic).
Additional context
All reactions