From dce252be67e1d76a1690cdc39b039636f341a9fd Mon Sep 17 00:00:00 2001 From: Ryan W Date: Thu, 21 Nov 2024 19:55:42 +0000 Subject: [PATCH] [fix] TariffSummaryTile: fix to show properly day-night and three-rate unit rates --- .../kunigami/domain/model/product/Tariff.kt | 23 ++- .../ui/components/TariffSummaryTile.kt | 149 ++++++++++++++---- 2 files changed, 140 insertions(+), 32 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/com/rwmobi/kunigami/domain/model/product/Tariff.kt b/composeApp/src/commonMain/kotlin/com/rwmobi/kunigami/domain/model/product/Tariff.kt index 7279a4d2..b3b6598b 100644 --- a/composeApp/src/commonMain/kotlin/com/rwmobi/kunigami/domain/model/product/Tariff.kt +++ b/composeApp/src/commonMain/kotlin/com/rwmobi/kunigami/domain/model/product/Tariff.kt @@ -88,7 +88,28 @@ data class Tariff( } } - // TODO: WIP - We do not support dual rates and don't know how it works for now + fun containsValidUnitRate(): Boolean { + return when (getElectricityTariffType()) { + ElectricityTariffType.STANDARD -> { + vatInclusiveStandardUnitRate != null || isVariable + } + + ElectricityTariffType.DAY_NIGHT -> { + vatInclusiveDayUnitRate != null && + vatInclusiveNightUnitRate != null + } + + ElectricityTariffType.THREE_RATE -> { + vatInclusiveDayUnitRate != null && + vatInclusiveNightUnitRate != null && + vatInclusiveOffPeakRate != null + } + + else -> false + } + } + + // TODO: This function will be removed once we have migrated to get billing data from GraphQL fun resolveUnitRate(referencePoint: Instant? = null): Double? { return when (getElectricityTariffType()) { ElectricityTariffType.STANDARD -> { diff --git a/composeApp/src/commonMain/kotlin/com/rwmobi/kunigami/ui/components/TariffSummaryTile.kt b/composeApp/src/commonMain/kotlin/com/rwmobi/kunigami/ui/components/TariffSummaryTile.kt index fac3d5d9..b06e5d10 100644 --- a/composeApp/src/commonMain/kotlin/com/rwmobi/kunigami/ui/components/TariffSummaryTile.kt +++ b/composeApp/src/commonMain/kotlin/com/rwmobi/kunigami/ui/components/TariffSummaryTile.kt @@ -35,11 +35,16 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha import androidx.compose.ui.draw.clip import androidx.compose.ui.text.font.FontWeight +import com.rwmobi.kunigami.domain.extensions.roundToTwoDecimalPlaces +import com.rwmobi.kunigami.domain.model.product.ElectricityTariffType import com.rwmobi.kunigami.domain.model.product.Tariff import com.rwmobi.kunigami.ui.composehelper.getScreenSizeInfo import com.rwmobi.kunigami.ui.previewsampledata.TariffSamples import com.rwmobi.kunigami.ui.theme.getDimension import kunigami.composeapp.generated.resources.Res +import kunigami.composeapp.generated.resources.day_unit_rate +import kunigami.composeapp.generated.resources.night_unit_rate +import kunigami.composeapp.generated.resources.off_peak_rate import kunigami.composeapp.generated.resources.standard_unit_rate import kunigami.composeapp.generated.resources.standing_charge import kunigami.composeapp.generated.resources.tariffs_variable @@ -90,38 +95,11 @@ internal fun TariffSummaryTile( ) } - val resolvedUnitRate = tariff.resolveUnitRate() - val rateString = when { - tariff.isVariable -> stringResource(resource = Res.string.tariffs_variable) - resolvedUnitRate != null -> stringResource(resource = Res.string.unit_p_kwh, resolvedUnitRate) - else -> null - } - - if (rateString != null) { - HorizontalDivider( - modifier = Modifier - .padding(vertical = dimension.grid_1) - .alpha(0.5f), + val shouldShowUnitRate = tariff.containsValidUnitRate() + if (shouldShowUnitRate) { + UnitRateLayout( + tariff = tariff, ) - - Row( - modifier = Modifier.fillMaxWidth(), - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.spacedBy(space = dimension.grid_0_5), - ) { - Text( - modifier = Modifier.weight(weight = 1f), - style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.onSurface, - text = stringResource(resource = Res.string.standard_unit_rate), - ) - Text( - modifier = Modifier.wrapContentWidth(), - style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.onSurface, - text = rateString, - ) - } } Spacer(modifier = Modifier.weight(1f)) @@ -135,6 +113,115 @@ internal fun TariffSummaryTile( } } +@Composable +private fun UnitRateLayout( + modifier: Modifier = Modifier, + tariff: Tariff, +) { + val dimension = getScreenSizeInfo().getDimension() + HorizontalDivider( + modifier = Modifier + .padding(vertical = dimension.grid_1) + .alpha(0.5f), + ) + + when (tariff.getElectricityTariffType()) { + ElectricityTariffType.STANDARD -> { + val rateString = when { + tariff.isVariable -> stringResource(resource = Res.string.tariffs_variable) + tariff.vatInclusiveStandardUnitRate != null -> stringResource(resource = Res.string.unit_p_kwh, tariff.vatInclusiveStandardUnitRate) + else -> null + } + + rateString?.let { + Row( + modifier = Modifier.fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(space = dimension.grid_0_5), + ) { + Text( + modifier = Modifier.weight(weight = 1f), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurface, + text = stringResource(resource = Res.string.standard_unit_rate), + ) + Text( + modifier = Modifier.wrapContentWidth(), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurface, + text = rateString, + ) + } + } + } + + else -> { + tariff.vatInclusiveDayUnitRate?.let { rate -> + Row( + modifier = Modifier.fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(space = dimension.grid_0_5), + ) { + Text( + modifier = Modifier.weight(weight = 1f), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurface, + text = stringResource(resource = Res.string.day_unit_rate), + ) + Text( + modifier = Modifier.wrapContentWidth(), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurface, + text = stringResource(resource = Res.string.unit_p_kwh, rate.roundToTwoDecimalPlaces()), + ) + } + } + + tariff.vatInclusiveNightUnitRate?.let { rate -> + Row( + modifier = Modifier.fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(space = dimension.grid_0_5), + ) { + Text( + modifier = Modifier.weight(weight = 1f), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurface, + text = stringResource(resource = Res.string.night_unit_rate), + ) + Text( + modifier = Modifier.wrapContentWidth(), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurface, + text = stringResource(resource = Res.string.unit_p_kwh, rate.roundToTwoDecimalPlaces()), + ) + } + } + + tariff.vatInclusiveOffPeakRate?.let { rate -> + Row( + modifier = Modifier.fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(space = dimension.grid_0_5), + ) { + Text( + modifier = Modifier.weight(weight = 1f), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurface, + text = stringResource(resource = Res.string.off_peak_rate), + ) + Text( + modifier = Modifier.wrapContentWidth(), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurface, + text = stringResource(resource = Res.string.unit_p_kwh, rate.roundToTwoDecimalPlaces()), + ) + } + } + } + } +} + @Preview @Composable private fun Preview() {