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

Commit d5041211 authored by Caitlin Shkuratov's avatar Caitlin Shkuratov
Browse files

[SB][Chips] Fix up the ShortTimeDelta Compose Chips.

Two changes:
1) I only just realized that the ShortTimeDelta time is in
   currentTimeMillis units instead of elapsedRealtimeUnits, so I updated
   the Compose view to use currentTimeMillis as the baseline.
2) Update TimeRemainingState to use the right duration from the outset
   so that the chip doesn't flash "now" for a brief moment.

Fixes: 400784488
Bug: 372657935
Flag: com.android.systemui.status_bar_chips_modernization
Test: Trigger notif chip with time in the future -> verify chip shows
the time delta correctly. Verify with ChipsModernization both off and on

Change-Id: Id1729675205b7add3275527f94999b25cda5c388
parent 96b5ce2f
Loading
Loading
Loading
Loading
+11 −4
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.systemui.statusbar.chips.ui.model

import android.annotation.CurrentTimeMillisLong
import android.annotation.ElapsedRealtimeLong
import android.view.View
import com.android.systemui.animation.Expandable
import com.android.systemui.common.shared.model.ContentDescription
@@ -102,7 +104,7 @@ sealed class OngoingActivityChipModel {
             * [ChipChronometer] is based off of elapsed realtime. See
             * [android.widget.Chronometer.setBase].
             */
            val startTimeMs: Long,
            @ElapsedRealtimeLong val startTimeMs: Long,
            override val onClickListenerLegacy: View.OnClickListener?,
            override val clickBehavior: ClickBehavior,
            override val isHidden: Boolean = false,
@@ -129,10 +131,15 @@ sealed class OngoingActivityChipModel {
            override val icon: ChipIcon,
            override val colors: ColorsModel,
            /**
             * The time of the event that this chip represents, relative to
             * [com.android.systemui.util.time.SystemClock.currentTimeMillis].
             * The time of the event that this chip represents. Relative to
             * [com.android.systemui.util.time.SystemClock.currentTimeMillis] because that's what's
             * required by [android.widget.DateTimeView].
             *
             * TODO(b/372657935): When the Compose chips are launched, we should convert this to be
             *   relative to [com.android.systemui.util.time.SystemClock.elapsedRealtime] so that
             *   this model and the [Timer] model use the same units.
             */
            val time: Long,
            @CurrentTimeMillisLong val time: Long,
            override val onClickListenerLegacy: View.OnClickListener?,
            override val clickBehavior: ClickBehavior,
            override val isHidden: Boolean = false,
+21 −4
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.systemui.statusbar.chips.ui.viewmodel

import android.os.SystemClock
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
@@ -39,7 +38,14 @@ import kotlinx.coroutines.delay
 * Manages state and updates for the duration remaining between now and a given time in the future.
 */
class TimeRemainingState(private val timeSource: TimeSource, private val futureTimeMillis: Long) {
    private var durationRemaining by mutableStateOf(Duration.ZERO)
    // Start with the right duration from the outset so we don't use "now" as an initial value.
    private var durationRemaining by
        mutableStateOf(
            calculateDurationRemaining(
                currentTimeMillis = timeSource.getCurrentTime(),
                futureTimeMillis = futureTimeMillis,
            )
        )
    private var startTimeMillis: Long = 0

    /**
@@ -56,7 +62,11 @@ class TimeRemainingState(private val timeSource: TimeSource, private val futureT
        while (true) {
            val currentTime = timeSource.getCurrentTime()
            durationRemaining =
                (futureTimeMillis - currentTime).toDuration(DurationUnit.MILLISECONDS)
                calculateDurationRemaining(
                    currentTimeMillis = currentTime,
                    futureTimeMillis = futureTimeMillis,
                )

            // No need to update if duration is more than 1 minute in the past. Because, we will
            // stop displaying anything.
            if (durationRemaining.inWholeMilliseconds < -1.minutes.inWholeMilliseconds) {
@@ -67,6 +77,13 @@ class TimeRemainingState(private val timeSource: TimeSource, private val futureT
        }
    }

    private fun calculateDurationRemaining(
        currentTimeMillis: Long,
        futureTimeMillis: Long,
    ): Duration {
        return (futureTimeMillis - currentTimeMillis).toDuration(DurationUnit.MILLISECONDS)
    }

    private fun calculateNextUpdateDelay(duration: Duration): Long {
        val durationAbsolute = duration.absoluteValue
        return when {
@@ -85,7 +102,7 @@ class TimeRemainingState(private val timeSource: TimeSource, private val futureT
@Composable
fun rememberTimeRemainingState(
    futureTimeMillis: Long,
    timeSource: TimeSource = remember { TimeSource { SystemClock.elapsedRealtime() } },
    timeSource: TimeSource = remember { TimeSource { System.currentTimeMillis() } },
): TimeRemainingState {

    val state =