package com.droiddeck.launcher import android.os.Handler import android.util.Log import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import com.droiddeck.launcher.session.ComponentsManager /** * The Components page's state and work: the Protons and their DXVK/VKD3D sets, the Nightlies * catalog, downloads and the swap/restore actions. The launcher screen keeps one and shows it. */ internal class ComponentsMenu(private val activity: android.app.Activity, private val ui: android.os.Handler) { var compSnapshot by mutableStateOf(null) var compCatalog by mutableStateOf>(emptyList()) var compCatalogAt by mutableStateOf(0L) var compProton by mutableStateOf(null) var compComp by mutableStateOf("dxvk") var compChecking by mutableStateOf(false) var compBusy by mutableStateOf(null) var compDownloads by mutableStateOf>(emptyMap()) /** Reads the Protons (and on first open saves their originals) off the UI thread; the Nightlies list comes from its cache. */ fun refreshComponents(snapshotFirst: Boolean = false) { Thread({ if (snapshotFirst) runCatching { ComponentsManager.snapshotAll(activity) } runCatching { ComponentsManager.applyQueued(activity) } val snap = runCatching { ComponentsManager.snapshot(activity) }.onFailure { Log.w(TAG, "components", it) }.getOrNull() val cat = runCatching { ComponentsManager.catalog(activity, false) }.getOrNull() ui.post { compSnapshot = snap ?: ComponentsManager.Snapshot(emptyList(), emptyList()) if (cat != null && cat.fetchedAt > 0) { compCatalog = cat.items; compCatalogAt = cat.fetchedAt } if (compProton == null || snap?.protons?.none { it.proton.id == compProton } == true) compProton = snap?.protons?.firstOrNull()?.proton?.id } }, "components").start() } fun componentAction(label: String, work: () -> String) { if (compBusy != null) return compBusy = label Thread({ val message = runCatching(work).getOrElse { e -> "$label failed: ${e.message ?: e.javaClass.simpleName}" } ui.post { compBusy = null android.widget.Toast.makeText(activity, message, android.widget.Toast.LENGTH_LONG).show() refreshComponents() } }, "components-action").start() } fun refreshComponentCatalog() { if (compChecking) return compChecking = true Thread({ val cat = runCatching { ComponentsManager.catalog(activity, true) }.getOrNull() ui.post { compChecking = false if (cat == null || cat.items.isEmpty()) android.widget.Toast.makeText(activity, "The Nightlies could not be reached", android.widget.Toast.LENGTH_LONG).show() else { compCatalog = cat.items; compCatalogAt = cat.fetchedAt } } }, "components-catalog").start() } fun downloadComponent(item: ComponentsManager.CatalogItem) { if (compDownloads.containsKey(item.file)) return compDownloads = compDownloads + (item.file to -1) Thread({ val message = runCatching { val pkg = ComponentsManager.download(activity, item) { pc -> ui.post { if (compDownloads.containsKey(item.file)) compDownloads = compDownloads + (item.file to pc) } } "Stored ${pkg.version}" }.getOrElse { e -> "Download failed: ${e.message ?: e.javaClass.simpleName}" } ui.post { compDownloads = compDownloads - item.file android.widget.Toast.makeText(activity, message, android.widget.Toast.LENGTH_SHORT).show() refreshComponents() } }, "components-download").start() } private companion object { const val TAG = "MainActivity" } }