From b7bcfe8d2818c9a67e95a98a6745677fbbfac7e2 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 21 Feb 2017 22:17:46 +0100 Subject: [PATCH] use selected hour format at weekly view --- .../calendar/activities/MainActivity.kt | 14 ++++++++++---- .../calendar/helpers/Formatter.kt | 9 ++++++++- .../main/res/layout/weekly_view_hour_textview.xml | 6 +++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt index 2dd679548..c57794272 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -49,6 +49,7 @@ class MainActivity : SimpleActivity(), NavigationListener { private var mStoredBackgroundColor = 0 private var mStoredPrimaryColor = 0 private var mStoredIsSundayFirst = false + private var mStoredUse24HourFormat = false private var mShouldFilterBeVisible = false private var mDefaultWeeklyPage = 0 @@ -82,8 +83,11 @@ class MainActivity : SimpleActivity(), NavigationListener { mStoredPrimaryColor = config.primaryColor mStoredBackgroundColor = config.backgroundColor - if (mStoredIsSundayFirst != config.isSundayFirst && config.storedView == WEEKLY_VIEW) - fillWeeklyViewPager() + if (config.storedView == WEEKLY_VIEW) { + if (mStoredIsSundayFirst != config.isSundayFirst || mStoredUse24HourFormat != config.use24hourFormat) { + fillWeeklyViewPager() + } + } updateWidgets() updateTextColors(calendar_coordinator) @@ -95,6 +99,7 @@ class MainActivity : SimpleActivity(), NavigationListener { mStoredIsSundayFirst = config.isSundayFirst mStoredBackgroundColor = config.backgroundColor mStoredPrimaryColor = config.primaryColor + mStoredUse24HourFormat = config.use24hourFormat } override fun onDestroy() { @@ -288,10 +293,11 @@ class MainActivity : SimpleActivity(), NavigationListener { main_weekly_scrollview.beVisible() week_view_hours_holder.removeAllViews() + val hourDateTime = DateTime().withTime(0, 0, 0, 0) for (i in 1..23) { - val value = i.toString() + val formattedHours = Formatter.getHours(this, hourDateTime.withHourOfDay(i)) (layoutInflater.inflate(R.layout.weekly_view_hour_textview, null, false) as TextView).apply { - text = if (value.length == 2) value else "0$value" + text = formattedHours setTextColor(mStoredTextColor) week_view_hours_holder.addView(this) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Formatter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Formatter.kt index da43b1e57..f0cab8cb8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Formatter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Formatter.kt @@ -16,6 +16,9 @@ object Formatter { private val PATTERN_TIME_12 = "hh:mm a" private val PATTERN_TIME_24 = "HH:mm" + private val PATTERN_HOURS_12 = "h a" + private val PATTERN_HOURS_24 = "HH" + fun getDateFromCode(context: Context, dayCode: String, shortMonth: Boolean = false): String { val dateTime = getDateTimeFromCode(dayCode) val day = dateTime.toString(DAY_PATTERN) @@ -23,7 +26,7 @@ object Formatter { val monthIndex = Integer.valueOf(dayCode.substring(4, 6))!! var month = getMonthName(context, monthIndex) if (shortMonth) - month = month.substring(0, Math.min(month.length, 3)) + month = month.substring(0, Math.min(month.length, 3)) var date = "$month $day" if (year != DateTime().toString(YEAR_PATTERN)) date += " $year" @@ -42,6 +45,8 @@ object Formatter { fun getDate(context: Context, dateTime: DateTime, addDayOfWeek: Boolean = true) = getDayTitle(context, getDayCodeFromDateTime(dateTime), addDayOfWeek) + fun getHours(context: Context, dateTime: DateTime) = dateTime.toString(getHourPattern(context)) + fun getTime(context: Context, dateTime: DateTime) = dateTime.toString(getTimePattern(context)) fun getDateTimeFromCode(dayCode: String) = DateTimeFormat.forPattern(DAYCODE_PATTERN).withZone(DateTimeZone.UTC).parseDateTime(dayCode) @@ -63,5 +68,7 @@ object Formatter { // use manually translated month names, as DateFormat and Joda have issues with a lot of languages fun getMonthName(context: Context, id: Int) = context.resources.getStringArray(R.array.months)[id - 1] + fun getHourPattern(context: Context) = if (context.config.use24hourFormat) PATTERN_HOURS_24 else PATTERN_HOURS_12 + fun getTimePattern(context: Context) = if (context.config.use24hourFormat) PATTERN_TIME_24 else PATTERN_TIME_12 } diff --git a/app/src/main/res/layout/weekly_view_hour_textview.xml b/app/src/main/res/layout/weekly_view_hour_textview.xml index 5c2f1f9d6..eee8a9950 100644 --- a/app/src/main/res/layout/weekly_view_hour_textview.xml +++ b/app/src/main/res/layout/weekly_view_hour_textview.xml @@ -3,8 +3,8 @@ xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" - android:gravity="bottom" + android:gravity="bottom|center_horizontal" android:minHeight="@dimen/weekly_view_row_height" - android:paddingLeft="@dimen/medium_margin" - android:paddingRight="@dimen/medium_margin" + android:paddingLeft="@dimen/small_margin" + android:paddingRight="@dimen/small_margin" android:textSize="@dimen/normal_text_size"/>