Skip to content

Added a shimmer effect for the wallpaper grid #114

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

Merged
merged 1 commit into from
Aug 15, 2023
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
30 changes: 30 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/ui/components/ShimmerGrid.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.bnyro.wallpaper.ui.components

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import com.bnyro.wallpaper.util.shimmer

@Composable
fun ShimmerGrid() {
LazyVerticalGrid(
columns = GridCells.Fixed(2)
) {
items(10) {
Box(
modifier = Modifier
.height(300.dp)
.padding(5.dp)
.clip(RoundedCornerShape(10.dp))
.shimmer(600f)
)
}
}
}
27 changes: 12 additions & 15 deletions app/src/main/java/com/bnyro/wallpaper/ui/pages/WallpaperPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.FilterList
import androidx.compose.material.icons.filled.Shuffle
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
Expand All @@ -24,6 +23,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.ui.components.ShimmerGrid
import com.bnyro.wallpaper.ui.components.WallpaperGrid
import com.bnyro.wallpaper.ui.components.WallpaperPreview
import com.bnyro.wallpaper.ui.components.dialogs.FilterDialog
Expand Down Expand Up @@ -64,10 +64,7 @@ fun WallpaperPage(
}
}
} else {
CircularProgressIndicator(
modifier = Modifier
.align(Alignment.Center)
)
ShimmerGrid()
}

Row(
Expand All @@ -79,19 +76,19 @@ fun WallpaperPage(
FloatingActionButton(
modifier = Modifier
.padding(horizontal = 10.dp),
/*.combinedClickable(
onLongClick = {
Toast.makeText(context, R.string.applying_random, Toast.LENGTH_SHORT).show()
CoroutineScope(Dispatchers.IO).launch {
val wallpaperUrl = viewModel.api.getRandomWallpaperUrl()
ImageHelper.urlToBitmap(this, wallpaperUrl, context) {
WallpaperHelper.setWallpaper(context, it, WallpaperMode.BOTH)
}
/*.combinedClickable(
onLongClick = {
Toast.makeText(context, R.string.applying_random, Toast.LENGTH_SHORT).show()
CoroutineScope(Dispatchers.IO).launch {
val wallpaperUrl = viewModel.api.getRandomWallpaperUrl()
ImageHelper.urlToBitmap(this, wallpaperUrl, context) {
WallpaperHelper.setWallpaper(context, it, WallpaperMode.BOTH)
}
}
),
}
),

*/
*/
onClick = {
selectedWallpaper = viewModel.wallpapers.randomOrNull()
}
Expand Down
59 changes: 59 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/util/ShimmerEffect.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.bnyro.wallpaper.util

import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

/**
* @param length Diagonal length of the element
*/
fun Modifier.shimmer(length: Float): Modifier = composed {
val transition = rememberInfiniteTransition(label = "shimmer transition")
val offset by transition.animateFloat(
initialValue = -100f,
targetValue = length + 100f,
animationSpec = infiniteRepeatable(
animation = tween(1500),
repeatMode = RepeatMode.Restart
),
label = "gradient offset"
)
val color = if (isSystemInDarkTheme()) Color.DarkGray else Color.LightGray
val startOffset = -length / 4 + offset
val endOffset = length / 4 + offset
val brush = Brush.linearGradient(
colors = listOf(
color.copy(alpha = 0.2f),
color.copy(alpha = 0.6f),
color.copy(alpha = 0.2f)
),
start = Offset(x = startOffset, y = startOffset),
end = Offset(x = endOffset, y = endOffset)
)
this.then(background(brush))
}

@Preview
@Composable
private fun ShimmerPreview() {
Box(
modifier = Modifier
.size(300.dp)
.shimmer(400f)
)
}