Skip to content

Commit ee27f7e

Browse files
InAppDelegate conversion.
1 parent 41e2bf1 commit ee27f7e

File tree

6 files changed

+83
-7
lines changed

6 files changed

+83
-7
lines changed

InAppClasses.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
'use strict';
22

3+
enum IterableInAppShowResponse {
4+
show = 0,
5+
skip = 1
6+
}
7+
38
enum IterableInAppTriggerType {
49
immediate = 0,
510
event = 1,
@@ -90,7 +95,7 @@ class IterableInAppMessage {
9095
readonly createdAt?: Date
9196
readonly expiresAt?: Date
9297
readonly saveToInbox: Boolean
93-
readonly inboxMetadata: IterableInboxMetadata
98+
readonly inboxMetadata?: IterableInboxMetadata
9499
readonly customPayload?: any
95100
readonly read: Boolean
96101

@@ -100,7 +105,7 @@ class IterableInAppMessage {
100105
createdAt: Date | undefined,
101106
expiresAt: Date | undefined,
102107
saveToInbox: Boolean,
103-
inboxMetadata: IterableInboxMetadata,
108+
inboxMetadata: IterableInboxMetadata | undefined,
104109
customPayload: any | undefined,
105110
read: Boolean) {
106111
this.campaignId = campaignId
@@ -131,7 +136,13 @@ class IterableInAppMessage {
131136
expiresAt = new Date(expiresAt as number)
132137
}
133138
let saveToInbox = dict["saveToInbox"] as Boolean
134-
let inboxMetadata = IterableInboxMetadata.fromDict(dict["inboxMetadata"])
139+
let inboxMetadataDict = dict["inboxMetadata"]
140+
let inboxMetadata: IterableInboxMetadata | undefined
141+
if (inboxMetadataDict) {
142+
inboxMetadata = IterableInboxMetadata.fromDict(inboxMetadataDict)
143+
} else {
144+
inboxMetadata = undefined
145+
}
135146
let customPayload = dict["customPayload"]
136147
let read = dict["read"] as Boolean
137148

@@ -150,6 +161,7 @@ class IterableInAppMessage {
150161
}
151162

152163
export {
164+
IterableInAppShowResponse,
153165
IterableInAppTriggerType,
154166
IterableInAppTrigger,
155167
IterableInAppContentType,

Iterable.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
import { NativeModules, NativeEventEmitter } from 'react-native';
4-
import { IterableInAppMessage } from './InAppClasses'
4+
import { IterableInAppMessage, IterableInAppShowResponse } from './InAppClasses'
55

66
const RNIterableAPI = NativeModules.RNIterableAPI
77
const RNEventEmitter = new NativeEventEmitter(RNIterableAPI)
@@ -27,6 +27,7 @@ class IterableConfig {
2727
inAppDisplayInterval: number = 30.0
2828
urlDelegate?: (url: String, context: IterableActionContext) => Boolean
2929
customActionDelegate?: (action: IterableAction, context: IterableActionContext) => Boolean
30+
inAppDelegate?: (message: IterableInAppMessage) => IterableInAppShowResponse
3031

3132
toDict(): any {
3233
return {
@@ -38,6 +39,7 @@ class IterableConfig {
3839
"inAppDisplayInterval": this.inAppDisplayInterval,
3940
"urlDelegatePresent": this.urlDelegate != undefined,
4041
"customActionDelegatePresent": this.customActionDelegate != undefined,
42+
"inAppDelegatePresent": this.inAppDelegate != undefined,
4143
}
4244
}
4345
}
@@ -103,6 +105,7 @@ class IterableCommerceItem {
103105
enum EventName {
104106
handleUrlCalled = "handleUrlCalled",
105107
handleCustomActionCalled = "handleCustomActionCalled",
108+
handleInAppCalled = "handleInAppCalled",
106109
}
107110

108111
class Iterable {
@@ -135,6 +138,16 @@ class Iterable {
135138
}
136139
)
137140
}
141+
if (config.inAppDelegate) {
142+
RNEventEmitter.addListener(
143+
EventName.handleInAppCalled,
144+
(messageDict) => {
145+
const message = IterableInAppMessage.fromDict(messageDict)
146+
const result = config.inAppDelegate!(message)
147+
RNIterableAPI.setInAppShowResponse(result)
148+
}
149+
)
150+
}
138151

139152
RNIterableAPI.initializeWithApiKey(apiKey, config.toDict())
140153
}

index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export class IterableConfig {
3737
inAppDisplayInterval: number
3838
urlDelegate?: (url: String, context: IterableActionContext) => Boolean
3939
customActionDelegate?: (action: IterableAction, context: IterableActionContext) => Boolean
40+
inAppDelegate?: (message: IterableInAppMessage) => IterableInAppShowResponse
4041
}
4142

4243
export class IterableAction {
@@ -71,6 +72,11 @@ export class IterableCommerceItem {
7172
constructor(id: String, name: String, price: number, quantity: number)
7273
}
7374

75+
export enum IterableInAppShowResponse {
76+
show = 0,
77+
skip = 1
78+
}
79+
7480
export enum IterableInAppTriggerType {
7581
immediate = 0,
7682
event = 1,

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
IterableCommerceItem
1717
} from './Iterable';
1818
import {
19+
IterableInAppShowResponse,
1920
IterableInAppContent,
2021
IterableInAppTriggerType,
2122
IterableInAppTrigger,
@@ -34,6 +35,7 @@ export {
3435
IterableActionContext,
3536
IterableAttributionInfo,
3637
IterableCommerceItem,
38+
IterableInAppShowResponse,
3739
IterableInAppContent,
3840
IterableInAppTriggerType,
3941
IterableInAppTrigger,

ios/RNIterableAPI/RNIterableAPI.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ @interface RCT_EXTERN_REMAP_MODULE(RNIterableAPI, ReactIterableAPI, NSObject)
2626

2727
RCT_EXTERN_METHOD(setUrlHandled: (BOOL) handled)
2828

29+
RCT_EXTERN_METHOD(setInAppShowResponse: (nonnull NSNumber *) inAppShowResponse)
30+
2931
RCT_EXTERN_METHOD(getLastPushPayload: (RCTPromiseResolveBlock) resolve
3032
rejecter: (RCTPromiseRejectBlock) reject)
3133

@@ -47,7 +49,6 @@ @interface RCT_EXTERN_REMAP_MODULE(RNIterableAPI, ReactIterableAPI, NSObject)
4749
items: (NSArray *) items
4850
dataFields: (NSDictionary *) dataFields)
4951

50-
5152
RCT_EXTERN_METHOD(getInAppMessages: (RCTPromiseResolveBlock) resolve
5253
rejecter: (RCTPromiseRejectBlock) reject)
5354

ios/RNIterableAPI/ReactIterableAPI.swift

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ReactIterableAPI: RCTEventEmitter {
2424
enum EventName: String, CaseIterable {
2525
case handleUrlCalled
2626
case handleCustomActionCalled
27+
case handleInAppCalled
2728
}
2829

2930
override func supportedEvents() -> [String]! {
@@ -55,6 +56,12 @@ class ReactIterableAPI: RCTEventEmitter {
5556
if let customActionDelegatePresent = configDict["customActionDelegatePresent"] as? Bool, customActionDelegatePresent == true {
5657
iterableConfig.customActionDelegate = self
5758
}
59+
if let inAppDelegatePresent = configDict["inAppDelegatePresent"] as? Bool, inAppDelegatePresent == true {
60+
iterableConfig.inAppDelegate = self
61+
}
62+
if let inAppDisplayInterval = configDict["inAppDisplayInterval"] as? Double {
63+
iterableConfig.inAppDisplayInterval = inAppDisplayInterval
64+
}
5865

5966
DispatchQueue.main.async {
6067
IterableAPI.initialize(apiKey: apiKey, launchOptions: launchOptions, config: iterableConfig)
@@ -92,6 +99,17 @@ class ReactIterableAPI: RCTEventEmitter {
9299
urlDelegateSemaphore.signal()
93100
}
94101

102+
@objc(setInAppShowResponse:)
103+
func set(inAppShowResponse: NSNumber) {
104+
ITBInfo()
105+
if let value = inAppShowResponse as? Int {
106+
self.inAppShowResponse = InAppShowResponse(rawValue: value) ?? .show
107+
} else {
108+
self.inAppShowResponse = .show
109+
}
110+
inAppDelegateSemapohore.signal()
111+
}
112+
95113
@objc(disableDeviceForCurrentUser)
96114
func disableDeviceForCurrentUser() {
97115
ITBInfo()
@@ -177,6 +195,10 @@ class ReactIterableAPI: RCTEventEmitter {
177195
private var urlHandled = false
178196
private var urlDelegateSemaphore = DispatchSemaphore(value: 0)
179197

198+
// Handling in-app delegate
199+
private var inAppShowResponse = InAppShowResponse.show
200+
private var inAppDelegateSemapohore = DispatchSemaphore(value: 0)
201+
180202
private func createLaunchOptions() -> [UIApplication.LaunchOptionsKey: Any]? {
181203
guard let bridge = bridge else {
182204
return nil
@@ -295,15 +317,14 @@ class ReactIterableAPI: RCTEventEmitter {
295317
extension ReactIterableAPI: IterableURLDelegate {
296318
func handle(iterableURL url: URL, inContext context: IterableActionContext) -> Bool {
297319
ITBInfo()
298-
299320
guard shouldEmit == true else {
300321
return false
301322
}
302323

303324
let contextDict = ReactIterableAPI.contextToDictionary(context: context)
304325
sendEvent(withName: EventName.handleUrlCalled.rawValue, body: ["url": url.absoluteString, "context": contextDict])
305-
306326
let timeoutResult = urlDelegateSemaphore.wait(timeout: .now() + 2.0)
327+
307328
if timeoutResult == .success {
308329
ITBInfo("urlHandled: \(urlHandled)")
309330
return urlHandled
@@ -344,6 +365,27 @@ extension ReactIterableAPI: IterableCustomActionDelegate {
344365
}
345366
}
346367

368+
extension ReactIterableAPI: IterableInAppDelegate {
369+
func onNew(message: IterableInAppMessage) -> InAppShowResponse {
370+
ITBInfo()
371+
guard shouldEmit == true else {
372+
return .show
373+
}
374+
375+
let messageDict = ReactIterableAPI.inAppMessageToDict(message: message)
376+
sendEvent(withName: EventName.handleInAppCalled.rawValue, body: messageDict)
377+
let timeoutResult = inAppDelegateSemapohore.wait(timeout: .now() + 2.0)
378+
379+
if timeoutResult == .success {
380+
ITBInfo("inAppShowResponse: \(inAppShowResponse == .show)")
381+
return inAppShowResponse
382+
} else {
383+
ITBInfo("timed out")
384+
return .show
385+
}
386+
}
387+
}
388+
347389
// TODO: make public
348390
extension Encodable {
349391
public func toDictionary() -> [String: Any]? {

0 commit comments

Comments
 (0)