Skip to content

Commit

Permalink
refactor to move setting to config
Browse files Browse the repository at this point in the history
  • Loading branch information
kiftio committed Feb 29, 2024
1 parent f944bbf commit ef8d041
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,6 @@ public abstract class DefaultCheckoutEventProcessor @JvmOverloads constructor(
// no-op, override to implement
}

/**
* Override this function to provide any URLs that should result in a call to onCheckoutLinkClicked when
* being loaded.
*
* Useful if you have a checkout extension or a store policy with a link that should open outside of checkout
* e.g. return listOf("https://my-store.myshopify.com/policies/.*")
*/
public open fun urlPatternsThatTriggerOnCheckoutLinkClicked(): List<String> {
return emptyList()
}

private fun Context.launchEmailApp(to: String) {
val intent = Intent(Intent.ACTION_SEND)
intent.type = "vnd.android.cursor.item/email"
Expand Down
18 changes: 6 additions & 12 deletions lib/src/main/java/com/shopify/checkoutsheetkit/CheckoutWebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,17 @@ internal class CheckoutWebView(context: Context, attributeSet: AttributeSet? = n
view: WebView,
request: WebResourceRequest
): Boolean {
val processor = checkoutBridge.getEventProcessor()
if (shouldOpenExternally(request, processor)) {
processor.onCheckoutViewLinkClicked(request.trimmedUri())
if (shouldOpenExternally(request)) {
checkoutBridge.getEventProcessor().onCheckoutViewLinkClicked(request.trimmedUri())
return true
}
return false
}

private fun shouldOpenExternally(
request: WebResourceRequest,
checkoutWebViewEventProcessor: CheckoutWebViewEventProcessor
): Boolean {
val processor = checkoutWebViewEventProcessor.getClientProcessor()
if (processor is DefaultCheckoutEventProcessor) {
if (processor.urlPatternsThatTriggerOnCheckoutLinkClicked().any { request.url.toString().matches(it.toRegex()) }) {
return true
}
private fun shouldOpenExternally(request: WebResourceRequest): Boolean {
val patterns = ShopifyCheckoutSheetKit.configuration.urlPatternsThatTriggerOnCheckoutLinkClicked
if (patterns.any { request.url.toString().matches(it.toRegex()) }) {
return true
}

return request.hasExternalAnnotation() || request.url?.isContactLink() == true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ internal class CheckoutWebViewEventProcessor(
eventProcessor.onWebPixelEvent(event)
}

fun getClientProcessor(): CheckoutEventProcessor {
return eventProcessor
}

private fun onMainThread(block: () -> Unit) {
Handler(Looper.getMainLooper()).post {
block()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ package com.shopify.checkoutsheetkit
public data class Configuration internal constructor(
var colorScheme: ColorScheme = ColorScheme.Automatic(),
var preloading: Preloading = Preloading(),

/**
* Provide any URLs that should result in a call to onCheckoutLinkClicked when
* being loaded.
*
* Useful if you have a checkout extension or a store policy with a link that should open outside of checkout
* e.g. return listOf("https://my-store.myshopify.com/policies/.*")
*/
var urlPatternsThatTriggerOnCheckoutLinkClicked: List<String> = emptyList(),
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class MobileBuyIntegration : Application() {
ShopifyCheckoutSheetKit.configure {
it.colorScheme = settings.colorScheme
it.preloading = settings.preloading
it.urlPatternsThatTriggerOnCheckoutLinkClicked = listOf("https://checkout-sdk.myshopify.com/policies/.*")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,6 @@ fun CheckoutSdkNavHost(
cartViewModel = cartViewModel,
setAppBarState = setAppBarState,
checkoutEventProcessor = object : DefaultCheckoutEventProcessor(activity) {

override fun urlPatternsThatTriggerOnCheckoutLinkClicked(): List<String> {
return listOf(
"https://checkout-sdk.myshopify.com/policies/.*"
)
}

override fun onCheckoutCompleted(checkoutCompletedEvent: CheckoutCompletedEvent) {
logger.log(checkoutCompletedEvent)

Expand Down

0 comments on commit ef8d041

Please sign in to comment.