itch.io: browse the Store tab by sort and price, with paging and a disk cache

The Store tab only had four fixed shelves and a title search. A row of sort
(Popular, New, Top rated, Top sellers) and price (Any, Free, On sale) chips now
sets a BrowseQuery; anything but the defaults swaps the shelves for that query's
games, loaded page by page as the grid scrolls.

ItchBrowse builds itch's listing-feed URL (platform and tag segments are in the
builder for the next change), tries each spelling of a price filter until one
answers, spaces requests 1.2 s apart, backs off once on a 429, and keeps pages
in the app cache for three hours. Every request is logged with its status and
game count so a wrong URL guess can be fixed from a device. Owned games replace
their browse copy so Install and the owned markers are right, and the detail
page finds browse results.

Strings for all 33 locales; URL-builder tests; docs/development/itch-browse.md.
This commit is contained in:
FX192 committed 2026-10-24 07:49:05 +03:00
1 parent 8f0bd51fd8
commit cc0a4f5aa9
40 files changed
+708 -8

No files matched your search

@@ -126,7 +126,9 @@ object StoresState {
fun installedGame(store: Store, id: String): InstalledStoreGame? = installed.firstOrNull { it.sidecar.store == store && it.sidecar.id == id }
fun detailItem(store: Store, id: String): CatalogItem? = detailCatalogItem(
store, id, library[store].orEmpty(), found[store].orEmpty(), shelves[store],
store, id, library[store].orEmpty(),
found[store].orEmpty() + if (store == Store.ITCH) com.droiddeck.launcher.stores.itch.ItchBrowseState.items else emptyList(),
shelves[store],
)
fun open(context: Context, store: Store, force: Boolean = false) {
@@ -0,0 +1,197 @@
package com.droiddeck.launcher.stores.itch
import android.content.Context
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.droiddeck.launcher.stores.CatalogItem
import com.droiddeck.launcher.stores.StoresState
import org.json.JSONArray
import java.io.File
import java.security.MessageDigest
/** What the Store tab is showing: an order, a price filter and, later, platforms and tags. The default is the front page. */
internal data class BrowseQuery(
val sort: String = ItchBrowse.SORT_POPULAR,
val price: String = ItchBrowse.PRICE_ANY,
val platforms: Set<String> = emptySet(),
val tags: List<String> = emptyList(),
) {
val isDefault: Boolean get() = this == BrowseQuery()
}
internal class BrowsePage(val items: List<CatalogItem>, val code: Int, val cached: Boolean)
internal object ItchBrowse {
const val SORT_POPULAR = "popular"
const val SORT_NEW = "new"
const val SORT_TOP_RATED = "top-rated"
const val SORT_TOP_SELLERS = "top-sellers"
val sorts = listOf(SORT_POPULAR, SORT_NEW, SORT_TOP_RATED, SORT_TOP_SELLERS)
const val PRICE_ANY = "any"
const val PRICE_FREE = "free"
const val PRICE_SALE = "sale"
val prices = listOf(PRICE_ANY, PRICE_FREE, PRICE_SALE)
private const val BASE = "https://itch.io/games"
private const val FRESH_MS = 3 * 60 * 60 * 1000L
private const val GAP_MS = 1_200L
private const val RETRY_MS = 8_000L
private const val FOLDER = "itch-browse"
@Volatile private var lastRequestAt = 0L
private val lock = Any()
private fun sortSegment(sort: String): String? = when (sort) {
SORT_NEW -> "newest"
SORT_TOP_RATED -> "top-rated"
SORT_TOP_SELLERS -> "top-sellers"
else -> null
}
/** The ways itch spells a price filter; the first that answers is used, so one wrong guess does not empty the tab. */
private fun priceSegments(price: String): List<String?> = when (price) {
PRICE_FREE -> listOf("free", "price-free")
PRICE_SALE -> listOf("on-sale")
else -> listOf(null)
}
/** Every feed URL that could answer [query] for [page], most likely first. */
fun urls(query: BrowseQuery, page: Int): List<String> = priceSegments(query.price).map { price ->
val segments = listOfNotNull(sortSegment(query.sort), price) +
query.platforms.sorted().map { "platform-$it" } +
query.tags.map { "tag-" + tagSlug(it) }
val path = if (segments.isEmpty()) BASE else BASE + "/" + segments.joinToString("/")
path + ".xml" + if (page > 1) "?page=$page" else ""
}
fun tagSlug(tag: String): String = tag.trim().lowercase().replace(Regex("[^a-z0-9]+"), "-").trim('-')
/** Page [page] of [query]: the disk copy when it is fresh, otherwise the feed, one request at a time and spaced out. */
fun page(context: Context, query: BrowseQuery, page: Int, force: Boolean = false): BrowsePage {
val app = context.applicationContext
val candidates = urls(query, page)
if (!force) for (url in candidates) {
val stored = read(app, url) ?: continue
return BrowsePage(stored, 200, true)
}
var code = 0
for (url in candidates) {
val (status, body) = fetch(url)
code = status
if (status !in 200..299 || body == null) continue
val items = ItchShelves.parse(body)
StoresState.logLine("itch.io browse: $url answered $status with ${items.size} games")
if (items.isEmpty()) continue
write(app, url, items)
return BrowsePage(items, status, false)
}
StoresState.logLine("itch.io browse: ${candidates.first()} gave nothing (last status $code)")
return BrowsePage(emptyList(), code, false)
}
private fun fetch(url: String): Pair<Int, String?> {
synchronized(lock) {
pause()
var answer = ItchShelves.open(url)
if (answer.first == 429) {
StoresState.logLine("itch.io browse: 429, waiting before one more try")
Thread.sleep(RETRY_MS)
pause()
answer = ItchShelves.open(url)
}
lastRequestAt = System.currentTimeMillis()
return answer
}
}
private fun pause() {
val gap = GAP_MS - (System.currentTimeMillis() - lastRequestAt)
if (gap > 0) Thread.sleep(gap)
}
private fun file(context: Context, url: String): File {
val digest = MessageDigest.getInstance("SHA-1").digest(url.toByteArray()).joinToString("") { "%02x".format(it) }
return File(File(context.cacheDir, FOLDER), "$digest.json")
}
private fun read(context: Context, url: String): List<CatalogItem>? {
val file = file(context, url)
if (!file.isFile || System.currentTimeMillis() - file.lastModified() > FRESH_MS) return null
return runCatching { ItchCatalog.list(JSONArray(file.readText())) }.getOrNull()?.takeIf { it.isNotEmpty() }
}
private fun write(context: Context, url: String, items: List<CatalogItem>) {
val file = file(context, url)
runCatching {
file.parentFile?.mkdirs()
file.writeText(ItchCatalog.array(items).toString())
}
}
}
/** The Store tab's result list: the query, the games loaded so far, and whether more can be asked for. */
internal object ItchBrowseState {
var query by mutableStateOf(BrowseQuery())
private set
var items by mutableStateOf<List<CatalogItem>>(emptyList())
private set
var loading by mutableStateOf(false)
private set
var ended by mutableStateOf(false)
private set
var failed by mutableStateOf(false)
private set
private var next = 1
private var generation = 0
/** Whether the tab shows these results instead of the front page shelves. */
val active: Boolean get() = !query.isDefault
fun choose(context: Context, wanted: BrowseQuery) {
if (wanted == query) return
generation++
query = wanted
items = emptyList()
ended = false
failed = false
loading = false
next = 1
if (active) more(context)
}
fun more(context: Context) {
if (loading || ended || !active) return
loading = true
failed = false
val app = context.applicationContext
val mine = generation
val asked = query
val page = next
Thread({
val answer = runCatching { ItchBrowse.page(app, asked, page) }.getOrNull()
StoresState.post {
if (mine != generation) return@post
loading = false
if (answer == null) {
failed = items.isEmpty()
ended = items.isNotEmpty()
return@post
}
if (answer.items.isEmpty()) {
failed = items.isEmpty() && answer.code !in 200..299
ended = true
return@post
}
val known = items.mapTo(HashSet()) { it.id }
val fresh = answer.items.filter { known.add(it.id) }
if (fresh.isEmpty()) ended = true else {
items = items + fresh
next = page + 1
}
}
}, "itch-browse").start()
}
}
@@ -108,7 +108,7 @@ internal object ItchShelves {
return shelves.takeUnless { it.isEmpty }
}
private fun open(url: String): Pair<Int, String?> {
internal fun open(url: String): Pair<Int, String?> {
var conn: HttpURLConnection? = null
return try {
conn = (URL(url).openConnection() as HttpURLConnection).apply {
@@ -0,0 +1,107 @@
package com.droiddeck.launcher.ui
import androidx.compose.foundation.LocalIndication
import androidx.compose.foundation.background
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.hoverable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.selection.selectableGroup
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.droiddeck.launcher.R
import com.droiddeck.launcher.stores.itch.ItchBrowse
import com.droiddeck.launcher.stores.itch.ItchBrowseState
/** Sort and price chips above the itch Store tab; picking anything but the defaults swaps the shelves for results. */
@Composable
internal fun ItchBrowseBar() {
val ctx = LocalContext.current
val query = ItchBrowseState.query
Column(Modifier.fillMaxWidth().padding(top = 6.dp, bottom = 8.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) {
ChipRow {
for (sort in ItchBrowse.sorts) BrowseChip(sortLabel(sort), query.sort == sort) { ItchBrowseState.choose(ctx, query.copy(sort = sort)) }
}
ChipRow {
for (price in ItchBrowse.prices) BrowseChip(priceLabel(price), query.price == price) { ItchBrowseState.choose(ctx, query.copy(price = price)) }
}
}
}
/** The end of the result list: it asks for the next page as it scrolls into view. */
@Composable
internal fun ItchLoadMore() {
val ctx = LocalContext.current
val count = ItchBrowseState.items.size
LaunchedEffect(count) { ItchBrowseState.more(ctx) }
if (ItchBrowseState.loading) LinearProgressIndicator(Modifier.fillMaxWidth().padding(vertical = 12.dp))
else if (!ItchBrowseState.ended && count > 0) Box(Modifier.fillMaxWidth().padding(vertical = 8.dp), contentAlignment = Alignment.Center) {
BrowseChip(stringResource(R.string.stores_itch_load_more), false) { ItchBrowseState.more(ctx) }
}
}
@Composable
private fun ChipRow(content: @Composable () -> Unit) {
Row(
horizontalArrangement = Arrangement.spacedBy(6.dp), verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxWidth().horizontalScroll(rememberScrollState()).selectableGroup(),
) { content() }
}
@Composable
private fun BrowseChip(label: String, selected: Boolean, onClick: () -> Unit) {
val colors = MaterialTheme.colorScheme
val pal = LocalPalette.current
val shape = RoundedCornerShape(8.dp)
val src = remember { MutableInteractionSource() }
val hot = rememberHot(src)
Text(
label, fontSize = 13.sp, fontWeight = FontWeight.SemiBold, maxLines = 1,
color = if (selected) pal.onSignal else if (hot) colors.onBackground else colors.onSurfaceVariant,
modifier = Modifier.clip(shape).background(if (selected) pal.signal else colors.surfaceVariant)
.glideBorder(hot, shape, pal.signal)
.hoverable(src).selectable(selected = selected, interactionSource = src, indication = LocalIndication.current, role = Role.Tab, onClick = onClick)
.controllerConfirm(onClick = onClick)
.padding(horizontal = 12.dp, vertical = 7.dp),
)
}
@Composable
private fun sortLabel(sort: String): String = stringResource(
when (sort) {
ItchBrowse.SORT_NEW -> R.string.stores_itch_sort_new
ItchBrowse.SORT_TOP_RATED -> R.string.stores_itch_sort_top_rated
ItchBrowse.SORT_TOP_SELLERS -> R.string.stores_itch_sort_top_sellers
else -> R.string.stores_itch_sort_popular
},
)
@Composable
private fun priceLabel(price: String): String = stringResource(
when (price) {
ItchBrowse.PRICE_FREE -> R.string.stores_itch_price_free
ItchBrowse.PRICE_SALE -> R.string.stores_itch_price_sale
else -> R.string.stores_itch_price_any
},
)
@@ -145,6 +145,7 @@ import com.droiddeck.launcher.R
import com.droiddeck.launcher.frontend.Library
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.stores.CatalogItem
import com.droiddeck.launcher.stores.itch.ItchBrowseState
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreTab
import com.droiddeck.launcher.stores.StoresState
@@ -954,7 +955,8 @@ private fun Storefront(
val everything = remember(library, shelves, found) { (found + library + (shelves?.all ?: emptyList())).distinctBy { it.id } }
LaunchedEffect(store, q) { StoresState.search(ctx, store, q) }
val loaded = StoresState.library.containsKey(store)
val showGrid = tab != "store" || q.isNotEmpty()
val browsing = store == Store.ITCH && tab == "store" && q.isEmpty() && ItchBrowseState.active
val showGrid = tab != "store" || q.isNotEmpty() || browsing
val sort = s.storesSort
val lastPlayedFor = remember(s.steamGames) {
val byKey = s.steamGames.associate { "${it.source}:${it.storeId}" to it.lastPlayed }
@@ -967,8 +969,13 @@ private fun Storefront(
lookup
}
val isSizeSort = tab == "library" && (sort == "sizeAsc" || sort == "sizeDesc")
val candidates = remember(library, everything, tab, q, s.storesShowMature) {
if (tab == "library") library.filter(::matches) else everything.filter { matches(it) && shown(it) }
val browseItems = if (browsing) ItchBrowseState.items else emptyList()
val candidates = remember(library, everything, tab, q, s.storesShowMature, browsing, browseItems) {
if (tab == "library") library.filter(::matches)
else if (browsing) {
val owned = library.associateBy { it.id }
browseItems.map { owned[it.id] ?: it }.filter(::shown)
} else everything.filter { matches(it) && shown(it) }
}
val candidateKeys = remember(candidates) { candidates.map { it.key } }
var resolvedSizeSortKey by remember(store, tab) { mutableStateOf<List<String>?>(null) }
@@ -1006,6 +1013,8 @@ private fun Storefront(
}
val empty = stringResource(when {
tab == "library" && library.isEmpty() && StoresState.status[store] != null -> R.string.stores_library_loading
browsing && ItchBrowseState.loading -> R.string.stores_shelves_loading
browsing && ItchBrowseState.failed -> R.string.stores_itch_browse_failed
else -> R.string.stores_nothing_matches
})
var firstPlaced = false
@@ -1016,8 +1025,9 @@ private fun Storefront(
// A bar only where the tab has nothing else on it: shelves, the shelf of owned games, or a grid still loading.
val gridEmpty = if (showGrid) visible.isEmpty() else (shelves == null || shelves.isEmpty) && library.isEmpty()
val barLine = StoresState.status[store]?.takeIf { gridEmpty && syncCount(it) != null }
val itchBar = store == Store.ITCH && tab == "store" && q.isEmpty()
val headerItems = 1 + (if (sizeSortPending) 1 else 0) + (if (barLine != null) 1 else 0) +
(if (StoresState.problems[store] != null) 1 else 0)
(if (StoresState.problems[store] != null) 1 else 0) + (if (itchBar) 1 else 0)
val listMode = s.storesView == "list" && tab == "library"
val capsules = s.storesView == "capsules" && tab == "library"
val tileWidth = if (capsules) CapsuleWidth else CardWidth
@@ -1102,6 +1112,7 @@ private fun Storefront(
}
barLine?.let { line -> item(key = "sync", span = { GridItemSpan(maxLineSpan) }) { Rise(3) { SyncBar(line) } } }
StoresState.problems[store]?.let { line -> item(key = "problem", span = { GridItemSpan(maxLineSpan) }) { Rise(3) { Box(Modifier.padding(bottom = 8.dp)) { Note(line) } } } }
if (itchBar) item(key = "itch-browse-bar", span = { GridItemSpan(maxLineSpan) }) { ItchBrowseBar() }
when (tab) {
"library" -> if (!loaded) Unit
else if (split == 0) storeGrid(visible, empty, card)
@@ -1112,8 +1123,10 @@ private fun Storefront(
storeGrid(visible.drop(split), empty, card)
}
else -> {
if (q.isNotEmpty()) storeGrid(visible, empty, card)
else {
if (q.isNotEmpty() || browsing) {
storeGrid(visible, empty, card)
if (browsing) item(key = "itch-more", span = { GridItemSpan(maxLineSpan) }) { ItchLoadMore() }
} else {
item(key = "shelves", span = { GridItemSpan(maxLineSpan) }) {
Column {
Shelf(stringResource(R.string.stores_shelf_new), shelf(shelves?.whatsNew), card)