Skip to content

Commit

Permalink
Update timeline values on scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
Faltenreich committed Mar 17, 2024
1 parent 4090165 commit 830b007
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ import androidx.compose.foundation.Canvas
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.fillMaxSize
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.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.util.VelocityTracker
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.rememberTextMeasurer
import androidx.compose.ui.unit.IntSize
import com.faltenreich.diaguard.AppTheme
import com.faltenreich.diaguard.shared.datetime.DateTimeFormatter
import com.faltenreich.diaguard.shared.datetime.DateUnit
Expand Down Expand Up @@ -56,6 +60,13 @@ fun Timeline(
// TODO: Reset remember when initialDate changes
val offsetX = remember { Animatable(0f) }
val offsetY = remember { Animatable(0f) }
var chartSize by remember { mutableStateOf(IntSize.Zero) }
LaunchedEffect(offsetX.value) {
val widthPerDay = chartSize.width
val offsetInDays = ceil(offsetX.value * -1) / widthPerDay
val date = state.initialDate.plus(offsetInDays.toInt(), DateUnit.DAY)
viewModel.dispatchIntent(TimelineIntent.SetDate(date))
}
val config by remember {
val config = TimelineConfig(
initialDate = state.initialDate,
Expand All @@ -78,6 +89,7 @@ fun Timeline(
Canvas(
modifier = modifier
.fillMaxSize()
.onGloballyPositioned { coordinates -> chartSize = coordinates.size }
.pointerInput(Unit) {
val decay = splineBasedDecay<Float>(this)
val animationSpec = FloatSpringSpec(
Expand All @@ -91,12 +103,6 @@ fun Timeline(
offsetX.snapTo(offsetX.value + dragAmount.x)
offsetY.snapTo(offsetY.value + dragAmount.y)
velocityTracker.addPosition(change.uptimeMillis, change.position)

val widthPerDay = size.width
val offsetInDays = ceil(offsetX.value * -1) / widthPerDay
val date = state.initialDate.plus(offsetInDays.toInt(), DateUnit.DAY)
viewModel.dispatchIntent(TimelineIntent.SetDate(date))

change.consume()
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.faltenreich.diaguard.shared.datetime.Date
import com.faltenreich.diaguard.shared.datetime.DateUnit
import com.faltenreich.diaguard.shared.datetime.GetTodayUseCase
import com.faltenreich.diaguard.shared.di.inject
import com.faltenreich.diaguard.shared.logging.Logger
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest
Expand All @@ -28,7 +29,8 @@ class TimelineViewModel(
private val initialDate = date ?: getToday()
private val currentDate = MutableStateFlow(initialDate)
private val values = currentDate.flatMapLatest { date ->
// FIXME: Does not update chart
// FIXME: Called twice on every date change
Logger.debug("flatMapLatest: Changing date to: $date")
valueRepository.observeByDateRange(
startDateTime = date.minus(2, DateUnit.DAY).atStartOfDay(),
endDateTime = date.plus(2, DateUnit.DAY).atEndOfDay(),
Expand Down

0 comments on commit 830b007

Please sign in to comment.