Remote Compose Storybook Русский GitHub

rememberNamedRemote* · Named values

String, float, colour, boolean, dp and int named values.

rememberNamedRemote* · Named values: 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

Values with a name, such as a title or a colour, that the app showing the screen can change later without the server sending a new design.

Good for

  • Live scores and prices
  • Personalised greetings
  • Theme colours chosen by the app

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.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.modifier.RemoteModifier
import androidx.compose.remote.creation.compose.modifier.background
import androidx.compose.remote.creation.compose.modifier.clip
import androidx.compose.remote.creation.compose.modifier.padding
import androidx.compose.remote.creation.compose.modifier.width
import androidx.compose.remote.creation.compose.shapes.RemoteRoundedCornerShape
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.rememberNamedRemoteBoolean
import androidx.compose.remote.creation.compose.state.rememberNamedRemoteColor
import androidx.compose.remote.creation.compose.state.rememberNamedRemoteDp
import androidx.compose.remote.creation.compose.state.rememberNamedRemoteFloat
import androidx.compose.remote.creation.compose.state.rememberNamedRemoteInt
import androidx.compose.remote.creation.compose.state.rememberNamedRemoteString
import androidx.compose.remote.creation.compose.state.rf
import androidx.compose.remote.creation.compose.state.rs
import androidx.compose.remote.creation.compose.state.rsp
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color

@Composable
@RemoteComposable
fun NamedValues(title: String, scale: Float, accent: Color, showBadge: Boolean) {
    // Named values have defaults in the document; a host can override them by name at play time
    // without a new document.
    val name = rememberNamedRemoteString("title", title)
    val fontScale = rememberNamedRemoteFloat("fontScale", scale.rf)
    val color = rememberNamedRemoteColor("accent", accent.rc)
    val badge = rememberNamedRemoteBoolean("showBadge", showBadge)
    val padding = rememberNamedRemoteDp("padding", 16.rdp)
    val unread = rememberNamedRemoteInt("unread", 3)
    RemoteColumn(
        RemoteModifier.width(280.rdp)
            .clip(RemoteRoundedCornerShape(16.rdp))
            .background(color)
            .padding(padding),
        verticalArrangement = RemoteArrangement.spacedBy(6.rdp),
    ) {
        RemoteText(name, color = Color.White.rc, fontSize = (18f.rf * fontScale).rsp)
        RemoteText(
            badge.select("unread: ".rs + unread.toRemoteString(), "no badge".rs),
            color = Color.White.rc,
        )
    }
}

API

creation.compose.state.rememberNamedRemoteString

RemoteString rememberNamedRemoteString(String name, String defaultValue, optional RemoteState.Domain domain)

creation.compose.state.rememberNamedRemoteFloat

RemoteFloat rememberNamedRemoteFloat(String name, RemoteFloat defaultValue, optional RemoteState.Domain domain)

creation.compose.state.rememberNamedRemoteColor

RemoteColor rememberNamedRemoteColor(String name, RemoteColor defaultValue, optional RemoteState.Domain domain)

creation.compose.state.rememberNamedRemoteBoolean

RemoteBoolean rememberNamedRemoteBoolean(String name, boolean initialValue, optional RemoteState.Domain domain)

creation.compose.state.rememberNamedRemoteInt

RemoteInt rememberNamedRemoteInt(String name, int defaultValue, optional RemoteState.Domain domain)

creation.compose.state.rememberNamedRemoteEnum

inline <reified T extends java.lang.Enum<T>> RemoteEnum<T> rememberNamedRemoteEnum(String name, T initialValue, optional RemoteState.Domain domain)
<T extends java.lang.Enum<T>> RemoteEnum<T> rememberNamedRemoteEnum(String name, T initialValue, enums.EnumEntries<T> enumEntries, optional RemoteState.Domain domain)

Reference

Other stories of this component

More in State & expressions