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

Ensure the correct error message is shown when failing to open a Uri in paywalls #1922

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
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 @@ -28,9 +28,7 @@ import androidx.compose.ui.Alignment.Companion.CenterHorizontally
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.platform.UriHandler
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.semantics
Expand Down Expand Up @@ -85,8 +83,8 @@ private fun Footer(
childModifier: Modifier = Modifier,
allPlansTapped: (() -> Unit)? = null,
) {
val context = LocalContext.current
val uriHandler = LocalUriHandler.current
// Not using LocalContext, as that is not an Activity context in (Robolectric) tests.
val context = LocalView.current.context
val shouldDisplayAllPlansButton = mode == PaywallMode.FOOTER_CONDENSED && allPlansTapped != null
val anyButtonDisplayed = shouldDisplayAllPlansButton ||
configuration.displayRestorePurchases ||
Expand Down Expand Up @@ -147,7 +145,7 @@ private fun Footer(
R.string.terms_and_conditions,
R.string.terms,
) {
openURL(context, uriHandler, it)
openURL(context, it)
}

if (configuration.privacyURL != null) {
Expand All @@ -162,7 +160,7 @@ private fun Footer(
R.string.privacy_policy,
R.string.privacy,
) {
openURL(context, uriHandler, it)
openURL(context, it)
}
}
}
Expand Down Expand Up @@ -256,8 +254,8 @@ private object FooterConstants {
fun style(): TextStyle = MaterialTheme.typography.bodySmall
}

private fun openURL(context: Context, uriHandler: UriHandler, url: URL) {
uriHandler.openUriOrElse(url.toString()) { exception ->
private fun openURL(context: Context, url: URL) {
context.openUriOrElse(url.toString()) { exception ->
val msg = if (exception is ActivityNotFoundException) {
context.getString(R.string.no_browser_cannot_open_link)
} else {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.revenuecat.purchases.ui.revenuecatui.extensions

import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri

/**
* Opens the given [uri] in a browser. If the [uri] is invalid or no browser is installed, [fallbackAction] is called.
*/
@Suppress("SwallowedException")
fun Context.openUriOrElse(uri: String, fallbackAction: (e: Exception) -> Unit) {
val parsed = try {
Uri.parse(uri)
} catch (e: IllegalArgumentException) {
fallbackAction(e)
return
}

try {
startActivity(Intent(Intent.ACTION_VIEW, parsed))
} catch (e: ActivityNotFoundException) {
fallbackAction(e)
return
}
}