Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.kotlin
.gradle
**/build/
**/.build/
xcuserdata
!src/**/build/
local.properties
Expand Down
53 changes: 53 additions & 0 deletions cards-android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
alias(libs.plugins.androidLibrary)
alias(libs.plugins.composeMultiplatform)
alias(libs.plugins.composeCompiler)
}

kotlin {
compilerOptions {
jvmTarget = JvmTarget.JVM_11
}
}

dependencies {
// api: CardView's public signature takes a CardEngine directly (see the M-Cards plan's
// "Architecture" section - cards-core has no :core dependency, so this module doesn't
// either), and ThreadwireColors/Typography appear in this module's own composables too.
api(projects.cardsCore)
api(projects.designTokensAndroid)

implementation(libs.compose.runtime)
implementation(libs.compose.foundation)
implementation(libs.compose.material3)
implementation(libs.compose.materialIconsCore)
implementation(libs.compose.ui)
implementation(libs.androidx.lifecycle.runtimeCompose)
// TextBlock/FactSet/etc. render inline markdown, same library :ui-android already uses for
// message text - every title/subtitle/fact value in the source design does too.
implementation(libs.mikepenz.markdown.core)
implementation(libs.mikepenz.markdown.m3)
// Image/Avatar - new dependency, this module's first need for network image loading.
implementation(libs.coil.compose)
implementation(libs.coil.network.okhttp)
// Media (video card) - real playback per the M-Cards plan, not an inert placeholder.
implementation(libs.media3.exoplayer)
implementation(libs.media3.ui)

debugImplementation(libs.compose.uiTooling)
}

android {
namespace = "com.fsk.threadwire.cards.ui"
compileSdk = libs.versions.android.compileSdk.get().toInt()

defaultConfig {
minSdk = libs.versions.android.minSdk.get().toInt()
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.fsk.threadwire.cards.ui

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.fsk.threadwire.cards.CardEngine
import com.fsk.threadwire.cards.schema.ActionStyle
import com.fsk.threadwire.cards.schema.AfterSelection
import com.fsk.threadwire.cards.schema.CardAction
import com.fsk.threadwire.cards.schema.CardElement
import com.fsk.threadwire.designtokens.ThreadwireTheme

/**
* [CardElement.ActionSet.afterSelection] is two independent visual behaviors, not one, matching
* the source design exactly: [AfterSelection.HIDE_UNSELECTED] (`choices`) keeps only the fired
* action's button once one has answered; [AfterSelection.MARK_SELECTED] (`carousel`) keeps every
* button visible and only relabels the fired one via [CardAction.doneTitle]. Each button's
* enabled state is [CardRuntimeState.canInvoke][com.fsk.threadwire.cards.CardRuntimeState.canInvoke]
* directly - that already correctly stays `true` indefinitely for actions with no `doneTitle`
* (rating, choices, carousel, poll all remain re-tappable) and flips to `false` permanently once
* a one-shot (`doneTitle`-bearing) action has fired.
*/
@Composable
internal fun ActionSetView(element: CardElement.ActionSet, engine: CardEngine, modifier: Modifier = Modifier) {
val state by engine.state.collectAsStateWithLifecycle()
val answered = element.actions.firstOrNull { it.id in state.answeredActionIds }

val visibleActions = if (element.afterSelection == AfterSelection.HIDE_UNSELECTED && answered != null) {
listOf(answered)
} else {
element.actions
}

Row(modifier = modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
visibleActions.forEach { action ->
val isAnswered = action.id in state.answeredActionIds
// Local val, not a direct action.doneTitle != null check: smart-casting a nullable
// property declared in a different module (CardAction lives in :cards-core) isn't
// allowed, so the null-check has to happen against a local copy.
val doneTitle = action.doneTitle
val label = if (isAnswered && doneTitle != null) doneTitle else action.title
ActionButton(
label = label,
style = action.style,
enabled = state.canInvoke(action),
onClick = { engine.invoke(action) },
)
}
}
}

@Composable
private fun ActionButton(label: String, style: ActionStyle, enabled: Boolean, onClick: () -> Unit) {
when (style) {
ActionStyle.DESTRUCTIVE -> Button(
onClick = onClick,
enabled = enabled,
colors = ButtonDefaults.buttonColors(containerColor = ThreadwireTheme.colors.destructive),
contentPadding = ButtonDefaults.ContentPadding,
) { Text(label) }

ActionStyle.POSITIVE -> Button(
onClick = onClick,
enabled = enabled,
colors = ButtonDefaults.buttonColors(containerColor = ThreadwireTheme.colors.accent),
) { Text(label) }

ActionStyle.DEFAULT -> OutlinedButton(onClick = onClick, enabled = enabled) { Text(label) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.fsk.threadwire.cards.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.fsk.threadwire.cards.CardEngine
import com.fsk.threadwire.cards.schema.CardElement
import com.fsk.threadwire.cards.schema.ContainerStyle
import com.fsk.threadwire.designtokens.ThreadwireTheme

@Composable
internal fun ContainerView(element: CardElement.Container, engine: CardEngine, modifier: Modifier = Modifier) {
val emphasized = element.style == ContainerStyle.EMPHASIS
Column(
modifier = modifier
.fillMaxWidth()
.then(
if (emphasized) {
Modifier
.background(ThreadwireTheme.colors.surfaceAlt, RoundedCornerShape(12.dp))
.padding(12.dp)
} else {
Modifier
},
),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
element.items.forEach { child -> ElementView(element = child, engine = engine) }
}
}

/** All columns get equal width ([Modifier.weight]`(1f)`) regardless of [CardElement.ColumnWidth] -
* a v1 simplification. Every design example ([weather]'s 3-day forecast) only ever needs equal
* columns; true `AUTO` (size-to-content) sizing is deferred until a real card actually needs it. */
@Composable
internal fun ColumnSetView(element: CardElement.ColumnSet, engine: CardEngine, modifier: Modifier = Modifier) {
Row(modifier = modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(12.dp)) {
element.columns.forEach { column ->
Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(4.dp)) {
column.items.forEach { child -> ElementView(element = child, engine = engine) }
}
}
}
}

@Composable
internal fun CarouselView(element: CardElement.Carousel, engine: CardEngine, modifier: Modifier = Modifier) {
if (element.pages.isEmpty()) return
val pagerState = rememberPagerState(pageCount = { element.pages.size })
HorizontalPager(state = pagerState, modifier = modifier.fillMaxWidth(), pageSpacing = 12.dp) { page ->
ContainerView(
element = element.pages[page].copy(style = ContainerStyle.EMPHASIS),
engine = engine,
)
}
}
Loading