Skip to content

Commit

Permalink
Paywalls: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto committed Oct 20, 2023
1 parent 064f4bc commit bd56068
Show file tree
Hide file tree
Showing 50 changed files with 109 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private fun AppNavHost(
route = AppScreen.Paywall.route.plus("/{${PaywallScreenViewModel.OFFERING_ID_KEY}}"),
arguments = listOf(navArgument(PaywallScreenViewModel.OFFERING_ID_KEY) { type = NavType.StringType }),
) {
PaywallScreen()
PaywallScreen(dismissRequest = navController::popBackStack)
}
composable(
route = AppScreen.PaywallFooter.route
Expand All @@ -68,7 +68,9 @@ private fun AppNavHost(
},
),
) {
PaywallFooterScreen()
PaywallFooterScreen(
dismissRequest = navController::popBackStack,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private fun FooterDialog(currentState: DisplayPaywallState.Footer, onDismiss: ()
Scaffold { scaffoldPadding ->
Box(modifier = Modifier.padding(scaffoldPadding)) {
PaywallFooter(
options = PaywallOptions.Builder()
options = PaywallOptions.Builder(dismissRequest = onDismiss)
.setOffering(currentState.offering)
.setListener(PaywallListenerImpl(onDismiss))
.build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.revenuecat.purchases.ui.revenuecatui.PaywallOptions
@Composable
fun PaywallScreen(
viewModel: PaywallScreenViewModel = viewModel<PaywallScreenViewModelImpl>(),
dismissRequest: () -> Unit,
) {
Column(
modifier = Modifier.fillMaxSize(),
Expand All @@ -32,7 +33,7 @@ fun PaywallScreen(
}
is PaywallScreenState.Loaded -> {
Paywall(
PaywallOptions.Builder()
PaywallOptions.Builder(dismissRequest)
.setOffering(state.offering)
.setListener(viewModel)
.build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.revenuecat.purchases.ui.revenuecatui.PaywallOptions
@Composable
fun PaywallFooterScreen(
viewModel: PaywallScreenViewModel = viewModel<PaywallScreenViewModelImpl>(),
dismissRequest: () -> Unit,
) {
Column(
modifier = Modifier.fillMaxSize(),
Expand All @@ -40,7 +41,7 @@ fun PaywallFooterScreen(
}
is PaywallScreenState.Loaded -> {
PaywallFooter(
options = PaywallOptions.Builder()
options = PaywallOptions.Builder(dismissRequest)
.setOffering(state.offering)
.setListener(viewModel)
.build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.lifecycle.viewmodel.compose.viewModel
import com.revenuecat.purchases.ui.revenuecatui.data.PaywallState
import com.revenuecat.purchases.ui.revenuecatui.data.PaywallViewModel
Expand All @@ -33,16 +39,20 @@ internal fun InternalPaywall(
) {
PaywallTheme(fontProvider = options.fontProvider) {
viewModel.refreshStateIfLocaleChanged()
val colors = MaterialTheme.colorScheme
viewModel.refreshStateIfColorsChanged(colors)
viewModel.refreshStateIfColorsChanged(MaterialTheme.colorScheme)

when (val state = viewModel.state.collectAsState().value) {
is PaywallState.Loading -> {
LoadingPaywall(mode = options.mode)
}

is PaywallState.Error -> {
Text(text = "Error: ${state.errorMessage}")
LoadingPaywall(mode = options.mode)

ErrorDialog(
dismissRequest = options.dismissRequest,
error = state.errorMessage,
)
}

is PaywallState.Loaded -> {
Expand Down Expand Up @@ -104,3 +114,27 @@ private fun getPaywallViewModel(
),
)
}

@Composable
private fun ErrorDialog(
dismissRequest: () -> Unit,
error: String,
) {
AlertDialog(
onDismissRequest = dismissRequest,
confirmButton = {
TextButton(
onClick = dismissRequest,
) {
Text(
text = stringResource(id = R.string.OK),
textAlign = TextAlign.Center
)
}
},
icon = { Icon(painter = painterResource(id = R.drawable.error), contentDescription = null) },
text = {
Text(text = error)
},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import androidx.compose.runtime.Composable
* Composable offering a full screen Paywall UI configured from the RevenueCat dashboard.
* @param options The options to configure the [Paywall] if needed.
*/
@Suppress("unused")
@Composable
fun Paywall(
options: PaywallOptions = PaywallOptions.Builder().build(),
) {
fun Paywall(options: PaywallOptions) {
InternalPaywall(options)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PaywallDialogOptions(builder: Builder) {
}

internal fun toPaywallOptions(): PaywallOptions {
return PaywallOptions.Builder()
return PaywallOptions.Builder(dismissRequest)
.setOffering(offering)
.setShouldDisplayDismissButton(shouldDisplayDismissButton)
.setFontProvider(fontProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import androidx.compose.ui.tooling.preview.Preview
*/
@Composable
fun PaywallFooter(
options: PaywallOptions = PaywallOptions.Builder().build(),
options: PaywallOptions,
condensed: Boolean = false,
mainContent: @Composable (PaddingValues) -> Unit,
) {
Expand All @@ -52,7 +52,9 @@ fun PaywallFooter(
@Preview(showBackground = true)
@Composable
private fun PaywallFooterPreview() {
PaywallFooter {
PaywallFooter(
options = PaywallOptions.Builder(dismissRequest = {}).build(),
) {
Column(
modifier = Modifier
.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@ internal sealed class OfferingSelection {
class PaywallOptions(builder: Builder) {

internal val offeringSelection: OfferingSelection
val shouldDisplayDismissButton: Boolean
private val shouldDisplayDismissButton: Boolean
val fontProvider: FontProvider?
val listener: PaywallListener?
internal var mode: PaywallMode = PaywallMode.default
val dismissRequest: () -> Unit

init {
this.offeringSelection = builder.offeringSelection
this.shouldDisplayDismissButton = builder.shouldDisplayDismissButton
this.fontProvider = builder.fontProvider
this.listener = builder.listener
this.dismissRequest = builder.dismissRequest
}

class Builder {
class Builder(
internal val dismissRequest: () -> Unit,
) {
internal var offeringSelection: OfferingSelection = OfferingSelection.None
internal var shouldDisplayDismissButton: Boolean = false
internal var fontProvider: FontProvider? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal class PaywallActivity : ComponentActivity(), PaywallListener {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val paywallOptions = PaywallOptions.Builder()
val paywallOptions = PaywallOptions.Builder(dismissRequest = ::finish)
.setOfferingId(getArgs()?.offeringId)
.setFontProvider(getFontProvider())
.setListener(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ internal fun DefaultPaywallPreview() {
serverDescription = "",
)
InternalPaywall(
options = PaywallOptions.Builder().build(),
options = PaywallOptions.Builder(dismissRequest = {}).build(),
viewModel = MockViewModel(offering = template2Offering),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,34 +174,34 @@ private object Template1UIConstants {

@Preview(showBackground = true)
@Composable
internal fun Template1PaywallPreview() {
private fun Template1PaywallPreview() {
InternalPaywall(
options = PaywallOptions.Builder().build(),
options = PaywallOptions.Builder(dismissRequest = {}).build(),
viewModel = MockViewModel(offering = TestData.template1Offering),
)
}

@Preview(showBackground = true)
@Composable
internal fun Template1FooterPaywallPreview() {
private fun Template1FooterPaywallPreview() {
InternalPaywall(
options = PaywallOptions.Builder().build(),
options = PaywallOptions.Builder(dismissRequest = {}).build(),
viewModel = MockViewModel(mode = PaywallMode.FOOTER, offering = TestData.template1Offering),
)
}

@Preview(showBackground = true)
@Composable
internal fun Template1CondensedFooterPaywallPreview() {
private fun Template1CondensedFooterPaywallPreview() {
InternalPaywall(
options = PaywallOptions.Builder().build(),
options = PaywallOptions.Builder(dismissRequest = {}).build(),
viewModel = MockViewModel(mode = PaywallMode.FOOTER_CONDENSED, offering = TestData.template1Offering),
)
}

@Preview(heightDp = 700, widthDp = 400)
@Composable
internal fun CircleMaskPreview() {
private fun CircleMaskPreview() {
Box {
Box(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private fun CheckmarkBox(isSelected: Boolean, colors: TemplateConfiguration.Colo
@Composable
private fun Template2PaywallPreview() {
InternalPaywall(
options = PaywallOptions.Builder().build(),
options = PaywallOptions.Builder(dismissRequest = {}).build(),
viewModel = MockViewModel(offering = TestData.template2Offering),
)
}
Expand All @@ -300,7 +300,7 @@ private fun Template2PaywallPreview() {
@Composable
private fun Template2PaywallFooterPreview() {
InternalPaywall(
options = PaywallOptions.Builder().build(),
options = PaywallOptions.Builder(dismissRequest = {}).build(),
viewModel = MockViewModel(mode = PaywallMode.FOOTER, offering = TestData.template2Offering),
)
}
Expand All @@ -310,7 +310,7 @@ private fun Template2PaywallFooterPreview() {
@Composable
private fun Template2PaywallFooterCondensedPreview() {
InternalPaywall(
options = PaywallOptions.Builder().build(),
options = PaywallOptions.Builder(dismissRequest = {}).build(),
viewModel = MockViewModel(mode = PaywallMode.FOOTER_CONDENSED, offering = TestData.template2Offering),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private fun Feature(
@Composable
private fun Template3Preview() {
InternalPaywall(
options = PaywallOptions.Builder().build(),
options = PaywallOptions.Builder(dismissRequest = {}).build(),
viewModel = MockViewModel(offering = TestData.template3Offering),
)
}
Expand All @@ -191,7 +191,7 @@ private fun Template3Preview() {
@Composable
private fun Template3FooterPreview() {
InternalPaywall(
options = PaywallOptions.Builder().build(),
options = PaywallOptions.Builder(dismissRequest = {}).build(),
viewModel = MockViewModel(mode = PaywallMode.FOOTER, offering = TestData.template3Offering),
)
}
Expand All @@ -200,7 +200,7 @@ private fun Template3FooterPreview() {
@Composable
private fun Template3CondensedFooterPreview() {
InternalPaywall(
options = PaywallOptions.Builder().build(),
options = PaywallOptions.Builder(dismissRequest = {}).build(),
viewModel = MockViewModel(mode = PaywallMode.FOOTER_CONDENSED, offering = TestData.template3Offering),
)
}
1 change: 1 addition & 0 deletions ui/revenuecatui/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="OK">نعم</string>
<string name="all_plans">كل الاشتراكات</string>
<string name="privacy">خصوصية</string>
<string name="privacy_policy">سياسة الخصوصية</string>
Expand Down
1 change: 1 addition & 0 deletions ui/revenuecatui/src/main/res/values-bg/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="OK">Добре</string>
<string name="all_plans">Всички абонаменти</string>
<string name="privacy">поверителност</string>
<string name="privacy_policy">Политика за поверителност</string>
Expand Down
1 change: 1 addition & 0 deletions ui/revenuecatui/src/main/res/values-ca/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="OK">D\'acord</string>
<string name="all_plans">Totes les subscripcions</string>
<string name="privacy">Privadesa</string>
<string name="privacy_policy">Política de privacitat</string>
Expand Down
1 change: 1 addition & 0 deletions ui/revenuecatui/src/main/res/values-cs/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="OK">OK</string>
<string name="all_plans">Všechna předplatná</string>
<string name="privacy">Soukromí</string>
<string name="privacy_policy">Zásady ochrany osobních údajů</string>
Expand Down
1 change: 1 addition & 0 deletions ui/revenuecatui/src/main/res/values-da/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="OK">Okay</string>
<string name="all_plans">Alle abonnementer</string>
<string name="privacy">Privatliv</string>
<string name="privacy_policy">Fortrolighedspolitik</string>
Expand Down
1 change: 1 addition & 0 deletions ui/revenuecatui/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="OK">OK</string>
<string name="all_plans">Alle Abonnements</string>
<string name="privacy">Privatsphäre</string>
<string name="privacy_policy">Datenschutzrichtlinie</string>
Expand Down
1 change: 1 addition & 0 deletions ui/revenuecatui/src/main/res/values-el/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="OK">Εντάξει</string>
<string name="all_plans">Όλες οι συνδρομές</string>
<string name="privacy">Μυστικότητα</string>
<string name="privacy_policy">Πολιτική απορρήτου</string>
Expand Down
1 change: 1 addition & 0 deletions ui/revenuecatui/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="OK">OK</string>
<string name="restore_purchases">Restaurar las compras</string>
<string name="restore">Restaurar</string>
<string name="terms_and_conditions">Términos y condiciones</string>
Expand Down
1 change: 1 addition & 0 deletions ui/revenuecatui/src/main/res/values-fi/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="OK">OK</string>
<string name="all_plans">Kaikki tilaukset</string>
<string name="privacy">Yksityisyys</string>
<string name="privacy_policy">Tietosuojakäytäntö</string>
Expand Down
1 change: 1 addition & 0 deletions ui/revenuecatui/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="OK">D\'ACCORD</string>
<string name="privacy">Confidentialité</string>
<string name="privacy_policy">Politique de confidentialité</string>
<string name="restore">Restaurer</string>
Expand Down
1 change: 1 addition & 0 deletions ui/revenuecatui/src/main/res/values-he/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="OK">בסדר</string>
<string name="all_plans">כל המנויים</string>
<string name="privacy">פְּרָטִיוּת</string>
<string name="privacy_policy">מדיניות הפרטיות</string>
Expand Down
1 change: 1 addition & 0 deletions ui/revenuecatui/src/main/res/values-hi/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="OK">ठीक है</string>
<string name="all_plans">सभी सदस्यताएँ</string>
<string name="privacy">गोपनीयता</string>
<string name="privacy_policy">गोपनीयता नीति</string>
Expand Down
Loading

0 comments on commit bd56068

Please sign in to comment.