Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions uhabits-android/src/main/res/xml/widget_history_info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="80dp"
android:minWidth="80dp"
android:minResizeWidth="80dp"
android:targetCellWidth="2"
android:targetCellHeight="2"
android:minResizeWidth="40dp"
android:minResizeHeight="80dp"
android:initialLayout="@layout/widget_graph"
android:previewImage="@drawable/widget_preview_history"
android:resizeMode="vertical|horizontal"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="3600000"
android:configure="org.isoron.uhabits.widgets.activities.HabitPickerDialog"
android:widgetCategory="home_screen">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ data class LocalDate(val daysSince2000: Int) {
}

interface LocalDateFormatter {
fun narrowWeekdayName(weekday: DayOfWeek): String
fun narrowWeekdayName(date: LocalDate): String
fun shortWeekdayName(weekday: DayOfWeek): String
fun shortWeekdayName(date: LocalDate): String
fun shortMonthName(date: LocalDate): String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import java.util.Calendar.LONG
import java.util.Calendar.MILLISECOND
import java.util.Calendar.MINUTE
import java.util.Calendar.MONTH
import java.util.Calendar.NARROW_FORMAT
import java.util.Calendar.SECOND
import java.util.Calendar.SHORT
import java.util.Calendar.YEAR
Expand Down Expand Up @@ -68,6 +69,17 @@ class JavaLocalDateFormatter(private val locale: Locale) : LocalDateFormatter {
return cal.getDisplayName(DAY_OF_WEEK, SHORT, locale)
}

override fun narrowWeekdayName(weekday: DayOfWeek): String {
val cal = GregorianCalendar()
cal.set(DAY_OF_WEEK, weekday.daysSinceSunday - 1)
return shortWeekdayName(LocalDate(cal.get(YEAR), cal.get(MONTH) + 1, cal.get(DAY_OF_MONTH)))
}

override fun narrowWeekdayName(date: LocalDate): String {
val cal = date.toGregorianCalendar()
return cal.getDisplayName(DAY_OF_WEEK, NARROW_FORMAT, locale)
}

fun longFormat(date: LocalDate): String {
val df = DateFormat.getDateInstance(DateFormat.LONG, locale)
df.timeZone = TimeZone.getTimeZone("UTC")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,9 @@ class HistoryChart(
squareSize = round((height - 2 * padding) / 8.0)
canvas.setFontSize(min(14.0, height * 0.06))

val weekdayColumnWidth = DayOfWeek.values().map { weekday ->
canvas.measureText(dateFormatter.shortWeekdayName(weekday)) + squareSize * 0.15
}.maxOrNull() ?: 0.0
val weekdayColumnWidth = calculateWeekdayColumnWidth(canvas)

nColumns = floor((width - 2 * padding - weekdayColumnWidth) / squareSize).toInt()
nColumns = calcNumOfColumns(weekdayColumnWidth)
val firstWeekdayOffset = (
today.dayOfWeek.daysSinceSunday -
firstWeekday.daysSinceSunday + 7
Expand All @@ -136,13 +134,43 @@ class HistoryChart(
val date = topLeftDate.plus(row)
canvas.setTextAlign(TextAlign.LEFT)
canvas.drawText(
dateFormatter.shortWeekdayName(date),
padding + nColumns * squareSize + squareSize * 0.15,
weekdayDisplayName(date),
padding + nColumns * squareSize + 1.5,
padding + squareSize * (row + 1) + squareSize / 2
)
}
}

private fun calcNumOfColumns(weekdayColumnWidth: Double) =
floor((width - 2 * padding - weekdayColumnWidth) / squareSize).toInt()

private fun weekdayDisplayName(weekday: DayOfWeek): String {
if (shouldUseNarrowWeekdayName()) {
return dateFormatter.narrowWeekdayName(weekday)
}
return dateFormatter.shortWeekdayName(weekday)
}

private fun weekdayDisplayName(localDate: LocalDate): String {
if (shouldUseNarrowWeekdayName()) {
return dateFormatter.narrowWeekdayName(localDate)
}
return dateFormatter.shortWeekdayName(localDate)
}

private fun calculateWeekdayColumnWidth(canvas: Canvas): Double {
if (shouldUseNarrowWeekdayName()) {
return 0.0
}
return DayOfWeek.values().map { weekday ->
canvas.measureText(weekdayDisplayName(weekday)) + 1.5
}.maxOrNull() ?: 0.0
}

private fun shouldUseNarrowWeekdayName(): Boolean {
// based off 80dp minWidth in widget_history_info.xml
return width < 80
}
private fun drawColumn(
canvas: Canvas,
column: Int,
Expand Down