From c4eb6be2ba3a0ddd44c5fcd885b905532e49f5f4 Mon Sep 17 00:00:00 2001 From: hyochan Date: Fri, 26 Jul 2024 17:05:03 +0900 Subject: [PATCH] refactor: events to struct and object --- .../java/expo/modules/iap/ExpoIapModule.kt | 17 +++++++---- ios/ExpoIapModule.swift | 28 ++++++++++--------- src/index.ts | 13 +++++++-- src/modules/ios.ts | 4 +-- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/android/src/main/java/expo/modules/iap/ExpoIapModule.kt b/android/src/main/java/expo/modules/iap/ExpoIapModule.kt index df5484e..8bf93b8 100644 --- a/android/src/main/java/expo/modules/iap/ExpoIapModule.kt +++ b/android/src/main/java/expo/modules/iap/ExpoIapModule.kt @@ -34,6 +34,11 @@ class ExpoIapModule : const val EMPTY_SKU_LIST = "EMPTY_SKU_LIST" } + object IapEvent { + const val PURCHASE_UPDATED = "purchase-updated" + const val PURCHASE_ERROR = "purchase-error" + } + private var billingClientCache: BillingClient? = null private val skus: MutableMap = mutableMapOf() private val context: Context @@ -57,7 +62,7 @@ class ExpoIapModule : val errorData = PlayUtils.getBillingResponseData(responseCode) error["code"] = errorData.code error["message"] = errorData.message - sendEvent("purchase-error", error) + sendEvent(IapEvent.PURCHASE_ERROR, error.toMap()) return } @@ -85,7 +90,7 @@ class ExpoIapModule : item["obfuscatedProfileIdAndroid"] = accountIdentifiers.obfuscatedProfileId } promiseItems.add(item.toMap()) - sendEvent("purchase-updated", item) + sendEvent(IapEvent.PURCHASE_UPDATED, item.toMap()) } } else { val result = @@ -95,7 +100,7 @@ class ExpoIapModule : "extraMessage" to "The purchases are null. This is a normal behavior if you have requested DEFERRED proration. If not please report an issue.", ) - sendEvent("purchase-updated", result) + sendEvent(IapEvent.PURCHASE_UPDATED, result.toMap()) } } @@ -105,7 +110,7 @@ class ExpoIapModule : Constants("PI" to Math.PI) - Events("purchase-error", "purchase-updated") + Events(IapEvent.PURCHASE_UPDATED, IapEvent.PURCHASE_ERROR) AsyncFunction("initConnection") { promise: Promise -> initBillingClient(promise) { promise.resolve(true) } @@ -303,7 +308,7 @@ class ExpoIapModule : val debugMessage = "The number of skus (${skuArr.size}) must match: the number of offerTokens (${offerTokenArr.size}) for Subscriptions" sendEvent( - "purchase-error", + IapEvent.PURCHASE_ERROR, mapOf( "debugMessage" to debugMessage, "code" to "E_SKU_OFFER_MISMATCH", @@ -320,7 +325,7 @@ class ExpoIapModule : val debugMessage = "The sku was not found. Please fetch products first by calling getItems" sendEvent( - "purchase-error", + IapEvent.PURCHASE_ERROR, mapOf( "debugMessage" to debugMessage, "code" to "E_SKU_NOT_FOUND", diff --git a/ios/ExpoIapModule.swift b/ios/ExpoIapModule.swift index 23b76cb..632e01f 100644 --- a/ios/ExpoIapModule.swift +++ b/ios/ExpoIapModule.swift @@ -9,6 +9,12 @@ func serializeDebug (_ s: String) -> String? { #endif } +struct IapEvent { + static let PurchaseUpdated = "purchase-updated" + static let PurchaseError = "purchase-error" + static let TransactionIapUpdated = "iap-transaction-updated" +} + @available(iOS 15.0, *) func serializeProduct(_ p: Product) -> [String: Any?] { return [ @@ -81,7 +87,7 @@ func serialize(_ transaction: Transaction, _ result: VerificationResult [String] { - return ["purchase-updated", "purchase-error", "iap-transaction-updated"] - } - private func currentWindowScene() async -> UIWindowScene? { await MainActor.run { return UIApplication.shared.connectedScenes.first as? UIWindowScene diff --git a/src/index.ts b/src/index.ts index 665f72f..7316a84 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,6 +31,12 @@ export * from './modules/ios'; // Get the native constant value. export const PI = ExpoIapModule.PI; +export enum IapEvent { + PurchaseUpdated = 'purchase-updated', + PurchaseError = 'purchase-error', + TransactionIapUpdated = 'iap-transaction-updated', +} + export async function setValueAsync(value: string) { return await ExpoIapModule.setValueAsync(value); } @@ -42,14 +48,17 @@ export const emitter = new EventEmitter( export const purchaseUpdatedListener = ( listener: (event: Purchase) => void, ) => { - const emitterSubscription = emitter.addListener('purchase-updated', listener); + const emitterSubscription = emitter.addListener( + IapEvent.PurchaseUpdated, + listener, + ); return emitterSubscription; }; export const purchaseErrorListener = ( listener: (error: PurchaseError) => void, ) => { - return emitter.addListener('purchase-error', listener); + return emitter.addListener(IapEvent.PurchaseError, listener); }; export function initConnection() { diff --git a/src/modules/ios.ts b/src/modules/ios.ts index 9ec4787..c5bedcb 100644 --- a/src/modules/ios.ts +++ b/src/modules/ios.ts @@ -1,5 +1,5 @@ import {Platform} from 'react-native'; -import {emitter} from '..'; +import {emitter, IapEvent} from '..'; import {Product, PurchaseError} from '../ExpoIap.types'; import type { ProductIos, @@ -21,7 +21,7 @@ export const transactionUpdatedIos = ( throw new Error('This method is only available on iOS'); } - return emitter.addListener('iap-transaction-updated', listener); + return emitter.addListener(IapEvent.TransactionIapUpdated, listener); }; // Functions