Skip to content

Commit

Permalink
extract float rounding method
Browse files Browse the repository at this point in the history
  • Loading branch information
eldcn committed Aug 25, 2024
1 parent 2755855 commit 098a2d4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.health.connect.client.records.HeartRateRecord
import androidx.health.connect.client.records.Record
import androidx.health.connect.client.records.WeightRecord
import edu.stanford.spezi.core.utils.LocaleProvider
import edu.stanford.spezi.core.utils.extensions.roundToDecimalPlaces
import java.time.DayOfWeek
import java.time.Instant
import java.time.LocalDate
Expand Down Expand Up @@ -108,10 +109,10 @@ class HealthUiStateMapper @Inject constructor(

private fun mapXValue(selectedTimeRange: TimeRange, zonedDateTime: ZonedDateTime): Float {
return when (selectedTimeRange) {
TimeRange.DAILY -> ((zonedDateTime.year.toFloat() + (zonedDateTime.dayOfYear - 1) / 365f) * 10).roundToTwoDecimalPlaces()
TimeRange.DAILY -> ((zonedDateTime.year.toFloat() + (zonedDateTime.dayOfYear - 1) / 365f) * 10)
TimeRange.WEEKLY -> (zonedDateTime.toEpochSecond() / (7 * 24 * 60 * 60)).toFloat()
TimeRange.MONTHLY -> zonedDateTime.year.toFloat() + (zonedDateTime.monthValue - 1) / 12f
}
}.roundToDecimalPlaces(places = 2)
}

private fun groupRecordsByTimeRange(
Expand Down Expand Up @@ -346,11 +347,6 @@ class HealthUiStateMapper @Inject constructor(
return date.format(DateTimeFormatter.ofPattern(pattern))
}

@Suppress("MagicNumber")
private fun Float.roundToTwoDecimalPlaces(): Float {
return Math.round(this * 100) / 100.0f
}

private data class ValueUnit(val value: Double, val unit: String)

private sealed interface EngageRecord {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import edu.stanford.bdh.engagehf.health.AggregatedHealthData
import edu.stanford.bdh.engagehf.health.HealthUiStateMapper.Companion.EPOCH_SECONDS_DIVISOR
import edu.stanford.bdh.engagehf.health.NewestHealthData
import edu.stanford.bdh.engagehf.health.TableEntryData
import edu.stanford.spezi.core.logging.SpeziLogger
import edu.stanford.spezi.core.utils.extensions.roundToDecimalPlaces
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
Expand Down Expand Up @@ -38,18 +40,17 @@ class SymptomsUiStateMapper @Inject constructor() {
val chartData = calculateChartData(symptomScoresByDay, selectedSymptomType)
val tableData = mapTableData(symptomScores, selectedSymptomType)

val newestData = symptomScores.maxByOrNull { it.date }
val newestData = symptomScores.maxBy { it.date }
val newestHealthData = NewestHealthData(
formattedValue = when (selectedSymptomType) {
SymptomType.OVERALL -> newestData?.overallScore.toString()
SymptomType.PHYSICAL_LIMITS -> newestData?.physicalLimitsScore.toString()
SymptomType.SOCIAL_LIMITS -> newestData?.socialLimitsScore.toString()
SymptomType.QUALITY_OF_LIFE -> newestData?.qualityOfLifeScore.toString()
SymptomType.SPECIFIC_SYMPTOMS -> newestData?.specificSymptomsScore.toString()
SymptomType.DIZZINESS -> newestData?.dizzinessScore.toString()
SymptomType.OVERALL -> newestData.overallScore.toString()
SymptomType.PHYSICAL_LIMITS -> newestData.physicalLimitsScore.toString()
SymptomType.SOCIAL_LIMITS -> newestData.socialLimitsScore.toString()
SymptomType.QUALITY_OF_LIFE -> newestData.qualityOfLifeScore.toString()
SymptomType.SPECIFIC_SYMPTOMS -> newestData.specificSymptomsScore.toString()
SymptomType.DIZZINESS -> newestData.dizzinessScore.toString()
} + "%",
formattedDate = newestData?.date?.format(dateTimeFormatter)
?: ""
formattedDate = newestData.date.format(dateTimeFormatter)
)

return SymptomsUiState.Success(
Expand Down Expand Up @@ -116,9 +117,12 @@ class SymptomsUiStateMapper @Inject constructor() {
}.average().toFloat()

yValues.add(averageScore)
xValues.add(date.toInstant().epochSecond / EPOCH_SECONDS_DIVISOR)
val xValue = (date.toInstant().epochSecond / EPOCH_SECONDS_DIVISOR)
.roundToDecimalPlaces(2)
xValues.add(xValue)
}

SpeziLogger.i { "XValues: $xValues" }
return AggregatedHealthData(
yValues = yValues,
xValues = xValues,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package edu.stanford.spezi.core.utils.extensions

import kotlin.math.pow

@Suppress("MagicNumber")
fun Float.roundToDecimalPlaces(places: Int): Float {
val factor = (10.0).pow(places).toFloat()
return Math.round(this * factor) / factor
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package edu.stanford.spezi.core.utils

import com.google.common.truth.Truth.assertThat
import edu.stanford.spezi.core.utils.extensions.roundToDecimalPlaces
import org.junit.Test

class FloatRoundingTest {

@Test
fun `it should round correctly`() {
// given
val float = 1.111111f
val places = 3

// when
val result = float.roundToDecimalPlaces(places = places)
val decimalPlaces = "$result".split(".").last()

// then
assertThat(decimalPlaces.length).isEqualTo(places)
}
}

0 comments on commit 098a2d4

Please sign in to comment.