Remote Compose Storybook Русский GitHub

Time & configuration · Clock

A ticking clock with no new documents.

Time & configuration · Clock: the document drawn by the Remote Compose player
Open the live, interactive demo →

Change its controls, read the remote document it produces and the diff after every change.

What it is

Things only the device knows: the current time and date, and the screen's size, density and text size.

Good for

  • Clocks and countdowns
  • Layouts that adapt to the screen
  • Respecting the user's font size

Code

The story's source with androidx.compose.remote imports. CI compiles every snippet on this site against AndroidX.

import androidx.compose.remote.creation.compose.layout.RemoteAlignment
import androidx.compose.remote.creation.compose.layout.RemoteArrangement
import androidx.compose.remote.creation.compose.layout.RemoteColumn
import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteText
import androidx.compose.remote.creation.compose.state.RemoteInt
import androidx.compose.remote.creation.compose.state.RemoteString
import androidx.compose.remote.creation.compose.state.RemoteTimeVariables
import androidx.compose.remote.creation.compose.state.rb
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.rf
import androidx.compose.remote.creation.compose.state.ri
import androidx.compose.remote.creation.compose.state.rs
import androidx.compose.remote.creation.compose.state.rsp
import androidx.compose.remote.creation.compose.text.RemoteTimeDefaults
import androidx.compose.runtime.Composable

/** An integer as two digits ("07"): a remote select, evaluated by the player. */
fun twoDigits(value: RemoteInt): RemoteString =
    value.isLessThan(10.ri).select("0".rs + value.toRemoteString(), value.toRemoteString())

@Composable
@RemoteComposable
fun Clock(is24Hour: Boolean) {
    // Time is read by the player, so the clock ticks with no new document.
    val time = RemoteTimeVariables
    val hh = twoDigits(time.hourOfDay)
    val mm = twoDigits(time.minuteOfDay % 60.ri)
    val ss = twoDigits(time.secondOfHour % 60.ri)
    RemoteColumn(
        horizontalAlignment = RemoteAlignment.CenterHorizontally,
        verticalArrangement = RemoteArrangement.spacedBy(4.rdp),
    ) {
        RemoteText(
            RemoteTimeDefaults.defaultTimeString(is24HourFormat = is24Hour.rb),
            fontSize = 40.rsp,
        )
        RemoteText(hh + ":".rs + mm + ":".rs + ss, fontSize = 16.rsp)
        // toRemoteString() groups digits as the player's locale does, so a year reads "2,026".
        RemoteText(
            "day ".rs +
                time.dayOfMonth.toRemoteString() +
                " / month ".rs +
                time.month.toRemoteString() +
                " / year ".rs +
                time.year.toRemoteString(),
            fontSize = 14.rsp,
        )
        RemoteText(
            "weekday ".rs +
                time.dayOfWeek.toRemoteString() +
                " · day of year ".rs +
                time.dayOfYear.toRemoteString() +
                " · UTC offset ".rs +
                time.utcOffsetSeconds.toRemoteString() +
                "s".rs,
            fontSize = 12.rsp,
        )
        RemoteText(
            "epoch ".rs +
                time.epochSecond.toRemoteString() +
                " · animation ".rs +
                time.animationTime.toRemoteInt().toRemoteString() +
                "s · frame Δ ".rs +
                (time.animationDeltaTime * 1000f.rf).toRemoteInt().toRemoteString() +
                "ms".rs,
            fontSize = 12.rsp,
        )
    }
}

API

creation.compose.state.RemoteTimeVariables

class RemoteTimeVariables
  RemoteFloat animationDeltaTime
  RemoteFloat animationTime
  RemoteFloat continuousSeconds
  RemoteInt dayOfMonth
  RemoteInt dayOfWeek
  RemoteInt dayOfYear
  RemoteInt epochSecond
  RemoteInt hourOfDay
  RemoteInt minuteOfDay
  RemoteInt month
  RemoteInt secondOfHour
  RemoteInt utcOffsetSeconds
  RemoteInt year
  RemoteTimeVariables INSTANCE

creation.compose.state.RemoteConfiguration

class RemoteConfiguration
  RemoteFloat density
  RemoteDp fontSize
  RemoteFloat fontSizePx
  RemoteDp windowHeight
  RemoteFloat windowHeightPx
  RemoteDp windowWidth
  RemoteFloat windowWidthPx
  RemoteConfiguration INSTANCE

creation.compose.text.RemoteTimeDefaults

class RemoteTimeDefaults
  RemoteString defaultTimeString(optional RemoteBoolean is24HourFormat)
  RemoteBoolean is24HourFormat()
  RemoteTimeDefaults INSTANCE

Reference

Other stories of this component

More in State & expressions