Skip to content

Commit

Permalink
fixes mozilla-mobile#23644: reload wallpapers on orientation change
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewTighe authored and Matt Tighe committed Feb 11, 2022
1 parent 50dfd0e commit 6363461
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.Font
Expand Down Expand Up @@ -204,6 +210,10 @@ private fun WallpaperThumbnailItem(
aspectRatio: Float = 1.1f,
onSelect: (Wallpaper) -> Unit
) {
var bitmap by remember { mutableStateOf(loadWallpaperResource(wallpaper)) }
DisposableEffect(LocalConfiguration.current.orientation) {
onDispose { bitmap = loadWallpaperResource(wallpaper) }
}
val thumbnailShape = RoundedCornerShape(8.dp)
val border = if (isSelected) {
Modifier.border(
Expand All @@ -214,9 +224,9 @@ private fun WallpaperThumbnailItem(
Modifier
}

val bitmap = loadWallpaperResource(wallpaper)
// Completely avoid drawing the item if a bitmap cannot be loaded and is required
if (bitmap == null && wallpaper != defaultWallpaper) return

Surface(
elevation = 4.dp,
shape = thumbnailShape,
Expand All @@ -228,9 +238,9 @@ private fun WallpaperThumbnailItem(
.then(border)
.clickable { onSelect(wallpaper) }
) {
if (bitmap != null) {
bitmap?.let {
Image(
bitmap = bitmap.asImageBitmap(),
bitmap = it.asImageBitmap(),
contentScale = ContentScale.FillBounds,
contentDescription = stringResource(
R.string.wallpapers_item_name_content_description, wallpaper.name
Expand Down

0 comments on commit 6363461

Please sign in to comment.