From 7aaac8b4c9d8d4ccd22a90039b7562864a4c3e09 Mon Sep 17 00:00:00 2001 From: chyngyz Date: Fri, 7 Jun 2024 17:00:11 +0600 Subject: [PATCH] Set fallback coin image for widget --- .../bankwallet/widgets/MarketWidget.kt | 16 +++++++--- .../bankwallet/widgets/MarketWidgetManager.kt | 30 +++++++++++++------ .../widgets/MarketWidgetRepository.kt | 4 ++- .../bankwallet/widgets/MarketWidgetState.kt | 3 +- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidget.kt b/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidget.kt index f9dcdcc882..a1f67d260c 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidget.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidget.kt @@ -21,6 +21,7 @@ import androidx.glance.appwidget.SizeMode import androidx.glance.appwidget.action.ActionCallback import androidx.glance.appwidget.action.actionRunCallback import androidx.glance.appwidget.action.actionStartActivity +import androidx.glance.appwidget.cornerRadius import androidx.glance.appwidget.lazy.LazyColumn import androidx.glance.appwidget.lazy.items import androidx.glance.appwidget.provideContent @@ -144,7 +145,7 @@ class MarketWidget : GlanceAppWidget() { actionStartActivity(Intent(Intent.ACTION_VIEW, deeplinkUri)) ) ) { - Item(item = item) + Item(item = item, state.type) } } } @@ -178,18 +179,25 @@ class MarketWidget : GlanceAppWidget() { } @Composable - private fun Item(item: MarketWidgetItem) { + private fun Item(item: MarketWidgetItem, type: MarketWidgetType) { Row( modifier = GlanceModifier .fillMaxHeight() .padding(horizontal = 16.dp), verticalAlignment = CenterVertically ) { + val modifier = when(type) { + MarketWidgetType.Watchlist, + MarketWidgetType.TopGainers -> GlanceModifier.size(32.dp).cornerRadius(16.dp) + MarketWidgetType.TopNfts, + MarketWidgetType.TopPlatforms -> GlanceModifier.size(32.dp) + } + Image( provider = imageProvider(item.imageLocalPath), contentDescription = null, - contentScale = ContentScale.FillBounds, - modifier = GlanceModifier.size(24.dp) + contentScale= ContentScale.FillBounds, + modifier = modifier ) Spacer(modifier = GlanceModifier.width(16.dp)) Column { diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidgetManager.kt b/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidgetManager.kt index dcfdfc9b97..6d91b323d5 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidgetManager.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidgetManager.kt @@ -68,8 +68,7 @@ class MarketWidgetManager { } } var marketItems = marketRepository.getMarketItems(state.type) - marketItems = - marketItems.map { it.copy(imageLocalPath = imagePathCache[it.imageRemoteUrl]) } + marketItems = marketItems.map { it.copy(imageLocalPath = imagePathCache[it.imageRemoteUrl]) } state = state.copy(items = marketItems, loading = false, error = null) setWidgetState(context, glanceId, state) @@ -78,7 +77,8 @@ class MarketWidgetManager { item.copy( imageLocalPath = item.imageLocalPath ?: getImage( context, - item.imageRemoteUrl + item.imageRemoteUrl, + item.alternativeRemoteUrl ) ) } @@ -92,19 +92,31 @@ class MarketWidgetManager { } @OptIn(ExperimentalCoilApi::class) - private suspend fun getImage(context: Context, url: String): String? { + private suspend fun getImage( + context: Context, + url: String, + alternativeUrl: String? + ): String? { + var downloadedUrl = url val request = ImageRequest.Builder(context) .data(url) .build() with(context.imageLoader) { - val result = execute(request) - if (result is ErrorResult) { - return null + var result = execute(request) + if (result is ErrorResult && alternativeUrl != null) { + val fallbackRequest = ImageRequest.Builder(context) + .data(alternativeUrl) + .build() + result = execute(fallbackRequest) + if (result is ErrorResult) { + return null + } + downloadedUrl = alternativeUrl } } - val localPath = context.imageLoader.diskCache?.openSnapshot(url)?.use { snapshot -> + val localPath = context.imageLoader.diskCache?.openSnapshot(downloadedUrl)?.use { snapshot -> snapshot.data.toFile().path } @@ -120,7 +132,7 @@ class MarketWidgetManager { private val MAX_RETRIES = 5 - private suspend inline fun executeWithRetry(call: () -> Unit){ + private suspend inline fun executeWithRetry(call: () -> Unit) { for (i in 0..MAX_RETRIES) { try { call.invoke() diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidgetRepository.kt b/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidgetRepository.kt index 3fcd52c099..1363b95302 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidgetRepository.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidgetRepository.kt @@ -2,6 +2,7 @@ package io.horizontalsystems.bankwallet.widgets import io.horizontalsystems.bankwallet.R import io.horizontalsystems.bankwallet.core.App +import io.horizontalsystems.bankwallet.core.alternativeImageUrl import io.horizontalsystems.bankwallet.core.iconUrl import io.horizontalsystems.bankwallet.core.imageUrl import io.horizontalsystems.bankwallet.core.managers.CurrencyManager @@ -176,7 +177,8 @@ class MarketWidgetRepository( ), diff = marketItem.diff, blockchainTypeUid = null, - imageRemoteUrl = marketItem.fullCoin.coin.imageUrl + imageRemoteUrl = marketItem.fullCoin.coin.imageUrl, + alternativeRemoteUrl = marketItem.fullCoin.coin.alternativeImageUrl ) } diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidgetState.kt b/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidgetState.kt index bbde86387c..8cafe97a74 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidgetState.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/widgets/MarketWidgetState.kt @@ -47,7 +47,8 @@ data class MarketWidgetItem( val blockchainTypeUid: String?, val imageRemoteUrl: String, - val imageLocalPath: String? = null + val imageLocalPath: String? = null, + val alternativeRemoteUrl: String? = null ) { override fun toString(): String { return "( title: $title, subtitle: $subtitle, label: $label, value: $value, diff: $diff, imageRemoteUrl: $imageRemoteUrl, imageLocalPath: $imageLocalPath )"