Skip to content

Commit ade5409

Browse files
committed
The smoothedData flow is now initialized with null instead of an empty list. This ensures that downstream consumers correctly handle the loading state and do not attempt to process data before it's available.
1 parent 93d0916 commit ade5409

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

android_app/app/src/main/java/com/health/openscale/ui/screen/components/MeasurementChart.kt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ import com.health.openscale.core.data.MeasurementTypeKey
7272
import com.health.openscale.core.data.TimeRangeFilter
7373
import com.health.openscale.core.facade.SettingsPreferenceKeys
7474
import com.health.openscale.core.facade.SettingsFacade
75-
import com.health.openscale.core.model.EnrichedMeasurement
7675
import com.health.openscale.ui.shared.SharedViewModel
7776
import com.health.openscale.ui.shared.TopBarAction
7877
import com.patrykandpatrick.vico.compose.cartesian.CartesianChartHost
@@ -226,19 +225,18 @@ fun MeasurementChart(
226225
}
227226

228227
var isChartDataLoading by remember { mutableStateOf(true) }
229-
val initialChartDataValue = remember { emptyList<EnrichedMeasurement>() }
230228

231-
val smoothedData by sharedViewModel
232-
.smoothedEnrichedMeasurements(
229+
val smoothedData by remember(startTimeMillis, endTimeMillis, currentSelectedTypeIntIds) {
230+
sharedViewModel.smoothedEnrichedMeasurements(
233231
startTimeMillisFlow = startTimeMillisFlow,
234232
endTimeMillisFlow = endTimeMillisFlow,
235233
typesToSmoothAndDisplayFlow = typesToSmoothFlow
236234
)
237-
.collectAsStateWithLifecycle(initialValue = initialChartDataValue)
235+
}.collectAsStateWithLifecycle(initialValue = null)
238236

239237
// Update loading state once data (or an empty list after loading) is received
240238
LaunchedEffect(smoothedData) {
241-
if (smoothedData !== initialChartDataValue) {
239+
if (smoothedData != null) {
242240
isChartDataLoading = false
243241
}
244242
}
@@ -247,14 +245,14 @@ fun MeasurementChart(
247245

248246
val lineChartMeasurements = remember(smoothedData, selectedPeriod) {
249247
if (selectedPeriod == null) smoothedData
250-
else smoothedData.filter { measurement ->
248+
else (smoothedData ?: emptyList()).filter { measurement ->
251249
val ts = measurement.measurementWithValues.measurement.timestamp
252250
ts >= selectedPeriod!!.startTimestamp && ts < selectedPeriod!!.endTimestamp
253251
}
254252
}
255253

256254
val measurementsForPeriodChart = remember(smoothedData) {
257-
smoothedData.map { it.measurementWithValues }
255+
(smoothedData ?: emptyList()).map { it.measurementWithValues }
258256
}
259257

260258
val periodChartData = remember(measurementsForPeriodChart, uiSelectedTimeRange) {
@@ -369,7 +367,7 @@ fun MeasurementChart(
369367

370368
// Extracting measurements with their values for plotting.
371369
val measurementsWithValues = remember(lineChartMeasurements) {
372-
lineChartMeasurements.map { it.measurementWithValues }
370+
(lineChartMeasurements?: emptyList()).map { it.measurementWithValues }
373371
}
374372

375373
// Determine which measurement types to actually plot based on current selections,
@@ -557,7 +555,7 @@ fun MeasurementChart(
557555
contentAlignment = Alignment.Center
558556
) {
559557
val message = if (lineTypesToActuallyPlot.isEmpty() && effectiveShowTypeFilterRow) {
560-
if (currentSelectedTypeIntIds.isNotEmpty() && smoothedData.none { m -> m.measurementWithValues.values.any { v -> v.type.id in currentSelectedTypeIntIds } }) {
558+
if (currentSelectedTypeIntIds.isNotEmpty() && (smoothedData ?: emptyList()).none { m -> m.measurementWithValues.values.any { v -> v.type.id in currentSelectedTypeIntIds } }) {
561559
stringResource(R.string.line_chart_no_data_for_selected_types)
562560
} else if (currentSelectedTypeIntIds.isEmpty()){
563561
stringResource(R.string.line_chart_please_select_types)
@@ -568,7 +566,7 @@ fun MeasurementChart(
568566
if (allAvailableMeasurementTypes.none { it.isEnabled && (it.inputType == InputFieldType.FLOAT || it.inputType == InputFieldType.INT) })
569567
stringResource(R.string.line_chart_no_plottable_types)
570568
else stringResource(R.string.line_chart_no_data_or_types_to_select)
571-
} else if (smoothedData.isEmpty() && measurementsWithValues.isEmpty() && currentSelectedTypeIntIds.isNotEmpty()){
569+
} else if ((smoothedData ?: emptyList()).isEmpty() && measurementsWithValues.isEmpty() && currentSelectedTypeIntIds.isNotEmpty()){
572570
stringResource(R.string.line_chart_no_data_to_display)
573571
}
574572
else {

0 commit comments

Comments
 (0)