extracted common logic
This commit is contained in:
@@ -52,6 +52,35 @@ private const val ASPECT_RATIO = 4f / 3f
|
|||||||
private const val PORTRAIT_ASPECT_RATIO = 3f / 4f
|
private const val PORTRAIT_ASPECT_RATIO = 3f / 4f
|
||||||
private val IMAGE_SPACING: Dp = Size5dp
|
private val IMAGE_SPACING: Dp = Size5dp
|
||||||
|
|
||||||
|
private data class FirstImageOrientation(
|
||||||
|
val aspectRatio: Float,
|
||||||
|
val isLandscape: Boolean,
|
||||||
|
)
|
||||||
|
|
||||||
|
private fun MediaUrlImage.resolvedAspectRatio(): Float? =
|
||||||
|
dim
|
||||||
|
?.takeIf { it.hasSize() }
|
||||||
|
?.aspectRatio()
|
||||||
|
?: MediaAspectRatioCache.get(url)
|
||||||
|
|
||||||
|
private fun MediaUrlImage?.resolveOrientation(
|
||||||
|
landscapeDefaultAspectRatio: Float,
|
||||||
|
portraitDefaultAspectRatio: Float,
|
||||||
|
defaultToLandscape: Boolean = false,
|
||||||
|
): FirstImageOrientation {
|
||||||
|
val ratio = this?.resolvedAspectRatio()
|
||||||
|
val isLandscape = ratio?.let { it >= 1f } ?: defaultToLandscape
|
||||||
|
val resolvedAspectRatio =
|
||||||
|
ratio
|
||||||
|
?.takeIf { it > 0f }
|
||||||
|
?: if (isLandscape) landscapeDefaultAspectRatio else portraitDefaultAspectRatio
|
||||||
|
|
||||||
|
return FirstImageOrientation(
|
||||||
|
aspectRatio = resolvedAspectRatio,
|
||||||
|
isLandscape = isLandscape,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
private fun GalleryImage(
|
private fun GalleryImage(
|
||||||
image: MediaUrlImage,
|
image: MediaUrlImage,
|
||||||
@@ -122,21 +151,9 @@ private fun TwoImageGallery(
|
|||||||
accountViewModel: AccountViewModel,
|
accountViewModel: AccountViewModel,
|
||||||
roundedCorner: Boolean,
|
roundedCorner: Boolean,
|
||||||
) {
|
) {
|
||||||
val firstImage = images.firstOrNull()
|
val orientation = images.firstOrNull().resolveOrientation(ASPECT_RATIO, PORTRAIT_ASPECT_RATIO)
|
||||||
val firstImageAspectRatio =
|
|
||||||
firstImage
|
|
||||||
?.dim
|
|
||||||
?.takeIf { it.hasSize() }
|
|
||||||
?.aspectRatio()
|
|
||||||
?: firstImage?.url?.let { MediaAspectRatioCache.get(it) }
|
|
||||||
|
|
||||||
val isLandscape = firstImageAspectRatio?.let { it >= 1f } ?: false
|
if (orientation.isLandscape) {
|
||||||
val itemAspectRatio =
|
|
||||||
firstImageAspectRatio
|
|
||||||
?.takeIf { it > 0f }
|
|
||||||
?: if (isLandscape) ASPECT_RATIO else PORTRAIT_ASPECT_RATIO
|
|
||||||
|
|
||||||
if (isLandscape) {
|
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
verticalArrangement = Arrangement.spacedBy(IMAGE_SPACING),
|
verticalArrangement = Arrangement.spacedBy(IMAGE_SPACING),
|
||||||
@@ -145,7 +162,7 @@ private fun TwoImageGallery(
|
|||||||
GalleryImage(
|
GalleryImage(
|
||||||
image = image,
|
image = image,
|
||||||
allImages = images,
|
allImages = images,
|
||||||
modifier = Modifier.fillMaxWidth().aspectRatio(itemAspectRatio),
|
modifier = Modifier.fillMaxWidth().aspectRatio(orientation.aspectRatio),
|
||||||
roundedCorner = roundedCorner,
|
roundedCorner = roundedCorner,
|
||||||
contentScale = ContentScale.Crop,
|
contentScale = ContentScale.Crop,
|
||||||
accountViewModel = accountViewModel,
|
accountViewModel = accountViewModel,
|
||||||
@@ -161,7 +178,7 @@ private fun TwoImageGallery(
|
|||||||
GalleryImage(
|
GalleryImage(
|
||||||
image = image,
|
image = image,
|
||||||
allImages = images,
|
allImages = images,
|
||||||
modifier = Modifier.weight(1f, fill = true).aspectRatio(itemAspectRatio),
|
modifier = Modifier.weight(1f, fill = true).aspectRatio(orientation.aspectRatio),
|
||||||
roundedCorner = roundedCorner,
|
roundedCorner = roundedCorner,
|
||||||
contentScale = ContentScale.Crop,
|
contentScale = ContentScale.Crop,
|
||||||
accountViewModel = accountViewModel,
|
accountViewModel = accountViewModel,
|
||||||
@@ -178,21 +195,10 @@ private fun ThreeImageGallery(
|
|||||||
roundedCorner: Boolean,
|
roundedCorner: Boolean,
|
||||||
) {
|
) {
|
||||||
val firstImage = images.first()
|
val firstImage = images.first()
|
||||||
val firstImageAspectRatio =
|
val orientation = firstImage.resolveOrientation(ASPECT_RATIO, PORTRAIT_ASPECT_RATIO)
|
||||||
firstImage
|
|
||||||
.dim
|
|
||||||
?.takeIf { it.hasSize() }
|
|
||||||
?.aspectRatio()
|
|
||||||
?: MediaAspectRatioCache.get(firstImage.url)
|
|
||||||
|
|
||||||
val remainingImages = images.drop(1)
|
val remainingImages = images.drop(1)
|
||||||
val isLandscape = firstImageAspectRatio?.let { it >= 1f } ?: false
|
|
||||||
val firstItemAspectRatio =
|
|
||||||
firstImageAspectRatio
|
|
||||||
?.takeIf { it > 0f }
|
|
||||||
?: if (isLandscape) ASPECT_RATIO else PORTRAIT_ASPECT_RATIO
|
|
||||||
|
|
||||||
if (isLandscape) {
|
if (orientation.isLandscape) {
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
verticalArrangement = Arrangement.spacedBy(IMAGE_SPACING),
|
verticalArrangement = Arrangement.spacedBy(IMAGE_SPACING),
|
||||||
@@ -200,7 +206,7 @@ private fun ThreeImageGallery(
|
|||||||
GalleryImage(
|
GalleryImage(
|
||||||
image = firstImage,
|
image = firstImage,
|
||||||
allImages = images,
|
allImages = images,
|
||||||
modifier = Modifier.fillMaxWidth().aspectRatio(firstItemAspectRatio),
|
modifier = Modifier.fillMaxWidth().aspectRatio(orientation.aspectRatio),
|
||||||
roundedCorner = roundedCorner,
|
roundedCorner = roundedCorner,
|
||||||
contentScale = ContentScale.Crop,
|
contentScale = ContentScale.Crop,
|
||||||
accountViewModel = accountViewModel,
|
accountViewModel = accountViewModel,
|
||||||
|
|||||||
Reference in New Issue
Block a user