Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import me.xdan.aperture.data.local.entity.RemoteLibraryEntity
import me.xdan.aperture.domain.catalog.CatalogItem
import me.xdan.aperture.domain.catalog.JellyfinImages
import me.xdan.aperture.domain.catalog.LibraryQuery
import me.xdan.aperture.platform.KeyValueStores
import me.xdan.aperture.domain.catalog.MediaInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
Expand All @@ -26,11 +25,8 @@ import org.jellyfin.sdk.model.serializer.toUUIDOrNull
/** Reads the active profile's Jellyfin index for the screens. */
class JellyfinCatalog(
private val dao: JellyfinDao,
private val accounts: JellyfinAccounts,
stores: KeyValueStores
private val accounts: JellyfinAccounts
) {
private val queries = stores.open(QUERY_STORE)

val activeAccount: Flow<JellyfinAccountEntity?> = accounts.activeAccount

/** Every library the user can see, in the server's order. */
Expand Down Expand Up @@ -138,7 +134,7 @@ class JellyfinCatalog(
s.width?.let { w -> s.height?.let { h -> MediaInfo.resolutionLabel(w, h) } },
s.codec?.uppercase(),
s.profile,
s.videoRangeType.takeIf { it != VideoRangeType.UNKNOWN && it != VideoRangeType.SDR }?.serialName,
rangeLabel(s.videoRangeType),
s.bitDepth?.let { "$it-bit" },
s.realFrameRate?.let { "${"%.3f".format(it).trimEnd('0').trimEnd('.')} fps" }
).joinToString(" · ")
Expand Down Expand Up @@ -170,13 +166,6 @@ class JellyfinCatalog(
}.getOrDefault(emptyList())
}

fun savedQuery(profileId: Long, libraryId: String): LibraryQuery =
LibraryQuery.decode(queries.getString("$profileId:$libraryId", null))

fun saveQuery(profileId: Long, libraryId: String, query: LibraryQuery) {
queries.edit().putString("$profileId:$libraryId", query.encode()).apply()
}

private fun sql(accountId: Long, libraryId: String, query: LibraryQuery, seed: Int): RoomRawQuery {
val where = buildList {
add("accountId = ?")
Expand All @@ -185,6 +174,8 @@ class JellyfinCatalog(
if (query.unwatchedOnly) add("played = 0")
if (query.favouritesOnly) add("isFavorite = 1")
if (query.genre != null) add("instr('|' || COALESCE(genres, '') || '|', ?) > 0")
if (query.decade != null) add("year BETWEEN ? AND ?")
if (query.uhdOnly) add("(width >= 3800 OR height >= 2100)")
}.joinToString(" AND ")
val direction = if (query.descending) "DESC" else "ASC"
val title = "COALESCE(sortName, name) COLLATE NOCASE"
Expand All @@ -204,12 +195,27 @@ class JellyfinCatalog(
statement.bindLong(index++, accountId)
statement.bindText(index++, libraryId)
query.genre?.let { statement.bindText(index++, "|$it|") }
query.decade?.let {
statement.bindLong(index++, it.toLong())
statement.bindLong(index++, it + 9L)
}
if (query.sort == LibraryQuery.Sort.RANDOM) statement.bindLong(index, (seed.toLong() and 0xFFFF) + 7919)
}
}

companion object {
const val ROW_LIMIT = 20
private const val QUERY_STORE = "library_queries"
}

/** HDR formats as people know them; SDR and unknown say nothing. */
private fun rangeLabel(type: VideoRangeType): String? = when (type) {
VideoRangeType.HDR10 -> "HDR10"
VideoRangeType.HDR10_PLUS -> "HDR10+"
VideoRangeType.HLG -> "HLG"
VideoRangeType.DOVI, VideoRangeType.DOVI_WITH_SDR, VideoRangeType.DOVI_WITH_EL, VideoRangeType.DOVI_INVALID -> "Dolby Vision"
VideoRangeType.DOVI_WITH_HDR10 -> "Dolby Vision · HDR10"
VideoRangeType.DOVI_WITH_HDR10_PLUS, VideoRangeType.DOVI_WITH_ELHDR10_PLUS -> "Dolby Vision · HDR10+"
VideoRangeType.DOVI_WITH_HLG -> "Dolby Vision · HLG"
else -> null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ val sharedModule = module {
single { me.xdan.aperture.data.jellyfin.JellyfinAccounts(get(), get(), get(), get()) }
single { me.xdan.aperture.data.jellyfin.JellyfinSync(get(), get()) }
single { me.xdan.aperture.data.jellyfin.JellyfinLibraryWatcher(get(), get(), get(), get()) }
single { me.xdan.aperture.data.jellyfin.JellyfinCatalog(get(), get(), get()) }
single { me.xdan.aperture.data.jellyfin.JellyfinCatalog(get(), get()) }
single<MediaRepository> { MediaRepositoryImpl(get(), get(), get(), get(), get(), get(), get()) }
single { SponsorVerificationManager(get(), get()) }
single { OpenSubtitlesSessionManager(get(), get()) }
Expand All @@ -116,13 +116,14 @@ val sharedModule = module {
viewModel { AmbientViewModel(get(), get(), get()) }
viewModel { MediaDetailsViewModel(get(), get()) }
viewModel { SearchViewModel(get(), get(), get()) }
viewModel { LibraryViewModel(get(), get()) }
viewModel { LibraryViewModel(get(), get(), get(), get()) }
viewModel { MediaActionsViewModel(get()) }
viewModel { PlayerViewModel(get(), get(), get(), get(), get(), get(), get(), get(), get()) }
viewModel { CompanionViewModel(get()) }
viewModel { me.xdan.aperture.ui.screen.profiles.ProfilesViewModel(get()) }
viewModel { me.xdan.aperture.ui.screen.jellyfin.JellyfinViewModel(get(), get(), get(), get(), get()) }
viewModel { params -> me.xdan.aperture.ui.screen.jellyfin.JellyfinLibraryViewModel(params.get(), get(), get()) }
single { me.xdan.aperture.domain.catalog.LibraryQueryStore(get()) }
viewModel { params -> me.xdan.aperture.ui.screen.jellyfin.JellyfinLibraryViewModel(params.get(), get(), get(), get()) }
viewModel { me.xdan.aperture.ui.screen.jellyfin.JellyfinHomeViewModel(get()) }
viewModel { params -> me.xdan.aperture.ui.screen.jellyfin.JellyfinDetailsViewModel(params.get(), get()) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ data class LibraryQuery(
val descending: Boolean = false,
val unwatchedOnly: Boolean = false,
val favouritesOnly: Boolean = false,
val genre: String? = null
val genre: String? = null,
/** The decade's first year, e.g. 1990 for the 1990s. */
val decade: Int? = null,
val uhdOnly: Boolean = false
) {
enum class Sort(val label: String, val defaultDescending: Boolean) {
TITLE("Title", false),
Expand All @@ -18,11 +21,14 @@ data class LibraryQuery(
RANDOM("Random", false)
}

val isFiltered: Boolean get() = unwatchedOnly || favouritesOnly || genre != null
val isFiltered: Boolean get() = unwatchedOnly || favouritesOnly || genre != null || decade != null || uhdOnly

fun encode(): String = listOf(sort.name, descending, unwatchedOnly, favouritesOnly, genre.orEmpty()).joinToString("\u001F")
fun encode(): String = listOf(
sort.name, descending, unwatchedOnly, favouritesOnly, genre.orEmpty(), decade?.toString().orEmpty(), uhdOnly
).joinToString("\u001F")

companion object {
/** Reads a saved query; fields added later fall back to their defaults. */
fun decode(value: String?): LibraryQuery {
val parts = value?.split('\u001F') ?: return LibraryQuery()
return runCatching {
Expand All @@ -31,9 +37,46 @@ data class LibraryQuery(
descending = parts[1].toBoolean(),
unwatchedOnly = parts[2].toBoolean(),
favouritesOnly = parts[3].toBoolean(),
genre = parts.getOrNull(4)?.takeIf { it.isNotBlank() }
genre = parts.getOrNull(4)?.takeIf { it.isNotBlank() },
decade = parts.getOrNull(5)?.toIntOrNull(),
uhdOnly = parts.getOrNull(6)?.toBoolean() ?: false
)
}.getOrDefault(LibraryQuery())
}

fun decadeLabel(decade: Int): String = "${decade}s"
}
}

/** Remembers each library's sort and filters, per profile. Local grids use ids like "local:movies". */
class LibraryQueryStore(stores: me.xdan.aperture.platform.KeyValueStores) {
private val queries = stores.open("library_queries")

fun load(profileId: Long, libraryId: String): LibraryQuery =
LibraryQuery.decode(queries.getString("$profileId:$libraryId", null))

fun save(profileId: Long, libraryId: String, query: LibraryQuery) {
queries.edit().putString("$profileId:$libraryId", query.encode()).apply()
}
}

/** The letter a title files under, ignoring a leading "The"/"A"/"An" like Jellyfin's sort names. "#" covers the rest. */
fun sortLetter(title: String): Char {
val trimmed = title.trim().replace(Regex("^(the|a|an)\\s+", RegexOption.IGNORE_CASE), "")
val first = trimmed.firstOrNull()?.uppercaseChar() ?: return '#'
return if (first in 'A'..'Z') first else '#'
}

/** The first position of each letter in a title-sorted list, for the A–Z rail. */
fun letterIndex(titles: List<String>, descending: Boolean): Map<Char, Int> {
val index = LinkedHashMap<Char, Int>()
titles.forEachIndexed { position, title ->
val letter = sortLetter(title)
if (letter !in index) index[letter] = position
}
return if (descending) index else index.toSortedMap(compareBy { if (it == '#') ' ' else it })
}

/** Title order that files "The Matrix" under M, matching [sortLetter]. */
fun sortableTitle(title: String): String =
title.trim().replace(Regex("^(the|a|an)\\s+", RegexOption.IGNORE_CASE), "").lowercase()
Loading
Loading