diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AddonBrowser.kt b/app/src/main/java/com/droiddeck/launcher/ui/AddonBrowser.kt index 052241a8b9..fc62ea718d 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/AddonBrowser.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/AddonBrowser.kt @@ -273,7 +273,7 @@ internal fun AddonBrowserDialog(targets: List, initial: WowAdd var text by remember { mutableStateOf("") } var category by remember { mutableStateOf(null) } var sort by remember { mutableStateOf(WowAddonCatalog.Sort.POPULAR) } - var source by remember { mutableStateOf(AddonSource.WOWI) } + var source by remember(curseForge != null) { mutableStateOf(if (curseForge != null) AddonSource.CURSE else AddonSource.WOWI) } val curse = if (source == AddonSource.CURSE && link != null) curseForge else null LaunchedEffect(link) { link?.refresh() } var notice by remember { mutableStateOf(null) } @@ -425,8 +425,8 @@ internal fun AddonBrowserDialog(targets: List, initial: WowAdd } val sourceChip: @Composable () -> Unit = { if (curseForge != null) BrowseDropdown(sourceName) { first, done -> - MenuItem(stringResource(R.string.addons_source_wowi), source == AddonSource.WOWI, focusRequester = first) { source = AddonSource.WOWI; done() } - MenuItem(stringResource(R.string.addons_source_curse), source == AddonSource.CURSE) { source = AddonSource.CURSE; done() } + MenuItem(stringResource(R.string.addons_source_wowi), curse == null, focusRequester = first) { source = AddonSource.WOWI; done() } + MenuItem(stringResource(R.string.addons_source_curse), curse != null) { source = AddonSource.CURSE; done() } } } val categoryChip: @Composable () -> Unit = { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt b/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt index 9a0b43e1d7..061c873670 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt @@ -56,6 +56,7 @@ import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.SideEffect import androidx.compose.runtime.key +import androidx.compose.runtime.mutableStateMapOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.produceState import androidx.compose.runtime.remember @@ -1069,8 +1070,36 @@ private fun AddonsTab(g: Library.SteamGame, targets: List, ses runCatching { CurseForgeApp.byFolder(link.installed(context, target, scanKey)) }.getOrDefault(emptyMap()) } } + val progress = remember(target.addOns) { mutableStateMapOf() } + val curseOf = { item: WowAddons.Installed -> link?.let { l -> managed[item.name.lowercase()]?.let { it to l } } } + val outdated = names.filter { item -> curseOf(item)?.first?.update ?: (updates[item.name] != null) } + val scope = rememberCoroutineScope() if (targets.size > 1) SectionLabel(target.flavor) MenuItem(stringResource(R.string.addons_download), checked = false) { browsing = target } + if (outdated.size > 1 && !SessionState.running) MenuItem( + stringResource(R.string.store_update_all, outdated.size), checked = false, enabled = progress.isEmpty(), + ) { + val plan = outdated.groupBy { item -> curseOf(item)?.first?.id?.let { "curse:$it" } ?: "folder:${item.name}" }.values.map { group -> + Triple(group.map { it.name }, updates[group.first().name], curseOf(group.first())) + } + plan.forEach { (folders) -> folders.forEach { progress[it] = 0 } } + val owned = plan.flatMap { it.first }.toMutableSet() + scope.launch { + try { + plan.forEach { (folders, wowi, curse) -> + val name = folders.first() + val (message, ok) = updateAddon(context, name, wowi.takeIf { curse == null }, target, curse) { pct -> + scope.launch { folders.forEach { progress[it] = pct } } + } + folders.forEach { progress.remove(it) } + owned.removeAll(folders.toSet()) + if (!ok && message != null) android.widget.Toast.makeText(context, message, android.widget.Toast.LENGTH_LONG).show() + } + } finally { + owned.forEach { progress.remove(it) } + } + } + } MenuItem( stringResource(R.string.games_import_addons), checked = false, detail = pluralStringResource(R.plurals.games_addons_count, names.size, names.size), @@ -1079,7 +1108,7 @@ private fun AddonsTab(g: Library.SteamGame, targets: List, ses target.addOns.mkdirs() a.onBrowseFiles(target.addOns) } - names.forEach { item -> key(target.addOns, item.name) { AddonRow(item, updates[item.name], target, a, link?.let { l -> managed[item.name.lowercase()]?.let { it to l } }) } } + names.forEach { item -> key(target.addOns, item.name) { AddonRow(item, updates[item.name], target, a, progress, curseOf(item)) } } } } } @@ -1133,12 +1162,12 @@ private fun CurseForgeEntry(a: FrontEndActions) { } @Composable -internal fun AddonRow(item: WowAddons.Installed, wowiUpdate: WowAddonCatalog.Addon?, target: WowAddons.Target, a: FrontEndActions, curse: Pair? = null) { +internal fun AddonRow(item: WowAddons.Installed, wowiUpdate: WowAddonCatalog.Addon?, target: WowAddons.Target, a: FrontEndActions, progresses: MutableMap, curse: Pair? = null) { val name = item.name val update = wowiUpdate.takeIf { curse == null } val outdated = update != null || curse?.first?.update == true var asking by remember(target.addOns, name) { mutableStateOf(false) } - var progress by remember(target.addOns, name) { mutableStateOf(null) } + val progress = progresses[name] val context = LocalContext.current val scope = rememberCoroutineScope() val delete = stringResource(R.string.common_delete) @@ -1153,31 +1182,39 @@ internal fun AddonRow(item: WowAddons.Installed, wowiUpdate: WowAddonCatalog.Add ) } if (outdated && !SessionState.running) IconAction(Icons.Outlined.Download, stringResource(R.string.common_update), "addon:update:${target.flavor}:$name", enabled = progress == null) { - progress = 0 + progresses[name] = 0 scope.launch { - val message = withContext(Dispatchers.IO) { - if (curse != null) { - val (project, link) = curse - val done = runCatching { link.change(context, target) { CurseForgeApp.update(context, it.inspector, it.guid, project.id) } }.getOrDefault(false) - WowAddons.changed() - if (done) context.getString(R.string.user_apps_updated, name) else context.getString(R.string.stores_dl_failed_reason, name) - } else update?.let { installAddon(context, it, target) { pct -> scope.launch { progress = pct } }.first } + try { + val message = updateAddon(context, name, update, target, curse) { pct -> scope.launch { progresses[name] = pct } }.first + if (message != null) android.widget.Toast.makeText(context, message, android.widget.Toast.LENGTH_LONG).show() + } finally { + progresses.remove(name) } - progress = null - if (message != null) android.widget.Toast.makeText(context, message, android.widget.Toast.LENGTH_LONG).show() } } - if (!SessionState.running) IconAction(Icons.Outlined.Delete, if (curse != null) stringResource(R.string.common_uninstall) else delete, "addon:delete:${target.flavor}:$name", danger = true) { asking = true } + if (!SessionState.running) IconAction(Icons.Outlined.Delete, if (curse != null) stringResource(R.string.common_uninstall) else delete, "addon:delete:${target.flavor}:$name", danger = true, enabled = progress == null) { asking = true } } if (asking) ConfirmDialog( title = if (curse != null) stringResource(R.string.stores_uninstall_title, name) else stringResource(R.string.games_delete_addon_title, name), text = stringResource(R.string.games_delete_addon_body), confirm = if (curse != null) stringResource(R.string.common_uninstall) else delete, - onConfirm = { a.onAddonDelete(target, name) }, + onConfirm = { asking = false; if (progresses[name] == null) a.onAddonDelete(target, name) }, onDismiss = { asking = false }, ) } +private suspend fun updateAddon( + context: android.content.Context, name: String, update: WowAddonCatalog.Addon?, target: WowAddons.Target, + curse: Pair?, onProgress: (Int) -> Unit, +): Pair = withContext(Dispatchers.IO) { + if (curse != null) { + val (project, link) = curse + val done = runCatching { link.change(context, target) { CurseForgeApp.update(context, it.inspector, it.guid, project.id) } }.getOrDefault(false) + WowAddons.changed() + (if (done) context.getString(R.string.user_apps_updated, name) else context.getString(R.string.stores_dl_failed_reason, name)) to done + } else update?.let { installAddon(context, it, target, onProgress).let { (message, version) -> message to (version != null) } } ?: (null to false) +} + @Composable private fun CloudRows(g: Library.SteamGame, onSynced: () -> Unit = {}) { val store = Store.byId(g.source)?.takeIf { it != Store.AMAZON } ?: return diff --git a/app/src/test/java/com/droiddeck/launcher/ui/CurseLinkTabTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/CurseLinkTabTest.kt index a64e56383f..b16a03071b 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/CurseLinkTabTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/CurseLinkTabTest.kt @@ -1,8 +1,12 @@ package com.droiddeck.launcher.ui import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.mutableStateMapOf import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.test.assertCountEquals +import androidx.compose.ui.test.assertIsNotEnabled +import androidx.compose.ui.test.onAllNodesWithText +import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onAllNodesWithContentDescription import com.droiddeck.launcher.R import com.droiddeck.launcher.runtime.AppImageManager @@ -302,12 +306,19 @@ class CurseLinkTabTest { onSetPhantomProcessLimit = { _, _ -> }, ) - private fun row(update: Boolean?, wowi: Boolean) { + private fun row(update: Boolean?, wowi: Boolean, progress: MutableMap = mutableMapOf()) { val target = WowAddons.Target("_retail_", temp.newFolder("AddOns")) val item = WowAddons.Installed("Leatrix_Maps", "1.60.35", null) val newer = WowAddonCatalog.Addon("1", "Leatrix Maps", "Leatrix", "1.60.36", 0, 0, null).takeIf { wowi } val curse = update?.let { CurseForgeApp.Project("298842", "m", listOf("Leatrix_Maps"), it) to CurseLink(app) } - compose.setContent { DroidDeckTheme { AddonRow(item, newer, target, actions, curse) } } + compose.setContent { DroidDeckTheme { AddonRow(item, newer, target, actions, progress, curse) } } + } + + @Test + fun aRowUpdatedByUpdateAllShowsItsProgressAndCannotStartASecondUpdate() { + row(update = true, wowi = false, progress = mutableStateMapOf("Leatrix_Maps" to 0)) + compose.onAllNodesWithText(RuntimeEnvironment.getApplication().getString(R.string.ctrl_percent, 0)).assertCountEquals(1) + compose.onNodeWithContentDescription(text(R.string.common_update)).assertIsNotEnabled() } @Test