Manos Chorianopoulos

add real time pacing data

......@@ -10,6 +10,8 @@ import UIKit
import WebKit
import SwiftEventBus
var timer: DispatchSourceTimer?
@objc public class CampaignViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {
@IBOutlet weak var webview: WKWebView!
......@@ -31,6 +33,8 @@ import SwiftEventBus
// navigationController?.setNavigationBarHidden(false, animated: animated)
self.navigationController?.isNavigationBarHidden = false
}
stopTimer()
}
public override func viewDidLoad() {
......@@ -54,6 +58,39 @@ import SwiftEventBus
webview.configuration.userContentController.add(self, name: "Cosmote")
}
// MARK: - Functions
func startTimer() {
print("========= Webview Timer Started! =========")
let queue = DispatchQueue(label: Bundle.main.bundleIdentifier! + ".webview.timer")
timer = DispatchSource.makeTimerSource(queue: queue)
timer!.schedule(deadline: .now(), repeating: .seconds(1))
timer!.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)
})
}
}
timer!.resume()
}
func stopTimer() {
print("========= Webview Timer Stopped! =========")
timer?.cancel()
timer = nil
}
// MARK: - API Calls
func startTrackingSteps() {
swiftApi().startTrackingSteps(startTrackingStepsCallback)
......@@ -74,6 +111,8 @@ import SwiftEventBus
print(object)
print("==== error ====")
print(error)
self.startTimer()
})
}
......
......@@ -50,6 +50,14 @@ public class swiftApi {
UserDefaults.standard.set(newValue, forKey: "steps")
}
public func getMetersCount() -> Double {
return UserDefaults.standard.double(forKey: "metersCount")
}
public func setMetersCount(_ newValue: Double) -> Void {
UserDefaults.standard.set(newValue, forKey: "metersCount")
}
public func startTrackingSteps(_ getStepsCallback: @escaping (_ steps: Int) -> Void) -> Void {
......@@ -91,6 +99,16 @@ public class swiftApi {
print((steps as! Int) - GlobalVariables.savedSteps)
swiftApi().setSteps((steps as! Int) - GlobalVariables.savedSteps)
let metersCount = (steps as? Double ?? 0.0) * 0.762
swiftApi().setMetersCount(metersCount)
let pacingEvent = swiftApi.HealthEventModel()
pacingEvent._meters = metersCount
SwiftEventBus.post("meters_sent", sender: pacingEvent)
print("=== MetersCount ===")
print(metersCount)
}
getStepsCallback(steps as? Int ?? 0)
......@@ -104,6 +122,9 @@ public class swiftApi {
stopTimer()
GlobalVariables.savedSteps = 0
swiftApi().setMetersCount(0.0)
let persistedSteps = swiftApi().getSteps()
if (persistedSteps > 0) {
......@@ -119,6 +140,7 @@ public class swiftApi {
if (responseData != nil) {
DispatchQueue.main.async {
if (responseData?.getStatus == 1) {
GlobalVariables.savedSteps = 0
}
}
} else {
......@@ -5164,4 +5186,22 @@ public class swiftApi {
}
public class HealthEventModel {
private var meters: Double
init() {
self.meters = 0.0
}
public var _meters: Double {
get { // getter
return self.meters
}
set(newValue) { //setter
self.meters = newValue
}
}
}
}
......