Remote Compose Storybook Русский GitHub

hostAction / combinedAction · combinedAction

Change local state and notify the host in one tap.

hostAction / combinedAction · combinedAction: 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

A tap that tells the app around the screen to do something, such as open a page or save a bookmark, optionally while also updating the screen.

Good for

  • Navigation to app screens
  • Like, bookmark and share buttons

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.combinedAction
import androidx.compose.remote.creation.compose.action.hostAction
import androidx.compose.remote.creation.compose.action.valueChangeAction
import androidx.compose.remote.creation.compose.layout.RemoteBox
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.clickable
import androidx.compose.remote.creation.compose.modifier.clip
import androidx.compose.remote.creation.compose.modifier.padding
import androidx.compose.remote.creation.compose.shapes.RemoteRoundedCornerShape
import androidx.compose.remote.creation.compose.state.MutableRemoteBoolean
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
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 CombinedActions(actionName: String) {
    val isBookmarked = remember { MutableRemoteBoolean(false) }
    val next = !isBookmarked
    RemoteBox(
        RemoteModifier.clip(RemoteRoundedCornerShape(12.rdp))
            .background(isBookmarked.select(Color(0xFF2E7D32).rc, Color(0xFF616161).rc))
            // Update local state and tell the host, in one tap.
            .clickable(
                combinedAction(
                    valueChangeAction(isBookmarked, next),
                    hostAction(actionName.rs, next),
                ),
            )
            .padding(16.rdp),
    ) {
        RemoteText(
            isBookmarked.select("Bookmarked".rs, "Tap to bookmark".rs),
            color = Color.White.rc,
        )
    }
}

API

creation.compose.action.hostAction

RemoteAction hostAction(RemoteString name)
RemoteAction hostAction(RemoteString name, RemoteBoolean value)
RemoteAction hostAction(RemoteString name, RemoteFloat value)
RemoteAction hostAction(RemoteString name, RemoteInt value)
RemoteAction hostAction(RemoteString name, RemoteString value)
RemoteAction hostAction(int id, optional RemoteString? metadata)

creation.compose.action.combinedAction

RemoteAction combinedAction(RemoteAction... actions)

creation.compose.action.RemoteAction

interface RemoteAction
  RemoteAction.Companion Companion
  RemoteAction None

Reference

Other stories of this component

More in Interaction