Skip to content

Commit 3e120eb

Browse files
committed
Fix installment plans' offer display
1 parent 66891bc commit 3e120eb

2 files changed

Lines changed: 74 additions & 18 deletions

File tree

  • modules
    • features/account/src/main/java/au/com/shiftyjelly/pocketcasts/account/onboarding/components
    • services/localization/src/main/res/values

modules/features/account/src/main/java/au/com/shiftyjelly/pocketcasts/account/onboarding/components/SubscriptionPlanRow.kt

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import au.com.shiftyjelly.pocketcasts.compose.components.TextP40
4343
import au.com.shiftyjelly.pocketcasts.compose.preview.ThemePreviewParameterProvider
4444
import au.com.shiftyjelly.pocketcasts.compose.theme
4545
import au.com.shiftyjelly.pocketcasts.payment.BillingCycle
46+
import au.com.shiftyjelly.pocketcasts.payment.SubscriptionOffer
4647
import au.com.shiftyjelly.pocketcasts.payment.SubscriptionPlan
4748
import au.com.shiftyjelly.pocketcasts.payment.SubscriptionTier
4849
import au.com.shiftyjelly.pocketcasts.ui.theme.Theme.ThemeType
@@ -75,8 +76,7 @@ fun UpgradePlanRow(
7576
modifier: Modifier = Modifier,
7677
priceComparisonPlan: SubscriptionPlan? = null,
7778
) {
78-
// Don't show savings percent for installment plans
79-
val calculatedSavingPercent = if (plan is SubscriptionPlan.Base && plan.isInstallment) {
79+
val calculatedSavingPercent = if (plan.isInstallment && plan.offer == null) {
8080
null
8181
} else {
8282
priceComparisonPlan?.let { plan.savingsPercent(priceComparisonPlan) }
@@ -202,7 +202,7 @@ private fun SubscriptionPlanRow(
202202
verticalArrangement = Arrangement.spacedBy(rowConfig.labelSpacing),
203203
) {
204204
TextH30(
205-
text = plan.name,
205+
text = plan.displayName(),
206206
fontSize = rowConfig.mainTextSize,
207207
)
208208
TextP40(
@@ -289,26 +289,46 @@ private fun CheckMark(
289289

290290
private val SubscriptionPlan.pricePerMonth: Float
291291
get() {
292+
val totalYearlyAmount = if (isInstallment) {
293+
recurringPrice.amount * monthsInYear
294+
} else {
295+
when (billingCycle) {
296+
BillingCycle.Monthly -> recurringPrice.amount
297+
BillingCycle.Yearly -> recurringPrice.amount
298+
}
299+
}
300+
292301
val pricePerMonth = when (billingCycle) {
293-
BillingCycle.Monthly -> recurringPrice.amount
294-
BillingCycle.Yearly -> recurringPrice.amount / monthsInYear
302+
BillingCycle.Monthly -> totalYearlyAmount
303+
BillingCycle.Yearly -> totalYearlyAmount / monthsInYear
295304
}
296305
return pricePerMonth.toFloat()
297306
}
298307

299308
private val SubscriptionPlan.pricePerWeek: Float
300309
get() {
301-
val pricePerWeek = when (billingCycle) {
302-
BillingCycle.Monthly -> recurringPrice.amount * monthsInYear
303-
BillingCycle.Yearly -> recurringPrice.amount
304-
} / weeksInYear
305-
return pricePerWeek.toFloat()
310+
val totalYearlyAmount = if (isInstallment) {
311+
recurringPrice.amount * monthsInYear
312+
} else {
313+
when (billingCycle) {
314+
BillingCycle.Monthly -> recurringPrice.amount * monthsInYear
315+
BillingCycle.Yearly -> recurringPrice.amount
316+
}
317+
}
318+
319+
return (totalYearlyAmount / weeksInYear).toFloat()
306320
}
307321

308322
private val monthsInYear = 12.toBigDecimal()
309323
private val weeksInYear = 52.toBigDecimal()
310324

311-
private fun SubscriptionPlan.Base.formattedTotalYearlyPrice(): String {
325+
private val SubscriptionPlan.isInstallment: Boolean
326+
get() = when (this) {
327+
is SubscriptionPlan.Base -> this.isInstallment
328+
is SubscriptionPlan.WithOffer -> this.isInstallment
329+
}
330+
331+
private fun SubscriptionPlan.formattedTotalYearlyPrice(): String {
312332
val totalAmount = recurringPrice.amount * monthsInYear
313333
val currencyCode = recurringPrice.currencyCode
314334

@@ -327,8 +347,26 @@ private fun SubscriptionPlan.Base.formattedTotalYearlyPrice(): String {
327347

328348
@Composable
329349
private fun SubscriptionPlan.pricePerPeriod(config: RowConfig): String? {
330-
if (this is SubscriptionPlan.Base && isInstallment) {
331-
return stringResource(LR.string.plus_per_year, formattedTotalYearlyPrice())
350+
if (isInstallment) {
351+
return when (config.pricePerPeriod) {
352+
PricePerPeriod.PRICE_PER_WEEK -> {
353+
val currencyCode = recurringPrice.currencyCode
354+
if (currencyCode == "USD") {
355+
stringResource(LR.string.price_per_week_usd, pricePerWeek)
356+
} else {
357+
stringResource(LR.string.price_per_week, pricePerWeek, currencyCode)
358+
}
359+
}
360+
361+
PricePerPeriod.PRICE_PER_MONTH -> {
362+
val currencyCode = recurringPrice.currencyCode
363+
if (currencyCode == "USD") {
364+
stringResource(LR.string.price_per_month_usd, pricePerMonth)
365+
} else {
366+
stringResource(LR.string.price_per_month, pricePerMonth, currencyCode)
367+
}
368+
}
369+
}
332370
}
333371

334372
return if (this.billingCycle == BillingCycle.Yearly) {
@@ -360,14 +398,31 @@ private fun SubscriptionPlan.savingsPercent(otherPlan: SubscriptionPlan) = 100 -
360398

361399
@Composable
362400
@ReadOnlyComposable
363-
private fun SubscriptionPlan.price(): String {
364-
val formattedPrice = recurringPrice.formattedPrice
401+
private fun SubscriptionPlan.displayName(): String {
402+
return when {
403+
this is SubscriptionPlan.WithOffer && offer == SubscriptionOffer.Trial && isInstallment -> {
404+
when (tier) {
405+
SubscriptionTier.Plus -> when (billingCycle) {
406+
BillingCycle.Yearly -> stringResource(LR.string.plus_trial_yearly_installments)
407+
BillingCycle.Monthly -> name
408+
}
409+
410+
SubscriptionTier.Patron -> name
411+
}
412+
}
413+
414+
else -> name
415+
}
416+
}
365417

366-
// For installment plans, show price per month with duration
367-
if (this is SubscriptionPlan.Base && isInstallment) {
368-
return stringResource(LR.string.price_per_month_for_months, formattedPrice, monthsInYear.toInt())
418+
@Composable
419+
@ReadOnlyComposable
420+
private fun SubscriptionPlan.price(): String {
421+
if (isInstallment) {
422+
return stringResource(LR.string.plus_per_year, formattedTotalYearlyPrice())
369423
}
370424

425+
val formattedPrice = recurringPrice.formattedPrice
371426
return when (billingCycle) {
372427
BillingCycle.Monthly -> stringResource(LR.string.plus_per_month, formattedPrice)
373428
BillingCycle.Yearly -> stringResource(LR.string.plus_per_year, formattedPrice)

modules/services/localization/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,7 @@
13621362
<string name="plus_day">day</string>
13631363
<string name="plus_yearly">Yearly</string>
13641364
<string name="plus_monthly">Monthly</string>
1365+
<string name="plus_trial_yearly_installments">Plus yearly trial (installments)</string>
13651366

13661367
<!-- Trial -->
13671368

0 commit comments

Comments
 (0)