Manos Chorianopoulos

fix webviewDidFocus for app kill redeisign

......@@ -77,6 +77,9 @@ var timer2: DispatchSourceTimer?
setBackButton()
}
// Add observers for application state changes
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
}
public override func viewWillAppear(_ animated: Bool) {
......@@ -90,14 +93,7 @@ var timer2: DispatchSourceTimer?
if(webViewLoaded) {
let scriptSource2 = "webviewDidFocus(\(self.webViewLoaded));"
webView.evaluateJavaScript(scriptSource2, completionHandler: { (object, error) in
// print("==== object viewWillAppear ====")
// print(object)
// print("==== error viewWillAppear ====")
// print(error)
})
sendWebviewDidFocus()
}
}
......@@ -110,27 +106,9 @@ var timer2: DispatchSourceTimer?
stopTimer()
let persistedSteps = swiftApi().getSteps()
if (persistedSteps > 0) {
let currentDateTime = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = dateFormatter.string(from: currentDateTime)
swiftApi().setPacingDetailsAsync(persistedSteps, dateString, setPacingDetailsAsyncCallback, failureCallback: {errorCode in })
}
saveSteps(startTracking: false)
func setPacingDetailsAsyncCallback (_ responseData: swiftApi.GenericResponseModel?) -> Void {
if (responseData != nil) {
DispatchQueue.main.async {
if (responseData?.getStatus == 1) {
// print("===== steps sent on back button press ====")
}
}
} else {
}
}
removeApplicationObservers()
}
// public override func viewDidLoad() {
......@@ -154,8 +132,77 @@ var timer2: DispatchSourceTimer?
//
// webview.configuration.userContentController.add(self, name: "Cosmote")
// }
deinit {
// Remove observers to avoid memory leaks
removeApplicationObservers()
}
@objc func applicationWillEnterForeground() {
// Handle code when the application is about to enter the foreground
sendWebviewDidFocus()
}
@objc func applicationDidEnterBackground() {
// Handle code when the application enters the background
saveSteps(startTracking: false)
}
// MARK: - Functions
func sendWebviewDidFocus() {
let scriptSource2 = "webviewDidFocus(\(self.webViewLoaded),\(swiftApi().getTrackingStepsEnabled()));"
webView.evaluateJavaScript(scriptSource2, completionHandler: { (object, error) in
// print("==== webviewDidFocus object ====")
// print(object)
// print("==== webviewDidFocus error ====")
// print(error)
})
}
func addApplicationObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
}
func removeApplicationObservers() {
NotificationCenter.default.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIApplication.didEnterBackgroundNotification, object: nil)
}
func saveSteps(startTracking: Bool) {
let persistedSteps = swiftApi().getSteps()
if (persistedSteps > 0) {
let currentDateTime = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = dateFormatter.string(from: currentDateTime)
swiftApi().setPacingDetailsAsync(persistedSteps, dateString, setPacingDetailsAsyncCallback, failureCallback: {errorCode in })
}
func setPacingDetailsAsyncCallback (_ responseData: swiftApi.GenericResponseModel?) -> Void {
if (responseData != nil) {
DispatchQueue.main.async {
if (responseData?.getStatus == 1) {
if (startTracking) {
// TODO: DELETE LOGS
// print("===== startTrackingSteps after save persisted steps ====")
self.sendWebviewDidFocus()
swiftApi().startTrackingSteps(self.startTrackingStepsCallback)
}
}
}
} else {
}
}
}
func startTimer() {
// print("========= Webview Timer Started! =========")
......@@ -172,10 +219,10 @@ var timer2: DispatchSourceTimer?
let scriptSource = "passMeters(\(metersParam));"
self?.webView.evaluateJavaScript(scriptSource, completionHandler: { (object, error) in
// print("==== object passMeters ====")
// print(object)
// print("==== error passMeters ====")
// print(error)
// print("==== object passMeters ====")
// print(object)
// print("==== error passMeters ====")
// print(error)
swiftApi().setSavedStepsWebview(swiftApi().getSavedStepsWebview() + swiftApi().getStepsWebview())
swiftApi().setStepsWebview(0)
......@@ -340,7 +387,14 @@ var timer2: DispatchSourceTimer?
// MARK: - API Calls
func startTrackingSteps() {
swiftApi().startTrackingSteps(startTrackingStepsCallback)
let persistedSteps = swiftApi().getSteps()
if (persistedSteps > 0) {
saveSteps(startTracking: true)
} else {
swiftApi().startTrackingSteps(startTrackingStepsCallback)
}
// swiftApi().startTrackingSteps(startTrackingStepsCallback)
}
func startTrackingStepsCallback (_ steps: Int) -> Void {
......@@ -355,9 +409,9 @@ var timer2: DispatchSourceTimer?
let scriptSource = "passParams(\(params));"
webView.evaluateJavaScript(scriptSource, completionHandler: { (object, error) in
// print("==== object passParams ====")
// print("==== object ====")
// print(object)
// print("==== error passParams ====")
// print("==== error ====")
// print(error)
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
......@@ -365,14 +419,14 @@ var timer2: DispatchSourceTimer?
}
})
let scriptSource2 = "webviewDidFocus(\(self.webViewLoaded));"
webView.evaluateJavaScript(scriptSource2, completionHandler: { (object, error) in
// print("==== object webView ====")
// let scriptSource2 = "webviewDidFocus(\(self.webViewLoaded),\(swiftApi().getTrackingStepsEnabled()));"
//
// webView.evaluateJavaScript(scriptSource2, completionHandler: { (object, error) in
// print("==== webviewDidFocus object didFinish ====")
// print(object)
// print("==== error webView ====")
// print("==== webviewDidFocus error didFinish ====")
// print(error)
})
// })
}
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
......