Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 55dc7548 authored by Hawkwood Glazier's avatar Hawkwood Glazier Committed by Android (Google) Code Review
Browse files

Merge "Update child formatters for large clock with correct time format" into main

parents 86c77d82 d62b1fac
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -129,7 +129,7 @@ class FlexClockFaceController(
        }

        override fun onLocaleChanged(locale: Locale) {
            timeFormatter.updateLocale(locale)
            timeFormatter.locale = locale
            layerController.events.onLocaleChanged(locale)
        }

+3 −1
Original line number Diff line number Diff line
@@ -123,15 +123,17 @@ open class SimpleDigitalHandLayerController(
            override var isReactiveTouchInteractionEnabled = false

            override fun onLocaleChanged(locale: Locale) {
                timespec.formatter.updateLocale(locale)
                timespec.formatter.locale = locale
                refreshTime()
            }

            override fun onTimeFormatChanged(formatKind: TimeFormatKind) {
                timespec.formatter.formatKind = formatKind
                refreshTime()
            }

            override fun onTimeZoneChanged(timeZone: TimeZone) {
                timespec.formatter.timeKeeper.timeZone = timeZone
                refreshTime()
            }

+26 −14
Original line number Diff line number Diff line
@@ -43,9 +43,11 @@ open class TimeKeeperImpl(private val cal: Calendar = Calendar.getInstance()) :
    override var timeZone: TimeZone
        get() = cal.timeZone
        set(value) {
            if (cal.timeZone != value) {
                cal.timeZone = value
                callbacks.forEach { it.onTimeZoneChanged() }
            }
        }

    override val callbacks = mutableListOf<TimeKeeper.Callback>()

@@ -82,35 +84,45 @@ private fun TimeFormatKind.getContentDescriptionPattern(pattern: String): String

class DigitalTimeFormatter(
    val pattern: String,
    private val timeKeeper: TimeKeeper,
    private val enableContentDescription: Boolean = false,
    val timeKeeper: TimeKeeper,
    val enableContentDescription: Boolean = false,
) : TimeKeeper.Callback {
    var formatKind = TimeFormatKind.HALF_DAY
        set(value) {
            if (field != value) {
                field = value
                applyPattern()
            }
        }

    var locale = Locale.getDefault()
        set(value) {
            if (field != value) {
                field = value
                onLocaleChanged()
            }
        }

    private lateinit var textFormat: SimpleDateFormat
    private var contentDescriptionFormat: SimpleDateFormat? = null

    init {
        timeKeeper.callbacks.add(this)
        updateLocale(Locale.getDefault())
        onLocaleChanged()
    }

    override fun onTimeZoneChanged() {
        textFormat.timeZone = TimeZone.getTimeZone(timeKeeper.timeZone.id)
        contentDescriptionFormat?.timeZone = TimeZone.getTimeZone(timeKeeper.timeZone.id)
        applyPattern()
    }

    fun updateLocale(locale: Locale) {
    fun onLocaleChanged() {
        textFormat = getTextFormat(locale)
        contentDescriptionFormat = getContentDescriptionFormat(locale)
        onTimeZoneChanged()
    }

    override fun onTimeZoneChanged() {
        textFormat.timeZone = timeKeeper.timeZone
        contentDescriptionFormat?.timeZone = timeKeeper.timeZone
        applyPattern()
    }

    private fun getTextFormat(locale: Locale): SimpleDateFormat {
        if (locale.language.equals(Locale.ENGLISH.language)) {
            // force date format in English, and time format to use format defined in json