-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e9674a
commit 9e03bbb
Showing
4 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// InAppMessageAction.swift | ||
// exponea | ||
// | ||
|
||
import Foundation | ||
import ExponeaSDK | ||
|
||
final class InAppMessageAction { | ||
let message: InAppMessage | ||
let button: InAppMessageButton? | ||
let interaction: Bool | ||
|
||
init(message: InAppMessage, button: InAppMessageButton?, interaction: Bool) { | ||
self.message = message | ||
self.button = button | ||
self.interaction = interaction | ||
} | ||
|
||
func toMap() -> [String:Any?] { | ||
var data : [String:Any?] = [ | ||
"message": [ | ||
"id": message.id, | ||
"name": message.name, | ||
"messageType": message.messageType.rawValue, | ||
"frequency": message.frequency?.rawValue, | ||
"payload": message.payload != nil ? try? String(data: JSONEncoder().encode(message.payload), encoding: .utf8) : nil, | ||
"variantId": message.variantId, | ||
"variantName": message.variantName, | ||
"trigger": try? String(data: JSONEncoder().encode(message.trigger), encoding: .utf8), | ||
"dateFilter": try? String(data: JSONEncoder().encode(message.dateFilter), encoding: .utf8), | ||
"loadPriority": message.priority, | ||
"loadDelay": message.delayMS, | ||
"closeTimeout": message.timeoutMS, | ||
"payloadHtml": message.payloadHtml, | ||
"isHtml": message.isHtml, | ||
"hasTrackingConsent": message.hasTrackingConsent, | ||
"consentCategoryTracking": message.consentCategoryTracking, | ||
], | ||
"interaction": interaction, | ||
] | ||
if let button { | ||
data["button"] = [ | ||
"text": button.text, | ||
"url": button.url | ||
] | ||
} | ||
return data | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// InAppMessageActionStreamHandler.swift | ||
// exponea | ||
// | ||
|
||
import Foundation | ||
import ExponeaSDK | ||
import Flutter | ||
|
||
public class InAppMessageActionStreamHandler: NSObject, FlutterStreamHandler, InAppMessageActionDelegate { | ||
private(set) static var currentInstance: InAppMessageActionStreamHandler = InAppMessageActionStreamHandler() | ||
|
||
// We have to hold inAppMessage until plugin is initialized and listener set | ||
private var pendingData: InAppMessageAction? | ||
|
||
public var overrideDefaultBehavior: Bool = false | ||
|
||
public var trackActions: Bool = true | ||
|
||
private override init() {} | ||
|
||
private var eventSink: FlutterEventSink? | ||
|
||
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? { | ||
eventSink = events | ||
if let data = pendingData { | ||
if handle(action: data) { | ||
pendingData = nil | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
public func onCancel(withArguments arguments: Any?) -> FlutterError? { | ||
eventSink = nil | ||
overrideDefaultBehavior = false | ||
trackActions = true | ||
return nil | ||
} | ||
|
||
public func inAppMessageAction(with message: ExponeaSDK.InAppMessage, button: ExponeaSDK.InAppMessageButton?, interaction: Bool) { | ||
_ = handle(action: InAppMessageAction( | ||
message: message, | ||
button: button, | ||
interaction: interaction | ||
)) | ||
} | ||
|
||
private func handle(action: InAppMessageAction) -> Bool { | ||
guard let sink = eventSink else { | ||
pendingData = action | ||
return false | ||
} | ||
sink(action.toMap()) | ||
return true | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters