CampaignViewController.swift
8.46 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// CampaignViewController.swift
// SwiftWarplyFramework
//
// Created by Βασιλης Σκουρας on 11/5/22.
//
import Foundation
import UIKit
import WebKit
import SwiftEventBus
var timer2: DispatchSourceTimer?
@objc public class CampaignViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {
@IBOutlet weak var webview: WKWebView!
public var campaignUrl: String = ""
public var params: String = ""
public var showHeader: Bool = false
public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if (!showHeader) {
// navigationController?.setNavigationBarHidden(true, animated: animated)
self.navigationController?.isNavigationBarHidden = true
}
}
public override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if (!showHeader) {
// navigationController?.setNavigationBarHidden(false, animated: animated)
self.navigationController?.isNavigationBarHidden = false
}
stopTimer()
}
public override func viewDidLoad() {
super.viewDidLoad()
var campaignUrl = campaignUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
print("Webview url: " + (campaignUrl ?? ""))
self.hidesBottomBarWhenPushed = true
if (showHeader) {
setBackButton()
}
webview.navigationDelegate = self
if let url = URL(string: campaignUrl ?? "") {
webview.load(URLRequest(url: url))
webview.allowsBackForwardNavigationGestures = true
}
webview.configuration.userContentController.add(self, name: "Cosmote")
}
// MARK: - Functions
func startTimer() {
print("========= Webview Timer Started! =========")
let queue = DispatchQueue(label: Bundle.main.bundleIdentifier! + ".webview.timer")
timer2 = DispatchSource.makeTimerSource(queue: queue)
timer2!.schedule(deadline: .now(), repeating: .seconds(1))
timer2!.setEventHandler { [weak self] in
// do whatever stuff you want on the background queue here here
print("========= Webview interval! =========")
DispatchQueue.main.async {
// update your model objects and/or UI here
let metersParam = swiftApi().getMetersCount()
let scriptSource = "passMeters(\(metersParam));"
self?.webview.evaluateJavaScript(scriptSource, completionHandler: { (object, error) in
print("==== object ====")
print(object)
print("==== error ====")
print(error)
})
}
}
timer2!.resume()
}
func stopTimer() {
print("========= Webview Timer Stopped! =========")
timer2?.cancel()
timer2 = nil
}
// MARK: - API Calls
func startTrackingSteps() {
swiftApi().startTrackingSteps(startTrackingStepsCallback)
}
func startTrackingStepsCallback (_ steps: Int) -> Void {
DispatchQueue.main.async {
}
}
// MARK: - WKNavigationDelegate
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
let scriptSource = "passParams(\(params));"
webView.evaluateJavaScript(scriptSource, completionHandler: { (object, error) in
print("==== object ====")
print(object)
print("==== error ====")
print(error)
self.startTimer()
})
}
// MARK: - WKScriptMessageHandler
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if let event = message.body as? String {
let eventArray = event.split(separator: ":")
if (eventArray[0] == "event") {
if (eventArray[1] == "closeArtwork") {
swiftApi().getProfileAsync(getProfileCallback)
func getProfileCallback (_ profileData: swiftApi.ProfileModel?) -> Void {
if (profileData != nil) {
DispatchQueue.main.async {
swiftApi().getCampaignsAsyncNew(language: "en", filters: [String : Any](), getCampaignsCallback)
func getCampaignsCallback (_ campaignsData: Array<swiftApi.CampaignItemModel>?) -> Void {
if (campaignsData != nil) {
DispatchQueue.main.async {
}
} else {
}
}
}
} else {
}
}
self.navigationController?.popViewController(animated: true)
} else if (eventArray[1] == "addUserTag") {
let questionnaireEvent = swiftApi.QuestionnaireEventModel()
questionnaireEvent._name = String(eventArray[1])
if (eventArray.count > 2) {
questionnaireEvent._parameter = String(eventArray[2])
swiftApi().setUserTag(String(eventArray[2]))
} else {
questionnaireEvent._parameter = ""
swiftApi().setUserTag("")
}
SwiftEventBus.post("questionnaire", sender: questionnaireEvent)
} else if (eventArray[1] == "couponRetrieved") {
// let couponEvent = swiftApi.CouponEventModel()
// SwiftEventBus.post("coupon_retrieved", sender: couponEvent)
swiftApi().getCouponsAsync(getCouponsCallback)
func getCouponsCallback (_ couponsData: Array<swiftApi.CouponItemModel>?) -> Void {
if (couponsData != nil) {
DispatchQueue.main.async {
print("========= getCouponsRequest SUCCESSSS CampaignVC =========")
print("========= getCouponsRequest count CampaignVC =========")
print(swiftApi().getCouponList().count)
}
} else {
print("========= getCouponsRequest ERROR CampaignVC =========")
}
}
} else if (eventArray[1] == "couponShared") {
}
// else if (eventArray[2] == "serviceDisabled") {
// let pacingService = swiftApi.WarplyPacingEventModel()
// pacingService._isEnabled = false
// SwiftEventBus.post("pacing", sender: pacingService)
// } else if (eventArray[2] == "serviceEnabled") {
// let pacingService = swiftApi.WarplyPacingEventModel()
// pacingService._isEnabled = true
// SwiftEventBus.post("pacing", sender: pacingService)
// }
else if (eventArray[1] == "steps" && eventArray[2] == "shortcutEnabled") {
let pacingEvent = swiftApi.WarplyPacingEventModel()
pacingEvent._isVisible = true
SwiftEventBus.post("pacing", sender: pacingEvent)
self.startTrackingSteps()
} else if (eventArray[1] == "steps" && eventArray[2] == "shortcutDisabled") {
let pacingEvent = swiftApi.WarplyPacingEventModel()
pacingEvent._isVisible = false
SwiftEventBus.post("pacing", sender: pacingEvent)
swiftApi().stopTrackingSteps()
} else if (eventArray[1] == "request" || eventArray[1] == "response") {
print("**************** WARPLY Webview Log START *****************")
print(event)
print("**************** WARPLY Webview Log END *****************")
}
}
}
}
}