refactor: .conditional extension method for Modifier

Also slightly adjust cover's shape
This commit is contained in:
Ahmad Ansori Palembani 2024-12-26 13:19:54 +07:00
parent 26021b2ad0
commit 8391a25ef4
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
2 changed files with 20 additions and 14 deletions

View file

@ -19,6 +19,7 @@ import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import coil3.compose.AsyncImagePainter
import eu.kanade.tachiyomi.R
import yokai.util.conditional
import yokai.util.rememberResourceBitmapPainter
@Composable
@ -30,7 +31,7 @@ fun MangaCover(
minimumHeight: (() -> Int)? = null,
modifier: Modifier = Modifier,
contentDescription: String = "",
shape: Shape = MaterialTheme.shapes.extraSmall,
shape: Shape = MaterialTheme.shapes.small,
contentScale: () -> ContentScale = { ContentScale.Crop },
onClick: (() -> Unit)? = null,
onLoading: ((AsyncImagePainter.State.Loading) -> Unit)? = null,
@ -45,30 +46,24 @@ fun MangaCover(
maximumHeightDp = maximumHeight?.let { it().toDp() } ?: Dp.Unspecified
}
val ratioModifier = if (adjustViewBounds()) Modifier.aspectRatio(ratio()) else Modifier
AsyncImage(
model = data,
placeholder = ColorPainter(CoverPlaceholderColor),
error = rememberResourceBitmapPainter(id = R.drawable.ic_broken_image_24dp),
contentDescription = contentDescription,
modifier = modifier
.then(ratioModifier)
.conditional(adjustViewBounds()) { aspectRatio(ratio()) }
.clip(shape)
.heightIn(
min = minimumHeightDp,
max = maximumHeightDp,
)
.then(
if (onClick != null) {
Modifier.clickable(
.conditional(onClick != null) {
clickable(
role = Role.Button,
onClick = onClick,
onClick = onClick!!,
)
} else {
Modifier
},
),
contentScale = contentScale(),
onLoading = onLoading,
onError = onError,

View file

@ -0,0 +1,11 @@
package yokai.util
import androidx.compose.ui.Modifier
fun Modifier.conditional(condition: Boolean, modifier: Modifier.() -> Modifier): Modifier {
return if (condition) {
modifier(this)
} else {
this
}
}