Skip to main content
Question

Best way to use Paywall within NavController (Jetpack Compose)


Forum|alt.badge.img

Let’s say I have a NavController with 5 pages, and 3 of them should be behind paywall - whenever the user navigates to those routes, i would like to show the paywall if needed (thankfully this is handled within the paywall composable itself) - but i don't want the composable behind the paywall to render/run at all.

The documentation shows this:
 

@OptIn(ExperimentalPreviewRevenueCatUIPurchasesAPI::class)
@Composable
private fun NavGraph(navController: NavHostController) {
    NavHost(
        navController = navController,
        startDestination = Screen.Main.route,
    ) {
        composable(route = Screen.Main.route) {
            MainScreen()

            PaywallDialog(
                PaywallDialogOptions.Builder()
                    .setShouldDisplayBlock { !it.entitlements.active.isEmpty() }
                    .setListener(
                        object : PaywallListener {
                            override fun onPurchaseCompleted(customerInfo: CustomerInfo, storeTransaction: StoreTransaction) {}
                            override fun onRestoreCompleted(customerInfo: CustomerInfo) {}
                        }
                    )
                    .build()
            )
        }
    }


But this technically runs all the code that is inside MainScreen()

What is the best practice to avoid that?
I tried retrieving if the customerinfo has the active subscription for that page, and if yes, i allow mainscreen() to be rendered, but I don’t know if thats the most optimal way.

This post has been closed for comments

Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings