diff --git a/app/src/main/kotlin/br/com/colman/petals/statistics/graph/data/DistributionPerDaySinceFirstUse.kt b/app/src/main/kotlin/br/com/colman/petals/statistics/graph/data/DistributionPerDaySinceFirstUse.kt index c7be6a28..bd963f34 100644 --- a/app/src/main/kotlin/br/com/colman/petals/statistics/graph/data/DistributionPerDaySinceFirstUse.kt +++ b/app/src/main/kotlin/br/com/colman/petals/statistics/graph/data/DistributionPerDaySinceFirstUse.kt @@ -13,15 +13,13 @@ import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.LineDataSet import java.time.LocalDate.now -private fun calculateGramsDistributionPerDaySinceFirstUse(uses: List): List { - val dayBeforeFirstUseDate = uses.minBy { it.date }.localDate.toEpochDay() - val daysSinceFirstUse = (dayBeforeFirstUseDate..now().toEpochDay()).toList() - return daysSinceFirstUse.associateWith {uses.filter { u -> u.localDate.toEpochDay() == it } }.toSortedMap() - .map { (k, v) -> Entry((k - dayBeforeFirstUseDate).toFloat(), v.totalGrams.toFloat()) - } +@Composable +fun createAllTimeDistribution(uses: List): LineDataSet { + val label = stringResource(R.string.grams_distribution_over_days_since_first_use) + return createAllTimeLineDataSet(calculateAllTimeGramsDistribution(uses), label) } -fun createLineDataSet(entryList: List, label: String):LineDataSet { +fun createAllTimeLineDataSet(entryList: List, label: String):LineDataSet { return LineDataSet(entryList, label).apply { valueFormatter = GramsValueFormatter lineWidth = 6f @@ -35,8 +33,10 @@ fun createLineDataSet(entryList: List, label: String):LineDataSet { } } -@Composable -fun createAllTimeDistribution(uses: List): LineDataSet { - val label = stringResource(R.string.grams_distribution_over_days_since_first_use) - return createLineDataSet(calculateGramsDistributionPerDaySinceFirstUse(uses), label) +private fun calculateAllTimeGramsDistribution(uses: List): List { + val dayBeforeFirstUseDate = uses.minBy { it.date }.localDate.toEpochDay() + val daysSinceFirstUse = (dayBeforeFirstUseDate..now().toEpochDay()).toList() + return daysSinceFirstUse.associateWith {uses.filter { u -> u.localDate.toEpochDay() == it } }.toSortedMap() + .map { (k, v) -> Entry((k - dayBeforeFirstUseDate).toFloat(), v.totalGrams.toFloat()) + } } diff --git a/app/src/test/kotlin/br/com/colman/petals/statistics/graph/data/DistributionPerDaySinceFirstUseTest.kt b/app/src/test/kotlin/br/com/colman/petals/statistics/graph/data/DistributionPerDaySinceFirstUseTest.kt deleted file mode 100644 index ac8c1d91..00000000 --- a/app/src/test/kotlin/br/com/colman/petals/statistics/graph/data/DistributionPerDaySinceFirstUseTest.kt +++ /dev/null @@ -1,29 +0,0 @@ -package br.com.colman.petals.statistics.graph.data - -import com.github.mikephil.charting.data.Entry -import com.github.mikephil.charting.data.LineDataSet -import io.kotest.core.spec.style.FunSpec -import io.kotest.data.Row2 -import io.kotest.matchers.shouldBe -import io.kotest.data.forAll -import java.util.* - -class DistributionPerDaySinceFirstUseTest: FunSpec({ - val entryList:List = listOf( - Entry(1f,0.8f), - Entry(2f, 0.5f), - Entry(3f, 0.2f ) - ) - - - test("Create LineDataSet Unit Test") { - Locale.setDefault(Locale.US) - forAll( - Row2(entryList, LineDataSet(entryList, "Grams distribution over days since first use")), - ) { dataSet: List, expected:LineDataSet -> - val actual = createLineDataSet(dataSet, "Grams distribution over days since first use") - - actual shouldBe expected - } - } -}) diff --git a/app/src/test/kotlin/br/com/colman/petals/statistics/graph/formatter/DaysSinceFirstUseFormatter.kt b/app/src/test/kotlin/br/com/colman/petals/statistics/graph/formatter/DaysSinceFirstUseFormatter.kt new file mode 100644 index 00000000..d49cd06b --- /dev/null +++ b/app/src/test/kotlin/br/com/colman/petals/statistics/graph/formatter/DaysSinceFirstUseFormatter.kt @@ -0,0 +1,25 @@ +package br.com.colman.petals.statistics.graph.formatter + +import io.kotest.core.spec.style.FunSpec +import io.kotest.data.Row2 +import io.kotest.data.forAll +import io.kotest.matchers.shouldBe +import java.time.LocalDateTime.now +import java.time.format.DateTimeFormatter + +class DaysSinceFirstUseFormatter: FunSpec({ + + test("getFormattedValue returns correctly formatted days since first use (yyyy-MM-dd)"){ + val dateFormat = "yyyy-MM-dd" + val formatter = DateTimeFormatter.ofPattern(dateFormat) + forAll( + Row2(0f, now().minusDays(3).format(formatter).toString()), + Row2(1f, now().minusDays(2).format(formatter).toString()), + Row2(2f, now().minusDays(1).format(formatter).toString()), + Row2(3f, now().format(formatter).toString()) + ){ value, expectedDay -> + val actual = DaysSinceFirstUseFormatter.getFormattedValue((value), null) + actual shouldBe expectedDay + } + } +})