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

Paywalls: PaywallDialogOptions no longer requires dismissRequest #1386

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -26,13 +26,10 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.revenuecat.paywallstester.MainActivity
import com.revenuecat.purchases.CustomerInfo
import com.revenuecat.purchases.Offering
import com.revenuecat.purchases.Offerings
import com.revenuecat.purchases.models.StoreTransaction
import com.revenuecat.purchases.ui.revenuecatui.PaywallDialog
import com.revenuecat.purchases.ui.revenuecatui.PaywallDialogOptions
import com.revenuecat.purchases.ui.revenuecatui.PaywallListener
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand Down Expand Up @@ -124,17 +121,9 @@ private fun OfferingsListScreen(

if (displayPaywallDialogOffering != null) {
PaywallDialog(
PaywallDialogOptions.Builder(
dismissRequest = {
displayPaywallDialogOffering = null
},
)
PaywallDialogOptions.Builder()
.setDismissRequest { displayPaywallDialogOffering = null }
.setOffering(displayPaywallDialogOffering)
.setListener(object : PaywallListener {
override fun onPurchaseCompleted(customerInfo: CustomerInfo, storeTransaction: StoreTransaction) {
displayPaywallDialogOffering = null
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is done by PaywallViewModelImpl now.

}
})
.build(),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ fun PaywallsScreen(
@Composable
private fun FullScreenDialog(currentState: DisplayPaywallState.FullScreen, onDismiss: () -> Unit) {
PaywallDialog(
PaywallDialogOptions.Builder(dismissRequest = onDismiss)
PaywallDialogOptions.Builder()
.setDismissRequest(onDismiss)
.setOffering(currentState.offering)
.setFontProvider(currentState.fontProvider)
.build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,26 @@ fun PaywallDialog(
}
}
if (shouldDisplayDialog) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm thinking, since we are going to be handling the visibility ourselves, we should remember this parameter through configuration changes. For example, when going between portrait/landscape, changing dark mode or locale,...

I haven't tested it but if I'm not wrong, currently, if we display the paywall, the user dismisses it, and then there is one of those configuration changes, it would reappear. I think we probably want to avoid that by making that state survive configuration changes. One of the options is to use rememberSaveable, instead of remember, that's probably the quickest. The other would be moving this state to a viewModel, which might be harder in this case since we don't have a viewModel for the dialog currently.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a really good point. I just read about the difference and that makes sense 👍🏻
I just changed this.

val dismissRequest = { shouldDisplayDialog = false }

Dialog(
onDismissRequest = paywallDialogOptions.dismissRequest,
onDismissRequest = dismissRequest,
properties = DialogProperties(usePlatformDefaultWidth = shouldUsePlatformDefaultWidth()),
) {
DialogScaffold(paywallDialogOptions)
DialogScaffold(paywallDialogOptions.toPaywallOptions(dismissRequest))
}
}
}

@Composable
private fun DialogScaffold(paywallDialogOptions: PaywallDialogOptions) {
private fun DialogScaffold(paywallOptions: PaywallOptions) {
Scaffold(modifier = Modifier.fillMaxSize()) { paddingValues ->
Box(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues),
) {
Paywall(paywallDialogOptions.toPaywallOptions())
Paywall(paywallOptions)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import com.revenuecat.purchases.ui.revenuecatui.helpers.shouldDisplayBlockForEnt

class PaywallDialogOptions(builder: Builder) {

val dismissRequest: () -> Unit
val shouldDisplayBlock: ((CustomerInfo) -> Boolean)?
val dismissRequest: (() -> Unit)?
val offering: Offering?
val shouldDisplayDismissButton: Boolean
val fontProvider: FontProvider?
Expand All @@ -23,19 +23,21 @@ class PaywallDialogOptions(builder: Builder) {
this.listener = builder.listener
}

internal fun toPaywallOptions(): PaywallOptions {
return PaywallOptions.Builder(dismissRequest)
internal fun toPaywallOptions(dismissRequest: () -> Unit): PaywallOptions {
return PaywallOptions.Builder {
dismissRequest()
this.dismissRequest?.invoke()
}
.setOffering(offering)
.setShouldDisplayDismissButton(shouldDisplayDismissButton)
.setFontProvider(fontProvider)
.setListener(listener)
.build()
}

class Builder(
val dismissRequest: () -> Unit,
) {
class Builder {
internal var shouldDisplayBlock: ((CustomerInfo) -> Boolean)? = null
internal var dismissRequest: (() -> Unit)? = null
internal var offering: Offering? = null
internal var shouldDisplayDismissButton: Boolean = true
internal var fontProvider: FontProvider? = null
Expand All @@ -57,6 +59,10 @@ class PaywallDialogOptions(builder: Builder) {
}
}

fun setDismissRequest(dismissRequest: () -> Unit) = apply {
this.dismissRequest = dismissRequest
}

fun setOffering(offering: Offering?) = apply {
this.offering = offering
}
Expand Down