Skip to content

Commit

Permalink
Fix detekt
Browse files Browse the repository at this point in the history
  • Loading branch information
toluo-stripe committed Nov 25, 2024
1 parent cc67469 commit 893dcbf
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 69 deletions.
13 changes: 12 additions & 1 deletion link/src/main/java/com/stripe/android/link/ui/LinkContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.stripe.android.link.ui.signup.SignUpViewModel
import com.stripe.android.link.ui.verification.VerificationScreen
import com.stripe.android.link.ui.verification.VerificationViewModel
import com.stripe.android.link.ui.wallet.WalletScreen
import com.stripe.android.link.ui.wallet.WalletViewModel
import com.stripe.android.ui.core.CircularProgressIndicator
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -155,7 +156,17 @@ private fun Screens(
}

composable(LinkScreen.Wallet.route) {
WalletScreen()
val linkAccount = getLinkAccount()
?: return@composable dismissWithResult(LinkActivityResult.Failed(NoLinkAccountFoundException()))
val viewModel: WalletViewModel = linkViewModel { parentComponent ->
WalletViewModel.factory(
parentComponent = parentComponent,
linkAccount = linkAccount,
navigate = { _, _ -> },
dismissWithResult = dismissWithResult
)
}
WalletScreen(viewModel)
}

composable(LinkScreen.CardEdit.route) {
Expand Down
167 changes: 107 additions & 60 deletions link/src/main/java/com/stripe/android/link/ui/wallet/WalletScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ internal fun WalletScreen(
) {
val state by viewModel.uiState.collectAsState()

WalletBody(
state = state,
onItemSelected = viewModel::onItemSelected,
onExpandedChanged = viewModel::setExpanded
)
}

@Composable
internal fun WalletBody(
state: WalletUiState,
onItemSelected: (ConsumerPaymentDetails.PaymentDetails) -> Unit,
onExpandedChanged: (Boolean) -> Unit,
) {
if (state.paymentDetailsList.isEmpty()) {
Box(
modifier = Modifier
Expand All @@ -56,6 +69,7 @@ internal fun WalletScreen(
) {
CircularProgressIndicator()
}
return
}

val focusManager = LocalFocusManager.current
Expand All @@ -79,32 +93,26 @@ internal fun WalletScreen(
if (state.isExpanded || selectedItem == null) {
ExpandedPaymentDetails(
uiState = state,
onItemSelected = viewModel::onItemSelected,
onItemSelected = onItemSelected,
onMenuButtonClick = {},
onAddNewPaymentMethodClick = {},
onCollapse = {}
onCollapse = {
onExpandedChanged(false)
}
)
} else {
CollapsedPaymentDetails(
selectedPaymentMethod = selectedItem,
enabled = !state.primaryButtonState.isBlocking,
onClick = {}
onClick = {
onExpandedChanged(true)
}
)
}
}

AnimatedVisibility(state.showBankAccountTerms) {
Html(
html = stringResource(R.string.stripe_wallet_bank_account_terms).replaceHyperlinks(),
color = MaterialTheme.colors.onSecondary,
style = MaterialTheme.typography.caption,
modifier = Modifier
.fillMaxWidth()
.padding(top = 12.dp),
urlSpanStyle = SpanStyle(
color = MaterialTheme.colors.primary
)
)
BankAccountTerms()
}
}
}
Expand Down Expand Up @@ -182,32 +190,10 @@ private fun ExpandedPaymentDetails(
)
) {
item {
Row(
modifier = Modifier
.height(44.dp)
.clickable(enabled = isEnabled, onClick = onCollapse),
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = stringResource(id = R.string.stripe_wallet_expanded_title),
modifier = Modifier
.padding(start = HorizontalPadding, top = 20.dp),
color = MaterialTheme.colors.onPrimary,
style = MaterialTheme.typography.button
)
Spacer(modifier = Modifier.weight(1f))
Icon(
painter = painterResource(id = R.drawable.stripe_link_chevron),
contentDescription = stringResource(id = R.string.stripe_wallet_expand_accessibility),
modifier = Modifier
.padding(top = 20.dp, end = 22.dp)
.rotate(180f)
.semantics {
testTag = "ChevronIcon"
},
tint = MaterialTheme.colors.onPrimary
)
}
ExpandedRowHeader(
isEnabled = isEnabled,
onCollapse = onCollapse
)
}

items(
Expand All @@ -231,31 +217,92 @@ private fun ExpandedPaymentDetails(
}

item {
Row(
modifier = Modifier
.fillMaxWidth()
.height(60.dp)
.clickable(enabled = isEnabled, onClick = onAddNewPaymentMethodClick),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
painter = painterResource(id = R.drawable.stripe_link_add_green),
contentDescription = null,
modifier = Modifier.padding(start = HorizontalPadding, end = 12.dp),
tint = Color.Unspecified
)
Text(
text = stringResource(id = R.string.stripe_add_payment_method),
modifier = Modifier.padding(end = HorizontalPadding),
color = MaterialTheme.linkColors.actionLabel,
style = MaterialTheme.typography.button
)
}
AddPaymentMethodRow(
isEnabled = isEnabled,
onAddNewPaymentMethodClick = onAddNewPaymentMethodClick
)
}
}
}

@Composable
private fun ExpandedRowHeader(
isEnabled: Boolean,
onCollapse: () -> Unit,
) {
Row(
modifier = Modifier
.height(44.dp)
.clickable(enabled = isEnabled, onClick = onCollapse),
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = stringResource(id = R.string.stripe_wallet_expanded_title),
modifier = Modifier
.padding(start = HorizontalPadding, top = 20.dp),
color = MaterialTheme.colors.onPrimary,
style = MaterialTheme.typography.button
)
Spacer(modifier = Modifier.weight(1f))
Icon(
painter = painterResource(id = R.drawable.stripe_link_chevron),
contentDescription = stringResource(id = R.string.stripe_wallet_expand_accessibility),
modifier = Modifier
.padding(top = 20.dp, end = 22.dp)
.rotate(CHEVRON_ICON_ROTATION)
.semantics {
testTag = "ChevronIcon"
},
tint = MaterialTheme.colors.onPrimary
)
}
}

@Composable
private fun AddPaymentMethodRow(
isEnabled: Boolean,
onAddNewPaymentMethodClick: () -> Unit,
) {
Row(
modifier = Modifier
.fillMaxWidth()
.height(60.dp)
.clickable(enabled = isEnabled, onClick = onAddNewPaymentMethodClick),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
painter = painterResource(id = R.drawable.stripe_link_add_green),
contentDescription = null,
modifier = Modifier.padding(start = HorizontalPadding, end = 12.dp),
tint = Color.Unspecified
)
Text(
text = stringResource(id = R.string.stripe_add_payment_method),
modifier = Modifier.padding(end = HorizontalPadding),
color = MaterialTheme.linkColors.actionLabel,
style = MaterialTheme.typography.button
)
}
}

@Composable
private fun BankAccountTerms() {
Html(
html = stringResource(R.string.stripe_wallet_bank_account_terms).replaceHyperlinks(),
color = MaterialTheme.colors.onSecondary,
style = MaterialTheme.typography.caption,
modifier = Modifier
.fillMaxWidth()
.padding(top = 12.dp),
urlSpanStyle = SpanStyle(
color = MaterialTheme.colors.primary
)
)
}

private fun String.replaceHyperlinks() = this.replace(
"<terms>",
"<a href=\"https://stripe.com/legal/ach-payments/authorization\">"
).replace("</terms>", "</a>")

private const val CHEVRON_ICON_ROTATION = 180f
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import com.stripe.android.model.ConsumerPaymentDetails

@Immutable
internal data class WalletUiState(
val supportedTypes: Set<String>,
val paymentDetailsList: List<ConsumerPaymentDetails.PaymentDetails>,
val selectedItem: ConsumerPaymentDetails.PaymentDetails?,
val isProcessing: Boolean,
val isExpanded: Boolean = false
val isExpanded: Boolean
) {

val showBankAccountTerms = selectedItem is ConsumerPaymentDetails.BankAccount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ internal class WalletViewModel @Inject constructor(

private val _uiState = MutableStateFlow(
value = WalletUiState(
supportedTypes = stripeIntent.supportedPaymentMethodTypes(linkAccount),
paymentDetailsList = emptyList(),
selectedItem = null,
isProcessing = false
isProcessing = false,
isExpanded = false
)
)

Expand All @@ -52,7 +52,7 @@ internal class WalletViewModel @Inject constructor(

viewModelScope.launch {
linkAccountManager.listPaymentDetails(
paymentMethodTypes = _uiState.value.supportedTypes
paymentMethodTypes = stripeIntent.supportedPaymentMethodTypes(linkAccount)
).fold(
onSuccess = { response ->
_uiState.update {
Expand Down Expand Up @@ -82,6 +82,12 @@ internal class WalletViewModel @Inject constructor(
}
}

fun setExpanded(expanded: Boolean) {
_uiState.update {
it.copy(isExpanded = expanded)
}
}

companion object {
fun factory(
parentComponent: NativeLinkComponent,
Expand Down
17 changes: 16 additions & 1 deletion link/src/test/java/com/stripe/android/link/TestFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ internal object TestFactory {
)
)

private val CONSUMER_PAYMENT_DETAILS_BANK_ACCOUNT = ConsumerPaymentDetails.BankAccount(
id = "pm_124",
last4 = "4242",
isDefault = false,
bankName = "Stripe Test Bank",
bankIconCode = null
)

private val CONSUMER_PAYMENT_DETAILS_PASSTHROUGH = ConsumerPaymentDetails.Passthrough(
id = "pm_125",
last4 = "4242",
)

val LINK_NEW_PAYMENT_DETAILS = LinkPaymentDetails.New(
paymentDetails = CONSUMER_PAYMENT_DETAILS_CARD,
paymentMethodCreateParams = PAYMENT_METHOD_CREATE_PARAMS,
Expand All @@ -92,7 +105,9 @@ internal object TestFactory {

val CONSUMER_PAYMENT_DETAILS: ConsumerPaymentDetails = ConsumerPaymentDetails(
paymentDetails = listOf(
CONSUMER_PAYMENT_DETAILS_CARD
CONSUMER_PAYMENT_DETAILS_CARD,
CONSUMER_PAYMENT_DETAILS_BANK_ACCOUNT,
CONSUMER_PAYMENT_DETAILS_PASSTHROUGH,
)
)

Expand Down
Loading

0 comments on commit 893dcbf

Please sign in to comment.