From 737c3ee1a1a812ce5ac97622e95ff97e849d0767 Mon Sep 17 00:00:00 2001 From: Chen Cen <79880926+ccen-stripe@users.noreply.github.com> Date: Fri, 24 May 2024 15:36:31 -0700 Subject: [PATCH] fix image resource in prod (#167) --- ...StripeIdentityVerificationSheetFragment.kt | 26 +++++++++++++++++-- example/android/app/build.gradle | 2 +- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/com/stripeidentityreactnative/StripeIdentityVerificationSheetFragment.kt b/android/src/main/java/com/stripeidentityreactnative/StripeIdentityVerificationSheetFragment.kt index c63217f..e72a6a3 100644 --- a/android/src/main/java/com/stripeidentityreactnative/StripeIdentityVerificationSheetFragment.kt +++ b/android/src/main/java/com/stripeidentityreactnative/StripeIdentityVerificationSheetFragment.kt @@ -1,10 +1,12 @@ package com.stripeidentityreactnative +import android.content.ContentResolver import android.net.Uri import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.webkit.URLUtil import android.widget.FrameLayout import androidx.fragment.app.Fragment import com.facebook.react.bridge.Promise @@ -34,8 +36,9 @@ class StripeIdentityVerificationSheetFragment : Fragment() { private fun createIdentityVerificationSheet(): IdentityVerificationSheet { verificationSessionId = arguments?.getString("sessionId").orEmpty() ephemeralKeySecret = arguments?.getString("ephemeralKeySecret").orEmpty() - val imageUri = arguments?.getBundle("brandLogo")?.getString("uri").orEmpty() - return IdentityVerificationSheet.create(this, IdentityVerificationSheet.Configuration(brandLogo = Uri.parse(imageUri))) { + val imageUriString = arguments?.getBundle("brandLogo")?.getString("uri").orEmpty() + return IdentityVerificationSheet.create(this, IdentityVerificationSheet.Configuration + (brandLogo = getUrlOrResourceId(imageUriString))) { promise?.let { currentPromise -> val result = WritableNativeMap() when (it) { @@ -51,6 +54,25 @@ class StripeIdentityVerificationSheetFragment : Fragment() { } } + private fun getUrlOrResourceId(uriString: String?): Uri { + return uriString?.let { + if (URLUtil.isValidUrl(it)) { + // Debug mode, Image.resolveAssetSource resolves to local http:// URL + Uri.parse(it) + } else { + // Release mode, Image.resolveAssetSource resolves to a drawable resource + val brandLogoId = resources.getIdentifier(it, "drawable", context?.packageName) // int + Uri.Builder() + .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) + .authority(resources.getResourcePackageName(brandLogoId)) + .appendPath(resources.getResourceTypeName(brandLogoId)) + .appendPath(resources.getResourceEntryName(brandLogoId)) + .build() + + } + } ?: Uri.parse("") + } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 69b1a77..c676445 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -77,7 +77,7 @@ import com.android.build.OutputFile project.ext.react = [ enableHermes: false, // clean and rebuild if changing - entryFile: "index.tsx", + entryFile: "index.js", ] apply from: "../../node_modules/react-native/react.gradle"