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: use LocalUriHandler for opening links #1388

Merged
merged 3 commits into from
Oct 25, 2023
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 @@ -166,7 +166,8 @@ object SamplePaywalls {
localization = mapOf(
"en_US" to PaywallData.LocalizedConfiguration(
title = "Ignite your child's curiosity",
subtitle = "Get access to all our educational content trusted by thousands of parents.",
subtitle = "Get access to **all our [educational content](https://rev.cat/paywalls)** " +
"trusted by _thousands_ of parents.",
callToAction = "Purchase for {{ price }}",
callToActionWithIntroOffer = "Purchase for {{ sub_price_per_month }} per month",
offerDetails = "{{ sub_price_per_month }} per month",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import com.revenuecat.purchases.ui.revenuecatui.templates.Template2
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import java.net.URL

@Composable
internal fun LoadingPaywall(mode: PaywallMode) {
Expand Down Expand Up @@ -175,10 +174,6 @@ private class LoadingViewModel(
error("Can't restore purchases")
}

override fun openURL(url: URL, context: Context) {
error("Can't open URL")
}

override fun clearActionError() = Unit
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
Expand Down Expand Up @@ -70,7 +70,7 @@ private fun Footer(
childModifier: Modifier = Modifier,
allPlansTapped: (() -> Unit)? = null,
) {
val context = LocalContext.current
val uriHandler = LocalUriHandler.current
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh nice! Yeah, I didn't know this existed, makes sense 👍


Row(
modifier = Modifier
Expand Down Expand Up @@ -118,7 +118,7 @@ private fun Footer(
childModifier = childModifier,
R.string.terms_and_conditions,
R.string.terms,
) { viewModel.openURL(it, context) }
) { uriHandler.openUri(it.toString()) }

if (configuration.privacyURL != null) {
Separator(color = color)
Expand All @@ -131,7 +131,7 @@ private fun Footer(
childModifier = childModifier,
R.string.privacy_policy,
R.string.privacy,
) { viewModel.openURL(it, context) }
) { uriHandler.openUri(it.toString()) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package com.revenuecat.purchases.ui.revenuecatui.composables

import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
Expand All @@ -14,6 +15,8 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextLayoutResult
Expand Down Expand Up @@ -346,14 +349,27 @@ private fun MarkdownText(
modifier: Modifier = Modifier,
) {
val layoutResult = remember { mutableStateOf<TextLayoutResult?>(null) }
val uriHandler = LocalUriHandler.current

Text(
text = text,
color = color,
style = style,
fontWeight = fontWeight,
textAlign = textAlign,
modifier = modifier,
modifier = modifier
.pointerInput(Unit) {
detectTapGestures { offset ->
layoutResult.value?.let { layoutResult ->
val position = layoutResult.getOffsetForPosition(offset)
text.getStringAnnotations(position, position)
.firstOrNull { it.tag == TAG_URL }
?.let {
uriHandler.openUri(it.item)
}
}
}
},
onTextLayout = { layoutResult.value = it },
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.revenuecat.purchases.ui.revenuecatui.data

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.compose.material3.ColorScheme
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.State
Expand Down Expand Up @@ -32,7 +30,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import java.net.URL

internal interface PaywallViewModel {
val state: StateFlow<PaywallState>
Expand All @@ -52,8 +49,6 @@ internal interface PaywallViewModel {
fun restorePurchases()

fun clearActionError()

fun openURL(url: URL, context: Context)
}

@Suppress("TooManyFunctions")
Expand Down Expand Up @@ -152,11 +147,6 @@ internal class PaywallViewModelImpl(
}
}

override fun openURL(url: URL, context: Context) {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is one downside of moving this outside the viewModel, which is that if we want to have events or some other logic when navigating to links, having it as part of the compose tree might not be as easy to test or handle... But we can do that later if we have to.

val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url.toString()))
context.startActivity(intent)
}

override fun clearActionError() {
_actionError.value = null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,6 @@ internal class MockViewModel(
}
}

override fun openURL(url: URL, context: Context) {
error("Can't open URL")
}

override fun clearActionError() = Unit

private fun simulateActionInProgress() {
Expand Down