diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreSizes.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreSizes.kt index 5e8ce7c6d7..b102c8114c 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreSizes.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreSizes.kt @@ -43,7 +43,7 @@ object StoreSizes { if (available != null) return CompletableFuture.completedFuture(available) val app = context.applicationContext val version = item.extra["version"].orEmpty() - return sizeRequests.computeIfAbsent(item.key) { + return shared(sizeRequests, item.key) { CompletableFuture.supplyAsync({ val cached = cache(app, item.store).get(item.id, version) val bytes = cached ?: runCatching { fetch(app, item) } @@ -66,6 +66,25 @@ object StoreSizes { } } + internal fun shared( + requests: ConcurrentHashMap>, + key: String, + start: () -> CompletableFuture, + ): CompletableFuture { + var created: CompletableFuture? = null + val shared = requests.computeIfAbsent(key) { CompletableFuture().also { created = it } } + if (created === shared) { + val source = runCatching(start).getOrElse { failure -> + CompletableFuture().apply { completeExceptionally(failure) } + } + source.whenComplete { bytes, error -> + if (bytes == null) requests.remove(key, shared) + if (error != null) shared.completeExceptionally(error) else shared.complete(bytes) + } + } + return shared + } + val onDisk = mutableStateMapOf() fun requestDisk(context: Context, key: String, game: InstalledStoreGame) { @@ -78,7 +97,7 @@ object StoreSizes { onDisk[key]?.let { return CompletableFuture.completedFuture(it) } val app = context.applicationContext val version = "disk:${game.sidecar.installedAt}:${game.sidecar.installVersion}" - return diskRequests.computeIfAbsent(key) { + return shared(diskRequests, key) { CompletableFuture.supplyAsync({ val c = cache(app, game.sidecar.store) c.get("disk:${game.sidecar.id}", version) 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 9ac3a96027..bee82bbd2c 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt @@ -309,7 +309,7 @@ private fun GameStatusLine(source: String, game: Library.SteamGame?, item: Catal item?.accessible == true -> listOf(stringResource(R.string.stores_eyebrow_library)) else -> listOf(stringResource(R.string.stores_eyebrow_store)) } - val size = if (folder != null && (installed != null || game != null)) diskSize(installed, folder) else (item?.sizeBytes ?: 0L).takeIf { it > 0 } + val size = if (folder != null && (installed != null || game != null)) diskSize(installed, folder) else catalogSize(item).takeIf { it > 0 } val line = (state + listOfNotNull(size?.let { formatBytes(it) })).filter { it.isNotBlank() }.joinToString(" ยท ").replaceFirstChar { it.uppercase() } val muted = colors.onBackground.copy(alpha = 0.78f) Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(10.dp)) { @@ -399,6 +399,7 @@ internal fun GameActions( val download = rawDownload?.takeIf { it.isActive } val failed = rawDownload?.takeIf { it.state == DownloadState.FAILED } val cloud = game?.library == Library.XBOX_CLOUD && installed == null + val downloadBytes = catalogSize(item) Actions { when { s.shortcutPicker && game != null -> { @@ -474,7 +475,7 @@ internal fun GameActions( if (item?.installable == true && item.xboxPc) { SecondaryButton( if (StoresState.isUnfinished(item)) stringResource(R.string.stores_resume_install) - else if (item.sizeBytes > 0) stringResource(R.string.stores_install_size, formatBytes(item.sizeBytes)) + else if (downloadBytes > 0) stringResource(R.string.stores_install_size, formatBytes(downloadBytes)) else stringResource(R.string.stores_install), ) { StoresState.requestInstall(ctx, item) } } @@ -492,7 +493,7 @@ internal fun GameActions( } item?.installable == true -> { InstallButton( - if (item.sizeBytes > 0) stringResource(R.string.stores_install_size, formatBytes(item.sizeBytes)) else stringResource(R.string.stores_install), + if (downloadBytes > 0) stringResource(R.string.stores_install_size, formatBytes(downloadBytes)) else stringResource(R.string.stores_install), leadOn, ) { StoresState.requestInstall(ctx, item) } if (store == Store.XBOX && item.xboxCloud) { @@ -1016,6 +1017,13 @@ internal fun FactRow(label: String, value: String, labelWidth: androidx.compose. } } +@Composable +internal fun catalogSize(item: CatalogItem?): Long { + val ctx = LocalContext.current + if (item != null) LaunchedEffect(item.key, item.accessible) { StoreSizes.request(ctx, item) } + return item?.let { StoreSizes.size(it) } ?: 0L +} + @Composable internal fun diskSize(installed: InstalledStoreGame?, folder: File): Long? { val ctx = LocalContext.current diff --git a/app/src/test/java/com/droiddeck/launcher/stores/StoreSizesTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/StoreSizesTest.kt new file mode 100644 index 0000000000..2a306aeff8 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/StoreSizesTest.kt @@ -0,0 +1,96 @@ +package com.droiddeck.launcher.stores + +import java.util.concurrent.CompletableFuture +import java.util.concurrent.ConcurrentHashMap +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotSame +import org.junit.Assert.assertSame +import org.junit.Assert.assertTrue +import org.junit.Test + +class StoreSizesTest { + private val ea = CatalogItem(Store.EA, "Origin.OFR.50.0001", "Game", imageUrl = null, owned = true) + + @After fun clear() { + StoreSizes.known.clear() + } + + @Test fun anUnresolvedSizeIsUnknown() { + assertEquals(0L, StoreSizes.size(ea)) + } + + @Test fun aResolvedSizeReplacesTheCatalogValue() { + StoreSizes.known[ea.key] = 4_200_000_000L + assertEquals(4_200_000_000L, StoreSizes.size(ea)) + } + + @Test fun aResolvedSizeOnlyAppliesToItsOwnGame() { + StoreSizes.known[ea.key] = 4_200_000_000L + assertEquals(0L, StoreSizes.size(ea.copy(id = "Origin.OFR.50.0002"))) + assertEquals(7L, StoreSizes.size(ea.copy(id = "Origin.OFR.50.0002", sizeBytes = 7L))) + } + + @Test fun anEaSizeDoesNotReplaceAnotherStoresCatalogSize() { + StoreSizes.known[ea.key] = 4_200_000_000L + val epic = ea.copy(store = Store.EPIC, id = "Epic.Game", sizeBytes = 7L) + assertEquals(7L, StoreSizes.size(epic)) + } + + @Test fun aFailedLookupIsRetriedOnTheNextRequest() { + val requests = ConcurrentHashMap>() + val failed = StoreSizes.shared(requests, ea.key) { CompletableFuture.completedFuture(null) } + assertEquals(null, failed.get()) + val retried = StoreSizes.shared(requests, ea.key) { CompletableFuture.completedFuture(9L) } + assertNotSame(failed, retried) + assertEquals(9L, retried.get()) + } + + @Test fun aResolvedLookupIsSharedAndKept() { + val requests = ConcurrentHashMap>() + val first = StoreSizes.shared(requests, ea.key) { CompletableFuture.completedFuture(5L) } + val second = StoreSizes.shared(requests, ea.key) { CompletableFuture.completedFuture(6L) } + assertSame(first, second) + assertEquals(5L, second.get()) + } + + @Test fun anInFlightLookupIsSharedBetweenRequests() { + val requests = ConcurrentHashMap>() + val pending = CompletableFuture() + val first = StoreSizes.shared(requests, ea.key) { pending } + val second = StoreSizes.shared(requests, ea.key) { CompletableFuture.completedFuture(1L) } + assertSame(first, second) + pending.complete(null) + assertEquals(null, second.get()) + assertEquals(0, requests.size) + } + + @Test fun aContinuationThatRequestsAgainAfterAFailureStartsANewLookup() { + val requests = ConcurrentHashMap>() + val pending = CompletableFuture() + val first = StoreSizes.shared(requests, ea.key) { pending } + val retried = CompletableFuture() + first.thenAccept { bytes -> + if (bytes == null) retried.complete(StoreSizes.shared(requests, ea.key) { CompletableFuture.completedFuture(9L) }.get()) + } + pending.complete(null) + assertEquals(9L, retried.get()) + } + + @Test fun concurrentCallersShareOneLookup() { + val requests = ConcurrentHashMap>() + val pending = CompletableFuture() + var starts = 0 + val callers = (1..3).map { StoreSizes.shared(requests, ea.key) { starts++; pending } } + pending.complete(3L) + assertEquals(1, starts) + callers.forEach { assertEquals(3L, it.get()) } + } + + @Test fun synchronousStartFailureCompletesAndAllowsRetry() { + val requests = ConcurrentHashMap>() + val failed = StoreSizes.shared(requests, ea.key) { throw IllegalStateException("boom") } + assertTrue(failed.isCompletedExceptionally) + assertEquals(4L, StoreSizes.shared(requests, ea.key) { CompletableFuture.completedFuture(4L) }.get()) + } +}