Skip to content

Commit

Permalink
Better calculation for period averages
Browse files Browse the repository at this point in the history
  • Loading branch information
albertvaka committed Dec 25, 2024
1 parent 8cc6a57 commit c921f57
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions app/src/main/java/org/kde/bettercounter/ui/ChartHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,24 @@ class ChartHolder(

val firstEntryDate = counter.leastRecent!!
val lastEntryDate = counter.mostRecent!!
val now = Calendar.getInstance().time

val startDate = max(rangeStart.time, min(firstEntryDate, now))
val isPeriodComplete = rangeEnd.time < now // true if we are looking at historical data
val startDate = max(rangeStart.time, firstEntryDate)
val endDate = min(rangeEnd.time, lastEntryDate)

val endDate = if (isPeriodComplete) { rangeEnd.time } else { lastEntryDate }
val numCounts = if (isPeriodComplete) { intervalEntries.size } else { intervalEntries.size - 1 }
val isFromRangeLimit = endDate == rangeEnd.time || startDate == rangeStart.time
val numEntries = if (isFromRangeLimit) {
intervalEntries.size
} else {
intervalEntries.size - 1
}

if (numCounts == 0) {
if (numEntries == 0) {
return activity.getString(R.string.stats_average_n_a)
}

return when (counter.interval) {
Interval.DAY, Interval.HOUR -> getAverageStringPerHour(numCounts, startDate, endDate)
else -> getAverageStringPerDay(numCounts, startDate, endDate)
Interval.DAY, Interval.HOUR -> getAverageStringPerHour(numEntries, startDate, endDate)
else -> getAverageStringPerDay(numEntries, startDate, endDate)
}
}

Expand Down

0 comments on commit c921f57

Please sign in to comment.