-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoCodesSandwich.swift
More file actions
191 lines (158 loc) · 6.78 KB
/
Copy pathNoCodesSandwich.swift
File metadata and controls
191 lines (158 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// NoCodesSandwich.swift
// QonversionSandwich
//
// Created by Suren Sarkisyan on 08.05.2025.
// Copyright © 2025 Qonversion Inc. All rights reserved.
//
#if os(iOS)
import Foundation
import Qonversion
import UIKit
public class NoCodesSandwich: NSObject {
private var noCodesEventListener: NoCodesEventListener?
private var defaultPresentationConfig: NoCodesPresentationConfiguration? = nil
private var screenPresentationConfigs: [String: NoCodesPresentationConfiguration] = [:]
private var isCustomizationDelegateSet = false
private var purchaseDelegateBridge: NoCodesPurchaseDelegateBridge?
private var purchaseContinuation: CheckedContinuation<Void, Error>?
private var restoreContinuation: CheckedContinuation<Void, Error>?
@objc public init(noCodesEventListener: NoCodesEventListener) {
self.noCodesEventListener = noCodesEventListener
}
@objc public func initialize(
projectKey: String,
proxyUrl: String? = nil
) {
let noCodesConfig = NoCodesConfiguration(projectKey: projectKey, proxyURL: proxyUrl?.isEmpty == true ? nil : proxyUrl)
NoCodes.initialize(with: noCodesConfig)
NoCodes.shared.set(delegate: self)
}
@objc public func storeSdkInfo(source: String, version: String) {
// Does nothing on iOS as No-Codes are integrated into the Qonversion SDK and share its source and version
}
@MainActor @objc public func setScreenPresentationConfig(_ configData: [String: Any], forContextKey contextKey: String? = nil) {
let config = configData.toPresentationConfig()
if (!isCustomizationDelegateSet) {
isCustomizationDelegateSet = true
NoCodes.shared.set(screenCustomizationDelegate: self)
}
if let contextKey = contextKey, !contextKey.isEmpty {
screenPresentationConfigs[contextKey] = config
} else {
screenPresentationConfigs = [:]
defaultPresentationConfig = config
}
}
@MainActor @objc public func showScreen(_ contextKey: String) {
NoCodes.shared.showScreen(withContextKey: contextKey)
}
@MainActor @objc public func close() {
NoCodes.shared.close()
}
@objc public func getAvailableEvents() -> [String] {
let availableEvents: [NoCodesEvent] = [
.screenShown,
.finished,
.actionStarted,
.actionFailed,
.actionFinished,
.screenFailedToLoad
]
return availableEvents.map { $0.rawValue }
}
@objc public func setPurchaseDelegate(_ delegate: NoCodesPurchaseDelegateBridge) {
purchaseDelegateBridge = delegate
NoCodes.shared.set(purchaseDelegate: self)
}
@objc public func delegatedPurchaseCompleted() {
guard let continuation = purchaseContinuation else { return }
purchaseContinuation = nil
continuation.resume(returning: ())
}
@objc public func delegatedPurchaseFailed(_ errorMessage: String) {
let error = NSError(
domain: "NoCodesBridge",
code: -1,
userInfo: [NSLocalizedDescriptionKey: errorMessage]
)
guard let continuation = purchaseContinuation else { return }
purchaseContinuation = nil
continuation.resume(throwing: error)
}
@objc public func delegatedRestoreCompleted() {
guard let continuation = restoreContinuation else { return }
restoreContinuation = nil
continuation.resume(returning: ())
}
@objc public func delegatedRestoreFailed(_ errorMessage: String) {
let error = NSError(
domain: "NoCodesBridge",
code: -1,
userInfo: [NSLocalizedDescriptionKey: errorMessage]
)
guard let continuation = restoreContinuation else { return }
restoreContinuation = nil
continuation.resume(throwing: error)
}
}
extension NoCodesSandwich: NoCodesScreenCustomizationDelegate {
public func presentationConfigurationForScreen(contextKey: String) -> NoCodesPresentationConfiguration {
return screenPresentationConfigs[contextKey] ?? defaultPresentationConfig ?? .defaultConfiguration()
}
public func presentationConfigurationForScreen(id: String) -> NoCodesPresentationConfiguration {
return screenPresentationConfigs[id] ?? defaultPresentationConfig ?? .defaultConfiguration()
}
public func viewForPopoverPresentation() -> UIView? {
return nil
}
}
extension NoCodesSandwich: NoCodesDelegate {
public func controllerForNavigation() -> UIViewController? {
return nil
}
public func noCodesHasShownScreen(id: String) {
let payload: BridgeData = ["screenId": id]
noCodesEventListener?.noCodesDidTrigger(event: NoCodesEvent.screenShown.rawValue, payload: payload.clearEmptyValues())
}
public func noCodesStartsExecuting(action: NoCodesAction) {
let payload: BridgeData = action.toMap()
noCodesEventListener?.noCodesDidTrigger(event: NoCodesEvent.actionStarted.rawValue, payload: payload.clearEmptyValues())
}
public func noCodesFailedToExecute(action: NoCodesAction, error: Error?) {
var payload: BridgeData = action.toMap()
payload["error"] = errorToMap(error)
noCodesEventListener?.noCodesDidTrigger(event: NoCodesEvent.actionFailed.rawValue, payload: payload.clearEmptyValues())
}
public func noCodesFinishedExecuting(action: NoCodesAction) {
let payload: BridgeData = action.toMap()
noCodesEventListener?.noCodesDidTrigger(event: NoCodesEvent.actionFinished.rawValue, payload: payload.clearEmptyValues())
}
public func noCodesFinished() {
noCodesEventListener?.noCodesDidTrigger(event: NoCodesEvent.finished.rawValue, payload: nil)
}
public func noCodesFailedToLoadScreen(error: Error?) {
noCodesEventListener?.noCodesDidTrigger(event: NoCodesEvent.screenFailedToLoad.rawValue, payload: errorToMap(error)?.clearEmptyValues())
}
}
extension NoCodesSandwich: NoCodesPurchaseDelegate {
public func purchase(product: Qonversion.Product) async throws {
guard let delegate = purchaseDelegateBridge else { return }
try await withCheckedThrowingContinuation { continuation in
purchaseContinuation = continuation
Task { @MainActor in
delegate.purchase(product.toMap().clearEmptyValues())
}
}
}
public func restore() async throws {
guard let delegate = purchaseDelegateBridge else { return }
try await withCheckedThrowingContinuation { continuation in
restoreContinuation = continuation
Task { @MainActor in
delegate.restore()
}
}
}
}
#endif