# Building a custom UI

> Read content cards from the SDK's observable store, render them in your own views, and log impressions, clicks and dismissals correctly.

Use your own views when cards belong in a carousel, a banner slot or a home screen section instead of a feed. You read the card list from the SDK and report what the user does with each card.

## Before you start

- The SDK is configured and the user is identified.
- `HeraldUI` and `herald-ui` are not needed for a custom UI. The core library is enough.

## 1. Observe the cards

`contentCards` always holds the current user's unexpired, undismissed cards, newest first. It updates after every sync, after every interaction you log, and when a card expires.

:::tabs
::tab ios
```swift
import SwiftUI
import HeraldSDK

struct HomeCards: View {
    @ObservedObject private var store = Herald.shared.contentCards

    var body: some View {
        ScrollView(.horizontal) {
            LazyHStack {
                ForEach(store.cards.filter { !$0.categories.contains("inbox") }) { card in
                    PromoTile(card: card)
                }
            }
        }
    }
}
```
::tab android
```kotlin
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import com.herald.sdk.Herald

@Composable
fun HomeCards() {
    val cards by Herald.contentCards.collectAsState()
    LazyRow {
        items(cards.filter { "inbox" !in it.categories }, key = { it.id }) { card ->
            PromoTile(card)
        }
    }
}
```
:::

Outside SwiftUI, iterate `Herald.shared.contentCards.updates`, an `AsyncStream` that yields the current list at once and then every change. On Android, `Herald.contentCards` is a `StateFlow`, so `collect` it from a coroutine in a `View`-based screen.

The list is in Herald's order. The default feed puts pinned cards first; if you want that, sort with `pinned` before `createdAt`.

## 2. Log what the user does

The default feed logs these for you. A custom UI must log them itself, or the agent cannot learn from your cards.

| Call | When | Rule |
|---|---|---|
| `logImpression(card)` | The card is actually visible to the user | Once per card each time it comes into view on a screen, not on every redraw. Do not log cards that are loaded but off screen. |
| `logClick(card)` | The user taps the card or its call to action | Log before you open the link or navigate. |
| `logDismissal(card)` | The user removes the card | Offer it only when `card.dismissible` is true. |

What each call changes:

- `logImpression` sets `viewed` on the card. The SDK sends at most one impression per card per session on both platforms, so an extra call does no harm.
- `logClick` sets `clicked` on the card.
- `logDismissal` removes the card from `contentCards` at once, remembers the dismissal on the device, and tells Herald, which stops returning the card.
- A call for a card the SDK no longer knows (for example after a sign-out): Android ignores it. iOS still sends the interaction but changes no card state; Herald ignores an interaction for a card that does not belong to the current user.

:::tabs
::tab ios
```swift
struct PromoTile: View {
    let card: ContentCard

    var body: some View {
        VStack(alignment: .leading) {
            Text(card.title ?? "").font(.headline)
            if card.dismissible {
                Button("Not now") { Herald.shared.logDismissal(card) }
            }
        }
        .onAppear { Herald.shared.logImpression(card) }
        .onTapGesture {
            Herald.shared.logClick(card)
            if let url = card.url { Herald.shared.openDeepLink(url) }
        }
    }
}
```
::tab android
```kotlin
@Composable
fun PromoTile(card: ContentCard) {
    LaunchedEffect(card.id) { Herald.logImpression(card) }
    Column(
        Modifier.clickable {
            Herald.logClick(card)
            card.url?.let(Herald::openUrl)
        },
    ) {
        Text(card.title.orEmpty())
        if (card.dismissible) {
            TextButton(onClick = { Herald.logDismissal(card) }) { Text("Not now") }
        }
    }
}
```
:::

`onAppear` and `LaunchedEffect` fire when a lazy row composes an item, which is close to "visible". If your layout composes items well before they scroll into view, measure visibility yourself, as the default feed does (it waits until half the card is on screen).

On iOS, `Herald.shared.openDeepLink(_:)` hands the URL to your `deepLinkHandler` first, then to the system. On Android, `Herald.openUrl(_:)` opens it with an `ACTION_VIEW` intent.

## 3. Refresh

Cards sync at session start and when a user is identified. Call `refreshContentCards()` for pull to refresh or when a screen opens. The request uses an ETag, so frequent calls are cheap when nothing changed.

:::tabs
::tab ios
```swift
List(store.cards) { card in PromoTile(card: card) }
    .refreshable { Herald.shared.refreshContentCards() }
```
::tab android
```kotlin
Button(onClick = { Herald.refreshContentCards() }) { Text("Refresh") }
```
:::

`refreshContentCards()` returns at once. The new list arrives through `contentCards` when the request finishes.

## Verify it works

1. Show a card and scroll it into view. Then tap it.
2. Background the app to send the queue. The card's outcome is credited to the agent's decision in Herald.
3. Dismiss a card and relaunch: it should not return.

## Troubleshooting

- **Cards show but the agent learns nothing.** Impressions and clicks are not logged, or they are logged with a copy of the card from an old list. Always pass the card from the current `contentCards` value.
- **Impressions are far higher than real views.** You log in a place that runs on every redraw. Log on appear, once per card per screen visit.
- **A dismissed card is back after a sync.** You hid it in your own state without calling `logDismissal`.

## Next

- [Pinning, expiry and categories](/docs/content-cards/pinning-expiry-categories)
- [Content cards overview](/docs/content-cards/overview): the full data model.
