-
Notifications
You must be signed in to change notification settings - Fork 629
/
PaymentMethodsUI.kt
177 lines (164 loc) · 6.2 KB
/
PaymentMethodsUI.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.stripe.android.paymentsheet
import androidx.annotation.VisibleForTesting
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.selection.selectable
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.stripe.android.paymentsheet.ui.LpmSelectorText
import com.stripe.android.ui.core.MeasureComposableWidth
import com.stripe.android.ui.core.PaymentsTheme
import com.stripe.android.ui.core.elements.SectionCard
import com.stripe.android.ui.core.forms.resources.LpmRepository.SupportedPaymentMethod
import com.stripe.android.ui.core.paymentsColors
internal const val ADD_PM_DEFAULT_PADDING = 12.0f
internal const val CARD_HORIZONTAL_PADDING = 6.0f
internal const val PM_LIST_PADDING = 17.0f
@VisibleForTesting
const val TEST_TAG_LIST = "PaymentMethodsUITestTag"
@Composable
internal fun PaymentMethodsUI(
paymentMethods: List<SupportedPaymentMethod>,
selectedIndex: Int,
isEnabled: Boolean,
onItemSelectedListener: (SupportedPaymentMethod) -> Unit
) {
val scope = rememberCoroutineScope()
val state = rememberLazyListState()
LaunchedEffect(selectedIndex) {
state.scrollToItem(selectedIndex, 0)
}
LaunchedEffect(isEnabled) {
if (isEnabled) {
state.reenableScrolling(scope)
} else {
state.disableScrolling(scope)
}
}
PaymentsTheme {
BoxWithConstraints(
modifier = Modifier
.testTag(TEST_TAG_LIST + "1")
) {
val viewWidth = calculateViewWidth(
this.maxWidth,
paymentMethods.size
)
// TODO: userScrollEnabled will be available in compose version 1.2.0-alpha01+
LazyRow(
state = state,
contentPadding = PaddingValues(horizontal = PM_LIST_PADDING.dp),
modifier = Modifier.testTag(TEST_TAG_LIST)
) {
itemsIndexed(items = paymentMethods, itemContent = { index, item ->
PaymentMethodUI(
modifier = Modifier.testTag(
TEST_TAG_LIST + stringResource(item.displayNameResource)
),
viewWidth = viewWidth,
iconRes = item.iconResource,
title = stringResource(item.displayNameResource),
isSelected = index == selectedIndex,
isEnabled = isEnabled,
tintOnSelected = item.tintIconOnSelection,
itemIndex = index,
onItemSelectedListener = {
onItemSelectedListener(paymentMethods[it])
}
)
})
}
}
}
}
internal fun calculateViewWidth(
maxWidth: Dp,
numberOfPaymentMethods: Int
): Dp {
val targetWidthDp = maxWidth - (PM_LIST_PADDING.dp * 2)
val minItemWidthDp = (100 + (2 * CARD_HORIZONTAL_PADDING)).dp
val viewWidth = if ((minItemWidthDp * numberOfPaymentMethods) < targetWidthDp) {
targetWidthDp / numberOfPaymentMethods
} else {
val maxNumVisibleItemsAtMinimumSize = (targetWidthDp / minItemWidthDp).toInt()
targetWidthDp / maxNumVisibleItemsAtMinimumSize
}
return viewWidth
}
@Composable
internal fun PaymentMethodUI(
viewWidth: Dp,
iconRes: Int,
title: String,
isSelected: Boolean,
isEnabled: Boolean,
tintOnSelected: Boolean,
itemIndex: Int,
modifier: Modifier = Modifier,
onItemSelectedListener: (Int) -> Unit
) {
val color = if (isSelected) MaterialTheme.colors.primary
else MaterialTheme.paymentsColors.onComponent
val lpmTextSelector: @Composable () -> Unit = {
LpmSelectorText(
text = title,
isEnabled = isEnabled,
textColor = color,
modifier = Modifier.padding(top = 6.dp, start = ADD_PM_DEFAULT_PADDING.dp)
)
}
MeasureComposableWidth(composable = lpmTextSelector) { lpmSelectorTextWidth ->
SectionCard(
isSelected = isSelected,
modifier = modifier
.alpha(alpha = if (isEnabled) 1.0F else 0.6F)
.height(60.dp)
.width(
maxOf(
viewWidth,
lpmSelectorTextWidth +
(CARD_HORIZONTAL_PADDING.dp * 2) +
ADD_PM_DEFAULT_PADDING.dp
)
)
.padding(horizontal = CARD_HORIZONTAL_PADDING.dp)
.selectable(
selected = isSelected,
enabled = isEnabled,
onClick = {
onItemSelectedListener(itemIndex)
}
)
) {
Column {
val colorFilter = if (tintOnSelected) ColorFilter.tint(color) else null
Image(
painter = painterResource(iconRes),
contentDescription = null,
colorFilter = colorFilter,
modifier = Modifier
.padding(top = ADD_PM_DEFAULT_PADDING.dp, start = ADD_PM_DEFAULT_PADDING.dp)
)
lpmTextSelector()
}
}
}
}