Skip to content

Commit 4d2bd99

Browse files
toluo-stripecodex
andcommitted
Add scoped PaymentSheet theme values
Committed-By-Agent: codex Co-authored-by: codex <noreply@openai.com>
1 parent dc307e5 commit 4d2bd99

31 files changed

Lines changed: 643 additions & 202 deletions

payments-ui-core/src/main/java/com/stripe/android/ui/core/elements/MandateTextUI.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import androidx.compose.ui.text.font.FontWeight
1414
import androidx.compose.ui.text.style.TextAlign
1515
import androidx.compose.ui.unit.TextUnit
1616
import androidx.compose.ui.unit.sp
17-
import com.stripe.android.uicore.StripeTheme
1817
import com.stripe.android.uicore.stripeColors
18+
import com.stripe.android.uicore.stripeTypography
1919
import com.stripe.android.uicore.text.EmbeddableImage
2020
import com.stripe.android.uicore.text.Html
2121

@@ -49,7 +49,7 @@ fun Mandate(
4949
style = caption.copy(
5050
textAlign = textAlign,
5151
lineHeight = lineHeight,
52-
fontSize = 11.sp * StripeTheme.typographyMutable.fontSizeMultiplier,
52+
fontSize = 11.sp * MaterialTheme.stripeTypography.fontSizeMultiplier,
5353
fontWeight = FontWeight.Normal
5454
),
5555
modifier = modifier

paymentsheet/src/main/java/com/stripe/android/common/ui/BottomSheetScaffold.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import androidx.compose.foundation.layout.navigationBars
1111
import androidx.compose.foundation.layout.windowInsetsBottomHeight
1212
import androidx.compose.foundation.rememberScrollState
1313
import androidx.compose.foundation.verticalScroll
14+
import androidx.compose.material.MaterialTheme
1415
import androidx.compose.material.Surface
1516
import androidx.compose.runtime.Composable
1617
import androidx.compose.runtime.derivedStateOf
@@ -19,7 +20,7 @@ import androidx.compose.runtime.remember
1920
import androidx.compose.ui.Modifier
2021
import androidx.compose.ui.unit.dp
2122
import androidx.compose.ui.zIndex
22-
import com.stripe.android.uicore.StripeTheme
23+
import com.stripe.android.uicore.stripeFormInsets
2324

2425
@Composable
2526
internal fun BottomSheetScaffold(
@@ -57,7 +58,7 @@ internal fun BottomSheetScaffold(
5758
.imePadding()
5859
.verticalScroll(scrollState)
5960
) {
60-
Spacer(Modifier.height(StripeTheme.formInsets.top.dp))
61+
Spacer(Modifier.height(MaterialTheme.stripeFormInsets.top.dp))
6162
content()
6263
Spacer(modifier = Modifier.windowInsetsBottomHeight(WindowInsets.navigationBars))
6364
}

paymentsheet/src/main/java/com/stripe/android/common/ui/PrimaryButton.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ import androidx.compose.ui.platform.LocalContext
2727
import androidx.compose.ui.res.painterResource
2828
import androidx.compose.ui.text.TextStyle
2929
import androidx.compose.ui.unit.dp
30-
import com.stripe.android.uicore.StripeTheme
3130
import com.stripe.android.uicore.getBackgroundColor
3231
import com.stripe.android.uicore.getBorderStrokeColor
3332
import com.stripe.android.uicore.getComposeTextStyle
3433
import com.stripe.android.uicore.getOnBackgroundColor
34+
import com.stripe.android.uicore.stripePrimaryButtonStyle
3535
import com.stripe.android.ui.core.R as UiCoreR
3636

3737
@Composable
@@ -48,16 +48,17 @@ internal fun PrimaryButton(
4848
// We need to use PaymentsTheme.primaryButtonStyle instead of MaterialTheme
4949
// because of the rules API for primary button.
5050
val context = LocalContext.current
51-
val background = Color(StripeTheme.primaryButtonStyle.getBackgroundColor(context))
52-
val onBackground = Color(StripeTheme.primaryButtonStyle.getOnBackgroundColor(context))
51+
val style = MaterialTheme.stripePrimaryButtonStyle
52+
val background = Color(style.getBackgroundColor(context))
53+
val onBackground = Color(style.getOnBackgroundColor(context))
5354
val borderStroke = BorderStroke(
54-
StripeTheme.primaryButtonStyle.shape.borderStrokeWidth.dp,
55-
Color(StripeTheme.primaryButtonStyle.getBorderStrokeColor(context))
55+
style.shape.borderStrokeWidth.dp,
56+
Color(style.getBorderStrokeColor(context))
5657
)
5758
val shape = RoundedCornerShape(
58-
StripeTheme.primaryButtonStyle.shape.cornerRadius.dp
59+
style.shape.cornerRadius.dp
5960
)
60-
val textStyle = StripeTheme.primaryButtonStyle.getComposeTextStyle()
61+
val textStyle = style.getComposeTextStyle()
6162

6263
CompositionLocalProvider(
6364
LocalContentAlpha provides if (isEnabled) ContentAlpha.high else ContentAlpha.disabled
@@ -71,7 +72,7 @@ internal fun PrimaryButton(
7172
modifier = Modifier
7273
.fillMaxWidth()
7374
.defaultMinSize(
74-
minHeight = StripeTheme.primaryButtonStyle.shape.height.dp
75+
minHeight = style.shape.height.dp
7576
),
7677
enabled = isEnabled,
7778
shape = shape,
@@ -110,7 +111,7 @@ private fun PrimaryButtonContent(
110111
displayLockIcon: Boolean,
111112
) {
112113
val context = LocalContext.current
113-
val onBackground = Color(StripeTheme.primaryButtonStyle.getOnBackgroundColor(context))
114+
val onBackground = Color(MaterialTheme.stripePrimaryButtonStyle.getOnBackgroundColor(context))
114115

115116
BoxWithConstraints(contentAlignment = Alignment.CenterStart) {
116117
Text(
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.stripe.android.paymentsheet
2+
3+
import androidx.compose.material.darkColors
4+
import androidx.compose.material.lightColors
5+
import androidx.compose.ui.graphics.Color
6+
import androidx.compose.ui.unit.sp
7+
import com.stripe.android.paymentelement.AppearanceAPIAdditionsPreview
8+
import com.stripe.android.uicore.FormInsets
9+
import com.stripe.android.uicore.PrimaryButtonColors
10+
import com.stripe.android.uicore.PrimaryButtonShape
11+
import com.stripe.android.uicore.PrimaryButtonStyle
12+
import com.stripe.android.uicore.PrimaryButtonTypography
13+
import com.stripe.android.uicore.StripeColors
14+
import com.stripe.android.uicore.StripeShapes
15+
import com.stripe.android.uicore.StripeThemeDefaults
16+
import com.stripe.android.uicore.StripeTypography
17+
import com.stripe.android.uicore.IconStyle as StripeIconStyle
18+
19+
internal data class PaymentElementThemeValues(
20+
val colorsLight: StripeColors,
21+
val colorsDark: StripeColors,
22+
val shapes: StripeShapes,
23+
val typography: StripeTypography,
24+
val primaryButtonStyle: PrimaryButtonStyle,
25+
val formInsets: FormInsets,
26+
val sectionSpacing: Float?,
27+
val textFieldInsets: FormInsets,
28+
val iconStyle: StripeIconStyle,
29+
val verticalModeRowPadding: Float,
30+
)
31+
32+
@OptIn(AppearanceAPIAdditionsPreview::class)
33+
internal fun PaymentSheet.Appearance.toPaymentElementThemeValues(): PaymentElementThemeValues {
34+
return PaymentElementThemeValues(
35+
colorsLight = colorsLight.toStripeColors(isDark = false),
36+
colorsDark = colorsDark.toStripeColors(isDark = true),
37+
shapes = shapes.toStripeShapes(),
38+
typography = typography.toStripeTypography(),
39+
primaryButtonStyle = primaryButton.toStripePrimaryButtonStyle(
40+
colorsLight = colorsLight,
41+
colorsDark = colorsDark,
42+
shapes = shapes,
43+
typography = typography,
44+
),
45+
formInsets = formInsetValues.toStripeFormInsets(),
46+
sectionSpacing = sectionSpacing.spacingDp.takeIf { it >= 0f },
47+
textFieldInsets = textFieldInsets.toStripeFormInsets(),
48+
iconStyle = iconStyle.toStripeIconStyle(),
49+
verticalModeRowPadding = verticalModeRowPadding,
50+
)
51+
}
52+
53+
private fun PaymentSheet.Colors.toStripeColors(isDark: Boolean): StripeColors {
54+
val materialColors = if (isDark) {
55+
darkColors(
56+
primary = Color(primary),
57+
surface = Color(surface),
58+
onSurface = Color(onSurface),
59+
error = Color(error),
60+
)
61+
} else {
62+
lightColors(
63+
primary = Color(primary),
64+
surface = Color(surface),
65+
onSurface = Color(onSurface),
66+
error = Color(error),
67+
)
68+
}
69+
70+
return StripeThemeDefaults.colors(isDark).copy(
71+
component = Color(component),
72+
componentBorder = Color(componentBorder),
73+
componentDivider = Color(componentDivider),
74+
onComponent = Color(onComponent),
75+
subtitle = Color(subtitle),
76+
placeholderText = Color(placeholderText),
77+
appBarIcon = Color(appBarIcon),
78+
materialColors = materialColors,
79+
)
80+
}
81+
82+
private fun PaymentSheet.Shapes.toStripeShapes(): StripeShapes {
83+
return StripeThemeDefaults.shapes.copy(
84+
cornerRadius = cornerRadiusDp,
85+
bottomSheetCornerRadius = bottomSheetCornerRadiusDp,
86+
borderStrokeWidth = borderStrokeWidthDp,
87+
)
88+
}
89+
90+
@OptIn(AppearanceAPIAdditionsPreview::class)
91+
private fun PaymentSheet.Typography.toStripeTypography(): StripeTypography {
92+
return StripeThemeDefaults.typography.copy(
93+
fontFamily = fontResId,
94+
fontSizeMultiplier = sizeScaleFactor,
95+
h4 = custom.h1?.toTextStyle(),
96+
)
97+
}
98+
99+
private fun PaymentSheet.PrimaryButton.toStripePrimaryButtonStyle(
100+
colorsLight: PaymentSheet.Colors,
101+
colorsDark: PaymentSheet.Colors,
102+
shapes: PaymentSheet.Shapes,
103+
typography: PaymentSheet.Typography,
104+
): PrimaryButtonStyle {
105+
return StripeThemeDefaults.primaryButtonStyle.copy(
106+
colorsLight = this.colorsLight.toStripePrimaryButtonColors(fallbackBackground = colorsLight.primary),
107+
colorsDark = this.colorsDark.toStripePrimaryButtonColors(fallbackBackground = colorsDark.primary),
108+
shape = PrimaryButtonShape(
109+
cornerRadius = shape.cornerRadiusDp ?: shapes.cornerRadiusDp,
110+
borderStrokeWidth = shape.borderStrokeWidthDp ?: shapes.borderStrokeWidthDp,
111+
height = shape.heightDp ?: StripeThemeDefaults.primaryButtonStyle.shape.height,
112+
),
113+
typography = PrimaryButtonTypography(
114+
fontFamily = this.typography.fontResId ?: typography.fontResId,
115+
fontSize = this.typography.fontSizeSp?.sp
116+
?: (StripeThemeDefaults.typography.largeFontSize * typography.sizeScaleFactor),
117+
),
118+
)
119+
}
120+
121+
private fun PaymentSheet.PrimaryButtonColors.toStripePrimaryButtonColors(
122+
fallbackBackground: Int,
123+
): PrimaryButtonColors {
124+
return PrimaryButtonColors(
125+
background = Color(background ?: fallbackBackground),
126+
onBackground = Color(onBackground),
127+
border = Color(border),
128+
successBackground = Color(successBackgroundColor),
129+
onSuccessBackground = Color(onSuccessBackgroundColor),
130+
)
131+
}
132+
133+
private fun PaymentSheet.Insets.toStripeFormInsets(): FormInsets {
134+
return FormInsets(
135+
start = startDp,
136+
top = topDp,
137+
end = endDp,
138+
bottom = bottomDp,
139+
)
140+
}
141+
142+
@OptIn(AppearanceAPIAdditionsPreview::class)
143+
private fun PaymentSheet.IconStyle.toStripeIconStyle(): StripeIconStyle {
144+
return when (this) {
145+
PaymentSheet.IconStyle.Filled -> StripeIconStyle.Filled
146+
PaymentSheet.IconStyle.Outlined -> StripeIconStyle.Outlined
147+
}
148+
}

paymentsheet/src/main/java/com/stripe/android/paymentsheet/PaymentSheetActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import com.stripe.android.PaymentConfiguration
1414
import com.stripe.android.common.model.asCommonConfiguration
1515
import com.stripe.android.common.ui.ElementsBottomSheetLayout
1616
import com.stripe.android.paymentsheet.ui.BaseSheetActivity
17+
import com.stripe.android.paymentsheet.ui.PaymentElementTheme
1718
import com.stripe.android.paymentsheet.ui.PaymentSheetScreen
1819
import com.stripe.android.paymentsheet.utils.applicationIsTaskOwner
19-
import com.stripe.android.uicore.StripeTheme
2020
import com.stripe.android.uicore.elements.bottomsheet.rememberStripeBottomSheetState
2121
import com.stripe.android.uicore.utils.collectAsState
2222
import kotlinx.coroutines.flow.filterNotNull
@@ -68,7 +68,7 @@ internal class PaymentSheetActivity : BaseSheetActivity<PaymentSheetResult>() {
6868
}
6969

7070
setContent {
71-
StripeTheme {
71+
PaymentElementTheme(appearance = starterArgs.config.appearance) {
7272
val isProcessing by viewModel.processing.collectAsState()
7373

7474
val bottomSheetState = rememberStripeBottomSheetState(

0 commit comments

Comments
 (0)