Skip to content
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

Implement CDN support for images in ArticleDetails & prevent article resizing #331

Merged
merged 1 commit into from
Feb 21, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.DpSize
import net.primal.android.attachments.domain.CdnResourceVariant
import net.primal.android.attachments.domain.calculateImageSize
import net.primal.android.attachments.domain.findNearestOrNull
import net.primal.android.core.compose.attachment.model.NoteAttachmentUi
Expand All @@ -24,3 +25,14 @@ fun BoxWithConstraintsScope.findImageSize(attachment: NoteAttachmentUi): DpSize
density = density,
)
}

@Composable
fun CdnResourceVariant?.findImageSize(maxWidth: Int): DpSize {
val density = LocalDensity.current.density
val maxHeight = (LocalConfiguration.current.screenHeightDp * MAX_SCREEN_HEIGHT_VISIBLE_AREA).toInt()
return calculateImageSize(
maxWidth = maxWidth,
maxHeight = maxHeight,
density = density,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -28,7 +29,9 @@ import java.util.*
import net.primal.android.attachments.domain.CdnImage
import net.primal.android.attachments.domain.findNearestOrNull
import net.primal.android.core.compose.preview.PrimalPreview
import net.primal.android.notes.feed.note.ui.attachment.findImageSize
import net.primal.android.theme.AppTheme
import net.primal.android.theme.domain.PrimalTheme
import net.primal.android.thread.articles.details.ui.rendering.MarkdownRenderer
import net.primal.android.thread.articles.details.ui.rendering.rememberPrimalMarkwon

Expand Down Expand Up @@ -73,9 +76,10 @@ fun ArticleDetailsHeader(
) {
val maxWidthPx = with(LocalDensity.current) { maxWidth.roundToPx() }
val variant = cover.variants.findNearestOrNull(maxWidthPx = maxWidthPx)
val maxWidth = maxWidth.value.toInt()

SubcomposeAsyncImage(
modifier = Modifier.fillMaxWidth(),
modifier = Modifier.size(variant.findImageSize(maxWidth)),
model = variant?.mediaUrl ?: cover.sourceUrl,
contentScale = ContentScale.FillWidth,
contentDescription = null,
Expand Down Expand Up @@ -132,7 +136,7 @@ private fun Instant.formatDate(): String {
@Preview
@Composable
fun PreviewArticleDetailsHeader() {
PrimalPreview(primalTheme = net.primal.android.theme.domain.PrimalTheme.Sunset) {
PrimalPreview(primalTheme = PrimalTheme.Sunset) {
Surface(modifier = Modifier.fillMaxWidth()) {
ArticleDetailsHeader(
modifier = Modifier.fillMaxWidth(),
Expand Down