Manos Chorianopoulos

fix webviewDidFocus for app termination

......@@ -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)
saveSteps(startTracking: false)
swiftApi().setPacingDetailsAsync(persistedSteps, dateString, setPacingDetailsAsyncCallback, failureCallback: {errorCode in })
}
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() {
......@@ -155,7 +133,76 @@ 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! =========")
......@@ -340,9 +387,16 @@ var timer2: DispatchSourceTimer?
// MARK: - API Calls
func startTrackingSteps() {
let persistedSteps = swiftApi().getSteps()
if (persistedSteps > 0) {
saveSteps(startTracking: true)
} else {
swiftApi().startTrackingSteps(startTrackingStepsCallback)
}
// swiftApi().startTrackingSteps(startTrackingStepsCallback)
}
func startTrackingStepsCallback (_ steps: Int) -> Void {
DispatchQueue.main.async {
}
......@@ -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) {
......