|
| 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 | +} |
0 commit comments