Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set custom DateTime format for smartspace #4500

Draft
wants to merge 3 commits into
base: 15-dev
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions lawnchair/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,16 @@
<!-- Date formats -->
<string name="smartspace_calendar_gregorian">Gregorian</string>
<string name="smartspace_calendar_persian">Persian</string>
<string name="smartspace_calendar_custom">Custom</string>

<string name="generic_smartspace_concatenated_desc">%1$s, %2$s</string>
<string name="smartspace_icu_date_pattern_gregorian_wday_month_day_no_year" translatable="false">EEEMMMd</string>
<string name="smartspace_icu_date_pattern_gregorian_time" translatable="false">HH:mm</string>
<string name="smartspace_icu_date_pattern_gregorian_time_12h" translatable="false">hh:mm aa</string>
<string name="smartspace_icu_date_pattern_gregorian_date" translatable="false"> dd MMMM</string>
<string name="smartspace_icu_date_pattern_custom_wday_month_day_no_year" translatable="false">EEEMMMd</string>
<string name="smartspace_icu_date_pattern_custom_time" translatable="false">HH:mm</string>
<string name="smartspace_icu_date_pattern_custom_date" translatable="false">dd MMMM</string>
<string name="smartspace_icu_date_pattern_persian_wday_month_day_no_year" translatable="false">l، j F</string>
<string name="smartspace_icu_date_pattern_persian_time" translatable="false">H:i</string>
<string name="smartspace_icu_date_pattern_persian_time_12h" translatable="false">g:i a</string>
Expand All @@ -343,12 +347,15 @@
<!-- Data and time format settings -->
<string name="smartspace_calendar">Calendar</string>
<string name="smartspace_date_and_time">Date &amp; time</string>
<string name="smartspace_custom_date_header">Custom Date</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_time_custom_format">Custom Time format</string>
<string name="smartspace_date_custom_format">Custom Date format</string>

<!-- Available targets -->
<string name="smartspace_weather">Weather</string>
Expand Down
15 changes: 15 additions & 0 deletions lawnchair/src/app/lawnchair/preferences2/PreferenceManager2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,21 @@ class PreferenceManager2 private constructor(private val context: Context) : Pre
save = { it.toString() },
)

val smartspaceCustomTimeFormat = preference(
key = stringPreferencesKey(name = "smartspace_custom_time_format"),
defaultValue = context.getString(R.string.smartspace_icu_date_pattern_custom_time),
)

val smartspaceCustomDate = preference(
key = stringPreferencesKey(name = "smartspace_custom_date"),
defaultValue = context.getString(R.string.smartspace_icu_date_pattern_custom_date),
)

val smartspaceCustomDateWithoutYear = preference(
key = stringPreferencesKey(name = "smartspace_custom_date_wmd"),
defaultValue = context.getString(R.string.smartspace_icu_date_pattern_custom_wday_month_day_no_year),
)

val smartspacerMaxCount = preference(
key = intPreferencesKey(name = "smartspace_max_count"),
defaultValue = 5,
Expand Down
23 changes: 23 additions & 0 deletions lawnchair/src/app/lawnchair/smartspace/IcuDateTextView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import android.icu.text.DisplayContext
import android.os.SystemClock
import android.text.format.DateFormat.is24HourFormat
import android.util.AttributeSet
import android.util.Log
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.firstBlocking
import app.lawnchair.util.repeatOnAttached
import app.lawnchair.util.subscribeBlocking
import com.android.launcher3.R
Expand Down Expand Up @@ -95,12 +97,33 @@ class IcuDateTextView @JvmOverloads constructor(
}
val formatter = when (calendar) {
SmartspaceCalendar.Persian -> createPersianFormatter()
SmartspaceCalendar.Custom -> createCustomFormatter()
else -> createGregorianFormatter()
}
formatterFunction = formatter
return formatter
}

private fun createCustomFormatter(): FormatterFunction {
val TAG = "createCustomFormatter"

var format: String
if (dateTimeOptions.showTime) {
format = prefs.smartspaceCustomTimeFormat.get().firstBlocking()
if (dateTimeOptions.showDate) format = prefs.smartspaceCustomDate.get().firstBlocking() + format
} else {
format = prefs.smartspaceCustomDateWithoutYear.get().firstBlocking()
}
try {
val formatter = DateFormat.getInstanceForSkeleton(format, Locale.getDefault())
formatter.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE)
return { formatter.format(it) }
} catch(T: Throwable) {
Log.w(TAG, "Fallback to Gregorian formatter", T)
return createGregorianFormatter()
}
}

private fun createPersianFormatter(): FormatterFunction {
var format: String
if (dateTimeOptions.showTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ sealed class SmartspaceCalendar(@StringRes val nameResourceId: Int, val formatCu

fun fromString(value: String): SmartspaceCalendar = when (value) {
"persian" -> Persian
"custom" -> Custom
else -> Gregorian
}

/**
* @return The list of all calendars.
*/
fun values() = listOf(Gregorian, Persian)
fun values() = listOf(Gregorian, Persian, Custom)
}

object Gregorian : SmartspaceCalendar(nameResourceId = R.string.smartspace_calendar_gregorian) {
Expand All @@ -29,4 +30,7 @@ sealed class SmartspaceCalendar(@StringRes val nameResourceId: Int, val formatCu
object Persian : SmartspaceCalendar(nameResourceId = R.string.smartspace_calendar_persian) {
override fun toString() = "persian"
}
object Custom : SmartspaceCalendar(nameResourceId = R.string.smartspace_calendar_custom) {
override fun toString() = "custom"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ sealed class SmartspaceTimeFormat(@StringRes val nameResourceId: Int) {
fun fromString(value: String): SmartspaceTimeFormat = when (value) {
"12_hour_format" -> TwelveHourFormat
"24_hour_format" -> TwentyFourHourFormat
"custom" -> CustomHourFormat
else -> FollowSystem
}

Expand All @@ -36,4 +37,10 @@ sealed class SmartspaceTimeFormat(@StringRes val nameResourceId: Int) {
) {
override fun toString() = "24_hour_format"
}

object CustomHourFormat : SmartspaceTimeFormat(
nameResourceId = R.string.smartspace_time_custom_format
) {
override fun toString() = "custom"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fun PreferenceGroup(
dividerStartIndent: Dp = 0.dp,
dividerEndIndent: Dp = 0.dp,
dividersToSkip: Int = 0,
elevation: Dp = 1.dp,
content: @Composable () -> Unit,
) {
Column(
Expand All @@ -53,7 +54,7 @@ fun PreferenceGroup(
Surface(
modifier = Modifier.padding(horizontal = 16.dp),
shape = MaterialTheme.shapes.large,
tonalElevation = 1.dp,
tonalElevation = elevation,
) {
if (showDividers) {
DividerColumn(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package app.lawnchair.ui.preferences.destinations

import android.app.Activity
import android.icu.text.DateFormat
import android.icu.text.DisplayContext
import android.util.Log
import android.view.ContextThemeWrapper
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
Expand Down Expand Up @@ -34,6 +37,7 @@ import app.lawnchair.ui.preferences.components.controls.ListPreferenceEntry
import app.lawnchair.ui.preferences.components.controls.MainSwitchPreference
import app.lawnchair.ui.preferences.components.controls.SliderPreference
import app.lawnchair.ui.preferences.components.controls.SwitchPreference
import app.lawnchair.ui.preferences.components.controls.TextPreference
import app.lawnchair.ui.preferences.components.layout.DividerColumn
import app.lawnchair.ui.preferences.components.layout.ExpandAndShrink
import app.lawnchair.ui.preferences.components.layout.PreferenceGroup
Expand Down Expand Up @@ -217,6 +221,13 @@ fun SmartspaceDateAndTimePreferences(
ExpandAndShrink(visible = calendarSelectionEnabled.state.value && showDateAdapter.state.value) {
SmartspaceCalendarPreference()
}
val smartspaceCalendar = preferenceManager2().smartspaceCalendar.getAdapter()
ExpandAndShrink(visible = smartspaceCalendar.state.value is SmartspaceCalendar.Custom) {
SmartspaceCustomDateTimePreference()
// TODO: wtf is this layout!!
val TAG = "SmartspacePrefCustom"
Log.w(TAG, "Not supposed to be here?")
}
SwitchPreference(
adapter = showTimeAdapter,
label = stringResource(id = R.string.smartspace_time),
Expand All @@ -230,6 +241,45 @@ fun SmartspaceDateAndTimePreferences(
}
}

@Composable
fun SmartspaceCustomDateTimePreference(
modifier: Modifier = Modifier
) {
PreferenceGroup(
heading = stringResource(id = R.string.smartspace_custom_date_header),
modifier = Modifier,
elevation = 2.dp,
) {
val preferenceManager2 = preferenceManager2()
val TAG = "SmartspaceCustomDateTimePref"

//val date = preferenceManager2.smartspaceCustomDate.getAdapter().state.value
//val formatter = DateTimeFormatter.ofPattern(date)
var result: String = "blah" // formatter.format(LocalDate.now())

// TODO
Log.w(TAG, "Live result is not implemented")

TextPreference(
adapter = preferenceManager2.smartspaceCustomTimeFormat.getAdapter(),
label = stringResource(id = R.string.smartspace_time_custom_format),
modifier = modifier,
)
result = "Not implemented"
TextPreference(
adapter = preferenceManager2.smartspaceCustomDate.getAdapter(),
label = stringResource(id = R.string.smartspace_date_custom_format),
modifier = modifier,
description = { "Format: $result" },
)
TextPreference(
adapter = preferenceManager2.smartspaceCustomDateWithoutYear.getAdapter(),
label = stringResource(id = R.string.smartspace_time_custom_format),
modifier = modifier,
)
}
}

@Composable
fun SmartspaceTimeFormatPreference(
modifier: Modifier = Modifier,
Expand Down Expand Up @@ -260,10 +310,10 @@ fun SmartspaceCalendarPreference(
}.toPersistentList()
}

val adapter = preferenceManager2().smartspaceCalendar.getAdapter()
val smartspaceCalendar = preferenceManager2().smartspaceCalendar.getAdapter()

ListPreference(
adapter = adapter,
adapter = smartspaceCalendar,
entries = entries,
label = stringResource(id = R.string.smartspace_calendar),
modifier = modifier,
Expand Down
Loading