Skip to content
Open
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 @@ -20,16 +20,13 @@ import androidx.compose.foundation.layout.width
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.BottomSheetDefaults
import androidx.compose.material3.BottomSheetScaffold
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.BlurredEdgeTreatment.Companion.Rectangle
Expand All @@ -38,11 +35,11 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.input.pointer.PointerEventPass
import androidx.compose.ui.input.pointer.PointerInputChange
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInRoot
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -64,7 +61,6 @@ import com.cornellappdev.resell.android.util.clickableNoIndication
import com.cornellappdev.resell.android.util.defaultHorizontalPadding
import com.cornellappdev.resell.android.viewmodel.pdp.PostDetailViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PostDetailPage(
postDetailViewModel: PostDetailViewModel = hiltViewModel(),
Expand Down Expand Up @@ -137,37 +133,67 @@ private fun Content(
onUserClick: () -> Unit = {},
showContact: Boolean = false,
) {
var sheetHeightFromBottom by remember { mutableStateOf(0.dp) }
val pagerState = rememberPagerState(pageCount = { images.size })

// Derive peekHeight as screen height minus image height:
val screenHeight = LocalConfiguration.current.screenHeightDp.dp

// TODO the plus at the end seems wrong. Test on other devices.
val peekHeight = screenHeight - imageHeight + 96.dp
val peekHeight = screenHeight - imageHeight + 180.dp

Box(
modifier = Modifier.fillMaxWidth()
) {
BottomSheetScaffold(
sheetContainerColor = Color.Transparent,
sheetShadowElevation = 0.dp,
sheetDragHandle = {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(24.dp)
) {
Box(
modifier = Modifier
.fillMaxWidth()
.gesturesDisabled()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not sure why you're completely disabling gestures here. This seems like it prevents me from bookmarking the item.

) {
WhichPage(
pagerState = pagerState,
modifier = Modifier
.align(Alignment.BottomCenter)
)

BookmarkFAB(
selected = bookmarked,
onClick = onBookmarkClick,
modifier = Modifier
.align(Alignment.BottomStart)
)
}
Row(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(topStart = 20.dp, topEnd = 20.dp))
.background(color = Color.White),
horizontalArrangement = Arrangement.Center,
) {
BottomSheetDefaults.DragHandle()
}
}
},
sheetContent = {
BottomSheetContent(
profilePictureUrl = userPfp,
username = username,
title = title,
price = price,
description = description,
onHeightChanged = {
sheetHeightFromBottom = it
},
onSimilarClick = onSimilarClick,
profilePictureUrl = userPfp,
username = username,
similarImageUrls = similarImageUrls,
onSimilarClick = onSimilarClick,
onUserClick = onUserClick
)
},
sheetPeekHeight = peekHeight,
sheetContainerColor = Color.White,
sheetShadowElevation = 12.dp,
containerColor = Color.White,
modifier = Modifier
.fillMaxSize()
Expand All @@ -177,7 +203,7 @@ private fun Content(
state = pagerState,
modifier = Modifier
.fillMaxSize()
.background(IconInactive),
.background(IconInactive)
) {
Column(modifier = Modifier.fillMaxHeight()) {
PdpImageBlurredBackground(
Expand Down Expand Up @@ -213,25 +239,21 @@ private fun Content(
state = contactButtonState
)
}

WhichPage(
pagerState = pagerState,
modifier = Modifier
.padding(bottom = sheetHeightFromBottom)
.align(Alignment.BottomCenter)
)

BookmarkFAB(
selected = bookmarked,
onClick = onBookmarkClick,
modifier = Modifier
.align(Alignment.BottomStart)
.defaultHorizontalPadding()
.padding(bottom = sheetHeightFromBottom)
)
}
}

fun Modifier.gesturesDisabled() =
pointerInput(Unit) {
awaitPointerEventScope {
while (true) {
awaitPointerEvent(pass = PointerEventPass.Initial)
.changes
.forEach(PointerInputChange::consume)
}
}
}


@Composable
private fun PdpImageBlurredBackground(
imageHeight: Dp,
Expand Down Expand Up @@ -303,14 +325,12 @@ private fun BottomSheetContent(
username: String,
paddingTop: Dp = 116.dp,
similarImageUrls: ResellApiResponse<List<String>>,
onHeightChanged: (Dp) -> Unit,
onSimilarClick: (Int) -> Unit,
onUserClick: () -> Unit,
) {

// Get screen height
val screenHeight = LocalConfiguration.current.screenHeightDp.dp
val density = LocalDensity.current

// Calculate maximum height for the sheet content based on padding from top
val maxSheetHeight = screenHeight - paddingTop
Expand All @@ -324,20 +344,7 @@ private fun BottomSheetContent(
Row(
modifier = Modifier
.fillMaxWidth()
.defaultHorizontalPadding()
.onGloballyPositioned { layoutCoordinates ->
val textPosition = layoutCoordinates.positionInRoot().y
val textHeight = layoutCoordinates.size.height

val screenHeightPx = with(density) { screenHeight.toPx() }

// Calculate distance from bottom in px and convert to dp
val distanceFromBottomPx = screenHeightPx - (textPosition + textHeight)
val textDistanceFromBottom = with(density) { distanceFromBottomPx.toDp() }

// Tell the parent that the height has changed.
onHeightChanged(textDistanceFromBottom + 170.dp)
},
.defaultHorizontalPadding(),
verticalAlignment = Alignment.CenterVertically
) {
Text(
Expand Down