mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
feat(stores): show the pre-install size in store game details (#2438)
* feat(stores): show the download size in store game details * test(stores): preserve other catalog sizes * fix(stores): restart the pre-install size lookup when the item becomes accessible * fix(stores): retry size lookups that returned no size * fix(stores): complete shared size lookups only after a failed one is cleared * fix(stores): complete synchronous size lookup failures without an API 31 call
This commit is contained in:
1 parent
a1dfb96708
commit
76f0c51235
3 files changed
+128
-5
No files matched your search
@@ -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<String, CompletableFuture<Long?>>,
|
||||
key: String,
|
||||
start: () -> CompletableFuture<Long?>,
|
||||
): CompletableFuture<Long?> {
|
||||
var created: CompletableFuture<Long?>? = null
|
||||
val shared = requests.computeIfAbsent(key) { CompletableFuture<Long?>().also { created = it } }
|
||||
if (created === shared) {
|
||||
val source = runCatching(start).getOrElse { failure ->
|
||||
CompletableFuture<Long?>().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<String, Long>()
|
||||
|
||||
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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<String, CompletableFuture<Long?>>()
|
||||
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<String, CompletableFuture<Long?>>()
|
||||
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<String, CompletableFuture<Long?>>()
|
||||
val pending = CompletableFuture<Long?>()
|
||||
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<String, CompletableFuture<Long?>>()
|
||||
val pending = CompletableFuture<Long?>()
|
||||
val first = StoreSizes.shared(requests, ea.key) { pending }
|
||||
val retried = CompletableFuture<Long?>()
|
||||
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<String, CompletableFuture<Long?>>()
|
||||
val pending = CompletableFuture<Long?>()
|
||||
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<String, CompletableFuture<Long?>>()
|
||||
val failed = StoreSizes.shared(requests, ea.key) { throw IllegalStateException("boom") }
|
||||
assertTrue(failed.isCompletedExceptionally)
|
||||
assertEquals(4L, StoreSizes.shared(requests, ea.key) { CompletableFuture.completedFuture(4L) }.get())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user