From 095f99b95c3da238fb30934bc1aaad161a1389ca Mon Sep 17 00:00:00 2001 From: Yasan Ghaffarian Date: Wed, 27 Jul 2022 15:40:51 +0430 Subject: [PATCH] Allow smartspace time to follow the system format Co-authored-by: Daria Hamrah Paytakht --- lawnchair/res/values/config.xml | 4 +- lawnchair/res/values/strings.xml | 3 ++ .../preferences2/PreferenceManager2.kt | 9 +++-- .../lawnchair/smartspace/IcuDateTextView.kt | 31 +++++++++----- .../smartspace/model/SmartspaceTimeFormat.kt | 40 +++++++++++++++++++ .../ui/preferences/SmartspacePreferences.kt | 26 +++++++++--- 6 files changed, 95 insertions(+), 18 deletions(-) create mode 100644 lawnchair/src/app/lawnchair/smartspace/model/SmartspaceTimeFormat.kt diff --git a/lawnchair/res/values/config.xml b/lawnchair/res/values/config.xml index a86658ff37..f85c7ee8f1 100644 --- a/lawnchair/res/values/config.xml +++ b/lawnchair/res/values/config.xml @@ -53,6 +53,9 @@ app_icon + + system + false true false @@ -78,7 +81,6 @@ false true false - false 1.0 1.0 diff --git a/lawnchair/res/values/strings.xml b/lawnchair/res/values/strings.xml index 4df3e92879..e250769934 100644 --- a/lawnchair/res/values/strings.xml +++ b/lawnchair/res/values/strings.xml @@ -282,6 +282,9 @@ Date & Time Date Time + Time Format + Follow System + 12-Hour Format 24-Hour Format Weather Battery Status diff --git a/lawnchair/src/app/lawnchair/preferences2/PreferenceManager2.kt b/lawnchair/src/app/lawnchair/preferences2/PreferenceManager2.kt index 3a7baa700f..ffd2328ad2 100644 --- a/lawnchair/src/app/lawnchair/preferences2/PreferenceManager2.kt +++ b/lawnchair/src/app/lawnchair/preferences2/PreferenceManager2.kt @@ -29,6 +29,7 @@ import app.lawnchair.icons.shape.IconShape import app.lawnchair.icons.shape.IconShapeManager import app.lawnchair.qsb.providers.QsbSearchProvider import app.lawnchair.smartspace.model.SmartspaceCalendar +import app.lawnchair.smartspace.model.SmartspaceTimeFormat import app.lawnchair.theme.color.ColorOption import com.android.launcher3.InvariantDeviceProfile import com.android.launcher3.LauncherAppState @@ -313,9 +314,11 @@ class PreferenceManager2(private val context: Context) : PreferenceManager { defaultValue = context.resources.getBoolean(R.bool.config_default_smartspace_show_time), ) - val smartspace24HourFormat = preference( - key = booleanPreferencesKey("smartspace_24_hour_format"), - defaultValue = context.resources.getBoolean(R.bool.config_default_smartspace_24_hour_format), + val smartspaceTimeFormat = preference( + key = stringPreferencesKey("smartspace_time_format"), + defaultValue = SmartspaceTimeFormat.fromString(context.getString(R.string.config_default_smartspace_time_format)), + parse = { SmartspaceTimeFormat.fromString(it) }, + save = { it.toString() }, ) val smartspaceCalendar = preference( diff --git a/lawnchair/src/app/lawnchair/smartspace/IcuDateTextView.kt b/lawnchair/src/app/lawnchair/smartspace/IcuDateTextView.kt index 7f669b7a93..4cd094194d 100644 --- a/lawnchair/src/app/lawnchair/smartspace/IcuDateTextView.kt +++ b/lawnchair/src/app/lawnchair/smartspace/IcuDateTextView.kt @@ -6,9 +6,11 @@ import android.content.IntentFilter import android.icu.text.DateFormat import android.icu.text.DisplayContext import android.os.SystemClock +import android.text.format.DateFormat.is24HourFormat import android.util.AttributeSet import app.lawnchair.preferences2.PreferenceManager2 import app.lawnchair.smartspace.model.SmartspaceCalendar +import app.lawnchair.smartspace.model.SmartspaceTimeFormat import app.lawnchair.util.broadcastReceiverFlow import app.lawnchair.util.repeatOnAttached import app.lawnchair.util.subscribeBlocking @@ -71,7 +73,10 @@ class IcuDateTextView @JvmOverloads constructor( } private fun shouldAlignToTextEnd(): Boolean { - val shouldNotAlignToEnd = dateTimeOptions.showTime && dateTimeOptions.time24HourFormat && !dateTimeOptions.showDate + val is24HourFormatManual = dateTimeOptions.timeFormat is SmartspaceTimeFormat.TwentyFourHourFormat + val is24HourFormatOnSystem = dateTimeOptions.timeFormat is SmartspaceTimeFormat.FollowSystem && is24HourFormat(context) + val is24HourFormat = is24HourFormatManual || is24HourFormatOnSystem + val shouldNotAlignToEnd = dateTimeOptions.showTime && is24HourFormat && !dateTimeOptions.showDate return calendar == SmartspaceCalendar.Persian && !shouldNotAlignToEnd } @@ -96,8 +101,12 @@ class IcuDateTextView @JvmOverloads constructor( var format: String if (dateTimeOptions.showTime) { format = context.getString( - if (dateTimeOptions.time24HourFormat) R.string.smartspace_icu_date_pattern_persian_time - else R.string.smartspace_icu_date_pattern_persian_time_12h + when { + dateTimeOptions.timeFormat is SmartspaceTimeFormat.TwelveHourFormat -> R.string.smartspace_icu_date_pattern_persian_time_12h + dateTimeOptions.timeFormat is SmartspaceTimeFormat.TwentyFourHourFormat -> R.string.smartspace_icu_date_pattern_persian_time + is24HourFormat(context) -> R.string.smartspace_icu_date_pattern_persian_time + else -> R.string.smartspace_icu_date_pattern_persian_time_12h + } ) if (dateTimeOptions.showDate) format = context.getString(R.string.smartspace_icu_date_pattern_persian_date) + format } else { @@ -111,8 +120,12 @@ class IcuDateTextView @JvmOverloads constructor( var format: String if (dateTimeOptions.showTime) { format = context.getString( - if (dateTimeOptions.time24HourFormat) R.string.smartspace_icu_date_pattern_gregorian_time - else R.string.smartspace_icu_date_pattern_gregorian_time_12h + when { + dateTimeOptions.timeFormat is SmartspaceTimeFormat.TwelveHourFormat -> R.string.smartspace_icu_date_pattern_gregorian_time_12h + dateTimeOptions.timeFormat is SmartspaceTimeFormat.TwentyFourHourFormat -> R.string.smartspace_icu_date_pattern_gregorian_time + is24HourFormat(context) -> R.string.smartspace_icu_date_pattern_gregorian_time + else -> R.string.smartspace_icu_date_pattern_gregorian_time_12h + } ) if (dateTimeOptions.showDate) format += context.getString(R.string.smartspace_icu_date_pattern_gregorian_date) } else { @@ -141,16 +154,16 @@ class IcuDateTextView @JvmOverloads constructor( data class DateTimeOptions( val showDate: Boolean, val showTime: Boolean, - val time24HourFormat: Boolean, + val timeFormat: SmartspaceTimeFormat, ) { companion object { fun fromPrefs(prefs: PreferenceManager2) = combine( prefs.smartspaceShowDate.get(), prefs.smartspaceShowTime.get(), - prefs.smartspace24HourFormat.get() - ) { showDate, showTime, time24HourFormat -> - DateTimeOptions(showDate, showTime, time24HourFormat) + prefs.smartspaceTimeFormat.get(), + ) { showDate, showTime, timeFormat -> + DateTimeOptions(showDate, showTime, timeFormat) } } } diff --git a/lawnchair/src/app/lawnchair/smartspace/model/SmartspaceTimeFormat.kt b/lawnchair/src/app/lawnchair/smartspace/model/SmartspaceTimeFormat.kt new file mode 100644 index 0000000000..28e9e78360 --- /dev/null +++ b/lawnchair/src/app/lawnchair/smartspace/model/SmartspaceTimeFormat.kt @@ -0,0 +1,40 @@ +package app.lawnchair.smartspace.model + +import androidx.annotation.StringRes +import com.android.launcher3.R + +open class SmartspaceTimeFormat(@StringRes val nameResourceId: Int) { + + companion object { + + fun fromString(value: String): SmartspaceTimeFormat = when (value) { + "12_hour_format" -> TwelveHourFormat + "24_hour_format" -> TwentyFourHourFormat + else -> FollowSystem + } + + /** + * @return The list of all time format options. + */ + fun values() = listOf(FollowSystem, TwelveHourFormat, TwentyFourHourFormat) + } + + object FollowSystem : SmartspaceTimeFormat( + nameResourceId = R.string.smartspace_time_follow_system, + ) { + override fun toString() = "system" + } + + object TwelveHourFormat : SmartspaceTimeFormat( + nameResourceId = R.string.smartspace_time_12_hour_format, + ) { + override fun toString() = "12_hour_format" + } + + object TwentyFourHourFormat : SmartspaceTimeFormat( + nameResourceId = R.string.smartspace_time_24_hour_format, + ) { + override fun toString() = "24_hour_format" + } + +} \ No newline at end of file diff --git a/lawnchair/src/app/lawnchair/ui/preferences/SmartspacePreferences.kt b/lawnchair/src/app/lawnchair/ui/preferences/SmartspacePreferences.kt index ce7cc54c71..65bf3be6ea 100644 --- a/lawnchair/src/app/lawnchair/ui/preferences/SmartspacePreferences.kt +++ b/lawnchair/src/app/lawnchair/ui/preferences/SmartspacePreferences.kt @@ -22,6 +22,7 @@ import app.lawnchair.preferences.getAdapter import app.lawnchair.preferences2.preferenceManager2 import app.lawnchair.smartspace.SmartspaceViewContainer import app.lawnchair.smartspace.model.SmartspaceCalendar +import app.lawnchair.smartspace.model.SmartspaceTimeFormat import app.lawnchair.smartspace.provider.SmartspaceProvider import app.lawnchair.ui.preferences.components.DividerColumn import app.lawnchair.ui.preferences.components.ExpandAndShrink @@ -123,7 +124,7 @@ fun SmartspaceDateAndTimePreferences() { val calendarAdapter = preferenceManager2.smartspaceCalendar.getAdapter() val showDateAdapter = preferenceManager2.smartspaceShowDate.getAdapter() val showTimeAdapter = preferenceManager2.smartspaceShowTime.getAdapter() - val use24HourFormatAdapter = preferenceManager2.smartspace24HourFormat.getAdapter() + val use24HourFormatAdapter = preferenceManager2.smartspaceTimeFormat.getAdapter() val calendarHasMinimumContent = !showDateAdapter.state.value || !showTimeAdapter.state.value @@ -146,10 +147,7 @@ fun SmartspaceDateAndTimePreferences() { enabled = if (showTimeAdapter.state.value) !calendarHasMinimumContent else true, ) ExpandAndShrink(visible = showTimeAdapter.state.value) { - SwitchPreference( - adapter = use24HourFormatAdapter, - label = stringResource(id = R.string.smartspace_time_24_hour_format), - ) + SmartspaceTimeFormatPreference() } } } @@ -161,6 +159,24 @@ fun SmartspaceDateAndTimePreferences() { } } +@Composable +fun SmartspaceTimeFormatPreference() { + + val entries = remember { + SmartspaceTimeFormat.values().map { format -> + ListPreferenceEntry(format) { stringResource(id = format.nameResourceId) } + } + } + + val adapter = preferenceManager2().smartspaceTimeFormat.getAdapter() + + ListPreference( + adapter = adapter, + entries = entries, + label = stringResource(id = R.string.smartspace_time_format), + ) +} + @Composable fun SmartspaceCalendarPreference() {