-
Notifications
You must be signed in to change notification settings - Fork 658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Show promo badge in bank form #9734
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package com.stripe.android.paymentsheet.ui | ||
|
||
import android.content.Context | ||
import android.os.Build.VERSION.SDK_INT | ||
import android.os.Build.VERSION_CODES.N | ||
import androidx.compose.foundation.background | ||
|
@@ -13,28 +14,40 @@ import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.Density | ||
import androidx.compose.ui.unit.dp | ||
import com.stripe.android.paymentsheet.R | ||
import com.stripe.android.uicore.StripeThemeDefaults | ||
import com.stripe.android.uicore.getOnSuccessBackgroundColor | ||
import com.stripe.android.uicore.getSuccessBackgroundColor | ||
import com.stripe.android.uicore.stripeColors | ||
import java.util.Locale | ||
|
||
@Composable | ||
internal fun PromoBadge( | ||
text: String, | ||
modifier: Modifier = Modifier, | ||
eligible: Boolean = true, | ||
tinyMode: Boolean = false, | ||
) { | ||
// TODO(tillh-stripe): Revisit how we want the badge text to scale in tiny mode | ||
FixedTextSize(fixed = tinyMode) { | ||
val backgroundColor = Color( | ||
color = StripeThemeDefaults.primaryButtonStyle.getSuccessBackgroundColor(LocalContext.current), | ||
) | ||
val backgroundColor = if (eligible) { | ||
Color( | ||
color = StripeThemeDefaults.primaryButtonStyle.getSuccessBackgroundColor(LocalContext.current), | ||
) | ||
} else { | ||
MaterialTheme.stripeColors.componentBorder | ||
} | ||
|
||
val foregroundColor = Color( | ||
color = StripeThemeDefaults.primaryButtonStyle.getOnSuccessBackgroundColor(LocalContext.current), | ||
) | ||
val foregroundColor = if (eligible) { | ||
Color( | ||
color = StripeThemeDefaults.primaryButtonStyle.getOnSuccessBackgroundColor(LocalContext.current), | ||
) | ||
} else { | ||
MaterialTheme.stripeColors.onComponent | ||
} | ||
|
||
val shape = MaterialTheme.shapes.medium | ||
|
||
|
@@ -47,7 +60,7 @@ internal fun PromoBadge( | |
) | ||
) { | ||
Text( | ||
text = formatPromoText(text), | ||
text = formatPromoText(text, eligible), | ||
color = foregroundColor, | ||
style = MaterialTheme.typography.caption.copy( | ||
fontSize = StripeThemeDefaults.typography.xSmallFontSize, | ||
|
@@ -58,17 +71,21 @@ internal fun PromoBadge( | |
} | ||
|
||
@Composable | ||
private fun formatPromoText(text: String): String { | ||
val context = LocalContext.current | ||
val currentLocale: Locale = if (SDK_INT >= N) { | ||
context.resources.configuration.locales[0] | ||
private fun formatPromoText( | ||
text: String, | ||
eligible: Boolean, | ||
): String { | ||
return if (eligible) { | ||
val context = LocalContext.current | ||
|
||
if (context.isEnglishLanguage) { | ||
"Get $text" | ||
} else { | ||
text | ||
} | ||
Comment on lines
+81
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No action - I guess this is a backend limitation, but shouldn't we get complete text from backend? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We only get the formatted amount from the backend. That’s the way we intended it, since we need this in multiple places with different text (or none) around it. |
||
} else { | ||
@Suppress("DEPRECATION") | ||
context.resources.configuration.locale | ||
stringResource(R.string.stripe_paymentsheet_bank_payment_promo_ineligible, text) | ||
} | ||
|
||
val isEnglish = currentLocale.language == Locale.ENGLISH.language | ||
return if (isEnglish) "Get $text" else text | ||
} | ||
|
||
@Composable | ||
|
@@ -86,6 +103,18 @@ private fun FixedTextSize( | |
} | ||
} | ||
|
||
private val Context.isEnglishLanguage: Boolean | ||
get() { | ||
val currentLocale: Locale = if (SDK_INT >= N) { | ||
resources.configuration.locales[0] | ||
} else { | ||
@Suppress("DEPRECATION") | ||
resources.configuration.locale | ||
} | ||
|
||
return currentLocale.language == Locale.ENGLISH.language | ||
} | ||
Comment on lines
+106
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] Likely useful on other scenarios, could be moved to a common module There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I honestly don’t want to further encourage this pattern, so I’ll keep it here for now 😅 |
||
|
||
private fun Density.copy( | ||
fontScale: Float = 1f, | ||
): Density { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is currently hard-coded to false, but will eventually be set in the bank auth flow.