forked from LeoColman/Petals
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Allow for an "All Time" view in statistics page LeoColman#554
- Loading branch information
Showing
6 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
app/src/main/kotlin/br/com/colman/petals/statistics/graph/AllTimeGraph.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package br.com.colman.petals.statistics.graph | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.res.stringResource | ||
import br.com.colman.petals.R.string | ||
import br.com.colman.petals.statistics.graph.component.LineChart | ||
import br.com.colman.petals.statistics.graph.data.createAllTimeDistribution | ||
import br.com.colman.petals.statistics.graph.formatter.DaysSinceFirstUseFormatter | ||
import br.com.colman.petals.use.repository.Use | ||
import com.github.mikephil.charting.components.LimitLine | ||
import com.github.mikephil.charting.data.LineDataSet | ||
import java.time.LocalDate | ||
import java.time.YearMonth | ||
|
||
@Composable | ||
fun AllTimeGraph(uses: List<Use>) { | ||
val description = stringResource(string.all_time_day_description) | ||
val gramsData = createAllTimeDistribution(uses) | ||
val gramsDataList = mutableListOf<LineDataSet>() | ||
gramsDataList.add(gramsData) | ||
|
||
LineChart(gramsDataList, description, 5f) { | ||
axisMinimum = 1f | ||
labelCount = 5 | ||
granularity = 1f | ||
valueFormatter = DaysSinceFirstUseFormatter | ||
addLimitLine(LimitLine(YearMonth.from(LocalDate.now()).monthValue.toFloat()).apply { lineWidth = 2f }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...main/kotlin/br/com/colman/petals/statistics/graph/data/DistributionPerDaySinceFirstUse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package br.com.colman.petals.statistics.graph.data | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.graphics.Color.Companion.Green | ||
import androidx.compose.ui.graphics.Color.Companion.White | ||
import androidx.compose.ui.graphics.toArgb | ||
import androidx.compose.ui.res.stringResource | ||
import br.com.colman.petals.R | ||
import br.com.colman.petals.statistics.graph.formatter.GramsValueFormatter | ||
import br.com.colman.petals.use.repository.Use | ||
import br.com.colman.petals.use.repository.totalGrams | ||
import com.github.mikephil.charting.data.Entry | ||
import com.github.mikephil.charting.data.LineDataSet | ||
import java.time.LocalDateTime.now | ||
|
||
private fun calculateGramsDistributionPerDaySinceFirstUse(uses: List<Use>): List<Entry> { | ||
val dayBeforeFirstUseDate = uses.minByOrNull { it.date }!!.localDate.toEpochDay().dec() | ||
val now = now().toLocalDate().toEpochDay() | ||
val dateRange = (dayBeforeFirstUseDate..now).toList() | ||
|
||
val usesPerDay = dateRange.associateWith { uses.filter { u -> u.localDate.toEpochDay() == it } } | ||
|
||
return usesPerDay.mapValues { it.value.totalGrams }.toSortedMap().map { (k, v) -> | ||
Entry( | ||
(k - dayBeforeFirstUseDate).toFloat(), | ||
v.toFloat() | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
fun createAllTimeDistribution(uses: List<Use>): LineDataSet { | ||
return LineDataSet(calculateGramsDistributionPerDaySinceFirstUse(uses), stringResource(R.string.all_time)).apply { | ||
valueFormatter = GramsValueFormatter | ||
lineWidth = 6f | ||
setDrawCircles(true) | ||
setDrawFilled(false) | ||
setDrawValues(true) | ||
fillColor = Green.toArgb() | ||
color = Green.toArgb() | ||
valueTextColor = White.toArgb() | ||
valueTextSize = 14f | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...main/kotlin/br/com/colman/petals/statistics/graph/formatter/DaysSinceFirstUseFormatter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package br.com.colman.petals.statistics.graph.formatter | ||
|
||
import br.com.colman.petals.koin | ||
import br.com.colman.petals.settings.SettingsRepository | ||
import br.com.colman.petals.use.repository.Use | ||
import br.com.colman.petals.use.repository.UseRepository | ||
import com.github.mikephil.charting.components.AxisBase | ||
import com.github.mikephil.charting.formatter.IAxisValueFormatter | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.runBlocking | ||
import java.time.LocalDate | ||
import java.time.format.DateTimeFormatter | ||
|
||
val DaysSinceFirstUseFormatter = object : IAxisValueFormatter { | ||
val settingsRepository: SettingsRepository = koin.get<SettingsRepository>() | ||
val useRepository: UseRepository = koin.get<UseRepository>() | ||
|
||
private var dateFormat: String = runBlocking { settingsRepository.dateFormat.first() } | ||
private val formatter = DateTimeFormatter.ofPattern(dateFormat) | ||
|
||
private var uses: List<Use> = runBlocking { useRepository.all().first() } | ||
private val dayBeforeFirstUseDateToEpochDay: Long = uses.minByOrNull { it.date }!!.localDate.toEpochDay().dec() | ||
|
||
override fun getFormattedValue(value: Float, axis: AxisBase?): String { | ||
val epochDay = (value + dayBeforeFirstUseDateToEpochDay).toLong() | ||
val localDate = LocalDate.ofEpochDay(epochDay) | ||
|
||
return formatter.format(localDate) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters