@joryio/react-native-sdk 1.4.0-beta.2 → 1.4.0-beta.4
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 +14 -0
- package/README.md +1 -0
- package/android/src/main/java/io/joryio/reactnative/JoryioCardFeedViewManager.kt +104 -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 +77 -0
- package/ios/JoryioModule.m +76 -0
- package/ios/JoryioModule.swift +296 -4
- package/ios/JoryioPlacementViewManager.m +14 -0
- package/ios/JoryioPlacementViewManager.swift +75 -0
- package/lib/index.d.ts +322 -0
- package/lib/index.js +566 -1
- package/lib/index.js.map +1 -1
- package/package.json +2 -1
- package/src/index.ts +720 -0
|
@@ -0,0 +1,77 @@
|
|
|
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`); http(s) opens in the system
|
|
11
|
+
/// 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
|
+
feed.onRender = { [weak self] f in
|
|
53
|
+
self?.onCardFeedRender?(["cardCount": f.cards.count, "unreadCount": f.unreadCount])
|
|
54
|
+
}
|
|
55
|
+
feed.onDeepLink = { [weak self] url, card in
|
|
56
|
+
self?.onCardDeepLink?(["url": url.absoluteString, "cardId": card.id])
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
required init?(coder: NSCoder) { nil }
|
|
61
|
+
|
|
62
|
+
// The controller is not contained in a parent (React Native owns the view
|
|
63
|
+
// tree), so its appearance - which subscribes to the feed and runs the
|
|
64
|
+
// viewability timer - follows this view entering and leaving the window.
|
|
65
|
+
override func didMoveToWindow() {
|
|
66
|
+
super.didMoveToWindow()
|
|
67
|
+
if window != nil, !appeared {
|
|
68
|
+
appeared = true
|
|
69
|
+
feed.beginAppearanceTransition(true, animated: false)
|
|
70
|
+
feed.endAppearanceTransition()
|
|
71
|
+
} else if window == nil, appeared {
|
|
72
|
+
appeared = false
|
|
73
|
+
feed.beginAppearanceTransition(false, animated: false)
|
|
74
|
+
feed.endAppearanceTransition()
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
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
|
@@ -28,7 +28,7 @@ class JoryioModule: RCTEventEmitter {
|
|
|
28
28
|
/// install its own before the old one is torn down, and a blind clear would
|
|
29
29
|
/// take the NEW one away. An ObjectIdentifier rather than a weak reference,
|
|
30
30
|
/// because a weak reference already reads nil inside `deinit`.
|
|
31
|
-
private enum NativeCallback { case inApp, auth }
|
|
31
|
+
private enum NativeCallback { case inApp, auth, link }
|
|
32
32
|
private static var owners: [NativeCallback: ObjectIdentifier] = [:]
|
|
33
33
|
private static let ownerLock = NSLock()
|
|
34
34
|
|
|
@@ -53,6 +53,9 @@ class JoryioModule: RCTEventEmitter {
|
|
|
53
53
|
/// message was then "handed to JS" and dropped, with no native rendering
|
|
54
54
|
/// either, until the next process start.
|
|
55
55
|
private func releaseNativeCallbacks() {
|
|
56
|
+
// This module's flags subscription (BACKLOG D446) goes with it.
|
|
57
|
+
flagsSubscription?.cancel()
|
|
58
|
+
flagsSubscription = nil
|
|
56
59
|
if releaseOwnership(.inApp) {
|
|
57
60
|
inAppHandedToJS = false
|
|
58
61
|
Joryio.shared.clearInAppMessageCallback()
|
|
@@ -60,6 +63,14 @@ class JoryioModule: RCTEventEmitter {
|
|
|
60
63
|
if releaseOwnership(.auth) {
|
|
61
64
|
Joryio.shared.clearSdkAuthenticationErrorHandler()
|
|
62
65
|
}
|
|
66
|
+
// The link handler forwards to THIS module's JS; without it links open natively again.
|
|
67
|
+
if releaseOwnership(.link) {
|
|
68
|
+
Joryio.shared.clearLinkHandler()
|
|
69
|
+
}
|
|
70
|
+
releasePendingLinks()
|
|
71
|
+
// This module's card feed listener (D444 Phase 3); a reload's new module installs its own.
|
|
72
|
+
cardsSubscription?.cancel()
|
|
73
|
+
cardsSubscription = nil
|
|
63
74
|
}
|
|
64
75
|
|
|
65
76
|
override func invalidate() {
|
|
@@ -75,7 +86,7 @@ class JoryioModule: RCTEventEmitter {
|
|
|
75
86
|
// category) to avoid a duplicate Objective-C selector definition.
|
|
76
87
|
|
|
77
88
|
override func supportedEvents() -> [String]! {
|
|
78
|
-
return ["JoryioInAppMessage", "JoryioSdkAuthError"]
|
|
89
|
+
return ["JoryioInAppMessage", "JoryioSdkAuthError", "JoryioPlacementsUpdated", "JoryioCardsUpdated", "JoryioLink", "JoryioFlagsUpdated"]
|
|
79
90
|
}
|
|
80
91
|
|
|
81
92
|
override func startObserving() {
|
|
@@ -179,6 +190,10 @@ class JoryioModule: RCTEventEmitter {
|
|
|
179
190
|
// the message) or disableInAppMessages (nobody listens; native draws it).
|
|
180
191
|
// Same key as the Android bridge.
|
|
181
192
|
let holdInAppMessagesForHandler = config["holdInAppMessagesForHandler"] as? Bool ?? false
|
|
193
|
+
// iOS only (Android routes verified App Links itself): an https link on
|
|
194
|
+
// one of the app's associated domains goes to its own universal-link
|
|
195
|
+
// handling instead of Safari. Same key as Unity's ForwardUniversalLinks.
|
|
196
|
+
let forwardUniversalLinks = config["forwardUniversalLinks"] as? Bool ?? false
|
|
182
197
|
|
|
183
198
|
// Consent and opt-out at start-up, the same options every Joryio SDK takes.
|
|
184
199
|
// Missing here, so an RN app could only opt out AFTER initialize() - by
|
|
@@ -221,7 +236,8 @@ class JoryioModule: RCTEventEmitter {
|
|
|
221
236
|
optOut: optOut,
|
|
222
237
|
trackingConsent: trackingConsent,
|
|
223
238
|
enableDebug: enableDebug,
|
|
224
|
-
logLevel: logLevel
|
|
239
|
+
logLevel: logLevel,
|
|
240
|
+
forwardUniversalLinks: forwardUniversalLinks
|
|
225
241
|
)
|
|
226
242
|
} else {
|
|
227
243
|
joryioConfig = JoryioConfig(
|
|
@@ -243,7 +259,8 @@ class JoryioModule: RCTEventEmitter {
|
|
|
243
259
|
sdkAuthenticationToken: sdkAuthenticationToken,
|
|
244
260
|
optOut: optOut,
|
|
245
261
|
enableDebug: enableDebug,
|
|
246
|
-
logLevel: logLevel
|
|
262
|
+
logLevel: logLevel,
|
|
263
|
+
forwardUniversalLinks: forwardUniversalLinks
|
|
247
264
|
)
|
|
248
265
|
}
|
|
249
266
|
|
|
@@ -262,6 +279,12 @@ class JoryioModule: RCTEventEmitter {
|
|
|
262
279
|
self?.emitSdkAuthError(authError)
|
|
263
280
|
}
|
|
264
281
|
|
|
282
|
+
// Every request names this bridge and its version (D427):
|
|
283
|
+
// X-Joryio-SDK-Wrapper: react-native + X-Joryio-SDK-Wrapper-Version.
|
|
284
|
+
// The JS layer sends its package version on every initialize().
|
|
285
|
+
if let wrapperVersion = config["sdkWrapperVersion"] as? String {
|
|
286
|
+
Joryio.setSdkWrapper(name: "react-native", version: wrapperVersion)
|
|
287
|
+
}
|
|
265
288
|
Joryio.shared.initialize(sdkKey: sdkKey, apiHost: apiHost, config: joryioConfig)
|
|
266
289
|
|
|
267
290
|
// Resolved even when the native SDK has not started YET. Before the
|
|
@@ -940,6 +963,72 @@ class JoryioModule: RCTEventEmitter {
|
|
|
940
963
|
])
|
|
941
964
|
}
|
|
942
965
|
|
|
966
|
+
// MARK: - Feature flags (BACKLOG D446)
|
|
967
|
+
//
|
|
968
|
+
// Every getter carries the app's key AND default to the native getter,
|
|
969
|
+
// which answers value-or-default and logs the exposure as for a Swift
|
|
970
|
+
// caller. getFlag and getJsonFlag resolve `{ value }`, so a JSON null stays
|
|
971
|
+
// null in JS. Before initialize() the native getters answer the default.
|
|
972
|
+
|
|
973
|
+
/// The one native subscription this module holds; cancelled in invalidate().
|
|
974
|
+
private var flagsSubscription: FlagsSubscription?
|
|
975
|
+
|
|
976
|
+
/// A JS null arrives as nil or NSNull: both mean "no default".
|
|
977
|
+
private func flagDefault(_ value: Any?) -> Any? {
|
|
978
|
+
value is NSNull ? nil : value
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
private func wrapFlagValue(_ value: Any?) -> [String: Any] {
|
|
982
|
+
["value": value ?? NSNull()]
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
@objc func getFlag(_ key: String?, defaultValue: Any?, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
986
|
+
let fallback = flagDefault(defaultValue)
|
|
987
|
+
guard let key, !key.isEmpty else { resolve(wrapFlagValue(fallback)); return }
|
|
988
|
+
resolve(wrapFlagValue(Joryio.shared.getFlag(key, default: fallback)))
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
@objc func getBoolFlag(_ key: String?, defaultValue: Bool, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
992
|
+
guard let key, !key.isEmpty else { resolve(defaultValue); return }
|
|
993
|
+
resolve(Joryio.shared.getBoolFlag(key, default: defaultValue))
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
@objc func getStringFlag(_ key: String?, defaultValue: String?, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
997
|
+
let fallback = defaultValue ?? ""
|
|
998
|
+
guard let key, !key.isEmpty else { resolve(fallback); return }
|
|
999
|
+
resolve(Joryio.shared.getStringFlag(key, default: fallback))
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
@objc func getNumberFlag(_ key: String?, defaultValue: Double, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
1003
|
+
guard let key, !key.isEmpty else { resolve(defaultValue); return }
|
|
1004
|
+
resolve(Joryio.shared.getNumberFlag(key, default: defaultValue))
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
@objc func getJsonFlag(_ key: String?, defaultValue: Any?, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
1008
|
+
let fallback = flagDefault(defaultValue)
|
|
1009
|
+
guard let key, !key.isEmpty else { resolve(wrapFlagValue(fallback)); return }
|
|
1010
|
+
resolve(wrapFlagValue(Joryio.shared.getJsonFlag(key, default: fallback)))
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
/// Every flag held, key -> value. Logs no exposure.
|
|
1014
|
+
@objc func getAllFlags(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
|
|
1015
|
+
resolve(Joryio.shared.getAllFlags())
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
/// Resolves once the answer is in (or the request failed: the held flags stay).
|
|
1019
|
+
@objc func refreshFlags(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
|
|
1020
|
+
Joryio.shared.refreshFlags { resolve(nil) }
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
/// Start JoryioFlagsUpdated events (one native subscription; JS fans out).
|
|
1024
|
+
@objc func enableFlagsUpdates() {
|
|
1025
|
+
guard flagsSubscription == nil else { return }
|
|
1026
|
+
flagsSubscription = Joryio.shared.onFlagsUpdated { [weak self] keys in
|
|
1027
|
+
guard let self, self.hasListeners else { return }
|
|
1028
|
+
self.sendEvent(withName: "JoryioFlagsUpdated", body: ["keys": keys])
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
|
|
943
1032
|
// MARK: - Utilities
|
|
944
1033
|
|
|
945
1034
|
@objc func flush() {
|
|
@@ -963,6 +1052,209 @@ class JoryioModule: RCTEventEmitter {
|
|
|
963
1052
|
Task { await Joryio.shared.syncInAppCampaigns() }
|
|
964
1053
|
}
|
|
965
1054
|
|
|
1055
|
+
// MARK: - Placements (docs/PLACEMENTS_PLAN.md, D444)
|
|
1056
|
+
//
|
|
1057
|
+
// The native SDK does the work - fetching, caching, viewability rules,
|
|
1058
|
+
// reporting, the identity guard. The bridge hands the data to JS as JSON and
|
|
1059
|
+
// routes JS's mark* calls back to the native objects it last handed out, so
|
|
1060
|
+
// a report always goes through the native generation check.
|
|
1061
|
+
|
|
1062
|
+
private var lastPlacements: [String: Placement] = [:]
|
|
1063
|
+
private let placementsLock = NSLock()
|
|
1064
|
+
private var placementsSubscription: PlacementsSubscription?
|
|
1065
|
+
|
|
1066
|
+
private func placementsJson(_ map: [String: Placement]) -> String {
|
|
1067
|
+
placementsLock.lock()
|
|
1068
|
+
for (k, p) in map { lastPlacements[k] = p }
|
|
1069
|
+
placementsLock.unlock()
|
|
1070
|
+
let dict = map.mapValues { $0.toJson() }
|
|
1071
|
+
guard let data = try? JSONSerialization.data(withJSONObject: dict), let text = String(data: data, encoding: .utf8) else { return "{}" }
|
|
1072
|
+
return text
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
@objc func getPlacements(_ keys: NSArray?, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
|
|
1076
|
+
let list = (keys as? [Any])?.compactMap { $0 as? String } ?? []
|
|
1077
|
+
Joryio.shared.getPlacements(list) { [weak self] map in
|
|
1078
|
+
resolve(self?.placementsJson(map) ?? "{}")
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
@objc func refreshPlacements(_ keys: NSArray?) {
|
|
1083
|
+
Joryio.shared.refreshPlacements((keys as? [Any])?.compactMap { $0 as? String })
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
@objc func enablePlacementsUpdates() {
|
|
1087
|
+
guard placementsSubscription == nil else { return }
|
|
1088
|
+
placementsSubscription = Joryio.shared.onPlacementsUpdated { [weak self] map in
|
|
1089
|
+
guard let self, self.hasListeners else { return }
|
|
1090
|
+
self.sendEvent(withName: "JoryioPlacementsUpdated", body: ["json": self.placementsJson(map)])
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
@objc func disablePlacementsUpdates() {
|
|
1095
|
+
placementsSubscription?.cancel()
|
|
1096
|
+
placementsSubscription = nil
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
/// `action`: displayed | clicked | dismissed for an item, `control` for the holdout spot.
|
|
1100
|
+
@objc func markPlacement(_ placementKey: String?, itemId: String?, action: String?, elementId: String?) {
|
|
1101
|
+
guard let placementKey, let action else { return }
|
|
1102
|
+
placementsLock.lock()
|
|
1103
|
+
let placement = lastPlacements[placementKey]
|
|
1104
|
+
placementsLock.unlock()
|
|
1105
|
+
guard let placement else { return }
|
|
1106
|
+
if action == "control" {
|
|
1107
|
+
placement.markDisplayed()
|
|
1108
|
+
return
|
|
1109
|
+
}
|
|
1110
|
+
guard let item = placement.items.first(where: { $0.id == itemId }) else { return }
|
|
1111
|
+
switch action {
|
|
1112
|
+
case "displayed": item.markDisplayed()
|
|
1113
|
+
case "clicked": item.markClicked(elementId: (elementId?.isEmpty ?? true) ? nil : elementId)
|
|
1114
|
+
case "dismissed": item.markDismissed()
|
|
1115
|
+
default: break
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
// MARK: - Content cards (docs/PLACEMENTS_PLAN.md section 11, D444 Phase 3)
|
|
1120
|
+
//
|
|
1121
|
+
// The native SDK does the work - fetching, caching, the rate limit, the
|
|
1122
|
+
// anonymous visitor's device state, reporting, the identity guard. The
|
|
1123
|
+
// bridge hands the feed to JS as JSON (CardFeed.toJson, no delivery tokens)
|
|
1124
|
+
// and routes JS's reports back to the native Card values it last handed
|
|
1125
|
+
// out, so a report carries the card's own token and a card of a person who
|
|
1126
|
+
// is no longer here is dropped by the native check. An id the bridge never
|
|
1127
|
+
// handed out goes to the id overload (the native SDK looks it up).
|
|
1128
|
+
|
|
1129
|
+
private var lastCards: [String: Card] = [:]
|
|
1130
|
+
private let cardsLock = NSLock()
|
|
1131
|
+
private var cardsSubscription: CardsSubscription?
|
|
1132
|
+
|
|
1133
|
+
/// `page`: a cursor page (added to what was handed out) rather than the whole feed (which replaces it).
|
|
1134
|
+
private func cardsJson(_ feed: CardFeed, page: Bool) -> String {
|
|
1135
|
+
cardsLock.lock()
|
|
1136
|
+
if !page { lastCards.removeAll() }
|
|
1137
|
+
for card in feed.cards { lastCards[card.id] = card }
|
|
1138
|
+
cardsLock.unlock()
|
|
1139
|
+
guard let data = try? JSONSerialization.data(withJSONObject: feed.toJson()), let text = String(data: data, encoding: .utf8) else { return "{}" }
|
|
1140
|
+
return text
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
private func lastCard(_ id: String) -> Card? {
|
|
1144
|
+
cardsLock.lock()
|
|
1145
|
+
defer { cardsLock.unlock() }
|
|
1146
|
+
return lastCards[id]
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
/// `cursor`: a feed's `nextCursor` for the next page, nil for the feed.
|
|
1150
|
+
@objc func getCards(_ cursor: String?, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
|
|
1151
|
+
let c = (cursor?.isEmpty ?? true) ? nil : cursor
|
|
1152
|
+
Joryio.shared.getCards(cursor: c) { [weak self] feed in
|
|
1153
|
+
resolve(self?.cardsJson(feed, page: c != nil) ?? "{}")
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
/// From the native cache, never a network call.
|
|
1158
|
+
@objc func getUnreadCardCount(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
|
|
1159
|
+
resolve(Joryio.shared.getUnreadCardCount())
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
@objc func markCardViewed(_ cardId: String?) {
|
|
1163
|
+
guard let id = cardId, !id.isEmpty else { return }
|
|
1164
|
+
if let card = lastCard(id) { Joryio.shared.markCardViewed(card) } else { Joryio.shared.markCardViewed(id) }
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
@objc func markCardRead(_ cardId: String?) {
|
|
1168
|
+
guard let id = cardId, !id.isEmpty else { return }
|
|
1169
|
+
if let card = lastCard(id) { Joryio.shared.markCardRead(card) } else { Joryio.shared.markCardRead(id) }
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
@objc func markCardClicked(_ cardId: String?) {
|
|
1173
|
+
guard let id = cardId, !id.isEmpty else { return }
|
|
1174
|
+
if let card = lastCard(id) { Joryio.shared.markCardClicked(card) } else { Joryio.shared.markCardClicked(id) }
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
@objc func dismissCard(_ cardId: String?) {
|
|
1178
|
+
guard let id = cardId, !id.isEmpty else { return }
|
|
1179
|
+
if let card = lastCard(id) { Joryio.shared.dismissCard(card) } else { Joryio.shared.dismissCard(id) }
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
@objc func refreshCards() {
|
|
1183
|
+
Joryio.shared.refreshCards()
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
@objc func enableCardsUpdates() {
|
|
1187
|
+
guard cardsSubscription == nil else { return }
|
|
1188
|
+
// nil before initialize(): JS enables again after it (the call is dispatched).
|
|
1189
|
+
cardsSubscription = Joryio.shared.attachCardFeedListener { [weak self] feed in
|
|
1190
|
+
guard let self, self.hasListeners else { return }
|
|
1191
|
+
self.sendEvent(withName: "JoryioCardsUpdated", body: ["json": self.cardsJson(feed, page: false)])
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
@objc func disableCardsUpdates() {
|
|
1196
|
+
cardsSubscription?.cancel()
|
|
1197
|
+
cardsSubscription = nil
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
// MARK: - Links (setLinkHandler)
|
|
1201
|
+
|
|
1202
|
+
/// Links waiting for the JS handler's answer, by id. Bounded: a JS side
|
|
1203
|
+
/// that never answers costs at most this many entries, oldest dropped.
|
|
1204
|
+
private var pendingLinks: [String: JoryioLink] = [:]
|
|
1205
|
+
private var pendingLinkOrder: [String] = []
|
|
1206
|
+
private static let maxPendingLinks = 32
|
|
1207
|
+
private var nextLinkId = 0
|
|
1208
|
+
|
|
1209
|
+
/// JS set a link handler: every link the native SDK is about to open goes
|
|
1210
|
+
/// to JS first (event `JoryioLink`), and waits there for completeLink. The
|
|
1211
|
+
/// native handler takes the link at once - a bridge cannot answer
|
|
1212
|
+
/// synchronously - and JS hands a declined one back through completeLink.
|
|
1213
|
+
@objc func enableLinkHandler() {
|
|
1214
|
+
takeOwnership(.link)
|
|
1215
|
+
Joryio.shared.setLinkHandler { [weak self] link in
|
|
1216
|
+
guard let self, self.hasListeners else { return false }
|
|
1217
|
+
self.nextLinkId += 1
|
|
1218
|
+
let id = "l\(self.nextLinkId)"
|
|
1219
|
+
self.pendingLinks[id] = link
|
|
1220
|
+
self.pendingLinkOrder.append(id)
|
|
1221
|
+
while self.pendingLinkOrder.count > Self.maxPendingLinks {
|
|
1222
|
+
self.pendingLinks[self.pendingLinkOrder.removeFirst()] = nil
|
|
1223
|
+
}
|
|
1224
|
+
var body: [String: Any] = [
|
|
1225
|
+
"linkId": id,
|
|
1226
|
+
"url": link.url.absoluteString,
|
|
1227
|
+
"source": link.source.rawValue,
|
|
1228
|
+
"isAppDeepLink": link.isAppDeepLink,
|
|
1229
|
+
]
|
|
1230
|
+
if let v = link.campaignId { body["campaignId"] = v }
|
|
1231
|
+
if let v = link.placementKey { body["placementKey"] = v }
|
|
1232
|
+
if let v = link.itemId { body["itemId"] = v }
|
|
1233
|
+
if let v = link.cardId { body["cardId"] = v }
|
|
1234
|
+
self.sendEvent(withName: "JoryioLink", body: body)
|
|
1235
|
+
return true
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
@objc func disableLinkHandler() {
|
|
1240
|
+
if releaseOwnership(.link) { Joryio.shared.clearLinkHandler() }
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
/// The JS handler's answer: `handled` false opens the link as the SDK does
|
|
1244
|
+
/// without a handler.
|
|
1245
|
+
@objc func completeLink(_ linkId: String?, handled: Bool) {
|
|
1246
|
+
DispatchQueue.main.async { [weak self] in
|
|
1247
|
+
guard let self, let linkId, let link = self.pendingLinks.removeValue(forKey: linkId) else { return }
|
|
1248
|
+
self.pendingLinkOrder.removeAll { $0 == linkId }
|
|
1249
|
+
if !handled { link.openDefault() }
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
private func releasePendingLinks() {
|
|
1254
|
+
pendingLinks.removeAll()
|
|
1255
|
+
pendingLinkOrder.removeAll()
|
|
1256
|
+
}
|
|
1257
|
+
|
|
966
1258
|
/// Forget which campaigns have already been shown on this device.
|
|
967
1259
|
///
|
|
968
1260
|
/// 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
|
+
}
|