Remote Compose Storybook English GitHub

Time & configuration · Clock

A ticking clock with no new documents.

Time & configuration · Clock: документ, нарисованный плеером Remote Compose
Открыть живое интерактивное демо →

Меняйте параметры, смотрите удалённый документ, который получается, и разницу после каждого изменения.

Что это

То, что знает только устройство: текущее время и дата, размер экрана, плотность и размер шрифта.

Подходит для

  • Часы и обратный отсчёт
  • Раскладки под размер экрана
  • Учёт размера шрифта пользователя

Код

Исходник примера с импортами androidx.compose.remote. CI компилирует каждый фрагмент кода на сайте против 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

Справочник

Другие примеры этого компонента

Ещё в разделе «Состояние и выражения»