Skip to content

Commit

Permalink
Add hourly as a display interval
Browse files Browse the repository at this point in the history
Fixes #67
  • Loading branch information
albertvaka committed Jun 18, 2024
1 parent 0238600 commit 7ede0b6
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fun Calendar.copy(): Calendar {

fun Calendar.addInterval(interval: Interval, times: Int) {
when (interval) {
Interval.HOUR -> add(Calendar.HOUR_OF_DAY, 1 * times)
Interval.DAY -> add(Calendar.DAY_OF_YEAR, 1 * times)
Interval.WEEK -> add(Calendar.DAY_OF_YEAR, 7 * times)
Interval.MONTH -> add(Calendar.MONTH, 1 * times)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.kde.bettercounter.R
import java.time.temporal.ChronoUnit

enum class Interval(val humanReadableResource: Int) {
HOUR(R.string.interval_hour),
DAY(R.string.interval_day),
WEEK(R.string.interval_week),
MONTH(R.string.interval_month),
Expand All @@ -21,6 +22,7 @@ enum class Interval(val humanReadableResource: Int) {

fun toChronoUnit(): ChronoUnit {
return when (this) {
HOUR -> ChronoUnit.HOURS
DAY -> ChronoUnit.DAYS
WEEK -> ChronoUnit.WEEKS
MONTH -> ChronoUnit.MONTHS
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/org/kde/bettercounter/ui/BetterChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class BetterChart : BarChart {
}

val (numBuckets, bucketIntervalAsCalendarField) = when (totalInterval) {
Interval.HOUR -> 60 to Calendar.MINUTE
Interval.DAY -> 24 to Calendar.HOUR_OF_DAY
Interval.WEEK -> 7 to Calendar.DAY_OF_WEEK
Interval.MONTH -> rangeStart.getActualMaximum(Calendar.DAY_OF_MONTH) to Calendar.DAY_OF_MONTH
Expand Down Expand Up @@ -148,7 +149,8 @@ class BetterChart : BarChart {
yAxis.axisMaximum = maxCount.toFloat()
xAxis.labelCount = series.size
xAxis.valueFormatter = when (bucketIntervalAsCalendarField) {
Calendar.HOUR_OF_DAY -> HourFormatter()
Calendar.MINUTE -> RawFormatter()
Calendar.HOUR_OF_DAY -> RawFormatter()
Calendar.DAY_OF_WEEK -> DayOfWeekFormatter()
Calendar.DAY_OF_MONTH -> MonthDayFormatter()
Calendar.MONTH -> MonthFormatter(rangeStart)
Expand Down Expand Up @@ -189,7 +191,7 @@ class BetterChart : BarChart {
}
}

class HourFormatter : ValueFormatter() {
class RawFormatter : ValueFormatter() {
override fun getFormattedValue(value: Float): String {
return (value.toInt()).toString()
}
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/org/kde/bettercounter/ui/ChartHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ChartHolder(
fun display(counter: CounterSummary, entries: List<Entry>, interval: Interval, rangeStart: Calendar, rangeEnd: Calendar, onIntervalChange: (Interval) -> Unit, onDateChange: (Calendar) -> Unit) {
// Chart name
val dateFormat = when (interval) {
Interval.HOUR -> SimpleDateFormat.getDateTimeInstance()
Interval.DAY, Interval.WEEK -> SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT)
Interval.MONTH -> SimpleDateFormat("LLL yyyy", Locale.getDefault())
Interval.YEAR -> SimpleDateFormat("yyyy", Locale.getDefault())
Expand All @@ -45,6 +46,7 @@ class ChartHolder(
popupMenu.setOnMenuItemClickListener { menuItem ->
menuItem.isChecked = true
val newInterval = when (menuItem.itemId) {
R.id.hour -> Interval.HOUR
R.id.day -> Interval.DAY
R.id.week -> Interval.WEEK
R.id.month -> Interval.MONTH
Expand All @@ -54,6 +56,7 @@ class ChartHolder(
return@setOnMenuItemClickListener true
}
val selectedItem = when (interval) {
Interval.HOUR -> R.id.hour
Interval.DAY -> R.id.day
Interval.WEEK -> R.id.week
Interval.MONTH -> R.id.month
Expand Down Expand Up @@ -132,7 +135,7 @@ class ChartHolder(
val endDate = min(rangeEnd.time, max(lastEntryDate, now))

return when (counter.interval) {
Interval.DAY -> getAverageStringPerHour(intervalEntries.size, startDate, endDate)
Interval.DAY, Interval.HOUR -> getAverageStringPerHour(intervalEntries.size, startDate, endDate)
else -> getAverageStringPerDay(intervalEntries.size, startDate, endDate)
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/menu/popup_menu.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="@+id/hour" android:title="@string/interval_hour" />
<item android:id="@+id/day" android:title="@string/interval_day" />
<item android:id="@+id/week" android:title="@string/interval_week" />
<item android:id="@+id/month" android:title="@string/interval_month" />
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<string name="edit_counter">Edit counter</string>
<string name="delete">Delete</string>
<string name="delete_or_reset">Delete/Reset</string>
<string name="interval_hour">Hourly</string>
<string name="interval_day">Daily</string>
<string name="interval_week">Weekly</string>
<string name="interval_month">Monthly</string>
Expand Down

0 comments on commit 7ede0b6

Please sign in to comment.