react-native-nami-sdk 2.0.5 → 3.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/.github/workflows/app_stg.yaml +203 -0
  2. package/.pre-commit-config.yaml +1 -1
  3. package/android/build.gradle +24 -13
  4. package/android/gradle/wrapper/gradle-wrapper.properties +5 -1
  5. package/android/src/main/java/com/nami/reactlibrary/NamiBridgeModule.kt +6 -50
  6. package/android/src/main/java/com/nami/reactlibrary/NamiBridgePackage.java +1 -0
  7. package/android/src/main/java/com/nami/reactlibrary/NamiCampaignManagerBridge.kt +133 -0
  8. package/android/src/main/java/com/nami/reactlibrary/NamiCustomerManagerBridge.kt +88 -19
  9. package/android/src/main/java/com/nami/reactlibrary/NamiEmitter.kt +24 -24
  10. package/android/src/main/java/com/nami/reactlibrary/NamiEntitlementManagerBridgeModule.kt +30 -129
  11. package/android/src/main/java/com/nami/reactlibrary/NamiPaywallManagerBridgeModule.kt +36 -147
  12. package/android/src/main/java/com/nami/reactlibrary/NamiPurchaseManagerBridge.kt +36 -38
  13. package/android/src/main/java/com/nami/reactlibrary/NamiUtil.kt +50 -180
  14. package/build-utils/get_version_code.py +140 -0
  15. package/index.d.ts +20 -0
  16. package/index.js +7 -6
  17. package/ios/Nami.m +9 -63
  18. package/ios/NamiBridgeUtil.h +4 -6
  19. package/ios/NamiBridgeUtil.m +16 -78
  20. package/ios/NamiCampaignManagerBridge.m +26 -0
  21. package/ios/NamiCampaignManagerBridge.swift +107 -0
  22. package/ios/NamiCustomerManager.m +18 -23
  23. package/ios/NamiCustomerManager.swift +122 -0
  24. package/ios/NamiEmitter.m +55 -65
  25. package/ios/NamiEntitlementManagerBridge.m +7 -107
  26. package/ios/NamiEntitlementManagerBridge.swift +74 -0
  27. package/ios/NamiMLManagerBridge.m +2 -2
  28. package/ios/NamiPaywallManagerBridge.m +22 -94
  29. package/ios/NamiPaywallManagerBridge.swift +93 -0
  30. package/ios/NamiPurchaseManagerBridge.m +38 -69
  31. package/ios/NamiPurchaseManagerBridge.swift +174 -0
  32. package/ios/Podfile +2 -2
  33. package/ios/RNNami-Bridging-Header.h +5 -0
  34. package/ios/RNNami.h +0 -1
  35. package/ios/RNNami.m +1 -1
  36. package/ios/RNNami.xcodeproj/project.pbxproj +84 -8
  37. package/ios/RNNami.xcodeproj/xcshareddata/xcschemes/RNNami.xcscheme +67 -0
  38. package/package.json +1 -1
  39. package/react-native-nami-sdk.podspec +3 -3
  40. package/src/Nami.d.ts +112 -0
  41. package/src/Nami.js +10 -0
  42. package/src/NamiCampaignManager.d.ts +54 -0
  43. package/src/NamiCampaignManager.js +43 -0
  44. package/src/NamiCustomerManager.d.ts +39 -0
  45. package/src/NamiCustomerManager.js +43 -0
  46. package/src/NamiEntitlementManager.d.ts +24 -0
  47. package/src/NamiEntitlementManager.js +23 -0
  48. package/src/NamiMLManager.d.ts +5 -0
  49. package/src/NamiMLManager.js +7 -0
  50. package/src/NamiPaywallManager.d.ts +57 -0
  51. package/src/NamiPaywallManager.js +28 -0
  52. package/src/NamiPurchaseManager.d.ts +57 -0
  53. package/src/NamiPurchaseManager.js +37 -0
  54. package/src/types.ts +36 -0
  55. package/android/src/main/java/com/nami/reactlibrary/NamiAnalyticsEmitter.kt +0 -121
  56. package/ios/NamiAnalyticsEmitter.m +0 -146
@@ -2,12 +2,11 @@
2
2
  // NamiBridgeUtil.m
3
3
  // RNNami
4
4
  //
5
- // Created by Kendall Gelner on 1/9/20.
6
- // Copyright © 2020 Nami ML Inc. All rights reserved.
5
+ // Copyright © 2020-2023 Nami ML Inc. All rights reserved.
7
6
  //
8
7
 
9
8
  #import <Foundation/Foundation.h>
10
- #import <Nami/Nami.h>
9
+ #import <NamiApple/NamiApple.h>
11
10
 
12
11
  #import "NamiBridgeUtil.h"
13
12
 
@@ -17,7 +16,7 @@
17
16
  + (NSDictionary<NSString *,NSString *> *) skuToSKUDict:(NamiSKU *)sku {
18
17
  NSMutableDictionary<NSString *,NSString *> *productDict = [NSMutableDictionary new];
19
18
 
20
- productDict[@"skuIdentifier"] = sku.platformID;
19
+ productDict[@"skuIdentifier"] = sku.platformId;
21
20
 
22
21
  SKProduct *productInt = sku.product;
23
22
  productDict[@"localizedTitle"] = productInt.localizedTitle;
@@ -30,8 +29,8 @@
30
29
  productDict[@"priceCurrency"] = productInt.priceLocale.currencyCode;
31
30
 
32
31
  // Add smart text processed values for sku buttons to sku dictionary
33
- productDict[@"displayText"] = [sku namiDisplayText];
34
- productDict[@"displaySubText"] = [sku namiSubDisplayText];
32
+ // productDict[@"displayText"] = [sku localizedDisplayText];
33
+ // productDict[@"displaySubText"] = [sku localizedSubDisplayText];
35
34
 
36
35
  if (@available(iOS 12.0, *)) {
37
36
  if (productInt != nil && productInt.subscriptionGroupIdentifier != nil) {
@@ -72,7 +71,7 @@
72
71
  + (NSDictionary<NSString *,NSString *> *) purchaseToPurchaseDict:(NamiPurchase *)purchase {
73
72
  NSMutableDictionary<NSString *,id> *purchaseDict = [NSMutableDictionary new];
74
73
 
75
- purchaseDict[@"skuIdentifier"] = purchase.skuID;
74
+ purchaseDict[@"skuIdentifier"] = purchase.skuId;
76
75
  purchaseDict[@"transactionIdentifier"] = purchase.transactionIdentifier;
77
76
 
78
77
  // Removed, not sure why, should add back in when possible.
@@ -123,12 +122,12 @@
123
122
  + (NSDictionary<NSString *,NSString *> *) entitlementToEntitlementDict:(NamiEntitlement *)entitlement {
124
123
  NSMutableDictionary<NSString *,id> *entitlementDict = [NSMutableDictionary new];
125
124
  NSLog(@"Converting entitlement %@", entitlement);
126
- entitlementDict[@"referenceID"] = [entitlement referenceID];
127
- entitlementDict[@"namiID"] = [entitlement namiID] ? [entitlement namiID] : @"";
125
+ entitlementDict[@"referenceID"] = [entitlement referenceId];
126
+ entitlementDict[@"namiID"] = [entitlement namiId] ? [entitlement namiId] : @"";
128
127
  entitlementDict[@"desc"] = [entitlement desc] ? [entitlement desc] : @"";
129
128
  entitlementDict[@"name"] = [entitlement name] ? [entitlement name] : @"";
130
129
 
131
- if (entitlementDict[@"referenceID"] == nil || [[entitlement referenceID] length] == 0) {
130
+ if (entitlementDict[@"referenceID"] == nil || [[entitlement referenceId] length] == 0) {
132
131
  NSLog(@"NamiBridge: Bad entitlement in system, empty referenceID.");
133
132
  return nil;
134
133
  }
@@ -143,7 +142,7 @@
143
142
  }
144
143
  entitlementDict[@"activePurchases"] = convertedActivePurchases;
145
144
 
146
- NSArray <NamiSKU *>*purchasedSKUs = [entitlement purchasedSKUs];
145
+ NSArray <NamiSKU *>*purchasedSKUs = [entitlement purchasedSkus];
147
146
  NSMutableArray *convertedPurchasedSKUs = [NSMutableArray array];
148
147
  for (NamiSKU *sku in purchasedSKUs) {
149
148
  NSDictionary *skuDict = [NamiBridgeUtil skuToSKUDict:sku];
@@ -154,7 +153,7 @@
154
153
  entitlementDict[@"purchasedSKUs"] = convertedPurchasedSKUs;
155
154
 
156
155
 
157
- NSArray <NamiSKU *>*relatedSKUs = [entitlement relatedSKUs];
156
+ NSArray <NamiSKU *>*relatedSKUs = [entitlement relatedSkus];
158
157
  NSMutableArray *convertedRelatedSKUs = [NSMutableArray array];
159
158
  for (NamiSKU *sku in relatedSKUs) {
160
159
  NSDictionary *skuDict = [NamiBridgeUtil skuToSKUDict:sku];
@@ -174,19 +173,19 @@
174
173
  // entitlementDict[@"latestPurchase"] = [NamiBridgeUtil purchaseToPurchaseDict:lastPurchase];
175
174
  }
176
175
 
177
- NSString *lastPurchaseSKUID = [lastPurchase skuID];
176
+ NSString *lastPurchaseSKUID = [lastPurchase skuId];
178
177
 
179
178
  NamiSKU *lastPurchasedSKU;
180
179
  if (lastPurchaseSKUID != NULL ) {
181
- for (NamiSKU *sku in [entitlement purchasedSKUs]) {
182
- if ( [[sku platformID] isEqualToString:lastPurchaseSKUID] ) {
180
+ for (NamiSKU *sku in [entitlement purchasedSkus]) {
181
+ if ( [[sku platformId] isEqualToString:lastPurchaseSKUID] ) {
183
182
  lastPurchasedSKU = sku;
184
183
  }
185
184
  }
186
185
  }
187
186
 
188
187
  if (lastPurchasedSKU != NULL) {
189
- lastPurchasedSKU = [[entitlement purchasedSKUs] lastObject];
188
+ lastPurchasedSKU = [[entitlement purchasedSkus] lastObject];
190
189
  }
191
190
 
192
191
  if (lastPurchasedSKU != NULL) {
@@ -207,7 +206,7 @@
207
206
 
208
207
 
209
208
  + (NSDictionary<NSString *,NSString *> *) customerJourneyStateDict {
210
- CustomerJourneyState *journeyState = [NamiCustomerManager currentCustomerJourneyState];
209
+ CustomerJourneyState *journeyState = [NamiCustomerManager journeyState];
211
210
 
212
211
  BOOL formerSubscriber = [journeyState formerSubscriber];
213
212
  BOOL inGracePeriod = [journeyState inGracePeriod];
@@ -229,65 +228,4 @@
229
228
  return journeyDict;
230
229
  }
231
230
 
232
- + (NSDictionary<NSString *,NSString *> *) paywallStylingToPaywallStylingDict:(PaywallStyleData *)styling {
233
- NSMutableDictionary<NSString *,id> *stylingDict = [NSMutableDictionary new];
234
- if (styling != nil) {
235
- stylingDict[@"backgroundColor"] = [NamiBridgeUtil hexStringForColor: styling.backgroundColor];
236
-
237
- stylingDict[@"bodyFontSize"] = @(styling.bodyFontSize);
238
- stylingDict[@"bodyTextColor"] = [NamiBridgeUtil hexStringForColor: styling.bodyTextColor];
239
- stylingDict[@"bodyShadowColor"] = [NamiBridgeUtil hexStringForColor: styling.bodyShadowColor];
240
- stylingDict[@"bodyShadowRadius"] = @(styling.bodyShadowRadius);
241
-
242
- stylingDict[@"titleFontSize"] = @(styling.titleFontSize);
243
- stylingDict[@"titleTextColor"] = [NamiBridgeUtil hexStringForColor: styling.titleTextColor];
244
- stylingDict[@"titleShadowColor"] = [NamiBridgeUtil hexStringForColor: styling.titleShadowColor];
245
- stylingDict[@"titleShadowRadius"] = @(styling.titleShadowRadius);
246
-
247
- stylingDict[@"closeButtonFontSize"] = @(styling.closeButtonFontSize);
248
- stylingDict[@"closeButtonTextColor"] = [NamiBridgeUtil hexStringForColor: styling.closeButtonTextColor];
249
- stylingDict[@"closeButtonShadowColor"] = [NamiBridgeUtil hexStringForColor: styling.closeButtonShadowColor];
250
- stylingDict[@"closeButtonShadowRadius"] = @(styling.closeButtonShadowRadius);
251
-
252
- stylingDict[@"bottomOverlayColor"] = [NamiBridgeUtil hexStringForColor: styling.bottomOverlayColor];
253
- stylingDict[@"bottomOverlayCornerRadius"] = @(styling.bottomOverlayCornerRadius);
254
-
255
- stylingDict[@"skuButtonColor"] = [NamiBridgeUtil hexStringForColor: styling.skuButtonColor];
256
- stylingDict[@"skuButtonTextColor"] = [NamiBridgeUtil hexStringForColor: styling.skuButtonTextColor];
257
-
258
- stylingDict[@"featuredSkusButtonColor"] = [NamiBridgeUtil hexStringForColor: styling.featuredSkusButtonColor];
259
- stylingDict[@"featuredSkusButtonTextColor"] = [NamiBridgeUtil hexStringForColor: styling.featuredSkusButtonTextColor];
260
-
261
- stylingDict[@"signinButtonFontSize"] = @(styling.signinButtonFontSize);
262
- stylingDict[@"signinButtonTextColor"] = [NamiBridgeUtil hexStringForColor: styling.signinButtonTextColor];
263
- stylingDict[@"signinButtonShadowColor"] = [NamiBridgeUtil hexStringForColor: styling.signinButtonShadowColor];
264
- stylingDict[@"signinButtonShadowRadius"] = @(styling.signinButtonShadowRadius);
265
-
266
- stylingDict[@"restoreButtonFontSize"] = @(styling.restoreButtonFontSize);
267
- stylingDict[@"restoreButtonTextColor"] = [NamiBridgeUtil hexStringForColor: styling.restoreButtonTextColor];
268
- stylingDict[@"restoreButtonShadowColor"] = [NamiBridgeUtil hexStringForColor: styling.restoreButtonShadowColor];
269
- stylingDict[@"restoreButtonShadowRadius"] = @(styling.restoreButtonShadowRadius);
270
-
271
- stylingDict[@"purchaseTermsFontSize"] = @(styling.purchaseTermsFontSize);
272
- stylingDict[@"purchaseTermsTextColor"] = [NamiBridgeUtil hexStringForColor: styling.purchaseTermsTextColor];
273
- stylingDict[@"purchaseTermsShadowColor"] = [NamiBridgeUtil hexStringForColor: styling.purchaseTermsShadowColor];
274
- stylingDict[@"purchaseTermsShadowRadius"] = @(styling.purchaseTermsShadowRadius);
275
-
276
-
277
- stylingDict[@"termsLinkColor"] = styling.termsLinkColor;
278
- }
279
-
280
- return stylingDict;
281
- }
282
-
283
- + (NSString *)hexStringForColor:(UIColor *)color {
284
- CGFloat r;
285
- CGFloat g;
286
- CGFloat b;
287
- CGFloat a;
288
- [color getRed:&r green:&g blue:&b alpha:&a];
289
- NSString *hexString=[NSString stringWithFormat:@"#%02X%02X%02X", (int)(r * 255), (int)(g * 255), (int)(b * 255)];
290
- return hexString;
291
- }
292
-
293
231
  @end
@@ -0,0 +1,26 @@
1
+ //
2
+ // NamiCampaignManager.m
3
+ // RNNami
4
+ //
5
+ // Copyright © 2023 Nami ML INc.. All rights reserved.
6
+ //
7
+
8
+ #import <React/RCTBridgeModule.h>
9
+
10
+ @interface RCT_EXTERN_MODULE(RNNamiCampaignManager, NSObject)
11
+
12
+ RCT_EXTERN_METHOD(launch:(nullable NSString *)label completion:(RCTResponseSenderBlock)callback paywallCompletion:(RCTResponseSenderBlock)cpaywallCallback)
13
+
14
+ RCT_EXTERN_METHOD(allCampaigns:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
15
+
16
+ RCT_EXTERN_METHOD(isCampaignAvailable:(nullable NSString *)label resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
17
+
18
+ RCT_EXTERN_METHOD(refresh)
19
+
20
+ RCT_EXTERN_METHOD(registerAvailableCampaignsHandler)
21
+
22
+ + (BOOL)requiresMainQueueSetup {
23
+ return YES;
24
+ }
25
+
26
+ @end
@@ -0,0 +1,107 @@
1
+ //
2
+ // NamiCampaignManagerBridge.swift
3
+ // RNNami
4
+ //
5
+ // Copyright © 2023 Nami ML Inc. All rights reserved.
6
+ //
7
+
8
+ import Foundation
9
+ import NamiApple
10
+ import React
11
+
12
+ @objc(RNNamiCampaignManager)
13
+ class RNNamiCampaignManager: RCTEventEmitter {
14
+ override func supportedEvents() -> [String]! {
15
+ return ["AvailableCampaignsChanged", "ResultCampaign"]
16
+ }
17
+
18
+ private func campaignInToDictionary(_ campaign: NamiCampaign) -> NSDictionary {
19
+ let dictionary: [String: Any?] = [
20
+ "id": campaign.id,
21
+ "rule": campaign.rule,
22
+ "segment": campaign.segment,
23
+ "paywall": campaign.paywall,
24
+ "type": campaign.type.rawValue,
25
+ "value": campaign.value,
26
+ ]
27
+ return NSDictionary(dictionary: dictionary.compactMapValues { $0 })
28
+ }
29
+
30
+ @objc(launch:completion:paywallCompletion:)
31
+ func launch(label: String?, callback: @escaping RCTResponseSenderBlock, paywallCallback _: @escaping RCTResponseSenderBlock) {
32
+ NamiCampaignManager.launch(label: label, launchHandler: { success, error in
33
+ callback([success, error?._code as Any])
34
+ }, paywallActionHandler: { action, sku, purchaseError, purchases in
35
+ let actionString: String
36
+ switch action {
37
+ case .show_paywall:
38
+ actionString = "SHOW_PAYWALL"
39
+ case .close_paywall:
40
+ actionString = "CLOSE_PAYWALL"
41
+ case .restore_purchases:
42
+ actionString = "RESTORE_PURCHASES"
43
+ case .sign_in:
44
+ actionString = "SIGN_IN"
45
+ case .buy_sku:
46
+ actionString = "BUY_SKU"
47
+ case .select_sku:
48
+ actionString = "SELECT_SKU"
49
+ case .purchase_selected_sku:
50
+ actionString = "PURCHASE_SELECTED_SKU"
51
+ case .purchase_success:
52
+ actionString = "PURCHASE_SUCCESS"
53
+ case .purchase_deferred:
54
+ actionString = "PURCHASE_DEFERRED"
55
+ case .purchase_failed:
56
+ actionString = "PURCHASE_FAILED"
57
+ case .purchase_cancelled:
58
+ actionString = "PURCHASE_CANCELLED"
59
+ case .purchase_unknown:
60
+ actionString = "PURCHASE_UNKNOWN"
61
+ @unknown default:
62
+ actionString = "PURCHASE_UNKNOWN"
63
+ }
64
+ let skuId = sku?.skuId
65
+ let errorSting = purchaseError?.localizedDescription
66
+ let dictionaries = purchases.map { purchase in NamiBridgeUtil.purchase(toPurchaseDict: purchase) }
67
+ let payload: [String: Any?] = [
68
+ "action": actionString,
69
+ "skuId": skuId,
70
+ "purchaseError": errorSting,
71
+ "purchases": dictionaries
72
+ ]
73
+ self.sendEvent(withName: "ResultCampaign", body: payload)
74
+ })
75
+ }
76
+
77
+ @objc(allCampaigns:rejecter:)
78
+ func allCampaigns(resolve: @escaping RCTPromiseResolveBlock, reject _: @escaping RCTPromiseRejectBlock) {
79
+ let campaigns = NamiCampaignManager.allCampaigns()
80
+ let dictionaries = campaigns.map { campaign in self.campaignInToDictionary(campaign) }
81
+ resolve(dictionaries)
82
+ }
83
+
84
+ @objc(isCampaignAvailable:resolver:rejecter:)
85
+ func isCampaignAvailable(label: String?, resolve: @escaping RCTPromiseResolveBlock, reject _: @escaping RCTPromiseRejectBlock) {
86
+ let isCampaignAvailable: Bool
87
+ if let label = label {
88
+ isCampaignAvailable = NamiCampaignManager.isCampaignAvailable(label: label)
89
+ } else {
90
+ isCampaignAvailable = NamiCampaignManager.isCampaignAvailable()
91
+ }
92
+ resolve(isCampaignAvailable)
93
+ }
94
+
95
+ @objc(refresh)
96
+ func refresh() {
97
+ NamiCampaignManager.refresh()
98
+ }
99
+
100
+ @objc(registerAvailableCampaignsHandler)
101
+ func registerForAvailableCampaigns() {
102
+ NamiCampaignManager.registerAvailableCampaignsHandler { availableCampaigns in
103
+ let dictionaries = availableCampaigns.map { campaign in self.campaignInToDictionary(campaign) }
104
+ self.sendEvent(withName: "AvailableCampaignsChanged", body: dictionaries)
105
+ }
106
+ }
107
+ }
@@ -2,42 +2,37 @@
2
2
  // NamiCustomerManager.m
3
3
  // RNNami
4
4
  //
5
- // Created by Kendall Gelner on 8/21/20.
6
- // Copyright © 2020 Nami ML Inc. All rights reserved.
5
+ // Copyright © 2020-2023 Nami ML Inc. All rights reserved.
7
6
  //
8
7
 
9
- #import <Foundation/Foundation.h>
10
- #import <Nami/Nami.h>
11
- #import "NamiBridgeUtil.h"
12
-
13
8
  #import <React/RCTBridgeModule.h>
14
- #import <React/RCTEventEmitter.h>
15
9
 
16
- #import "React/RCTViewManager.h"
10
+ @interface RCT_EXTERN_MODULE(RNNamiCustomerManager, NSObject)
17
11
 
18
- @interface NamiCustomerManagerBridge : NSObject <RCTBridgeModule>
19
- @end
20
- @implementation NamiCustomerManagerBridge (RCTExternModule)
12
+ RCT_EXTERN_METHOD(setCustomerAttribute:(NSString *)key value:(NSString *)value)
21
13
 
14
+ RCT_EXTERN_METHOD(getCustomerAttribute:(NSString *)key resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
22
15
 
23
- RCT_EXPORT_METHOD(currentCustomerJourneyState:(RCTResponseSenderBlock)completion)
24
- {
25
- NSDictionary *journeyDict = [NamiBridgeUtil customerJourneyStateDict];
16
+ RCT_EXTERN_METHOD(clearCustomerAttribute:(NSString *)key)
26
17
 
27
- completion(@[journeyDict]);
28
- }
18
+ RCT_EXTERN_METHOD(clearAllCustomerAttributes)
29
19
 
20
+ RCT_EXTERN_METHOD(journeyState:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
30
21
 
31
- @end
22
+ RCT_EXTERN_METHOD(isLoggedIn:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
32
23
 
24
+ RCT_EXTERN_METHOD(loggedInId:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
33
25
 
34
- @implementation NamiCustomerManagerBridge
35
- RCT_EXPORT_MODULE_NO_LOAD(NamiCustomerManagerBridge, NamiCustomerManagerBridge)
26
+ RCT_EXTERN_METHOD(deviceId:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
27
+
28
+ RCT_EXTERN_METHOD(login:(NSString *)customerId completion:(RCTResponseSenderBlock)callback)
29
+
30
+ RCT_EXTERN_METHOD(logout:(RCTResponseSenderBlock)callback)
31
+
32
+ RCT_EXTERN_METHOD(registerJourneyStateHandler)
33
+
34
+ RCT_EXTERN_METHOD(registerAccountStateHandler)
36
35
 
37
- - (dispatch_queue_t)methodQueue
38
- {
39
- return dispatch_get_main_queue();
40
- }
41
36
 
42
37
  + (BOOL)requiresMainQueueSetup {
43
38
  return YES;
@@ -0,0 +1,122 @@
1
+ //
2
+ // NamiCustomerManager.swift
3
+ // RNNami
4
+ //
5
+ // Copyright © 2023 Nami ML INc.. All rights reserved.
6
+ //
7
+
8
+ import Foundation
9
+ import NamiApple
10
+ import React
11
+
12
+ @objc(RNNamiCustomerManager)
13
+ class RNNamiCustomerManager: RCTEventEmitter {
14
+ override func supportedEvents() -> [String]! {
15
+ return ["JourneyStateChanged", "AccountStateChanged"]
16
+ }
17
+
18
+ private func journeyStateToDictionary(_ journeyState: CustomerJourneyState) -> NSDictionary {
19
+ let dictionary: [String: Any?] = [
20
+ "formerSubscriber": journeyState.formerSubscriber,
21
+ "inGracePeriod": journeyState.inGracePeriod,
22
+ "inTrialPeriod": journeyState.inTrialPeriod,
23
+ "inIntroOfferPeriod": journeyState.inIntroOfferPeriod,
24
+ "isCancelled": journeyState.isCancelled,
25
+ "inPause": journeyState.inPause,
26
+ "inAccountHold": journeyState.inAccountHold,
27
+ ]
28
+ return NSDictionary(dictionary: dictionary.compactMapValues { $0 })
29
+ }
30
+
31
+ @objc(setCustomerAttribute:value:)
32
+ func setCustomerAttribute(key: String, value: String) {
33
+ NamiCustomerManager.setCustomerAttribute(key, value)
34
+ }
35
+
36
+ @objc(getCustomerAttribute:resolver:rejecter:)
37
+ func getCustomerAttribute(key: String, resolve: @escaping RCTPromiseResolveBlock, reject _: @escaping RCTPromiseRejectBlock) {
38
+ let customerAttribute = NamiCustomerManager.getCustomerAttribute(key: key)
39
+ resolve(customerAttribute)
40
+ }
41
+
42
+ @objc(clearCustomerAttribute:)
43
+ func clearCustomerAttribute(key: String) {
44
+ NamiCustomerManager.clearCustomerAttribute(key)
45
+ }
46
+
47
+ @objc(clearAllCustomerAttributes)
48
+ func clearAllCustomerAttributes() {
49
+ NamiCustomerManager.clearAllCustomerAttributes()
50
+ }
51
+
52
+ @objc(journeyState:rejecter:)
53
+ func journeyState(resolve: @escaping RCTPromiseResolveBlock, reject _: @escaping RCTPromiseRejectBlock) {
54
+ if let journeyState = NamiCustomerManager.journeyState() {
55
+ let dictionary = journeyStateToDictionary(journeyState)
56
+ resolve(dictionary)
57
+ } else {
58
+ resolve(nil)
59
+ }
60
+ }
61
+
62
+ @objc(isLoggedIn:rejecter:)
63
+ func isLoggedIn(resolve: @escaping RCTPromiseResolveBlock, reject _: @escaping RCTPromiseRejectBlock) {
64
+ let isLoggedIn = NamiCustomerManager.isLoggedIn()
65
+ resolve(isLoggedIn)
66
+ }
67
+
68
+ @objc(loggedInId:rejecter:)
69
+ func loggedInId(resolve: @escaping RCTPromiseResolveBlock, reject _: @escaping RCTPromiseRejectBlock) {
70
+ let id = NamiCustomerManager.loggedInId()
71
+ resolve(id)
72
+ }
73
+
74
+ @objc(deviceId:rejecter:)
75
+ func deviceId(resolve: @escaping RCTPromiseResolveBlock, reject _: @escaping RCTPromiseRejectBlock) {
76
+ let id = NamiCustomerManager.deviceId()
77
+ resolve(id)
78
+ }
79
+
80
+ @objc(login:completion:)
81
+ func login(customerId: String, callback: @escaping RCTResponseSenderBlock) {
82
+ NamiCustomerManager.login(withId: customerId, loginCompleteHandler: { success, error in
83
+ callback([success, error?._code as Any])
84
+ })
85
+ }
86
+
87
+ @objc(logout:)
88
+ func logout(callback: @escaping RCTResponseSenderBlock) {
89
+ NamiCustomerManager.logout(logoutCompleteHandler: { success, error in
90
+ callback([success, error?._code as Any])
91
+ })
92
+ }
93
+
94
+ @objc(registerJourneyStateHandler)
95
+ func registerJourneyStateHandler() {
96
+ NamiCustomerManager.registerJourneyStateHandler { journeyState in
97
+ let dictionary = self.journeyStateToDictionary(journeyState)
98
+ self.sendEvent(withName: "JourneyStateChanged", body: dictionary)
99
+ }
100
+ }
101
+
102
+ @objc(registerAccountStateHandler)
103
+ func registerAccountStateHandler() {
104
+ NamiCustomerManager.registerAccountStateHandler({action, success, error in
105
+ let actionString: String
106
+ switch action {
107
+ case .login:
108
+ actionString = "login"
109
+ case .logout:
110
+ actionString = "logout"
111
+ @unknown default:
112
+ actionString = "unknown"
113
+ }
114
+ let payload: [String: Any?] = [
115
+ "action": actionString,
116
+ "success": success,
117
+ "error": error?._code as Any
118
+ ]
119
+ self.sendEvent(withName: "AccountStateChanged", body: payload)
120
+ })
121
+ }
122
+ }