@joryio/react-native-sdk 1.4.0-beta.2 → 1.4.0-beta.5
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.
- package/CHANGELOG.md +16 -0
- package/README.md +1 -0
- package/android/src/main/java/io/joryio/reactnative/JoryioCardFeedViewManager.kt +106 -0
- package/android/src/main/java/io/joryio/reactnative/JoryioModule.kt +331 -0
- package/android/src/main/java/io/joryio/reactnative/JoryioPackage.kt +2 -1
- package/android/src/main/java/io/joryio/reactnative/JoryioPlacementViewManager.kt +105 -0
- package/ios/JoryioCardFeedViewManager.m +14 -0
- package/ios/JoryioCardFeedViewManager.swift +85 -0
- package/ios/JoryioModule.m +76 -0
- package/ios/JoryioModule.swift +298 -5
- package/ios/JoryioPlacementViewManager.m +14 -0
- package/ios/JoryioPlacementViewManager.swift +75 -0
- package/lib/index.d.ts +337 -0
- package/lib/index.js +578 -1
- package/lib/index.js.map +1 -1
- package/package.json +2 -1
- package/src/index.ts +749 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import UIKit
|
|
2
|
+
import React
|
|
3
|
+
import Joryio
|
|
4
|
+
import JoryioUI
|
|
5
|
+
|
|
6
|
+
/// The native view behind `<JoryioCardFeed />` (D444 Phase 3): the iOS SDK's
|
|
7
|
+
/// own CardFeedViewController, so the feed, paging, viewability (viewed then
|
|
8
|
+
/// read), the clicked / dismissed reports and the link rule are exactly the
|
|
9
|
+
/// native ones - one implementation per platform. App deep links
|
|
10
|
+
/// (`myapp://...`) go to JS (`onCardDeepLink`, with the whole card); http(s)
|
|
11
|
+
/// opens in the system natively; any other scheme is never opened.
|
|
12
|
+
@objc(JoryioCardFeedViewManager)
|
|
13
|
+
class JoryioCardFeedViewManager: RCTViewManager {
|
|
14
|
+
override static func requiresMainQueueSetup() -> Bool { true }
|
|
15
|
+
|
|
16
|
+
override func view() -> UIView! {
|
|
17
|
+
JoryioCardFeedHostView()
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
final class JoryioCardFeedHostView: UIView {
|
|
22
|
+
private let feed = CardFeedViewController()
|
|
23
|
+
private var appeared = false
|
|
24
|
+
|
|
25
|
+
@objc var onCardFeedRender: RCTDirectEventBlock?
|
|
26
|
+
@objc var onCardDeepLink: RCTDirectEventBlock?
|
|
27
|
+
|
|
28
|
+
@objc var emptyText: NSString? {
|
|
29
|
+
didSet { feed.emptyText = (emptyText as String?) ?? "No messages yet" }
|
|
30
|
+
}
|
|
31
|
+
@objc var pinnedLabel: NSString? {
|
|
32
|
+
didSet { feed.pinnedLabel = (pinnedLabel as String?) ?? "Pinned" }
|
|
33
|
+
}
|
|
34
|
+
@objc var dismissLabel: NSString? {
|
|
35
|
+
didSet { feed.dismissLabel = (dismissLabel as String?) ?? "Dismiss" }
|
|
36
|
+
}
|
|
37
|
+
@objc var unreadLabel: NSString? {
|
|
38
|
+
didSet { feed.unreadLabel = (unreadLabel as String?) ?? "Unread" }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
override init(frame: CGRect) {
|
|
42
|
+
super.init(frame: frame)
|
|
43
|
+
let content: UIView = feed.view
|
|
44
|
+
content.translatesAutoresizingMaskIntoConstraints = false
|
|
45
|
+
addSubview(content)
|
|
46
|
+
NSLayoutConstraint.activate([
|
|
47
|
+
content.topAnchor.constraint(equalTo: topAnchor),
|
|
48
|
+
content.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
49
|
+
content.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
50
|
+
content.bottomAnchor.constraint(equalTo: bottomAnchor),
|
|
51
|
+
])
|
|
52
|
+
// The feed and the card cross as their toJson() text, the same JSON as
|
|
53
|
+
// getCards, so JS hands onRender / onDeepLink the same CardFeed and Card
|
|
54
|
+
// as every other Joryio SDK.
|
|
55
|
+
feed.onRender = { [weak self] f in
|
|
56
|
+
self?.onCardFeedRender?(["feed": Self.jsonText(f.toJson())])
|
|
57
|
+
}
|
|
58
|
+
feed.onDeepLink = { [weak self] url, card in
|
|
59
|
+
self?.onCardDeepLink?(["url": url.absoluteString, "card": Self.jsonText(card.toJson())])
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
required init?(coder: NSCoder) { nil }
|
|
64
|
+
|
|
65
|
+
private static func jsonText(_ object: [String: Any]) -> String {
|
|
66
|
+
guard let data = try? JSONSerialization.data(withJSONObject: object), let text = String(data: data, encoding: .utf8) else { return "{}" }
|
|
67
|
+
return text
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// The controller is not contained in a parent (React Native owns the view
|
|
71
|
+
// tree), so its appearance - which subscribes to the feed and runs the
|
|
72
|
+
// viewability timer - follows this view entering and leaving the window.
|
|
73
|
+
override func didMoveToWindow() {
|
|
74
|
+
super.didMoveToWindow()
|
|
75
|
+
if window != nil, !appeared {
|
|
76
|
+
appeared = true
|
|
77
|
+
feed.beginAppearanceTransition(true, animated: false)
|
|
78
|
+
feed.endAppearanceTransition()
|
|
79
|
+
} else if window == nil, appeared {
|
|
80
|
+
appeared = false
|
|
81
|
+
feed.beginAppearanceTransition(false, animated: false)
|
|
82
|
+
feed.endAppearanceTransition()
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
package/ios/JoryioModule.m
CHANGED
|
@@ -100,6 +100,82 @@ RCT_EXTERN_METHOD(getAnonymousId:(RCTPromiseResolveBlock)resolve
|
|
|
100
100
|
|
|
101
101
|
RCT_EXTERN_METHOD(syncInAppCampaigns)
|
|
102
102
|
|
|
103
|
+
RCT_EXTERN_METHOD(getPlacements:(NSArray *)keys
|
|
104
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
105
|
+
rejecter:(RCTPromiseRejectBlock)reject)
|
|
106
|
+
|
|
107
|
+
RCT_EXTERN_METHOD(refreshPlacements:(NSArray *)keys)
|
|
108
|
+
|
|
109
|
+
RCT_EXTERN_METHOD(enablePlacementsUpdates)
|
|
110
|
+
|
|
111
|
+
RCT_EXTERN_METHOD(disablePlacementsUpdates)
|
|
112
|
+
|
|
113
|
+
RCT_EXTERN_METHOD(markPlacement:(NSString *)placementKey
|
|
114
|
+
itemId:(NSString *)itemId
|
|
115
|
+
action:(NSString *)action
|
|
116
|
+
elementId:(NSString *)elementId)
|
|
117
|
+
|
|
118
|
+
RCT_EXTERN_METHOD(getCards:(NSString *)cursor
|
|
119
|
+
resolver:(RCTPromiseResolveBlock)resolve
|
|
120
|
+
rejecter:(RCTPromiseRejectBlock)reject)
|
|
121
|
+
|
|
122
|
+
RCT_EXTERN_METHOD(getUnreadCardCount:(RCTPromiseResolveBlock)resolve
|
|
123
|
+
rejecter:(RCTPromiseRejectBlock)reject)
|
|
124
|
+
|
|
125
|
+
RCT_EXTERN_METHOD(markCardViewed:(NSString *)cardId)
|
|
126
|
+
|
|
127
|
+
RCT_EXTERN_METHOD(markCardRead:(NSString *)cardId)
|
|
128
|
+
|
|
129
|
+
RCT_EXTERN_METHOD(markCardClicked:(NSString *)cardId)
|
|
130
|
+
|
|
131
|
+
RCT_EXTERN_METHOD(dismissCard:(NSString *)cardId)
|
|
132
|
+
|
|
133
|
+
RCT_EXTERN_METHOD(refreshCards)
|
|
134
|
+
|
|
135
|
+
RCT_EXTERN_METHOD(enableCardsUpdates)
|
|
136
|
+
|
|
137
|
+
RCT_EXTERN_METHOD(disableCardsUpdates)
|
|
138
|
+
|
|
139
|
+
RCT_EXTERN_METHOD(enableLinkHandler)
|
|
140
|
+
|
|
141
|
+
RCT_EXTERN_METHOD(disableLinkHandler)
|
|
142
|
+
|
|
143
|
+
RCT_EXTERN_METHOD(completeLink:(NSString *)linkId
|
|
144
|
+
handled:(BOOL)handled)
|
|
145
|
+
|
|
146
|
+
RCT_EXTERN_METHOD(getFlag:(NSString *)key
|
|
147
|
+
defaultValue:(id)defaultValue
|
|
148
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
149
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
150
|
+
|
|
151
|
+
RCT_EXTERN_METHOD(getBoolFlag:(NSString *)key
|
|
152
|
+
defaultValue:(BOOL)defaultValue
|
|
153
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
154
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
155
|
+
|
|
156
|
+
RCT_EXTERN_METHOD(getStringFlag:(NSString *)key
|
|
157
|
+
defaultValue:(NSString *)defaultValue
|
|
158
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
159
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
160
|
+
|
|
161
|
+
RCT_EXTERN_METHOD(getNumberFlag:(NSString *)key
|
|
162
|
+
defaultValue:(double)defaultValue
|
|
163
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
164
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
165
|
+
|
|
166
|
+
RCT_EXTERN_METHOD(getJsonFlag:(NSString *)key
|
|
167
|
+
defaultValue:(id)defaultValue
|
|
168
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
169
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
170
|
+
|
|
171
|
+
RCT_EXTERN_METHOD(getAllFlags:(RCTPromiseResolveBlock)resolve
|
|
172
|
+
rejecter:(RCTPromiseRejectBlock)reject)
|
|
173
|
+
|
|
174
|
+
RCT_EXTERN_METHOD(refreshFlags:(RCTPromiseResolveBlock)resolve
|
|
175
|
+
rejecter:(RCTPromiseRejectBlock)reject)
|
|
176
|
+
|
|
177
|
+
RCT_EXTERN_METHOD(enableFlagsUpdates)
|
|
178
|
+
|
|
103
179
|
RCT_EXTERN_METHOD(resetDisplayedCampaigns)
|
|
104
180
|
|
|
105
181
|
RCT_EXTERN_METHOD(getDiagnostics:(RCTPromiseResolveBlock)resolve
|
package/ios/JoryioModule.swift
CHANGED
|
@@ -6,7 +6,8 @@ import UserNotifications
|
|
|
6
6
|
// type 'RCTEventEmitter' in scope", which is what happened here: iOS had never
|
|
7
7
|
// been built for either demo app, so nothing had ever compiled this file.
|
|
8
8
|
import React
|
|
9
|
-
|
|
9
|
+
// attachCardFeedListener is SDK-internal (SPI): apps use onCardsUpdated.
|
|
10
|
+
@_spi(JoryioInternal) import Joryio
|
|
10
11
|
|
|
11
12
|
/// React Native bridge for the Joryio iOS SDK.
|
|
12
13
|
/// Each method delegates to the native `Joryio.shared` singleton using its
|
|
@@ -28,7 +29,7 @@ class JoryioModule: RCTEventEmitter {
|
|
|
28
29
|
/// install its own before the old one is torn down, and a blind clear would
|
|
29
30
|
/// take the NEW one away. An ObjectIdentifier rather than a weak reference,
|
|
30
31
|
/// because a weak reference already reads nil inside `deinit`.
|
|
31
|
-
private enum NativeCallback { case inApp, auth }
|
|
32
|
+
private enum NativeCallback { case inApp, auth, link }
|
|
32
33
|
private static var owners: [NativeCallback: ObjectIdentifier] = [:]
|
|
33
34
|
private static let ownerLock = NSLock()
|
|
34
35
|
|
|
@@ -53,6 +54,9 @@ class JoryioModule: RCTEventEmitter {
|
|
|
53
54
|
/// message was then "handed to JS" and dropped, with no native rendering
|
|
54
55
|
/// either, until the next process start.
|
|
55
56
|
private func releaseNativeCallbacks() {
|
|
57
|
+
// This module's flags subscription (BACKLOG D446) goes with it.
|
|
58
|
+
flagsSubscription?.cancel()
|
|
59
|
+
flagsSubscription = nil
|
|
56
60
|
if releaseOwnership(.inApp) {
|
|
57
61
|
inAppHandedToJS = false
|
|
58
62
|
Joryio.shared.clearInAppMessageCallback()
|
|
@@ -60,6 +64,14 @@ class JoryioModule: RCTEventEmitter {
|
|
|
60
64
|
if releaseOwnership(.auth) {
|
|
61
65
|
Joryio.shared.clearSdkAuthenticationErrorHandler()
|
|
62
66
|
}
|
|
67
|
+
// The link handler forwards to THIS module's JS; without it links open natively again.
|
|
68
|
+
if releaseOwnership(.link) {
|
|
69
|
+
Joryio.shared.clearLinkHandler()
|
|
70
|
+
}
|
|
71
|
+
releasePendingLinks()
|
|
72
|
+
// This module's card feed listener (D444 Phase 3); a reload's new module installs its own.
|
|
73
|
+
cardsSubscription?.cancel()
|
|
74
|
+
cardsSubscription = nil
|
|
63
75
|
}
|
|
64
76
|
|
|
65
77
|
override func invalidate() {
|
|
@@ -75,7 +87,7 @@ class JoryioModule: RCTEventEmitter {
|
|
|
75
87
|
// category) to avoid a duplicate Objective-C selector definition.
|
|
76
88
|
|
|
77
89
|
override func supportedEvents() -> [String]! {
|
|
78
|
-
return ["JoryioInAppMessage", "JoryioSdkAuthError"]
|
|
90
|
+
return ["JoryioInAppMessage", "JoryioSdkAuthError", "JoryioPlacementsUpdated", "JoryioCardsUpdated", "JoryioLink", "JoryioFlagsUpdated"]
|
|
79
91
|
}
|
|
80
92
|
|
|
81
93
|
override func startObserving() {
|
|
@@ -179,6 +191,10 @@ class JoryioModule: RCTEventEmitter {
|
|
|
179
191
|
// the message) or disableInAppMessages (nobody listens; native draws it).
|
|
180
192
|
// Same key as the Android bridge.
|
|
181
193
|
let holdInAppMessagesForHandler = config["holdInAppMessagesForHandler"] as? Bool ?? false
|
|
194
|
+
// iOS only (Android routes verified App Links itself): an https link on
|
|
195
|
+
// one of the app's associated domains goes to its own universal-link
|
|
196
|
+
// handling instead of Safari. Same key as Unity's ForwardUniversalLinks.
|
|
197
|
+
let forwardUniversalLinks = config["forwardUniversalLinks"] as? Bool ?? false
|
|
182
198
|
|
|
183
199
|
// Consent and opt-out at start-up, the same options every Joryio SDK takes.
|
|
184
200
|
// Missing here, so an RN app could only opt out AFTER initialize() - by
|
|
@@ -221,7 +237,8 @@ class JoryioModule: RCTEventEmitter {
|
|
|
221
237
|
optOut: optOut,
|
|
222
238
|
trackingConsent: trackingConsent,
|
|
223
239
|
enableDebug: enableDebug,
|
|
224
|
-
logLevel: logLevel
|
|
240
|
+
logLevel: logLevel,
|
|
241
|
+
forwardUniversalLinks: forwardUniversalLinks
|
|
225
242
|
)
|
|
226
243
|
} else {
|
|
227
244
|
joryioConfig = JoryioConfig(
|
|
@@ -243,7 +260,8 @@ class JoryioModule: RCTEventEmitter {
|
|
|
243
260
|
sdkAuthenticationToken: sdkAuthenticationToken,
|
|
244
261
|
optOut: optOut,
|
|
245
262
|
enableDebug: enableDebug,
|
|
246
|
-
logLevel: logLevel
|
|
263
|
+
logLevel: logLevel,
|
|
264
|
+
forwardUniversalLinks: forwardUniversalLinks
|
|
247
265
|
)
|
|
248
266
|
}
|
|
249
267
|
|
|
@@ -262,6 +280,12 @@ class JoryioModule: RCTEventEmitter {
|
|
|
262
280
|
self?.emitSdkAuthError(authError)
|
|
263
281
|
}
|
|
264
282
|
|
|
283
|
+
// Every request names this bridge and its version (D427):
|
|
284
|
+
// X-Joryio-SDK-Wrapper: react-native + X-Joryio-SDK-Wrapper-Version.
|
|
285
|
+
// The JS layer sends its package version on every initialize().
|
|
286
|
+
if let wrapperVersion = config["sdkWrapperVersion"] as? String {
|
|
287
|
+
Joryio.setSdkWrapper(name: "react-native", version: wrapperVersion)
|
|
288
|
+
}
|
|
265
289
|
Joryio.shared.initialize(sdkKey: sdkKey, apiHost: apiHost, config: joryioConfig)
|
|
266
290
|
|
|
267
291
|
// Resolved even when the native SDK has not started YET. Before the
|
|
@@ -940,6 +964,72 @@ class JoryioModule: RCTEventEmitter {
|
|
|
940
964
|
])
|
|
941
965
|
}
|
|
942
966
|
|
|
967
|
+
// MARK: - Feature flags (BACKLOG D446)
|
|
968
|
+
//
|
|
969
|
+
// Every getter carries the app's key AND default to the native getter,
|
|
970
|
+
// which answers value-or-default and logs the exposure as for a Swift
|
|
971
|
+
// caller. getFlag and getJsonFlag resolve `{ value }`, so a JSON null stays
|
|
972
|
+
// null in JS. Before initialize() the native getters answer the default.
|
|
973
|
+
|
|
974
|
+
/// The one native subscription this module holds; cancelled in invalidate().
|
|
975
|
+
private var flagsSubscription: JoryioSubscription?
|
|
976
|
+
|
|
977
|
+
/// A JS null arrives as nil or NSNull: both mean "no default".
|
|
978
|
+
private func flagDefault(_ value: Any?) -> Any? {
|
|
979
|
+
value is NSNull ? nil : value
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
private func wrapFlagValue(_ value: Any?) -> [String: Any] {
|
|
983
|
+
["value": value ?? NSNull()]
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
@objc func getFlag(_ key: String?, defaultValue: Any?, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
987
|
+
let fallback = flagDefault(defaultValue)
|
|
988
|
+
guard let key, !key.isEmpty else { resolve(wrapFlagValue(fallback)); return }
|
|
989
|
+
resolve(wrapFlagValue(Joryio.shared.getFlag(key, default: fallback)))
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
@objc func getBoolFlag(_ key: String?, defaultValue: Bool, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
993
|
+
guard let key, !key.isEmpty else { resolve(defaultValue); return }
|
|
994
|
+
resolve(Joryio.shared.getBoolFlag(key, default: defaultValue))
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
@objc func getStringFlag(_ key: String?, defaultValue: String?, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
998
|
+
let fallback = defaultValue ?? ""
|
|
999
|
+
guard let key, !key.isEmpty else { resolve(fallback); return }
|
|
1000
|
+
resolve(Joryio.shared.getStringFlag(key, default: fallback))
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
@objc func getNumberFlag(_ key: String?, defaultValue: Double, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
1004
|
+
guard let key, !key.isEmpty else { resolve(defaultValue); return }
|
|
1005
|
+
resolve(Joryio.shared.getNumberFlag(key, default: defaultValue))
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
@objc func getJsonFlag(_ key: String?, defaultValue: Any?, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
1009
|
+
let fallback = flagDefault(defaultValue)
|
|
1010
|
+
guard let key, !key.isEmpty else { resolve(wrapFlagValue(fallback)); return }
|
|
1011
|
+
resolve(wrapFlagValue(Joryio.shared.getJsonFlag(key, default: fallback)))
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
/// Every flag held, key -> value. Logs no exposure.
|
|
1015
|
+
@objc func getAllFlags(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
|
|
1016
|
+
resolve(Joryio.shared.getAllFlags())
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/// Resolves once the answer is in (or the request failed: the held flags stay).
|
|
1020
|
+
@objc func refreshFlags(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
|
|
1021
|
+
Joryio.shared.refreshFlags { resolve(nil) }
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
/// Start JoryioFlagsUpdated events (one native subscription; JS fans out).
|
|
1025
|
+
@objc func enableFlagsUpdates() {
|
|
1026
|
+
guard flagsSubscription == nil else { return }
|
|
1027
|
+
flagsSubscription = Joryio.shared.onFlagsUpdated { [weak self] keys in
|
|
1028
|
+
guard let self, self.hasListeners else { return }
|
|
1029
|
+
self.sendEvent(withName: "JoryioFlagsUpdated", body: ["keys": keys])
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
|
|
943
1033
|
// MARK: - Utilities
|
|
944
1034
|
|
|
945
1035
|
@objc func flush() {
|
|
@@ -963,6 +1053,209 @@ class JoryioModule: RCTEventEmitter {
|
|
|
963
1053
|
Task { await Joryio.shared.syncInAppCampaigns() }
|
|
964
1054
|
}
|
|
965
1055
|
|
|
1056
|
+
// MARK: - Placements (docs/PLACEMENTS_PLAN.md, D444)
|
|
1057
|
+
//
|
|
1058
|
+
// The native SDK does the work - fetching, caching, viewability rules,
|
|
1059
|
+
// reporting, the identity guard. The bridge hands the data to JS as JSON and
|
|
1060
|
+
// routes JS's mark* calls back to the native objects it last handed out, so
|
|
1061
|
+
// a report always goes through the native generation check.
|
|
1062
|
+
|
|
1063
|
+
private var lastPlacements: [String: Placement] = [:]
|
|
1064
|
+
private let placementsLock = NSLock()
|
|
1065
|
+
private var placementsSubscription: JoryioSubscription?
|
|
1066
|
+
|
|
1067
|
+
private func placementsJson(_ map: [String: Placement]) -> String {
|
|
1068
|
+
placementsLock.lock()
|
|
1069
|
+
for (k, p) in map { lastPlacements[k] = p }
|
|
1070
|
+
placementsLock.unlock()
|
|
1071
|
+
let dict = map.mapValues { $0.toJson() }
|
|
1072
|
+
guard let data = try? JSONSerialization.data(withJSONObject: dict), let text = String(data: data, encoding: .utf8) else { return "{}" }
|
|
1073
|
+
return text
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
@objc func getPlacements(_ keys: NSArray?, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
|
|
1077
|
+
let list = (keys as? [Any])?.compactMap { $0 as? String } ?? []
|
|
1078
|
+
Joryio.shared.getPlacements(list) { [weak self] map in
|
|
1079
|
+
resolve(self?.placementsJson(map) ?? "{}")
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
@objc func refreshPlacements(_ keys: NSArray?) {
|
|
1084
|
+
Joryio.shared.refreshPlacements((keys as? [Any])?.compactMap { $0 as? String })
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
@objc func enablePlacementsUpdates() {
|
|
1088
|
+
guard placementsSubscription == nil else { return }
|
|
1089
|
+
placementsSubscription = Joryio.shared.onPlacementsUpdated { [weak self] map in
|
|
1090
|
+
guard let self, self.hasListeners else { return }
|
|
1091
|
+
self.sendEvent(withName: "JoryioPlacementsUpdated", body: ["json": self.placementsJson(map)])
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
@objc func disablePlacementsUpdates() {
|
|
1096
|
+
placementsSubscription?.cancel()
|
|
1097
|
+
placementsSubscription = nil
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
/// `action`: displayed | clicked | dismissed for an item, `control` for the holdout spot.
|
|
1101
|
+
@objc func markPlacement(_ placementKey: String?, itemId: String?, action: String?, elementId: String?) {
|
|
1102
|
+
guard let placementKey, let action else { return }
|
|
1103
|
+
placementsLock.lock()
|
|
1104
|
+
let placement = lastPlacements[placementKey]
|
|
1105
|
+
placementsLock.unlock()
|
|
1106
|
+
guard let placement else { return }
|
|
1107
|
+
if action == "control" {
|
|
1108
|
+
placement.markDisplayed()
|
|
1109
|
+
return
|
|
1110
|
+
}
|
|
1111
|
+
guard let item = placement.items.first(where: { $0.id == itemId }) else { return }
|
|
1112
|
+
switch action {
|
|
1113
|
+
case "displayed": item.markDisplayed()
|
|
1114
|
+
case "clicked": item.markClicked(elementId: (elementId?.isEmpty ?? true) ? nil : elementId)
|
|
1115
|
+
case "dismissed": item.markDismissed()
|
|
1116
|
+
default: break
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
// MARK: - Content cards (docs/PLACEMENTS_PLAN.md section 11, D444 Phase 3)
|
|
1121
|
+
//
|
|
1122
|
+
// The native SDK does the work - fetching, caching, the rate limit, the
|
|
1123
|
+
// anonymous visitor's device state, reporting, the identity guard. The
|
|
1124
|
+
// bridge hands the feed to JS as JSON (CardFeed.toJson, no delivery tokens)
|
|
1125
|
+
// and routes JS's reports back to the native Card values it last handed
|
|
1126
|
+
// out, so a report carries the card's own token and a card of a person who
|
|
1127
|
+
// is no longer here is dropped by the native check. An id the bridge never
|
|
1128
|
+
// handed out goes to the id overload (the native SDK looks it up).
|
|
1129
|
+
|
|
1130
|
+
private var lastCards: [String: Card] = [:]
|
|
1131
|
+
private let cardsLock = NSLock()
|
|
1132
|
+
private var cardsSubscription: JoryioSubscription?
|
|
1133
|
+
|
|
1134
|
+
/// `page`: a cursor page (added to what was handed out) rather than the whole feed (which replaces it).
|
|
1135
|
+
private func cardsJson(_ feed: CardFeed, page: Bool) -> String {
|
|
1136
|
+
cardsLock.lock()
|
|
1137
|
+
if !page { lastCards.removeAll() }
|
|
1138
|
+
for card in feed.cards { lastCards[card.id] = card }
|
|
1139
|
+
cardsLock.unlock()
|
|
1140
|
+
guard let data = try? JSONSerialization.data(withJSONObject: feed.toJson()), let text = String(data: data, encoding: .utf8) else { return "{}" }
|
|
1141
|
+
return text
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
private func lastCard(_ id: String) -> Card? {
|
|
1145
|
+
cardsLock.lock()
|
|
1146
|
+
defer { cardsLock.unlock() }
|
|
1147
|
+
return lastCards[id]
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
/// `cursor`: a feed's `nextCursor` for the next page, nil for the feed.
|
|
1151
|
+
@objc func getCards(_ cursor: String?, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
|
|
1152
|
+
let c = (cursor?.isEmpty ?? true) ? nil : cursor
|
|
1153
|
+
Joryio.shared.getCards(cursor: c) { [weak self] feed in
|
|
1154
|
+
resolve(self?.cardsJson(feed, page: c != nil) ?? "{}")
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
/// From the native cache, never a network call.
|
|
1159
|
+
@objc func getUnreadCardCount(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
|
|
1160
|
+
resolve(Joryio.shared.getUnreadCardCount())
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
@objc func markCardViewed(_ cardId: String?) {
|
|
1164
|
+
guard let id = cardId, !id.isEmpty else { return }
|
|
1165
|
+
if let card = lastCard(id) { Joryio.shared.markCardViewed(card) } else { Joryio.shared.markCardViewed(id) }
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
@objc func markCardRead(_ cardId: String?) {
|
|
1169
|
+
guard let id = cardId, !id.isEmpty else { return }
|
|
1170
|
+
if let card = lastCard(id) { Joryio.shared.markCardRead(card) } else { Joryio.shared.markCardRead(id) }
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
@objc func markCardClicked(_ cardId: String?) {
|
|
1174
|
+
guard let id = cardId, !id.isEmpty else { return }
|
|
1175
|
+
if let card = lastCard(id) { Joryio.shared.markCardClicked(card) } else { Joryio.shared.markCardClicked(id) }
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
@objc func dismissCard(_ cardId: String?) {
|
|
1179
|
+
guard let id = cardId, !id.isEmpty else { return }
|
|
1180
|
+
if let card = lastCard(id) { Joryio.shared.dismissCard(card) } else { Joryio.shared.dismissCard(id) }
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
@objc func refreshCards() {
|
|
1184
|
+
Joryio.shared.refreshCards()
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
@objc func enableCardsUpdates() {
|
|
1188
|
+
guard cardsSubscription == nil else { return }
|
|
1189
|
+
// nil before initialize(): JS enables again after it (the call is dispatched).
|
|
1190
|
+
cardsSubscription = Joryio.shared.attachCardFeedListener { [weak self] feed in
|
|
1191
|
+
guard let self, self.hasListeners else { return }
|
|
1192
|
+
self.sendEvent(withName: "JoryioCardsUpdated", body: ["json": self.cardsJson(feed, page: false)])
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
@objc func disableCardsUpdates() {
|
|
1197
|
+
cardsSubscription?.cancel()
|
|
1198
|
+
cardsSubscription = nil
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
// MARK: - Links (setLinkHandler)
|
|
1202
|
+
|
|
1203
|
+
/// Links waiting for the JS handler's answer, by id. Bounded: a JS side
|
|
1204
|
+
/// that never answers costs at most this many entries, oldest dropped.
|
|
1205
|
+
private var pendingLinks: [String: JoryioLink] = [:]
|
|
1206
|
+
private var pendingLinkOrder: [String] = []
|
|
1207
|
+
private static let maxPendingLinks = 32
|
|
1208
|
+
private var nextLinkId = 0
|
|
1209
|
+
|
|
1210
|
+
/// JS set a link handler: every link the native SDK is about to open goes
|
|
1211
|
+
/// to JS first (event `JoryioLink`), and waits there for completeLink. The
|
|
1212
|
+
/// native handler takes the link at once - a bridge cannot answer
|
|
1213
|
+
/// synchronously - and JS hands a declined one back through completeLink.
|
|
1214
|
+
@objc func enableLinkHandler() {
|
|
1215
|
+
takeOwnership(.link)
|
|
1216
|
+
Joryio.shared.setLinkHandler { [weak self] link in
|
|
1217
|
+
guard let self, self.hasListeners else { return false }
|
|
1218
|
+
self.nextLinkId += 1
|
|
1219
|
+
let id = "l\(self.nextLinkId)"
|
|
1220
|
+
self.pendingLinks[id] = link
|
|
1221
|
+
self.pendingLinkOrder.append(id)
|
|
1222
|
+
while self.pendingLinkOrder.count > Self.maxPendingLinks {
|
|
1223
|
+
self.pendingLinks[self.pendingLinkOrder.removeFirst()] = nil
|
|
1224
|
+
}
|
|
1225
|
+
var body: [String: Any] = [
|
|
1226
|
+
"linkId": id,
|
|
1227
|
+
"url": link.url.absoluteString,
|
|
1228
|
+
"source": link.source.rawValue,
|
|
1229
|
+
"isAppDeepLink": link.isAppDeepLink,
|
|
1230
|
+
]
|
|
1231
|
+
if let v = link.campaignId { body["campaignId"] = v }
|
|
1232
|
+
if let v = link.placementKey { body["placementKey"] = v }
|
|
1233
|
+
if let v = link.itemId { body["itemId"] = v }
|
|
1234
|
+
if let v = link.cardId { body["cardId"] = v }
|
|
1235
|
+
self.sendEvent(withName: "JoryioLink", body: body)
|
|
1236
|
+
return true
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
@objc func disableLinkHandler() {
|
|
1241
|
+
if releaseOwnership(.link) { Joryio.shared.clearLinkHandler() }
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
/// The JS handler's answer: `handled` false opens the link as the SDK does
|
|
1245
|
+
/// without a handler.
|
|
1246
|
+
@objc func completeLink(_ linkId: String?, handled: Bool) {
|
|
1247
|
+
DispatchQueue.main.async { [weak self] in
|
|
1248
|
+
guard let self, let linkId, let link = self.pendingLinks.removeValue(forKey: linkId) else { return }
|
|
1249
|
+
self.pendingLinkOrder.removeAll { $0 == linkId }
|
|
1250
|
+
if !handled { link.openDefault() }
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1254
|
+
private func releasePendingLinks() {
|
|
1255
|
+
pendingLinks.removeAll()
|
|
1256
|
+
pendingLinkOrder.removeAll()
|
|
1257
|
+
}
|
|
1258
|
+
|
|
966
1259
|
/// Forget which campaigns have already been shown on this device.
|
|
967
1260
|
///
|
|
968
1261
|
/// The SDK filters out any campaign in `displayedCampaigns`, and that set is
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#import <React/RCTViewManager.h>
|
|
2
|
+
|
|
3
|
+
// <JoryioPlacement placementKey="..." /> (D444). The view is Swift
|
|
4
|
+
// (JoryioPlacementViewManager.swift); this exposes its props and events.
|
|
5
|
+
@interface RCT_EXTERN_MODULE(JoryioPlacementViewManager, RCTViewManager)
|
|
6
|
+
|
|
7
|
+
RCT_EXPORT_VIEW_PROPERTY(placementKey, NSString)
|
|
8
|
+
RCT_EXPORT_VIEW_PROPERTY(showProducts, BOOL)
|
|
9
|
+
RCT_EXPORT_VIEW_PROPERTY(productButtonText, NSString)
|
|
10
|
+
RCT_EXPORT_VIEW_PROPERTY(productCardWidth, CGFloat)
|
|
11
|
+
RCT_EXPORT_VIEW_PROPERTY(onPlacementRender, RCTDirectEventBlock)
|
|
12
|
+
RCT_EXPORT_VIEW_PROPERTY(onPlacementDeepLink, RCTDirectEventBlock)
|
|
13
|
+
|
|
14
|
+
@end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import UIKit
|
|
2
|
+
import React
|
|
3
|
+
import Joryio
|
|
4
|
+
import JoryioUI
|
|
5
|
+
|
|
6
|
+
/// The native view behind `<JoryioPlacement placementKey="..." />` (D444): the
|
|
7
|
+
/// iOS SDK's own PlacementView, so fetching, viewability, the holdout and the
|
|
8
|
+
/// reports are exactly the native ones. Tells JS the content height
|
|
9
|
+
/// (`onPlacementRender`) so the JS component can size the spot - `collapse`
|
|
10
|
+
/// gives 0 - and hands `deep_link` buttons to JS (`onPlacementDeepLink`).
|
|
11
|
+
@objc(JoryioPlacementViewManager)
|
|
12
|
+
class JoryioPlacementViewManager: RCTViewManager {
|
|
13
|
+
override static func requiresMainQueueSetup() -> Bool { true }
|
|
14
|
+
|
|
15
|
+
override func view() -> UIView! {
|
|
16
|
+
JoryioPlacementHostView()
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
final class JoryioPlacementHostView: UIView {
|
|
21
|
+
private let placement = PlacementView(placementKey: "")
|
|
22
|
+
|
|
23
|
+
@objc var onPlacementRender: RCTDirectEventBlock?
|
|
24
|
+
@objc var onPlacementDeepLink: RCTDirectEventBlock?
|
|
25
|
+
|
|
26
|
+
@objc var placementKey: NSString = "" {
|
|
27
|
+
didSet { placement.setPlacementKey(placementKey as String) }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// The built-in product cards row: the native PlacementView's properties.
|
|
31
|
+
@objc var showProducts: Bool = true {
|
|
32
|
+
didSet { placement.showProducts = showProducts }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@objc var productButtonText: NSString? {
|
|
36
|
+
didSet { placement.productButtonText = productButtonText as String? }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@objc var productCardWidth: CGFloat = CGFloat(Placements.PLACEMENT_PRODUCT_CARD_WIDTH) {
|
|
40
|
+
didSet { placement.productCardWidth = productCardWidth }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
override init(frame: CGRect) {
|
|
44
|
+
super.init(frame: frame)
|
|
45
|
+
placement.translatesAutoresizingMaskIntoConstraints = false
|
|
46
|
+
addSubview(placement)
|
|
47
|
+
NSLayoutConstraint.activate([
|
|
48
|
+
placement.topAnchor.constraint(equalTo: topAnchor),
|
|
49
|
+
placement.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
50
|
+
placement.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
51
|
+
])
|
|
52
|
+
placement.onRender = { [weak self] p in
|
|
53
|
+
guard let self else { return }
|
|
54
|
+
DispatchQueue.main.async {
|
|
55
|
+
let width = self.bounds.width > 0 ? self.bounds.width : UIScreen.main.bounds.width
|
|
56
|
+
let size = self.placement.systemLayoutSizeFitting(
|
|
57
|
+
CGSize(width: width, height: UIView.layoutFittingCompressedSize.height),
|
|
58
|
+
withHorizontalFittingPriority: .required,
|
|
59
|
+
verticalFittingPriority: .fittingSizeLevel)
|
|
60
|
+
self.onPlacementRender?([
|
|
61
|
+
"placementKey": p.placementKey,
|
|
62
|
+
"itemCount": p.items.count,
|
|
63
|
+
"isControl": p.isControl,
|
|
64
|
+
"emptyBehavior": p.emptyBehavior,
|
|
65
|
+
"height": size.height,
|
|
66
|
+
])
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
placement.onDeepLink = { [weak self] url, item in
|
|
70
|
+
self?.onPlacementDeepLink?(["url": url.absoluteString, "itemId": item.id])
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
required init?(coder: NSCoder) { nil }
|
|
75
|
+
}
|