|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Adyen N.V. |
| 3 | + * |
| 4 | + * This file is open source and available under the MIT license. See the LICENSE file for more info. |
| 5 | + * |
| 6 | + * Created by ararat on 7/11/2025. |
| 7 | + */ |
| 8 | + |
| 9 | +package com.adyen.checkout.card.internal.ui.view |
| 10 | + |
| 11 | +import androidx.compose.animation.AnimatedVisibility |
| 12 | +import androidx.compose.foundation.layout.Arrangement |
| 13 | +import androidx.compose.foundation.layout.Column |
| 14 | +import androidx.compose.foundation.layout.FlowRow |
| 15 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 16 | +import androidx.compose.foundation.layout.padding |
| 17 | +import androidx.compose.foundation.layout.size |
| 18 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 19 | +import androidx.compose.foundation.text.KeyboardOptions |
| 20 | +import androidx.compose.foundation.text.input.maxLength |
| 21 | +import androidx.compose.runtime.Composable |
| 22 | +import androidx.compose.runtime.remember |
| 23 | +import androidx.compose.ui.Modifier |
| 24 | +import androidx.compose.ui.draw.clip |
| 25 | +import androidx.compose.ui.draw.dropShadow |
| 26 | +import androidx.compose.ui.focus.onFocusChanged |
| 27 | +import androidx.compose.ui.graphics.shadow.Shadow |
| 28 | +import androidx.compose.ui.text.input.KeyboardType |
| 29 | +import androidx.compose.ui.tooling.preview.Preview |
| 30 | +import androidx.compose.ui.unit.DpOffset |
| 31 | +import androidx.compose.ui.unit.dp |
| 32 | +import com.adyen.checkout.core.common.CardBrand |
| 33 | +import com.adyen.checkout.core.common.CardType |
| 34 | +import com.adyen.checkout.core.common.helper.CardNumberValidator |
| 35 | +import com.adyen.checkout.core.common.internal.ui.CheckoutNetworkLogo |
| 36 | +import com.adyen.checkout.core.common.localization.CheckoutLocalizationKey |
| 37 | +import com.adyen.checkout.core.common.localization.internal.helper.resolveString |
| 38 | +import com.adyen.checkout.core.components.internal.ui.state.model.TextInputState |
| 39 | +import com.adyen.checkout.ui.internal.CheckoutTextField |
| 40 | +import com.adyen.checkout.ui.internal.CheckoutThemeProvider |
| 41 | +import com.adyen.checkout.ui.internal.DigitOnlyInputTransformation |
| 42 | +import com.adyen.checkout.ui.internal.Dimensions |
| 43 | + |
| 44 | +@Composable |
| 45 | +internal fun CardNumberField( |
| 46 | + cardNumberState: TextInputState, |
| 47 | + supportedCardBrands: List<CardBrand>, |
| 48 | + isSupportedCardBrandsShown: Boolean, |
| 49 | + isAmex: Boolean, |
| 50 | + onCardNumberChanged: (String) -> Unit, |
| 51 | + onCardNumberFocusChanged: (Boolean) -> Unit, |
| 52 | + modifier: Modifier = Modifier, |
| 53 | +) { |
| 54 | + Column( |
| 55 | + modifier = modifier, |
| 56 | + ) { |
| 57 | + CardNumberInputField( |
| 58 | + cardNumberState = cardNumberState, |
| 59 | + isAmex = isAmex, |
| 60 | + onCardNumberChanged = onCardNumberChanged, |
| 61 | + onCardNumberFocusChanged = onCardNumberFocusChanged, |
| 62 | + ) |
| 63 | + |
| 64 | + CardBrandsList( |
| 65 | + cardBrands = supportedCardBrands, |
| 66 | + visible = isSupportedCardBrandsShown, |
| 67 | + ) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +@Composable |
| 72 | +private fun CardNumberInputField( |
| 73 | + cardNumberState: TextInputState, |
| 74 | + isAmex: Boolean, |
| 75 | + onCardNumberChanged: (String) -> Unit, |
| 76 | + onCardNumberFocusChanged: (Boolean) -> Unit, |
| 77 | + modifier: Modifier = Modifier, |
| 78 | +) { |
| 79 | + val showCardNumberError = |
| 80 | + cardNumberState.errorMessage != null && cardNumberState.showError |
| 81 | + val supportingTextCardNumber = if (showCardNumberError) { |
| 82 | + cardNumberState.errorMessage?.let { resolveString(it) } |
| 83 | + } else { |
| 84 | + null |
| 85 | + } |
| 86 | + |
| 87 | + val outputTransformation = remember(isAmex) { |
| 88 | + CardNumberOutputTransformation(isAmex = isAmex) |
| 89 | + } |
| 90 | + |
| 91 | + CheckoutTextField( |
| 92 | + modifier = modifier |
| 93 | + .fillMaxWidth() |
| 94 | + .onFocusChanged { focusState -> |
| 95 | + onCardNumberFocusChanged(focusState.isFocused) |
| 96 | + }, |
| 97 | + label = resolveString(CheckoutLocalizationKey.CARD_NUMBER), |
| 98 | + initialValue = cardNumberState.text, |
| 99 | + isError = showCardNumberError, |
| 100 | + supportingText = supportingTextCardNumber, |
| 101 | + onValueChange = { value -> |
| 102 | + onCardNumberChanged(value) |
| 103 | + }, |
| 104 | + inputTransformation = DigitOnlyInputTransformation().maxLength( |
| 105 | + maxLength = CardNumberValidator.MAXIMUM_CARD_NUMBER_LENGTH, |
| 106 | + ), |
| 107 | + outputTransformation = outputTransformation, |
| 108 | + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), |
| 109 | + shouldFocus = cardNumberState.isFocused, |
| 110 | + ) |
| 111 | +} |
| 112 | + |
| 113 | +@Composable |
| 114 | +private fun CardBrandsList( |
| 115 | + cardBrands: List<CardBrand>, |
| 116 | + visible: Boolean, |
| 117 | + modifier: Modifier = Modifier, |
| 118 | +) { |
| 119 | + AnimatedVisibility( |
| 120 | + modifier = modifier.fillMaxWidth(), |
| 121 | + visible = visible, |
| 122 | + ) { |
| 123 | + FlowRow( |
| 124 | + modifier = Modifier.padding(top = Dimensions.ExtraSmall), |
| 125 | + horizontalArrangement = Arrangement.spacedBy(Dimensions.ExtraSmall), |
| 126 | + verticalArrangement = Arrangement.spacedBy(Dimensions.ExtraSmall), |
| 127 | + ) { |
| 128 | + for (cardBrand in cardBrands) { |
| 129 | + CheckoutNetworkLogo( |
| 130 | + modifier = Modifier |
| 131 | + .size(24.dp, 16.dp) |
| 132 | + .dropShadow( |
| 133 | + shape = RoundedCornerShape(Dimensions.CornerRadius), |
| 134 | + shadow = Shadow( |
| 135 | + radius = 1.dp, |
| 136 | + offset = DpOffset(x = 0.dp, 2.dp), |
| 137 | + color = CheckoutThemeProvider.colors.container, |
| 138 | + ), |
| 139 | + ) |
| 140 | + .clip(RoundedCornerShape(Dimensions.CornerRadius)), |
| 141 | + txVariant = cardBrand.txVariant, |
| 142 | + ) |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +@Preview(showBackground = true) |
| 149 | +@Composable |
| 150 | +private fun CardNumberFieldPreview() { |
| 151 | + CardNumberField( |
| 152 | + cardNumberState = TextInputState( |
| 153 | + "5555444433331111", |
| 154 | + ), |
| 155 | + supportedCardBrands = listOf( |
| 156 | + CardBrand(CardType.MASTERCARD.txVariant), |
| 157 | + CardBrand(CardType.VISA.txVariant), |
| 158 | + CardBrand(CardType.AMERICAN_EXPRESS.txVariant), |
| 159 | + ), |
| 160 | + isSupportedCardBrandsShown = true, |
| 161 | + isAmex = false, |
| 162 | + onCardNumberChanged = {}, |
| 163 | + onCardNumberFocusChanged = {}, |
| 164 | + ) |
| 165 | +} |
0 commit comments