Manos Chorianopoulos

add real time pacing data

...@@ -10,6 +10,8 @@ import UIKit ...@@ -10,6 +10,8 @@ import UIKit
10 import WebKit 10 import WebKit
11 import SwiftEventBus 11 import SwiftEventBus
12 12
13 +var timer: DispatchSourceTimer?
14 +
13 @objc public class CampaignViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler { 15 @objc public class CampaignViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {
14 @IBOutlet weak var webview: WKWebView! 16 @IBOutlet weak var webview: WKWebView!
15 17
...@@ -31,6 +33,8 @@ import SwiftEventBus ...@@ -31,6 +33,8 @@ import SwiftEventBus
31 // navigationController?.setNavigationBarHidden(false, animated: animated) 33 // navigationController?.setNavigationBarHidden(false, animated: animated)
32 self.navigationController?.isNavigationBarHidden = false 34 self.navigationController?.isNavigationBarHidden = false
33 } 35 }
36 +
37 + stopTimer()
34 } 38 }
35 39
36 public override func viewDidLoad() { 40 public override func viewDidLoad() {
...@@ -54,6 +58,39 @@ import SwiftEventBus ...@@ -54,6 +58,39 @@ import SwiftEventBus
54 webview.configuration.userContentController.add(self, name: "Cosmote") 58 webview.configuration.userContentController.add(self, name: "Cosmote")
55 } 59 }
56 60
61 + // MARK: - Functions
62 + func startTimer() {
63 + print("========= Webview Timer Started! =========")
64 +
65 + let queue = DispatchQueue(label: Bundle.main.bundleIdentifier! + ".webview.timer")
66 + timer = DispatchSource.makeTimerSource(queue: queue)
67 + timer!.schedule(deadline: .now(), repeating: .seconds(1))
68 + timer!.setEventHandler { [weak self] in
69 + // do whatever stuff you want on the background queue here here
70 + print("========= Webview interval! =========")
71 +
72 + DispatchQueue.main.async {
73 + // update your model objects and/or UI here
74 + let metersParam = swiftApi().getMetersCount()
75 + let scriptSource = "passMeters(\(metersParam));"
76 +
77 + self?.webview.evaluateJavaScript(scriptSource, completionHandler: { (object, error) in
78 + print("==== object ====")
79 + print(object)
80 + print("==== error ====")
81 + print(error)
82 + })
83 + }
84 + }
85 + timer!.resume()
86 + }
87 +
88 + func stopTimer() {
89 + print("========= Webview Timer Stopped! =========")
90 + timer?.cancel()
91 + timer = nil
92 + }
93 +
57 // MARK: - API Calls 94 // MARK: - API Calls
58 func startTrackingSteps() { 95 func startTrackingSteps() {
59 swiftApi().startTrackingSteps(startTrackingStepsCallback) 96 swiftApi().startTrackingSteps(startTrackingStepsCallback)
...@@ -74,6 +111,8 @@ import SwiftEventBus ...@@ -74,6 +111,8 @@ import SwiftEventBus
74 print(object) 111 print(object)
75 print("==== error ====") 112 print("==== error ====")
76 print(error) 113 print(error)
114 +
115 + self.startTimer()
77 }) 116 })
78 } 117 }
79 118
......
...@@ -50,6 +50,14 @@ public class swiftApi { ...@@ -50,6 +50,14 @@ public class swiftApi {
50 UserDefaults.standard.set(newValue, forKey: "steps") 50 UserDefaults.standard.set(newValue, forKey: "steps")
51 } 51 }
52 52
53 + public func getMetersCount() -> Double {
54 + return UserDefaults.standard.double(forKey: "metersCount")
55 + }
56 +
57 + public func setMetersCount(_ newValue: Double) -> Void {
58 + UserDefaults.standard.set(newValue, forKey: "metersCount")
59 + }
60 +
53 61
54 public func startTrackingSteps(_ getStepsCallback: @escaping (_ steps: Int) -> Void) -> Void { 62 public func startTrackingSteps(_ getStepsCallback: @escaping (_ steps: Int) -> Void) -> Void {
55 63
...@@ -91,6 +99,16 @@ public class swiftApi { ...@@ -91,6 +99,16 @@ public class swiftApi {
91 99
92 print((steps as! Int) - GlobalVariables.savedSteps) 100 print((steps as! Int) - GlobalVariables.savedSteps)
93 swiftApi().setSteps((steps as! Int) - GlobalVariables.savedSteps) 101 swiftApi().setSteps((steps as! Int) - GlobalVariables.savedSteps)
102 +
103 + let metersCount = (steps as? Double ?? 0.0) * 0.762
104 + swiftApi().setMetersCount(metersCount)
105 +
106 + let pacingEvent = swiftApi.HealthEventModel()
107 + pacingEvent._meters = metersCount
108 + SwiftEventBus.post("meters_sent", sender: pacingEvent)
109 +
110 + print("=== MetersCount ===")
111 + print(metersCount)
94 } 112 }
95 113
96 getStepsCallback(steps as? Int ?? 0) 114 getStepsCallback(steps as? Int ?? 0)
...@@ -104,6 +122,9 @@ public class swiftApi { ...@@ -104,6 +122,9 @@ public class swiftApi {
104 122
105 stopTimer() 123 stopTimer()
106 124
125 + GlobalVariables.savedSteps = 0
126 + swiftApi().setMetersCount(0.0)
127 +
107 let persistedSteps = swiftApi().getSteps() 128 let persistedSteps = swiftApi().getSteps()
108 if (persistedSteps > 0) { 129 if (persistedSteps > 0) {
109 130
...@@ -119,6 +140,7 @@ public class swiftApi { ...@@ -119,6 +140,7 @@ public class swiftApi {
119 if (responseData != nil) { 140 if (responseData != nil) {
120 DispatchQueue.main.async { 141 DispatchQueue.main.async {
121 if (responseData?.getStatus == 1) { 142 if (responseData?.getStatus == 1) {
143 + GlobalVariables.savedSteps = 0
122 } 144 }
123 } 145 }
124 } else { 146 } else {
...@@ -5164,4 +5186,22 @@ public class swiftApi { ...@@ -5164,4 +5186,22 @@ public class swiftApi {
5164 } 5186 }
5165 5187
5166 5188
5189 + public class HealthEventModel {
5190 + private var meters: Double
5191 +
5192 + init() {
5193 + self.meters = 0.0
5194 + }
5195 +
5196 + public var _meters: Double {
5197 + get { // getter
5198 + return self.meters
5199 + }
5200 + set(newValue) { //setter
5201 + self.meters = newValue
5202 + }
5203 + }
5204 + }
5205 +
5206 +
5167 } 5207 }
......