Skip to content

Commit

Permalink
[fix] TariffSummaryTile: fix to show properly day-night and three-rat…
Browse files Browse the repository at this point in the history
…e unit rates
  • Loading branch information
ryanw-mobile committed Nov 21, 2024
1 parent 417a89e commit dce252b
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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() {
Expand Down

0 comments on commit dce252b

Please sign in to comment.