-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from NielsMasdorp/feature/images
Refactor the way images are shown
- Loading branch information
Showing
8 changed files
with
111 additions
and
67 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
63 changes: 63 additions & 0 deletions
63
app/src/main/java/com/nielsmasdorp/nederadio/ui/components/StreamImage.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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.nielsmasdorp.nederadio.ui.components | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.aspectRatio | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material3.Card | ||
import androidx.compose.runtime.* | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.Shape | ||
import androidx.compose.ui.graphics.toArgb | ||
import androidx.compose.ui.layout.ContentScale | ||
import com.nielsmasdorp.nederadio.domain.stream.Stream | ||
import com.nielsmasdorp.nederadio.ui.extension.edgeColor | ||
import com.skydoves.landscapist.glide.GlideImage | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
const val AspectRatio = 1.31f | ||
|
||
@Composable | ||
fun StreamImage( | ||
stream: Stream, | ||
shape: Shape, | ||
scope: CoroutineScope, | ||
modifier: Modifier = Modifier, | ||
onSelectStream: ((String) -> Unit)? = null | ||
) { | ||
val clickableModifier = if (onSelectStream != null) { | ||
modifier | ||
.aspectRatio(ratio = AspectRatio) | ||
.clip(shape) | ||
.clickable { onSelectStream.invoke(stream.id) } | ||
} else { | ||
modifier | ||
.aspectRatio(ratio = AspectRatio) | ||
.clip(shape) | ||
} | ||
Card(modifier = clickableModifier) { | ||
var edgeColor by remember { mutableStateOf(Color.White.toArgb()) } | ||
GlideImage( | ||
imageModel = { stream.imageUrl }, | ||
success = { success, painter -> | ||
LaunchedEffect(Unit) { | ||
scope.launch { | ||
success.imageBitmap?.let { edgeColor = it.edgeColor() } | ||
} | ||
} | ||
Image( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.background(color = Color(color = edgeColor)), | ||
painter = painter, | ||
contentDescription = null, | ||
contentScale = ContentScale.Fit | ||
) | ||
} | ||
) | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/nielsmasdorp/nederadio/ui/extension/BitmapExtensions.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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.nielsmasdorp.nederadio.ui.extension | ||
|
||
import androidx.compose.ui.graphics.ImageBitmap | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
@Suppress("MagicNumber") | ||
suspend fun ImageBitmap.edgeColor(): Int = withContext(Dispatchers.IO) { | ||
val pixels = IntArray(width * height) | ||
readPixels(pixels) | ||
pixels.last() | ||
} |
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
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