Skip to content

Commit

Permalink
Merge pull request #650 from SUPLA/develop
Browse files Browse the repository at this point in the history
v24.10.02
  • Loading branch information
przemyslawzygmunt authored Oct 12, 2024
2 parents fe0e7c6 + cfce765 commit 78321ea
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 110 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ android {
minSdk = Versions.MinSdk
targetSdk = Versions.TargetSdk
multiDexEnabled = true
versionCode = 250
versionName = "24.10.01"
versionCode = 252
versionName = "24.10.02"

ndk {
moduleName = "suplaclient"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,69 +22,48 @@ import androidx.compose.runtime.compositionLocalOf
import java.text.SimpleDateFormat
import java.util.Date

@SuppressLint("SimpleDateFormat")
class DateFormatter {
@SuppressLint("SimpleDateFormat")
private val hourFormatter = SimpleDateFormat("HH:mm")
private val monthFormatter = SimpleDateFormat("dd MMM")
private val dateFormatter = SimpleDateFormat("dd.MM.yyyy")
private val shortDateFormatter = SimpleDateFormat("dd.MM.yy")
private val fullDateFormatter = SimpleDateFormat("dd.MM.yyyy HH:mm")
private val dayHourFormatter = SimpleDateFormat("EEEE HH:mm")
private val dayAndHourFormatter = SimpleDateFormat("dd MMM HH:mm")
private val dayAndHourShortFormatter = SimpleDateFormat("dd.MM HH:mm")
private val monthAndYearFormatter = SimpleDateFormat("LLLL yyyy")
private val yearFormatter = SimpleDateFormat("yyyy")

fun getHourString(date: Date?): String? =
date?.let {
val formatter = SimpleDateFormat("HH:mm")
formatter.format(it)
}
date?.let { hourFormatter.format(it) }

@SuppressLint("SimpleDateFormat")
fun getMonthString(date: Date?): String? =
date?.let {
val formatter = SimpleDateFormat("dd MMM")
formatter.format(it)
}
date?.let { monthFormatter.format(it) }

@SuppressLint("SimpleDateFormat")
fun getDateString(date: Date?): String? =
date?.let {
val formatter = SimpleDateFormat("dd.MM.yyyy")
formatter.format(it)
}
date?.let { dateFormatter.format(it) }

@SuppressLint("SimpleDateFormat")
fun getShortDateString(date: Date?): String? =
date?.let {
val formatter = SimpleDateFormat("dd.MM.yy")
formatter.format(it)
}
date?.let { shortDateFormatter.format(it) }

@SuppressLint("SimpleDateFormat")
fun getFullDateString(date: Date?): String? =
date?.let {
val formatter = SimpleDateFormat("dd.MM.yyyy HH:mm")
formatter.format(it)
}
date?.let { fullDateFormatter.format(it) }

@SuppressLint("SimpleDateFormat")
fun getDayHourDateString(date: Date?): String? =
date?.let {
val formatter = SimpleDateFormat("EEEE HH:mm")
formatter.format(it)
}
date?.let { dayHourFormatter.format(it) }

@SuppressLint("SimpleDateFormat")
fun getDayAndHourDateString(date: Date?): String? =
date?.let {
val formatter = SimpleDateFormat("dd MMM HH:mm")
formatter.format(it)
}
date?.let { dayAndHourFormatter.format(it) }

fun getDayAndHourShortDateString(date: Date?): String? =
date?.let { dayAndHourShortFormatter.format(it) }

@SuppressLint("SimpleDateFormat")
fun getMonthAndYearString(date: Date?): String? =
date?.let {
val formatter = SimpleDateFormat("LLLL yyyy")
formatter.format(it)
}
date?.let { monthAndYearFormatter.format(it) }

@SuppressLint("SimpleDateFormat")
fun getYearString(date: Date?): String? =
date?.let {
val formatter = SimpleDateFormat("yyyy")
formatter.format(it)
}
date?.let { yearFormatter.format(it) }
}

val LocalDateFormatter = compositionLocalOf { DateFormatter() }
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,4 @@ private interface SetWithResetDetection {
}
}

fun Long.toKWh(): Float = this.div(100000.00f)
fun Long.toKWh(): Float = this.div(100_000.00f)
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ enum class HomeScreenContent(val value: Int) {
TIME(1 shl 3),
TIME_DATE(1 shl 4),
TEMPERATURE_TIME(1 shl 5),
MAIN_AND_AUX_TEMPERATURE(1 shl 6)
MAIN_AND_AUX_TEMPERATURE(1 shl 6),
MODE_OR_TEMPERATURE(1 shl 7)
}

data class HomeScreenContentField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ enum class SuplaElectricityMeasurementType(val rawValue: Int, val ordering: Int,

FORWARD_REACTIVE_ENERGY,
REVERSE_REACTIVE_ENERGY -> "kvarh"

else -> ""
}

val precision: Int
Expand All @@ -126,17 +124,45 @@ enum class SuplaElectricityMeasurementType(val rawValue: Int, val ordering: Int,
else -> 0
}

fun merge(values: List<Float>): Float? =
val showEnergyLabel: Boolean
get() = when (this) {
FORWARD_ACTIVE_ENERGY,
REVERSE_ACTIVE_ENERGY,
FORWARD_REACTIVE_ENERGY,
REVERSE_REACTIVE_ENERGY -> true

else -> false
}

val shortLabel: Int
get() = when (this) {
FREQUENCY -> R.string.details_em_frequency
VOLTAGE -> R.string.details_em_voltage
CURRENT, CURRENT_OVER_65A -> R.string.details_em_current
POWER_ACTIVE, POWER_ACTIVE_KW -> R.string.details_em_power_active
POWER_REACTIVE, POWER_REACTIVE_KVAR -> R.string.details_em_power_reactive
POWER_APPARENT, POWER_APPARENT_KVA -> R.string.details_em_power_apparent
POWER_FACTOR -> R.string.details_em_power_factor
PHASE_ANGLE -> R.string.details_em_phase_angle
FORWARD_ACTIVE_ENERGY -> R.string.details_em_forward_active_energy_short
REVERSE_ACTIVE_ENERGY -> R.string.details_em_reverse_active_energy_short
FORWARD_ACTIVE_ENERGY_BALANCED -> R.string.details_em_forward_active_energy_short
REVERSE_ACTIVE_ENERGY_BALANCED -> R.string.details_em_reverse_active_energy_short
FORWARD_REACTIVE_ENERGY -> R.string.details_em_forward_reactive_energy_short
REVERSE_REACTIVE_ENERGY -> R.string.details_em_reverse_reactive_energy_short
}

fun merge(values: List<Float>): Value? =
when (this) {
FREQUENCY -> values.first()
VOLTAGE -> values.average().toFloat()
FREQUENCY -> Value.Single(values.first())
VOLTAGE -> Value.Double(values.min(), values.max())
POWER_ACTIVE,
POWER_REACTIVE,
POWER_APPARENT,
FORWARD_ACTIVE_ENERGY,
REVERSE_ACTIVE_ENERGY,
FORWARD_REACTIVE_ENERGY,
REVERSE_REACTIVE_ENERGY -> values.sum()
REVERSE_REACTIVE_ENERGY -> Value.Single(values.sum())

else -> null
}
Expand All @@ -163,6 +189,11 @@ enum class SuplaElectricityMeasurementType(val rawValue: Int, val ordering: Int,
}
}
}

sealed interface Value {
data class Single(val value: Float) : Value
data class Double(val first: Float, val second: Float) : Value
}
}

val Int.suplaElectricityMeterMeasuredTypes: List<SuplaElectricityMeasurementType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,14 @@ private fun PhaseWithMeasurements.Companion.allPhases(
.filterValues { it.isNotEmpty() }
.mapValues { it.key.merge(it.value) }
.filterValues { it != null }
.mapValues { formatter.custom(it.value!!, it.key.precision) }
.mapValues {
when (val value = it.value!!) {
is SuplaElectricityMeasurementType.Value.Single ->
formatter.custom(value.value, it.key.precision)
is SuplaElectricityMeasurementType.Value.Double ->
"${formatter.custom(value.first, 0)}- ${formatter.custom(value.second, 0)}"
}
}

return PhaseWithMeasurements(R.string.em_chart_all_phases, values)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ private fun PhasesData(measurementTypes: List<SuplaElectricityMeasurementType>,
}

@Composable
private fun PhaseDataLabels(measurementTypes: List<SuplaElectricityMeasurementType>, withHeader: Boolean = true) =
private fun PhaseDataLabels(
measurementTypes: List<SuplaElectricityMeasurementType>,
withHeader: Boolean = true,
withLabel: Boolean = true
) =
Column(
modifier = Modifier
.width(IntrinsicSize.Max)
Expand All @@ -167,16 +171,22 @@ private fun PhaseDataLabels(measurementTypes: List<SuplaElectricityMeasurementTy
if (withHeader) {
PhaseHeader()
}
var energyShown = false
measurementTypes.forEach {
TypeLabel(stringResource(id = it.labelRes))
if (!energyShown && it.showEnergyLabel && withLabel) {
EnergyLabel(text = stringResource(id = R.string.details_em_energy_label))
energyShown = true
}
TypeLabel(stringResource(id = it.shortLabel))
}
}

@Composable
private fun PhaseDataSinglePhase(
phase: PhaseWithMeasurements,
types: List<SuplaElectricityMeasurementType>,
showPhaseName: Boolean
showPhaseName: Boolean,
withLabel: Boolean = true
) =
Column(
modifier = Modifier
Expand All @@ -192,15 +202,25 @@ private fun PhaseDataSinglePhase(
modifier = Modifier.width(IntrinsicSize.Max),
horizontalAlignment = Alignment.CenterHorizontally
) {
var energyShown = false
types.forEach {
if (!energyShown && it.showEnergyLabel && withLabel) {
EnergyLabel()
energyShown = true
}
PhaseValue(phase.values[it] ?: ValuesFormatter.NO_VALUE_TEXT)
}
}
Column(
modifier = Modifier.width(IntrinsicSize.Max),
horizontalAlignment = Alignment.CenterHorizontally
) {
var energyShown = false
types.forEach {
if (!energyShown && it.showEnergyLabel && withLabel) {
EnergyLabel()
energyShown = true
}
PhaseValueUnit(if (phase.values.contains(it) && phase.values[it] != null) it.unit else "")
}
}
Expand Down Expand Up @@ -241,6 +261,19 @@ private fun PhaseHeader(text: String = "", alignment: TextAlign = TextAlign.Star
.wrapContentHeight(align = Alignment.CenterVertically)
)

@Composable
private fun EnergyLabel(text: String = "") =
Text(
text = text,
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.onBackground,
modifier = Modifier
.fillMaxWidth()
.background(MaterialTheme.colorScheme.surface)
.padding(top = Distance.small, bottom = Distance.tiny)
.wrapContentHeight(align = Alignment.CenterVertically)
)

@Composable
private fun TypeLabel(text: String = "") =
Text(
Expand All @@ -252,7 +285,6 @@ private fun TypeLabel(text: String = "") =
.height(35.dp)
.fillMaxWidth()
.background(MaterialTheme.colorScheme.surface)
.padding(end = Distance.small)
.wrapContentHeight(align = Alignment.CenterVertically)
)

Expand Down Expand Up @@ -361,31 +393,35 @@ private fun Preview() {
phaseMeasurementTypes = listOf(
SuplaElectricityMeasurementType.FREQUENCY,
SuplaElectricityMeasurementType.CURRENT,
SuplaElectricityMeasurementType.VOLTAGE
SuplaElectricityMeasurementType.VOLTAGE,
SuplaElectricityMeasurementType.FORWARD_ACTIVE_ENERGY
),
phaseMeasurementValues = listOf(
PhaseWithMeasurements(
R.string.details_em_phase1,
mapOf(
SuplaElectricityMeasurementType.FREQUENCY to "50",
SuplaElectricityMeasurementType.CURRENT to "5",
SuplaElectricityMeasurementType.VOLTAGE to "245"
SuplaElectricityMeasurementType.VOLTAGE to "245",
SuplaElectricityMeasurementType.FORWARD_ACTIVE_ENERGY to "245"
)
),
PhaseWithMeasurements(
R.string.details_em_phase2,
mapOf(
SuplaElectricityMeasurementType.FREQUENCY to "50",
SuplaElectricityMeasurementType.CURRENT to "3",
SuplaElectricityMeasurementType.VOLTAGE to "243"
SuplaElectricityMeasurementType.VOLTAGE to "243",
SuplaElectricityMeasurementType.FORWARD_ACTIVE_ENERGY to "245"
)
),
PhaseWithMeasurements(
R.string.details_em_phase3,
mapOf(
SuplaElectricityMeasurementType.FREQUENCY to "50",
SuplaElectricityMeasurementType.CURRENT to "4",
SuplaElectricityMeasurementType.VOLTAGE to "248"
SuplaElectricityMeasurementType.VOLTAGE to "248",
SuplaElectricityMeasurementType.FORWARD_ACTIVE_ENERGY to "245"
)
)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.supla.android.features.details.detailbase.electricitymeter

import org.supla.android.extensions.ifNotZero
import org.supla.android.usecases.channel.valueformatter.ChannelValueFormatter
import java.util.Locale
import java.text.DecimalFormat

data class EnergyData(
val energy: String,
Expand All @@ -29,7 +29,13 @@ data class EnergyData(
operator fun invoke(formatter: ChannelValueFormatter, energy: Double, pricePerUnit: Double, currency: String): EnergyData =
EnergyData(
energy = formatter.format(energy),
price = pricePerUnit.ifNotZero { String.format(Locale.getDefault(), "%.2f %s", it.times(energy), currency) }
price = pricePerUnit.ifNotZero {
val decimalFormatter = DecimalFormat()
decimalFormatter.minimumFractionDigits = 2
decimalFormatter.maximumFractionDigits = 2

"${decimalFormatter.format(it.times(energy))} $currency"
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -635,18 +635,6 @@ data class HistoryDetailViewState(
}
}

val showBottomBar: Boolean
get() = when (filters.selectedRange) {
ChartRange.DAY,
ChartRange.WEEK,
ChartRange.MONTH,
ChartRange.QUARTER,
ChartRange.YEAR,
ChartRange.ALL_HISTORY -> true

else -> false
}

val allowNavigation: Boolean
get() = when (filters.selectedRange) {
ChartRange.DAY,
Expand Down
Loading

0 comments on commit 78321ea

Please sign in to comment.