Remote Compose Storybook Русский GitHub

Product card

Tap the button: the count updates on the device and the app gets add_to_cart (Actions tab).

Product card: 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 complete shop card built from the blocks on this site, with a working cart button.

Good for

  • E-commerce promos the server can change daily
  • Personalised offers

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.RemoteAlignment
import androidx.compose.remote.creation.compose.layout.RemoteArrangement
import androidx.compose.remote.creation.compose.layout.RemoteBox
import androidx.compose.remote.creation.compose.layout.RemoteColumn
import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteContentScale
import androidx.compose.remote.creation.compose.layout.RemoteImage
import androidx.compose.remote.creation.compose.layout.RemoteText
import androidx.compose.remote.creation.compose.modifier.RemoteModifier
import androidx.compose.remote.creation.compose.modifier.RemoteRole
import androidx.compose.remote.creation.compose.modifier.background
import androidx.compose.remote.creation.compose.modifier.border
import androidx.compose.remote.creation.compose.modifier.clickable
import androidx.compose.remote.creation.compose.modifier.clip
import androidx.compose.remote.creation.compose.modifier.fillMaxWidth
import androidx.compose.remote.creation.compose.modifier.height
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.MutableRemoteInt
import androidx.compose.remote.creation.compose.state.RemoteImageBitmap
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.remote.creation.compose.state.rsp
import androidx.compose.remote.creation.compose.text.RemoteFontWeight
import androidx.compose.remote.creation.compose.text.RemoteTextOverflow
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Canvas
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.Paint

/**
 * A shop's product card: image, title, price, and a cart button whose count lives in the document
 * while every tap also tells the app.
 */
@Composable
@RemoteComposable
fun ProductCard(title: String, price: String, inStock: Boolean, accent: Color) {
    val photo = remember {
        val bitmap = ImageBitmap(160, 120)
        Canvas(bitmap).apply {
            drawRect(0f, 0f, 160f, 120f, Paint().apply { color = Color(0xFFFFF3E0) })
            drawCircle(Offset(80f, 62f), 38f, Paint().apply { color = Color(0xFFFF7043) })
            drawRect(76f, 14f, 84f, 30f, Paint().apply { color = Color(0xFF6D4C41) })
        }
        RemoteImageBitmap(bitmap)
    }
    val inCart = remember { MutableRemoteInt(0) }
    RemoteColumn(
        RemoteModifier.width(220.rdp)
            .clip(RemoteRoundedCornerShape(20.rdp))
            .background(Color.White.rc)
            .border(1.rdp, Color(0xFFE0E0E0).rc, RemoteRoundedCornerShape(20.rdp)),
    ) {
        RemoteImage(
            photo,
            contentDescription = title.rs,
            modifier = RemoteModifier.fillMaxWidth().height(140.rdp),
            contentScale = RemoteContentScale.Crop,
        )
        RemoteColumn(
            RemoteModifier.padding(14.rdp),
            verticalArrangement = RemoteArrangement.spacedBy(6.rdp),
        ) {
            RemoteText(
                title.rs,
                fontSize = 16.rsp,
                fontWeight = RemoteFontWeight.SemiBold,
                maxLines = 1,
                overflow = RemoteTextOverflow.Ellipsis,
            )
            RemoteText(
                price.rs,
                fontSize = 20.rsp,
                fontWeight = RemoteFontWeight.Bold,
                color = accent.rc,
            )
            if (inStock) {
                RemoteBox(
                    RemoteModifier.fillMaxWidth()
                        .clip(RemoteRoundedCornerShape(12.rdp))
                        .background(accent.rc)
                        .clickable(
                            combinedAction(
                                valueChangeAction(inCart, inCart + 1.ri),
                                hostAction("add_to_cart".rs, title.rs),
                            ),
                            role = RemoteRole.Button,
                        )
                        .padding(vertical = 10.rdp),
                    contentAlignment = RemoteAlignment.Center,
                ) {
                    RemoteText(
                        inCart
                            .isGreaterThan(0.ri)
                            .select(
                                "In cart: ".rs + inCart.toRemoteString(),
                                "Add to cart".rs,
                            ),
                        color = Color.White.rc,
                        fontWeight = RemoteFontWeight.Medium,
                    )
                }
            } else {
                RemoteText("Out of stock".rs, color = Color(0xFF9E9E9E).rc)
            }
        }
    }
}

API

creation.compose.layout.RemoteImage

void RemoteImage(RemoteImageBitmap bitmap, RemoteString? contentDescription, optional RemoteModifier modifier, optional RemoteContentScale contentScale, optional RemoteFloat alpha)

creation.compose.modifier.clickable

RemoteModifier clickable(RemoteModifier, RemoteAction onClick, optional boolean enabled, optional RemoteRole? role)

creation.compose.action.combinedAction

RemoteAction combinedAction(RemoteAction... actions)

Reference

More in Recipes