-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update images in Jetsnack example: host them locally and use Resource…
…s library (#5131) We used to rely on an external service to fetch the images. But they are not available anymore
- Loading branch information
Showing
45 changed files
with
131 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 60 additions & 16 deletions
76
...snack/common/src/androidMain/kotlin/com/example/jetsnack/ui/components/SnackAsyncImage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,69 @@ | ||
package com.example.jetsnack.ui.components | ||
|
||
import androidx.compose.foundation.layout.fillMaxSize | ||
import android.annotation.SuppressLint | ||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import androidx.compose.animation.AnimatedContent | ||
import androidx.compose.animation.ExperimentalAnimationApi | ||
import androidx.compose.animation.core.TweenSpec | ||
import androidx.compose.animation.fadeIn | ||
import androidx.compose.animation.fadeOut | ||
import androidx.compose.animation.with | ||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.asImageBitmap | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.painterResource | ||
import coil.compose.AsyncImage | ||
import coil.request.ImageRequest | ||
import com.example.jetsnack.R | ||
import com.example.common.generated.resources.Res | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.jetbrains.compose.resources.ExperimentalResourceApi | ||
|
||
private val imagesCache = mutableMapOf<String, ImageBitmap>() | ||
|
||
@SuppressLint("UnusedContentLambdaTargetStateParameter") | ||
@OptIn(ExperimentalAnimationApi::class, ExperimentalResourceApi::class) | ||
@Composable | ||
actual fun SnackAsyncImage(imageUrl: String, contentDescription: String?, modifier: Modifier) { | ||
AsyncImage( | ||
model = ImageRequest.Builder(LocalContext.current) | ||
.data(imageUrl) | ||
.crossfade(true) | ||
.build(), | ||
contentDescription = contentDescription, | ||
placeholder = painterResource(R.drawable.placeholder), | ||
modifier = Modifier.fillMaxSize(), | ||
contentScale = ContentScale.Crop, | ||
) | ||
var img: ImageBitmap? by remember(imageUrl) { mutableStateOf(null) } | ||
|
||
|
||
AnimatedContent(img, transitionSpec = { | ||
fadeIn(TweenSpec()) with fadeOut(TweenSpec()) | ||
}) { | ||
if (img != null) { | ||
Image(img!!, contentDescription = contentDescription, modifier = modifier, contentScale = ContentScale.Crop) | ||
} else { | ||
Box(modifier = modifier) | ||
} | ||
} | ||
|
||
LaunchedEffect(imageUrl) { | ||
if (imagesCache.contains(imageUrl)) { | ||
img = imagesCache[imageUrl] | ||
} else { | ||
withContext(Dispatchers.IO) { | ||
img = try { | ||
Res.readBytes(imageUrl).toAndroidBitmap().asImageBitmap().also { | ||
imagesCache[imageUrl] = it | ||
img = it | ||
} | ||
} catch (e: Throwable) { | ||
e.printStackTrace() | ||
null | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun ByteArray.toAndroidBitmap(): Bitmap { | ||
return BitmapFactory.decodeByteArray(this, 0, size) | ||
} |
Binary file added
BIN
+332 KB
examples/jetsnack/common/src/commonMain/composeResources/files/almonds.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+228 KB
examples/jetsnack/common/src/commonMain/composeResources/files/apple_chips.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+86.4 KB
examples/jetsnack/common/src/commonMain/composeResources/files/apple_juice.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+44.7 KB
examples/jetsnack/common/src/commonMain/composeResources/files/apple_pie.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+92.7 KB
examples/jetsnack/common/src/commonMain/composeResources/files/apple_sauce.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+67.1 KB
examples/jetsnack/common/src/commonMain/composeResources/files/apples.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+199 KB
examples/jetsnack/common/src/commonMain/composeResources/files/cheese.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+248 KB
examples/jetsnack/common/src/commonMain/composeResources/files/chips.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+42.8 KB
examples/jetsnack/common/src/commonMain/composeResources/files/cupcake.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+143 KB
examples/jetsnack/common/src/commonMain/composeResources/files/desserts.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+159 KB
examples/jetsnack/common/src/commonMain/composeResources/files/donut.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+201 KB
examples/jetsnack/common/src/commonMain/composeResources/files/eclair.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+111 KB
examples/jetsnack/common/src/commonMain/composeResources/files/froyo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+382 KB
examples/jetsnack/common/src/commonMain/composeResources/files/fruit.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+653 KB
examples/jetsnack/common/src/commonMain/composeResources/files/gingerbread.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+496 KB
examples/jetsnack/common/src/commonMain/composeResources/files/gluten_free.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+212 KB
examples/jetsnack/common/src/commonMain/composeResources/files/grapes.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+380 KB
examples/jetsnack/common/src/commonMain/composeResources/files/honeycomb.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+75.8 KB
...es/jetsnack/common/src/commonMain/composeResources/files/ice_cream_sandwich.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+197 KB
examples/jetsnack/common/src/commonMain/composeResources/files/jelly_bean.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+198 KB
examples/jetsnack/common/src/commonMain/composeResources/files/kitkat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+250 KB
examples/jetsnack/common/src/commonMain/composeResources/files/kiwi.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+210 KB
examples/jetsnack/common/src/commonMain/composeResources/files/lollipop.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+112 KB
examples/jetsnack/common/src/commonMain/composeResources/files/mango.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+137 KB
examples/jetsnack/common/src/commonMain/composeResources/files/marshmallow.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+182 KB
examples/jetsnack/common/src/commonMain/composeResources/files/nougat.jpg
Oops, something went wrong.
Binary file added
BIN
+300 KB
examples/jetsnack/common/src/commonMain/composeResources/files/nuts.jpg
Oops, something went wrong.
Binary file added
BIN
+129 KB
examples/jetsnack/common/src/commonMain/composeResources/files/oreo.jpg
Oops, something went wrong.
Binary file added
BIN
+217 KB
examples/jetsnack/common/src/commonMain/composeResources/files/organic.jpg
Oops, something went wrong.
Binary file added
BIN
+218 KB
examples/jetsnack/common/src/commonMain/composeResources/files/paleo.jpg
Oops, something went wrong.
Binary file added
BIN
+135 KB
examples/jetsnack/common/src/commonMain/composeResources/files/pie.jpg
Oops, something went wrong.
Binary file added
BIN
+6.97 KB
examples/jetsnack/common/src/commonMain/composeResources/files/placeholder.jpg
Oops, something went wrong.
Binary file added
BIN
+139 KB
examples/jetsnack/common/src/commonMain/composeResources/files/popcorn.jpg
Oops, something went wrong.
Binary file added
BIN
+152 KB
examples/jetsnack/common/src/commonMain/composeResources/files/pretzels.jpg
Oops, something went wrong.
Binary file added
BIN
+150 KB
examples/jetsnack/common/src/commonMain/composeResources/files/smoothies.jpg
Oops, something went wrong.
Binary file added
BIN
+187 KB
examples/jetsnack/common/src/commonMain/composeResources/files/vegan.jpg
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.