Skip to content

Commit

Permalink
fix image resource in prod (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccen-stripe authored May 24, 2024
1 parent 10c031c commit 737c3ee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 737c3ee

Please sign in to comment.