Allow smartspace time to follow the system format

Co-authored-by: Daria Hamrah Paytakht <info@dariarnd.ir>
This commit is contained in:
Yasan Ghaffarian
2022-07-27 15:40:51 +04:30
parent 995f89f9eb
commit 095f99b95c
6 changed files with 95 additions and 18 deletions

View File

@@ -53,6 +53,9 @@
<!-- which notification dot color to use by default -->
<string name="config_default_notification_dot_color" translatable="false">app_icon</string>
<!-- which time format to use by default on smartspace -->
<string name="config_default_smartspace_time_format" translatable="false">system</string>
<bool name="config_default_dark_status_bar">false</bool>
<bool name="config_default_dock_search_bar">true</bool>
<bool name="config_default_show_notification_count">false</bool>
@@ -78,7 +81,6 @@
<bool name="config_default_show_component_names">false</bool>
<bool name="config_default_smartspace_show_date">true</bool>
<bool name="config_default_smartspace_show_time">false</bool>
<bool name="config_default_smartspace_24_hour_format">false</bool>
<item name="config_default_home_icon_size_factor" type="dimen" format="float">1.0</item>
<item name="config_default_folder_preview_background_opacity" type="dimen" format="float">1.0</item>

View File

@@ -282,6 +282,9 @@
<string name="smartspace_date_and_time">Date &amp; Time</string>
<string name="smartspace_date">Date</string>
<string name="smartspace_time">Time</string>
<string name="smartspace_time_format">Time Format</string>
<string name="smartspace_time_follow_system">Follow System</string>
<string name="smartspace_time_12_hour_format">12-Hour Format</string>
<string name="smartspace_time_24_hour_format">24-Hour Format</string>
<string name="smartspace_weather">Weather</string>
<string name="smartspace_battery_status">Battery Status</string>

View File

@@ -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(

View File

@@ -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)
}
}
}

View File

@@ -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"
}
}

View File

@@ -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() {