From 05149c52a9db350f209430244543cf32c001491a Mon Sep 17 00:00:00 2001 From: zfurtak Date: Thu, 19 Dec 2024 13:14:30 +0000 Subject: [PATCH] Refactored android methods --- .../react-native-wallet/android/build.gradle | 2 - .../src/main/java/com/wallet/WalletModule.kt | 144 ++++++++---------- modules/react-native-wallet/package-lock.json | 4 +- modules/react-native-wallet/package.json | 2 +- 4 files changed, 67 insertions(+), 85 deletions(-) diff --git a/modules/react-native-wallet/android/build.gradle b/modules/react-native-wallet/android/build.gradle index 2628b05827ff..8c77c59ddf78 100644 --- a/modules/react-native-wallet/android/build.gradle +++ b/modules/react-native-wallet/android/build.gradle @@ -39,14 +39,12 @@ def getExtOrIntegerDefault(name) { } android { - if (supportsNamespace()) { namespace "com.wallet" sourceSets { main { manifest.srcFile "src/main/AndroidManifestNew.xml" } - } } compileSdkVersion getExtOrIntegerDefault("compileSdkVersion") diff --git a/modules/react-native-wallet/android/src/main/java/com/wallet/WalletModule.kt b/modules/react-native-wallet/android/src/main/java/com/wallet/WalletModule.kt index 92e5dd2ccf7b..1d130cacd2da 100644 --- a/modules/react-native-wallet/android/src/main/java/com/wallet/WalletModule.kt +++ b/modules/react-native-wallet/android/src/main/java/com/wallet/WalletModule.kt @@ -1,6 +1,5 @@ package com.wallet -import android.R.attr.data import android.app.Activity import android.app.Activity.RESULT_CANCELED import android.app.Activity.RESULT_OK @@ -51,57 +50,28 @@ class WalletModule internal constructor(context: ReactApplicationContext) : Wall activity: Activity?, requestCode: Int, resultCode: Int, intent: Intent? ) { if (requestCode == REQUEST_CREATE_WALLET) { - pendingCreateWalletPromise?.let { - if (resultCode == RESULT_OK) { - it.resolve(true); - return - } - it.resolve(false); - } + pendingCreateWalletPromise?.resolve(resultCode == RESULT_OK) pendingCreateWalletPromise = null } else if (requestCode == REQUEST_CODE_PUSH_TOKENIZE) { if (resultCode == RESULT_OK) { - val tokenId: String = intent?.getStringExtra(TapAndPay.EXTRA_ISSUER_TOKEN_ID).toString() - sendEvent(context, "onCardActivated", OnCardActivatedEvent("active", tokenId).toMap()) - return + intent?.let{ + val tokenId = it.getStringExtra(TapAndPay.EXTRA_ISSUER_TOKEN_ID).toString() + sendEvent(context, "onCardActivated", OnCardActivatedEvent("active", tokenId).toMap()) + } } else if (resultCode == RESULT_CANCELED) { sendEvent(context, "onCardActivated", OnCardActivatedEvent("cancelled", null).toMap()) - return } } - return } - override fun onNewIntent(p0: Intent?) {} }) } - private fun sendEvent(reactContext: ReactContext, eventName: String, params: WritableMap?) { - reactContext - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) - .emit(eventName, params) - } - - private fun getCardNetwork(network: String): Int { - return when (network.uppercase(Locale.getDefault())) { - TSP_VISA -> TapAndPay.TOKEN_PROVIDER_VISA - TSP_MASTERCARD -> TapAndPay.TOKEN_PROVIDER_MASTERCARD - else -> 1000 - } - } - - private fun getTokenServiceProvider(network: String): Int { - return when (network.uppercase(Locale.getDefault())) { - TSP_VISA -> TapAndPay.TOKEN_PROVIDER_VISA - TSP_MASTERCARD -> TapAndPay.TOKEN_PROVIDER_MASTERCARD - else -> 1000 - } - } @ReactMethod override fun checkWalletAvailability(promise: Promise) { val localPromise = PromiseImpl({ _ -> promise.resolve(true) - }, { e -> + }, { _ -> pendingCreateWalletPromise = promise tapAndPayClient!!.createWallet(currentActivity!!, REQUEST_CREATE_WALLET) }) @@ -118,35 +88,23 @@ class WalletModule internal constructor(context: ReactApplicationContext) : Wall platform = "android", deviceID = hardwareId.await(), walletAccountID = walletId.await() ) - val walletDataJson = JSONObject().apply { - put("platform", walletData.platform) - put("deviceID", walletData.deviceID) - put("walletAccountID", walletData.walletAccountID) + val walletDataMap: WritableMap = Arguments.createMap().apply { + putString("platform", walletData.platform) + putString("deviceID", walletData.deviceID) + putString("walletAccountID", walletData.walletAccountID) } - promise.resolve(walletDataJson.toString()) + promise.resolve(walletDataMap) } catch (e: Exception) { promise.reject("Error", "Failed to retrieve IDs: ${e.localizedMessage}") } } } - private fun getCardStatusCode(code: Int): Int { - return when (code) { - TapAndPay.TOKEN_STATE_ACTIVE -> CardStatus.ACTIVE.code - TapAndPay.TOKEN_STATE_PENDING -> CardStatus.PENDING.code - TapAndPay.TOKEN_STATE_SUSPENDED -> CardStatus.SUSPENDED.code - TapAndPay.TOKEN_STATE_NEEDS_IDENTITY_VERIFICATION -> CardStatus.REQUIRE_IDENTITY_VERIFICATION.code - TapAndPay.TOKEN_STATE_FELICA_PENDING_PROVISIONING -> CardStatus.PENDING.code - else -> CardStatus.NOT_FOUND_IN_WALLET.code - } - } - @ReactMethod override fun getCardStatus(last4Digits: String, promise: Promise) { - if (!ensureTapAndPayClientInitialized()) { - promise.reject("Initialization error", "TapAndPay client initialization failed") + if (!ensureTapAndPayClientInitialized(promise)) { return } @@ -156,13 +114,12 @@ class WalletModule internal constructor(context: ReactApplicationContext) : Wall promise.reject("error", "no tokens available") return@addOnCompleteListener } - val token = task.result.find { it.fpanLastFour == last4Digits } - token?.let { + task.result.find { it.fpanLastFour == last4Digits }?.let { Log.i("getCardStatus", "Card Token State: ${it.tokenState}") promise.resolve( - getCardStatusCode(token.tokenState) + getCardStatusCode(it.tokenState) ) - } ?: promise.resolve(CardStatus.NOT_FOUND_IN_WALLET.code) + } ?: promise.resolve(CardStatus.NOT_FOUND_IN_WALLET.code) } .addOnFailureListener { e -> promise.reject("getCardStatus function failed", e) } .addOnCanceledListener { @@ -175,8 +132,7 @@ class WalletModule internal constructor(context: ReactApplicationContext) : Wall @ReactMethod override fun getCardTokenStatus(tsp: String, tokenRefId: String, promise: Promise) { - if (!ensureTapAndPayClientInitialized()) { - promise.reject("Initialization error", "TapAndPay client initialization failed") + if (!ensureTapAndPayClientInitialized(promise)) { return } @@ -186,12 +142,11 @@ class WalletModule internal constructor(context: ReactApplicationContext) : Wall promise.resolve(CardStatus.NOT_FOUND_IN_WALLET.code) return@addOnCompleteListener } - val token = task.result - token?.let { + task.result?.let { promise.resolve( - getCardStatusCode(token.tokenState) + getCardStatusCode(it.tokenState) ) - } ?: promise.resolve(CardStatus.NOT_FOUND_IN_WALLET.code) + } ?: promise.resolve(CardStatus.NOT_FOUND_IN_WALLET.code) } .addOnFailureListener { e -> promise.reject("getCardStatus function failed", e) } .addOnCanceledListener { @@ -207,7 +162,7 @@ class WalletModule internal constructor(context: ReactApplicationContext) : Wall override fun addCardToWallet( data: ReadableMap, promise: Promise ) { - if (!ensureTapAndPayClientInitialized()) return + if (!ensureTapAndPayClientInitialized(promise)) return try { val cardData = data.toCardData() ?: return promise.reject("Reject: ", "Insufficient data") @@ -227,33 +182,29 @@ class WalletModule internal constructor(context: ReactApplicationContext) : Wall .setUserAddress(cardData.userAddress) .build() - tapAndPayClient?.pushTokenize( + tapAndPayClient!!.pushTokenize( currentActivity!!, pushTokenizeRequest, REQUEST_CODE_PUSH_TOKENIZE ) - tapAndPayClient?.registerDataChangedListener { - Log.i("DUPA", "TEST") - } } catch (e: java.lang.Exception) { promise.reject(e) } } private fun getWalletId(promise: Promise) { - if (!ensureTapAndPayClientInitialized()) { - promise.reject("Initialization error", "TapAndPay client initialization failed") + if (!ensureTapAndPayClientInitialized(promise)) { return } - tapAndPayClient?.activeWalletId?.addOnCompleteListener { task -> + tapAndPayClient!!.activeWalletId.addOnCompleteListener { task -> if (task.isSuccessful) { val walletId = task.result if (walletId != null) { promise.resolve(walletId) } } - }?.addOnFailureListener { e -> + }.addOnFailureListener { e -> promise.reject("Wallet id retrieval failed", e) - }?.addOnCanceledListener { + }.addOnCanceledListener { promise.reject( "Reject: ", "Wallet id retrieval canceled" ) @@ -261,29 +212,62 @@ class WalletModule internal constructor(context: ReactApplicationContext) : Wall } private fun getHardwareId(promise: Promise) { - if (!ensureTapAndPayClientInitialized()) { - promise.reject("Initialization error", "TapAndPay client initialization failed") + if (!ensureTapAndPayClientInitialized(promise)) { return } - tapAndPayClient?.stableHardwareId?.addOnCompleteListener { task -> + tapAndPayClient!!.stableHardwareId.addOnCompleteListener { task -> if (task.isSuccessful) { val hardwareId = task.result promise.resolve(hardwareId) } - }?.addOnFailureListener { e -> + }.addOnFailureListener { e -> promise.reject("Stable hardware id retrieval failed", e) - }?.addOnCanceledListener { + }.addOnCanceledListener { promise.reject( "Reject: ", "Stable hardware id retrieval canceled" ) } } - private fun ensureTapAndPayClientInitialized(): Boolean { + private fun sendEvent(reactContext: ReactContext, eventName: String, params: WritableMap?) { + reactContext + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) + .emit(eventName, params) + } + + private fun getCardNetwork(network: String): Int { + return when (network.uppercase(Locale.getDefault())) { + TSP_VISA -> TapAndPay.TOKEN_PROVIDER_VISA + TSP_MASTERCARD -> TapAndPay.TOKEN_PROVIDER_MASTERCARD + else -> 1000 + } + } + + private fun getTokenServiceProvider(network: String): Int { + return when (network.uppercase(Locale.getDefault())) { + TSP_VISA -> TapAndPay.TOKEN_PROVIDER_VISA + TSP_MASTERCARD -> TapAndPay.TOKEN_PROVIDER_MASTERCARD + else -> 1000 + } + } + + private fun getCardStatusCode(code: Int): Int { + return when (code) { + TapAndPay.TOKEN_STATE_ACTIVE -> CardStatus.ACTIVE.code + TapAndPay.TOKEN_STATE_PENDING -> CardStatus.PENDING.code + TapAndPay.TOKEN_STATE_SUSPENDED -> CardStatus.SUSPENDED.code + TapAndPay.TOKEN_STATE_NEEDS_IDENTITY_VERIFICATION -> CardStatus.REQUIRE_IDENTITY_VERIFICATION.code + TapAndPay.TOKEN_STATE_FELICA_PENDING_PROVISIONING -> CardStatus.PENDING.code + else -> CardStatus.NOT_FOUND_IN_WALLET.code + } + } + + private fun ensureTapAndPayClientInitialized(promise: Promise): Boolean { if (tapAndPayClient == null && currentActivity != null) { tapAndPayClient = TapAndPay.getClient(currentActivity!!) } if (tapAndPayClient == null) { + promise.reject("Initialization error", "TapAndPay client initialization failed") return false } return true diff --git a/modules/react-native-wallet/package-lock.json b/modules/react-native-wallet/package-lock.json index fde4c31c6940..481021e60c9b 100644 --- a/modules/react-native-wallet/package-lock.json +++ b/modules/react-native-wallet/package-lock.json @@ -1,11 +1,11 @@ { - "name": "@expensify/react-native-wallet", + "name": "react-native-wallet", "version": "0.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "@expensify/react-native-wallet", + "name": "react-native-wallet", "version": "0.1.0", "license": "MIT", "workspaces": [ diff --git a/modules/react-native-wallet/package.json b/modules/react-native-wallet/package.json index 4610972ceabe..7b1cf7333059 100644 --- a/modules/react-native-wallet/package.json +++ b/modules/react-native-wallet/package.json @@ -198,4 +198,4 @@ "languages": "kotlin-objc", "version": "0.43.0" } -} +} \ No newline at end of file