Skip to content
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

Dismiss PaywallFragment after PaywallActivity is dismissed #1016

Merged
merged 4 commits into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.revenuecat.purchases.hybridcommon.ui

import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import com.revenuecat.purchases.Purchases
import com.revenuecat.purchases.ui.revenuecatui.ExperimentalPreviewRevenueCatUIPurchasesAPI
import com.revenuecat.purchases.ui.revenuecatui.activity.PaywallActivityLauncher
import com.revenuecat.purchases.ui.revenuecatui.activity.PaywallDisplayCallback
Expand Down Expand Up @@ -94,6 +96,18 @@ internal class PaywallFragment : Fragment(), PaywallResultHandler {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// This should normally never happen, but just in case, we don't want to try to present the paywall
// if the SDK is not configured.
if (!Purchases.isConfigured) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed... But I added it as a failsafe, in case we're missing any more cases.

Log.e(
"PaywallFragment",
"Purchases is not configured. " +
"Make sure to call Purchases.configure() before launching the paywall. Dismissing.",
)
removeFragment()
return
}

launcher = PaywallActivityLauncher(
this,
this,
Expand All @@ -108,6 +122,11 @@ internal class PaywallFragment : Fragment(), PaywallResultHandler {
override fun onActivityResult(result: PaywallResult) {
viewModel.paywallResultListener?.onPaywallResult(result)
viewModel.paywallResultListener?.onPaywallResult(result.name)
removeFragment()
}

private fun removeFragment() {
parentFragmentManager.beginTransaction().remove(this).commit()
}

private fun launchPaywallIfNeeded(requiredEntitlementIdentifier: String) {
Expand All @@ -119,6 +138,7 @@ internal class PaywallFragment : Fragment(), PaywallResultHandler {
override fun onPaywallDisplayResult(wasDisplayed: Boolean) {
if (!wasDisplayed) {
viewModel.paywallResultListener?.onPaywallResult(notPresentedPaywallResult)
removeFragment()
}
}
}
Expand Down