Skip to content

Commit

Permalink
rename and add function
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroaki404 committed Sep 5, 2024
1 parent 0a4ac95 commit bd458ed
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ import io.github.droidkaigi.confsched.sessions.component.TimetableGridItem
import io.github.droidkaigi.confsched.sessions.component.TimetableGridRooms
import io.github.droidkaigi.confsched.sessions.section.ScreenScrollState.Companion
import io.github.droidkaigi.confsched.sessions.timetableDetailSharedContentStateKey
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.datetime.DateTimeUnit
Expand Down Expand Up @@ -116,6 +118,7 @@ fun TimetableGrid(
onTimetableItemClick: (TimetableItem) -> Unit,
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
scrolledToCurrentTimeState: ScrolledToCurrentTimeState = ScrolledToCurrentTimeState(),
) {
TimetableGrid(
timetable = uiState.timetable,
Expand All @@ -125,6 +128,7 @@ fun TimetableGrid(
onTimetableItemClick = onTimetableItemClick,
modifier = modifier,
contentPadding = contentPadding,
scrolledToCurrentTimeState = scrolledToCurrentTimeState,
)
}

Expand All @@ -138,6 +142,7 @@ fun TimetableGrid(
onTimetableItemClick: (TimetableItem) -> Unit,
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
scrolledToCurrentTimeState: ScrolledToCurrentTimeState = ScrolledToCurrentTimeState(),
) {
val coroutineScope = rememberCoroutineScope()
val layoutDirection = LocalLayoutDirection.current
Expand Down Expand Up @@ -183,6 +188,7 @@ fun TimetableGrid(
start = 16.dp + contentPadding.calculateStartPadding(layoutDirection),
end = 16.dp + contentPadding.calculateEndPadding(layoutDirection),
),
scrolledToCurrentTimeState = scrolledToCurrentTimeState,
) { timetableItem, itemHeightPx ->
val timetableGridItemModifier = if (sharedTransitionScope != null && animatedScope != null) {
with(sharedTransitionScope) {
Expand Down Expand Up @@ -219,6 +225,7 @@ fun TimetableGrid(
selectedDay: DroidKaigi2024Day,
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
scrolledToCurrentTimeState: ScrolledToCurrentTimeState = ScrolledToCurrentTimeState(),
content: @Composable (TimetableItem, Int) -> Unit,
) {
val coroutineScope = rememberCoroutineScope()
Expand Down Expand Up @@ -255,42 +262,33 @@ fun TimetableGrid(
val currentTimeDotRadius = with(timetableState.density) { TimetableSizes.currentTimeDotRadius.toPx() }

LaunchedEffect(Unit) {
val progressingSession = timetable.timetableItems.timetableItems
.run {
// Insert dummy at a position after the start of the last session to allow scrolling
val endOfTheDayInstant = first().startsAt.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.plus(1, DateTimeUnit.DAY)
.atStartOfDayIn(TimeZone.currentSystemDefault())
plus(
Session.Companion.fake().copy(
startsAt = endOfTheDayInstant,
endsAt = endOfTheDayInstant,
),
if (scrolledToCurrentTimeState.inTimetableGrid.not()) {
val progressingSession = timetable.timetableItems.timetableItems
.insertDummyEndOfTheDayItem() // Insert dummy at a position after last session to allow scrolling
.windowed(2, 1, true)
.find { clock.now() in it.first().startsAt..it.last().startsAt }
?.firstOrNull()

progressingSession?.let { session ->
val timeZone = TimeZone.currentSystemDefault()
val period = with(session.startsAt) {
toLocalDateTime(timeZone)
.date.atTime(10, 0)
.toInstant(timeZone)
.periodUntil(this, timeZone)
}
val minuteHeightPx =
with(density) { TimetableSizes.minuteHeight.times(verticalScale).toPx() }
val scrollOffsetY =
-with(period) { hours * minuteHeightPx * 60 + minutes * minuteHeightPx }
timetableScreen.scroll(
Offset(0f, scrollOffsetY),
0,
Offset.Zero,
nestedScrollDispatcher,
)
scrolledToCurrentTimeState.scrolledInTimetableGrid()
}
.windowed(2, 1, true)
.find { clock.now() in it.first().startsAt..it.last().startsAt }
?.first()

progressingSession?.let { session ->
val timeZone = TimeZone.currentSystemDefault()
val period = with(session.startsAt) {
toLocalDateTime(timeZone)
.date.atTime(10, 0)
.toInstant(timeZone)
.periodUntil(this, timeZone)
}
val minuteHeightPx =
with(density) { TimetableSizes.minuteHeight.times(verticalScale).toPx() }
val scrollOffsetY =
-with(period) { hours * minuteHeightPx * 60 + minutes * minuteHeightPx }
timetableScreen.scroll(
Offset(0f, scrollOffsetY),
0,
Offset.Zero,
nestedScrollDispatcher,
)
}
}

Expand Down Expand Up @@ -1028,3 +1026,17 @@ object TimetableSizes {
val minuteHeight = 4.dp
val currentTimeDotRadius = 6.dp
}

private fun PersistentList<TimetableItem>.insertDummyEndOfTheDayItem(): PersistentList<TimetableItem> {
val endOfTheDayInstant =
first().startsAt.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.plus(1, DateTimeUnit.DAY)
.atStartOfDayIn(TimeZone.currentSystemDefault())
return plus(
Session.Companion.fake().copy(
startsAt = endOfTheDayInstant,
endsAt = endOfTheDayInstant,
),
).toPersistentList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ import io.github.droidkaigi.confsched.sessions.component.rememberTimetableNested
import io.github.droidkaigi.confsched.sessions.component.rememberTimetableNestedScrollStateHolder
import io.github.droidkaigi.confsched.sessions.section.TimetableListUiState.TimeSlot
import io.github.droidkaigi.confsched.sessions.timetableDetailSharedContentStateKey
import kotlinx.collections.immutable.ImmutableSet
import kotlinx.collections.immutable.PersistentMap
import kotlinx.collections.immutable.toImmutableSet
import kotlinx.datetime.DateTimeUnit
import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone
Expand Down Expand Up @@ -82,11 +84,10 @@ internal fun TimetableList(
onTimetableItemClick: (TimetableItem) -> Unit,
contentPadding: PaddingValues,
modifier: Modifier = Modifier,
nestedScrollStateHolder: TimetableNestedScrollStateHolder = rememberTimetableNestedScrollStateHolder(
true,
),
nestedScrollStateHolder: TimetableNestedScrollStateHolder = rememberTimetableNestedScrollStateHolder(true),
highlightWord: String = "",
enableAutoScrolling: Boolean = true,
scrolledToCurrentTimeState: ScrolledToCurrentTimeState = ScrolledToCurrentTimeState(),
) {
val layoutDirection = LocalLayoutDirection.current
val clock = LocalClock.current
Expand All @@ -106,29 +107,16 @@ internal fun TimetableList(
)

LaunchedEffect(Unit) {
if (enableAutoScrolling) {
if (enableAutoScrolling && scrolledToCurrentTimeState.inTimetableList.not()) {
val progressingSessionIndex = uiState.timetableItemMap.keys
.run {
// Insert dummy at a position after the start of the last session to allow scrolling
val endOfTheDayInstant = first().startTime.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.plus(1, DateTimeUnit.DAY)
.atStartOfDayIn(TimeZone.currentSystemDefault())
plus(
TimeSlot(
startTime = endOfTheDayInstant,
endTime = endOfTheDayInstant,
),
)
}
.insertDummyEndOfTheDayItem() // Insert dummy at a position after last session to allow scrolling
.windowed(2, 1, true)
.indexOfFirst {
clock.now() in it.first().startTime..it.last().startTime
}
.indexOfFirst { clock.now() in it.first().startTime..it.last().startTime }

progressingSessionIndex.takeIf { it != -1 }?.let {
scrollState.scrollToItem(it)
}
scrolledToCurrentTimeState.scrolledInTimetableList()
}
}

Expand Down Expand Up @@ -241,3 +229,16 @@ internal fun TimetableList(
}
}
}

private fun ImmutableSet<TimeSlot>.insertDummyEndOfTheDayItem(): ImmutableSet<TimeSlot> {
val endOfTheDayInstant = first().startTime.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.plus(1, DateTimeUnit.DAY)
.atStartOfDayIn(TimeZone.currentSystemDefault())
return plus(
TimeSlot(
startTime = endOfTheDayInstant,
endTime = endOfTheDayInstant,
),
).toImmutableSet()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
Expand Down Expand Up @@ -69,7 +70,14 @@ fun Timetable(
}
val layoutDirection = LocalLayoutDirection.current

val nestedScrollStateHolder = rememberTimetableNestedScrollStateHolder(isListTimetable = uiState is ListTimetable)
val nestedScrollStateHolder =
rememberTimetableNestedScrollStateHolder(isListTimetable = uiState is ListTimetable)
val scrolledToCurrentTimeState = rememberSaveable(
saver = listSaver(
save = { listOf(it.inTimetableList, it.inTimetableGrid) },
restore = { ScrolledToCurrentTimeState(it[0], it[1]) },
),
) { ScrolledToCurrentTimeState() }

Surface(
modifier = modifier.padding(contentPadding.calculateTopPadding()),
Expand Down Expand Up @@ -108,6 +116,7 @@ fun Timetable(
start = contentPadding.calculateStartPadding(layoutDirection),
end = contentPadding.calculateEndPadding(layoutDirection),
),
scrolledToCurrentTimeState = scrolledToCurrentTimeState,
)
}

Expand All @@ -124,6 +133,7 @@ fun Timetable(
bottom = contentPadding.calculateBottomPadding(),
start = contentPadding.calculateStartPadding(layoutDirection),
),
scrolledToCurrentTimeState = scrolledToCurrentTimeState,
)
}

Expand Down Expand Up @@ -174,3 +184,21 @@ private fun timetableModifier(
.fillMaxSize()
.background(Color.Transparent)
}

class ScrolledToCurrentTimeState(
inTimetableList: Boolean = false,
inTimetableGrid: Boolean = false,
) {
var inTimetableList: Boolean by mutableStateOf(inTimetableList)
private set
var inTimetableGrid: Boolean by mutableStateOf(inTimetableGrid)
private set

fun scrolledInTimetableList() {
inTimetableList = true
}

fun scrolledInTimetableGrid() {
inTimetableGrid = true
}
}

0 comments on commit bd458ed

Please sign in to comment.