From 93270b90df3ef70fa9b05b495ea9d2af284f98dd Mon Sep 17 00:00:00 2001 From: Cody Henthorne Date: Mon, 13 Dec 2021 10:16:27 -0500 Subject: [PATCH] Fix 24hr time format bug on older OSes. --- .../manual/models/NotificationProfileSelection.kt | 4 ++-- .../EditNotificationProfileScheduleFragment.kt | 11 ++++++----- .../profiles/NotificationProfileDetailsFragment.kt | 4 ++-- .../notifications/profiles/NotificationProfiles.kt | 4 ++-- .../thoughtcrime/securesms/util/JavaTimeExtensions.kt | 11 +++++++++-- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/manual/models/NotificationProfileSelection.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/manual/models/NotificationProfileSelection.kt index a74be7b90f..32bf44e07d 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/manual/models/NotificationProfileSelection.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/manual/models/NotificationProfileSelection.kt @@ -87,7 +87,7 @@ object NotificationProfileSelection { expansion.visible = model.isExpanded timeSlotB.text = context.getString( R.string.NotificationProfileSelection__until_s, - LocalTime.from(model.timeSlotB).formatHours() + LocalTime.from(model.timeSlotB).formatHours(context) ) if (TOGGLE_EXPANSION in payload || UPDATE_TIMESLOT in payload) { @@ -107,7 +107,7 @@ object NotificationProfileSelection { timeSlotB.text = context.getString( R.string.NotificationProfileSelection__until_s, - LocalTime.from(model.timeSlotB).formatHours() + LocalTime.from(model.timeSlotB).formatHours(context) ) itemView.isSelected = model.isOn diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleFragment.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleFragment.kt index 15582f982b..1535365975 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleFragment.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleFragment.kt @@ -1,5 +1,6 @@ package org.thoughtcrime.securesms.components.settings.app.notifications.profiles +import android.content.Context import android.os.Bundle import android.text.Spannable import android.text.SpannableString @@ -105,7 +106,7 @@ class EditNotificationProfileScheduleFragment : LoggingFragment(R.layout.fragmen val days: Map = listOf(day1, day2, day3, day4, day5, day6, day7).zip(Locale.getDefault().orderOfDaysInWeek()).toMap() days.forEach { (view, day) -> - DrawableCompat.setTintList(view.background, ContextCompat.getColorStateList(requireContext(), R.color.notification_profile_schedule_background_tint)) + DrawableCompat.setTintList(view.background, ContextCompat.getColorStateList(view.context, R.color.notification_profile_schedule_background_tint)) view.setOnClickListener { viewModel.toggleDay(day) } view.setText(DAY_TO_STARTING_LETTER[day]!!) } @@ -121,11 +122,11 @@ class EditNotificationProfileScheduleFragment : LoggingFragment(R.layout.fragmen view.isEnabled = schedule.enabled } - startTime.text = schedule.startTime().formatTime() + startTime.text = schedule.startTime().formatTime(view.context) startTime.setOnClickListener { showTimeSelector(true, schedule.startTime()) } startTime.isEnabled = schedule.enabled - endTime.text = schedule.endTime().formatTime() + endTime.text = schedule.endTime().formatTime(view.context) endTime.setOnClickListener { showTimeSelector(false, schedule.endTime()) } endTime.isEnabled = schedule.enabled @@ -168,11 +169,11 @@ class EditNotificationProfileScheduleFragment : LoggingFragment(R.layout.fragmen } } -private fun LocalTime.formatTime(): SpannableString { +private fun LocalTime.formatTime(context: Context): SpannableString { val amPm = DateTimeFormatter.ofPattern("a") .format(this) - val formattedTime: String = this.formatHours() + val formattedTime: String = this.formatHours(context) return SpannableString(formattedTime).apply { val amPmIndex = formattedTime.indexOf(string = amPm, ignoreCase = true) diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/NotificationProfileDetailsFragment.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/NotificationProfileDetailsFragment.kt index 785e8d9549..fffec2b6ba 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/NotificationProfileDetailsFragment.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/NotificationProfileDetailsFragment.kt @@ -234,8 +234,8 @@ class NotificationProfileDetailsFragment : DSLSettingsFragment() { return getString(R.string.NotificationProfileDetails__schedule) } - val startTime = startTime().formatHours() - val endTime = endTime().formatHours() + val startTime = startTime().formatHours(requireContext()) + val endTime = endTime().formatHours(requireContext()) val days = StringBuilder() if (daysEnabled.size == 7) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfiles.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfiles.kt index 407eee2d50..b6ca3ed652 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfiles.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfiles.kt @@ -52,11 +52,11 @@ object NotificationProfiles { if (storeValues.manuallyEnabledUntil.isForever()) { return context.getString(R.string.NotificationProfilesFragment__on) } else if (now < storeValues.manuallyEnabledUntil) { - return context.getString(R.string.NotificationProfileSelection__on_until_s, storeValues.manuallyEnabledUntil.toLocalTime().formatHours()) + return context.getString(R.string.NotificationProfileSelection__on_until_s, storeValues.manuallyEnabledUntil.toLocalTime().formatHours(context)) } } - return context.getString(R.string.NotificationProfileSelection__on_until_s, profile.schedule.endTime().formatHours()) + return context.getString(R.string.NotificationProfileSelection__on_until_s, profile.schedule.endTime().formatHours(context)) } private fun Long.isForever(): Boolean { diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/JavaTimeExtensions.kt b/app/src/main/java/org/thoughtcrime/securesms/util/JavaTimeExtensions.kt index 9c96bd48d6..1e0e3e2a93 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/JavaTimeExtensions.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/util/JavaTimeExtensions.kt @@ -1,5 +1,8 @@ package org.thoughtcrime.securesms.util +import android.content.Context +import android.os.Build +import android.text.format.DateFormat import java.time.DayOfWeek import java.time.Instant import java.time.LocalDateTime @@ -51,8 +54,12 @@ fun Long.toLocalTime(zoneId: ZoneId = ZoneId.systemDefault()): LocalTime { /** * Formats [LocalTime] as localized time. For example, "8:00 AM" */ -fun LocalTime.formatHours(): String { - return DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).format(this) +fun LocalTime.formatHours(context: Context): String { + return if (Build.VERSION.SDK_INT >= 26 || !DateFormat.is24HourFormat(context)) { + DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).format(this) + } else { + DateTimeFormatter.ofPattern("HH:mm", Locale.getDefault()).format(this) + } } /**