Remote Compose Storybook Русский GitHub

RemoteStateLayout · Int

Three branches selected by a RemoteInt.

RemoteStateLayout · Int: 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

Holds several versions of a screen — for example loading, content and error — and shows the right one for the current state, switching on the device.

Good for

  • Loading / success / error states
  • Step-by-step flows
  • Collapsed and expanded views

How it works

The state type exists only in your Kotlin, while the document is captured. The device gets an integer and every variant of the screen at once.

  1. MutableRemoteEnum(Screen.Loading) is a MutableRemoteInt holding the ordinal; the enum entries stay in Kotlin. The player never sees Screen or its names.
  2. RemoteStateLayout calls your lambda once per state at capture time. Each call becomes one branch of a StateLayout operation that points at that integer, with its colours, texts and your when already resolved to constants (see the Document tab).
  3. valueChangeAction(screen, RemoteEnum(next)) is written as “set the integer to N”.
  4. On the device the player shows the branch for the current value; a tap changes the value and the branch switches at once, offline, with no server and no Kotlin.

So: switching is instant, but every state adds a branch to the document; logic in the lambda runs once on the server, not on each tap. With rememberNamedRemoteEnum("screen", …) the integer gets a name (USER:screen) and the host app can switch the screen too (Host values tab).

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.action.valueChangeAction
import androidx.compose.remote.creation.compose.layout.RemoteAlignment
import androidx.compose.remote.creation.compose.layout.RemoteBox
import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteStateLayout
import androidx.compose.remote.creation.compose.layout.RemoteText
import androidx.compose.remote.creation.compose.modifier.RemoteModifier
import androidx.compose.remote.creation.compose.modifier.background
import androidx.compose.remote.creation.compose.modifier.clickable
import androidx.compose.remote.creation.compose.modifier.size
import androidx.compose.remote.creation.compose.state.MutableRemoteInt
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.ri
import androidx.compose.remote.creation.compose.state.rs
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color

@Composable
@RemoteComposable
fun StateLayoutInt(initial: Int) {
    val step = remember { MutableRemoteInt(initial) }
    // Each listed state is one branch of the document; the player shows the current one.
    RemoteStateLayout(
        step,
        0,
        1,
        2,
        modifier = RemoteModifier.clickable(valueChangeAction(step, (step + 1.ri) % 3.ri)),
    ) { index ->
        val color =
            when (index) {
                0 -> Color(0xFF1E88E5)
                1 -> Color(0xFFFB8C00)
                else -> Color(0xFF8E24AA)
            }
        RemoteBox(
            RemoteModifier.size(220.rdp, 90.rdp).background(color.rc),
            contentAlignment = RemoteAlignment.Center,
        ) {
            RemoteText("Step $index of 3 — tap for next".rs, color = Color.White.rc)
        }
    }
}

API

creation.compose.layout.RemoteStateLayout

void RemoteStateLayout(RemoteBoolean currentState, optional RemoteModifier modifier, java.lang.Boolean.() -> Unit content)
<T extends java.lang.Enum<T>> void RemoteStateLayout(RemoteEnum<T> currentState, optional RemoteModifier modifier, T.() -> Unit content)
void RemoteStateLayout(RemoteInt currentState, int... states, optional RemoteModifier modifier, java.lang.Integer.() -> Unit content)

creation.compose.state.MutableRemoteBoolean

class MutableRemoteBoolean extends RemoteBoolean implements MutableRemoteState<java.lang.Boolean>
  Boolean? constantValueOrNull
MutableRemoteBoolean MutableRemoteBoolean(boolean initialValue)

creation.compose.state.MutableRemoteInt

class MutableRemoteInt extends RemoteInt implements MutableRemoteState<java.lang.Integer>
MutableRemoteInt MutableRemoteInt(int initialValue)

creation.compose.state.MutableRemoteEnum

class MutableRemoteEnum<T extends java.lang.Enum<T>> extends RemoteEnum<T> implements MutableRemoteState<T>
  MutableRemoteEnum(MutableRemoteInt remoteInt, enums.EnumEntries<T> enumEntries)
  MutableRemoteInt remoteInt
inline <reified T extends java.lang.Enum<T>> MutableRemoteEnum<T> MutableRemoteEnum(T initialValue)

Reference

Other stories of this component

More in State & expressions