Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set fallback coin image for widget #7536

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -144,7 +145,7 @@ class MarketWidget : GlanceAppWidget() {
actionStartActivity(Intent(Intent.ACTION_VIEW, deeplinkUri))
)
) {
Item(item = item)
Item(item = item, state.type)
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -78,7 +77,8 @@ class MarketWidgetManager {
item.copy(
imageLocalPath = item.imageLocalPath ?: getImage(
context,
item.imageRemoteUrl
item.imageRemoteUrl,
item.alternativeRemoteUrl
)
)
}
Expand All @@ -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
}

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 )"
Expand Down
Loading