mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
Detect itch.io updates and offer to install them
Installed itch games are compared with the uploads itch lists for them. A game with a newer upload shows an Update label in the Library and an Update button on its page, a replaced build asks first, and a Check for updates button in the itch Library header runs the check on demand.
This commit is contained in:
42 files changed
+392
-3
No files matched your search
@@ -191,7 +191,10 @@ object StoresState {
|
||||
backend?.signOut(app)
|
||||
if (store == Store.GOG) com.droiddeck.launcher.stores.gog.GogComet.replaceAccount { StoreAccounts.clear(app, store) }
|
||||
else if (store != Store.XBOX || backend == null) StoreAccounts.clear(app, store)
|
||||
if (store == Store.ITCH) com.droiddeck.launcher.stores.itch.ItchCollections.clear()
|
||||
if (store == Store.ITCH) {
|
||||
com.droiddeck.launcher.stores.itch.ItchCollections.clear()
|
||||
com.droiddeck.launcher.stores.itch.ItchUpdates.clear(app)
|
||||
}
|
||||
main.post {
|
||||
if (store == Store.XBOX && StoreAccounts.signedInAs(app, store) != null) return@post
|
||||
accounts.remove(store); library.remove(store); status.remove(store); problems.remove(store); libraryAsked.remove(store)
|
||||
|
||||
@@ -58,6 +58,7 @@ internal class ItchInstaller(
|
||||
download(key, downloadKeyId, uploadId, upload.optString("updated_at", upload.optString("updatedAt")), archive, size, handle)
|
||||
if (cancelled.get()) return null
|
||||
handle.stage(DownloadStage.INSTALL)
|
||||
if (StoreGameSidecar.read(folder)?.isInstalled == true) StoreInstalls.markUpdating(folder)
|
||||
install(archive, filename, handle, resuming)
|
||||
} catch (t: Throwable) {
|
||||
if (!cancelled.get()) StoreInstalls.discard(app, folder, listOf(scratch))
|
||||
@@ -83,6 +84,7 @@ internal class ItchInstaller(
|
||||
cloud = false,
|
||||
)
|
||||
StoreInstalls.complete(app, folder, sidecar) { done, steps -> handle.stageProgress(done.toLong(), steps.toLong()) }
|
||||
ItchUpdates.applied(app, item.key)
|
||||
return folder.path
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
package com.droiddeck.launcher.stores.itch
|
||||
|
||||
import android.content.Context
|
||||
import android.widget.Toast
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
import androidx.compose.runtime.mutableStateMapOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.stores.CatalogItem
|
||||
import com.droiddeck.launcher.stores.InstalledStoreGame
|
||||
import com.droiddeck.launcher.stores.Store
|
||||
import com.droiddeck.launcher.stores.StoresState
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
internal class ItchUpdate(val uploadId: String, val updatedAt: String, val filename: String, val replaced: Boolean)
|
||||
|
||||
internal object ItchUpdates {
|
||||
private const val KEY_AT = "updates_checked_at"
|
||||
private const val KEY_FOUND = "updates_found"
|
||||
private const val THROTTLE_MS = 6 * 60 * 60 * 1000L
|
||||
private const val GAP_MS = 1_000L
|
||||
|
||||
val available = mutableStateMapOf<String, ItchUpdate>()
|
||||
var checking by mutableStateOf(false)
|
||||
private set
|
||||
var done by mutableIntStateOf(0)
|
||||
private set
|
||||
var total by mutableIntStateOf(0)
|
||||
private set
|
||||
|
||||
private val running = AtomicBoolean(false)
|
||||
private val restored = AtomicBoolean(false)
|
||||
|
||||
internal fun evaluate(uploads: JSONArray?, uploadId: String, version: String): ItchUpdate? {
|
||||
if (uploads == null || version.isBlank()) return null
|
||||
val current = ItchUploads.byId(uploads, uploadId)
|
||||
if (current != null) {
|
||||
val stamp = ItchUploads.updatedAt(current)
|
||||
return if (stamp > version) ItchUpdate(uploadId, stamp, ItchUploads.filename(current), false) else null
|
||||
}
|
||||
val next = ItchUploads.windows(uploads) ?: return null
|
||||
val stamp = ItchUploads.updatedAt(next)
|
||||
if (uploadId.isBlank() && stamp <= version) return null
|
||||
return ItchUpdate(next.optString("id"), stamp, ItchUploads.filename(next), uploadId.isNotBlank())
|
||||
}
|
||||
|
||||
internal fun encode(found: Map<String, ItchUpdate>): String = JSONObject().also { out ->
|
||||
for ((key, u) in found) out.put(key, JSONObject().put("uploadId", u.uploadId).put("updatedAt", u.updatedAt).put("filename", u.filename).put("replaced", u.replaced))
|
||||
}.toString()
|
||||
|
||||
internal fun decode(raw: String?): Map<String, ItchUpdate> {
|
||||
val json = runCatching { JSONObject(raw.orEmpty()) }.getOrNull() ?: return emptyMap()
|
||||
val out = LinkedHashMap<String, ItchUpdate>()
|
||||
for (key in json.keys()) {
|
||||
val u = json.optJSONObject(key) ?: continue
|
||||
val id = u.optString("uploadId").ifBlank { continue }
|
||||
out[key] = ItchUpdate(id, u.optString("updatedAt"), u.optString("filename"), u.optBoolean("replaced"))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
private fun restore(app: Context) {
|
||||
if (!restored.compareAndSet(false, true)) return
|
||||
val saved = decode(ItchPrefs.get(app).getString(KEY_FOUND, null))
|
||||
StoresState.post { for ((k, v) in saved) if (k !in available) available[k] = v }
|
||||
}
|
||||
|
||||
private fun save(app: Context) {
|
||||
ItchPrefs.get(app).edit().putString(KEY_FOUND, encode(available.toMap())).apply()
|
||||
}
|
||||
|
||||
fun check(context: Context, force: Boolean) {
|
||||
val app = context.applicationContext
|
||||
if (!running.compareAndSet(false, true)) return
|
||||
Thread({
|
||||
try {
|
||||
restore(app)
|
||||
val prefs = ItchPrefs.get(app)
|
||||
if (!force && System.currentTimeMillis() - prefs.getLong(KEY_AT, 0L) < THROTTLE_MS) return@Thread
|
||||
val key = ItchAuth.key(app)
|
||||
if (key.isBlank()) return@Thread
|
||||
val games = StoresState.installed.toList().filter { it.sidecar.store == Store.ITCH && it.sidecar.isInstalled }
|
||||
StoresState.post { total = games.size; done = 0; checking = true }
|
||||
val found = LinkedHashMap<String, ItchUpdate>()
|
||||
val answered = HashSet<String>()
|
||||
var failed = false
|
||||
games.forEachIndexed { i, game ->
|
||||
if (i > 0) Thread.sleep(GAP_MS)
|
||||
val extra = game.sidecar.extra
|
||||
val gameId = extra["gameId"].orEmpty()
|
||||
if (gameId.isNotEmpty()) {
|
||||
val uploads = ItchApi.uploads(key, gameId, extra["downloadKeyId"].orEmpty())
|
||||
if (uploads == null) failed = true
|
||||
else {
|
||||
answered += game.key
|
||||
evaluate(uploads, extra["uploadId"].orEmpty(), game.sidecar.installVersion)?.let { found[game.key] = it }
|
||||
}
|
||||
}
|
||||
StoresState.post { done = i + 1 }
|
||||
}
|
||||
StoresState.post {
|
||||
for (k in answered) {
|
||||
val u = found[k]
|
||||
if (u != null) available[k] = u else available.remove(k)
|
||||
}
|
||||
save(app)
|
||||
}
|
||||
if (!failed) prefs.edit().putLong(KEY_AT, System.currentTimeMillis()).apply()
|
||||
StoresState.logLine("itch.io: checked ${games.size} installed games, ${found.size} with updates")
|
||||
if (force) StoresState.post {
|
||||
val text = if (found.isNotEmpty()) app.getString(R.string.stores_itch_updates_found, found.size) else app.getString(R.string.upd_current_status)
|
||||
Toast.makeText(app, text, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
} finally {
|
||||
running.set(false)
|
||||
StoresState.post { checking = false }
|
||||
}
|
||||
}, "itch-updates").start()
|
||||
}
|
||||
|
||||
fun start(context: Context, game: InstalledStoreGame, update: ItchUpdate) {
|
||||
val app = context.applicationContext
|
||||
val root = game.folder.parentFile?.parentFile ?: return
|
||||
val extra = game.sidecar.extra + ("uploadId" to update.uploadId)
|
||||
val item = CatalogItem(
|
||||
Store.ITCH, game.sidecar.id, game.sidecar.title, game.sidecar.hero, tallImageUrl = game.sidecar.cover,
|
||||
isFree = extra["downloadKeyId"].isNullOrBlank(), owned = true, extra = extra,
|
||||
)
|
||||
StoresState.install(app, item, root)
|
||||
}
|
||||
|
||||
fun applied(context: Context, key: String) {
|
||||
val app = context.applicationContext
|
||||
StoresState.post {
|
||||
available.remove(key)
|
||||
save(app)
|
||||
}
|
||||
}
|
||||
|
||||
fun clear(context: Context) {
|
||||
val app = context.applicationContext
|
||||
ItchPrefs.get(app).edit().remove(KEY_AT).remove(KEY_FOUND).apply()
|
||||
StoresState.post { available.clear() }
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,8 @@ internal object ItchUploads {
|
||||
return (0 until uploads.length()).mapNotNull { uploads.optJSONObject(it) }.firstOrNull { it.optString("id") == id }
|
||||
}
|
||||
|
||||
fun updatedAt(upload: JSONObject): String = upload.optString("updated_at", upload.optString("updatedAt"))
|
||||
|
||||
fun size(upload: JSONObject): Long = upload.optLong("size", upload.optLong("size_bytes"))
|
||||
|
||||
fun filename(upload: JSONObject): String = upload.optString("filename").substringAfterLast('/')
|
||||
|
||||
@@ -357,6 +357,7 @@ internal fun GameActions(
|
||||
GameShortcutMenu(game, a)
|
||||
EditArtButton(game)
|
||||
}
|
||||
if (store == Store.ITCH && installed != null) ItchUpdateButton(installed, download == null)
|
||||
if (store == Store.XBOX && id != null) XboxInstalledStream(id, game?.name ?: item?.title.orEmpty(), item)
|
||||
if (store == Store.XBOX && installed != null && id != null) {
|
||||
SecondaryButton(stringResource(R.string.stores_xbox_update), enabled = download == null && !XboxRuntime.isActive(id)) {
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.stores.InstalledStoreGame
|
||||
import com.droiddeck.launcher.stores.itch.ItchUpdate
|
||||
import com.droiddeck.launcher.stores.itch.ItchUpdates
|
||||
|
||||
@Composable
|
||||
internal fun ItchUpdateButton(installed: InstalledStoreGame, enabled: Boolean) {
|
||||
val ctx = LocalContext.current
|
||||
val update = ItchUpdates.available[installed.key] ?: return
|
||||
var asking by remember { mutableStateOf(false) }
|
||||
SecondaryButton(stringResource(R.string.common_update), enabled = enabled) {
|
||||
if (update.replaced) asking = true else ItchUpdates.start(ctx, installed, update)
|
||||
}
|
||||
if (asking) ReplacedDialog(installed, update) { asking = false }
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ReplacedDialog(installed: InstalledStoreGame, update: ItchUpdate, onDismiss: () -> Unit) {
|
||||
val ctx = LocalContext.current
|
||||
val shown = rememberShown(onDismiss)
|
||||
val close = { shown.targetState = false }
|
||||
AppDialog(shown, close, "itchUpdate", wide = false, maxWidth = 460.dp) {
|
||||
DialogTitleRow(installed.sidecar.title, close)
|
||||
Text(
|
||||
stringResource(R.string.stores_itch_update_replaced, update.filename.ifBlank { update.uploadId }),
|
||||
fontSize = 13.sp, color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
PrimaryButton(stringResource(R.string.common_update)) {
|
||||
close()
|
||||
ItchUpdates.start(ctx, installed, update)
|
||||
}
|
||||
SecondaryButton(stringResource(R.string.common_cancel)) { close() }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,7 @@ import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.FilterAlt
|
||||
import androidx.compose.material.icons.filled.GridView
|
||||
import androidx.compose.material.icons.filled.PlayArrow
|
||||
import androidx.compose.material.icons.filled.Refresh
|
||||
import androidx.compose.material.icons.filled.Sort
|
||||
import androidx.compose.material.icons.filled.ViewList
|
||||
import androidx.compose.material.icons.filled.ViewModule
|
||||
@@ -91,6 +92,7 @@ import com.droiddeck.launcher.stores.download.DownloadEntry
|
||||
import com.droiddeck.launcher.stores.formatBytes
|
||||
import com.droiddeck.launcher.stores.itch.ItchCollection
|
||||
import com.droiddeck.launcher.stores.itch.ItchCollections
|
||||
import com.droiddeck.launcher.stores.itch.ItchUpdates
|
||||
import com.droiddeck.launcher.stores.retainOrder
|
||||
import com.droiddeck.launcher.stores.xbox.XboxLibraryFilter
|
||||
import com.droiddeck.launcher.stores.sortGames
|
||||
@@ -194,7 +196,10 @@ internal fun LibraryPage(s: FrontEndState, a: FrontEndActions, selected: String,
|
||||
val source = LibraryView.source.takeIf { it == LIBRARY_ALL || it in sources } ?: LIBRARY_ALL
|
||||
val q = LibraryView.query.trim().lowercase()
|
||||
val xboxFilter = if (source == Store.XBOX.id) XboxLibraryFilter.current else XboxLibraryFilter.ALL
|
||||
LaunchedEffect(source, itchSignedIn) { if (source == Store.ITCH.id && itchSignedIn) ItchCollections.load(ctx) }
|
||||
LaunchedEffect(source, itchSignedIn) { if (source == Store.ITCH.id && itchSignedIn) {
|
||||
ItchCollections.load(ctx)
|
||||
ItchUpdates.check(ctx, false)
|
||||
} }
|
||||
val collection = if (source == Store.ITCH.id) ItchCollections.chosen() else null
|
||||
val pool = if (collection == null) all else collectionEntries(all, ItchCollections.gamesOf(collection.id).orEmpty())
|
||||
val matching = pool.filter {
|
||||
@@ -468,7 +473,10 @@ private fun LibraryHeader(
|
||||
},
|
||||
) {
|
||||
if (showUninstalled != null || xbox) LibraryFilterButton(showUninstalled, onShowUninstalled, xbox)
|
||||
if (collections) CollectionsButton()
|
||||
if (collections) {
|
||||
CollectionsButton()
|
||||
UpdatesButton()
|
||||
}
|
||||
SortButton(s, a)
|
||||
ViewButton(s, a)
|
||||
GameFileFolderActions(s, a)
|
||||
@@ -582,6 +590,7 @@ internal fun LibraryMarker(e: LibraryEntry, download: DownloadEntry?) {
|
||||
download != null -> Text("${download.percent}%", fontSize = 10.sp, fontWeight = FontWeight.Bold, color = pal.signal, maxLines = 1)
|
||||
e.item != null && StoresState.isUnfinished(e.item) -> Text(stringResource(R.string.stores_resume), fontSize = 10.sp, fontWeight = FontWeight.Bold, color = pal.signal, maxLines = 1)
|
||||
else -> Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(3.dp)) {
|
||||
if (e.installed && e.storeKey?.let { it in ItchUpdates.available } == true) Text(stringResource(R.string.common_update), fontSize = 10.sp, fontWeight = FontWeight.Bold, color = pal.signal, maxLines = 1)
|
||||
if (size != null && size > 0) Text(formatBytes(size), fontSize = 11.sp, fontWeight = FontWeight.Bold, color = Color.White, maxLines = 1)
|
||||
if (!e.installed) Icon(Icons.Outlined.Download, stringResource(R.string.stores_section_not_installed), tint = Color.White, modifier = Modifier.size(12.dp))
|
||||
}
|
||||
@@ -722,6 +731,14 @@ private fun LibraryFilterButton(showUninstalled: Boolean?, onShowUninstalled: (B
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun UpdatesButton() {
|
||||
val ctx = LocalContext.current
|
||||
val title = stringResource(R.string.store_check_updates)
|
||||
val description = if (ItchUpdates.checking) title + " · " + ItchUpdates.done + "/" + ItchUpdates.total else title
|
||||
HeaderIconButton(description, Icons.Filled.Refresh, "itch-updates") { ItchUpdates.check(ctx, true) }
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun CollectionsButton() {
|
||||
val pal = LocalPalette.current
|
||||
|
||||
Reference in new issue
Block a user