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

[Android] sticky title on TimetableGrid #1262

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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 @@ -10,6 +10,7 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
Expand All @@ -23,16 +24,25 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableIntStateOf
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.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInParent
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
Expand Down Expand Up @@ -104,6 +114,21 @@ fun TimetableGridItem(
it.copy(fontSize = titleFontSize, lineHeight = titleLineHeight, color = textColor)
}

var displayedPosition by remember { mutableFloatStateOf(0f) }
var itemHeight by remember { mutableIntStateOf(0) }
var titleHeight by remember { mutableIntStateOf(0) }
var timeHeight by remember { mutableIntStateOf(0) }
var speakerHeight by remember { mutableIntStateOf(0) }
Comment on lines +117 to +121
Copy link
Contributor Author

@ked4ma ked4ma Sep 16, 2023

Choose a reason for hiding this comment

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

theere are possibility that height of column composable for title, time speaker have a variety of values according to session name.
And it was difficult to use spacer to get a room of item (due to weight and so on).
so here, getting each height and calculating maxOffset from these values.

val titleOffset by remember(localDensity) {
derivedStateOf {
// [available height] - [height already in use]
val maxOffset = with(localDensity) {
itemHeight - (titleHeight + timeHeight + speakerHeight + TimetableGridItemSizes.titleToSchedulePadding.roundToPx())
}.coerceAtLeast(0)
displayedPosition.unaryMinus().toInt().coerceIn(0, maxOffset)
}
}

Column(
modifier = modifier
.background(
Expand All @@ -119,14 +144,22 @@ fun TimetableGridItem(
.clickable {
onTimetableItemClick(timetableItem)
}
.padding(TimetableGridItemSizes.padding),
.padding(TimetableGridItemSizes.padding)
.onGloballyPositioned {
displayedPosition = it.positionInParent().y
itemHeight = it.size.height
},
) {
Column(
modifier = Modifier.weight(3f),
modifier = Modifier
.weight(3f)
.offset { IntOffset(0, titleOffset) },
verticalArrangement = Arrangement.Top,
) {
Text(
modifier = Modifier.weight(1f, fill = false),
modifier = Modifier
.weight(1f, fill = false)
.onGloballyPositioned { titleHeight = it.size.height },
text = timetableItem.title.currentLangTitle,
style = titleTextStyle,
overflow = TextOverflow.Ellipsis,
Expand All @@ -135,7 +168,8 @@ fun TimetableGridItem(
Row(
modifier = Modifier
.weight(1f, fill = false)
.padding(top = TimetableGridItemSizes.titleToSchedulePadding),
.padding(top = TimetableGridItemSizes.titleToSchedulePadding)
.onGloballyPositioned { timeHeight = it.size.height },
) {
Icon(
modifier = Modifier.height(TimetableGridItemSizes.scheduleHeight),
Expand Down Expand Up @@ -168,7 +202,10 @@ fun TimetableGridItem(
verticalAlignment = Alignment.CenterVertically,
) {
if (speakers.isNotEmpty()) {
val speakerModifier = Modifier.weight(1f)
val speakerModifier =
Modifier
.weight(1f)
.onGloballyPositioned { speakerHeight = it.size.height }
if (speakers.size == 1) {
var speakerTextStyle = MaterialTheme.typography.labelMedium
if (titleTextStyle.fontSize < speakerTextStyle.fontSize) {
Expand Down
Loading