Manos Chorianopoulos

fix webviewDidFocus for app termination

...@@ -77,6 +77,9 @@ var timer2: DispatchSourceTimer? ...@@ -77,6 +77,9 @@ var timer2: DispatchSourceTimer?
77 setBackButton() 77 setBackButton()
78 } 78 }
79 79
80 + // Add observers for application state changes
81 + NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
82 + NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
80 } 83 }
81 84
82 public override func viewWillAppear(_ animated: Bool) { 85 public override func viewWillAppear(_ animated: Bool) {
...@@ -90,14 +93,7 @@ var timer2: DispatchSourceTimer? ...@@ -90,14 +93,7 @@ var timer2: DispatchSourceTimer?
90 93
91 if(webViewLoaded) { 94 if(webViewLoaded) {
92 95
93 - let scriptSource2 = "webviewDidFocus(\(self.webViewLoaded));" 96 + sendWebviewDidFocus()
94 -
95 - webView.evaluateJavaScript(scriptSource2, completionHandler: { (object, error) in
96 -// print("==== object viewWillAppear ====")
97 -// print(object)
98 -// print("==== error viewWillAppear ====")
99 -// print(error)
100 - })
101 } 97 }
102 } 98 }
103 99
...@@ -110,27 +106,9 @@ var timer2: DispatchSourceTimer? ...@@ -110,27 +106,9 @@ var timer2: DispatchSourceTimer?
110 106
111 stopTimer() 107 stopTimer()
112 108
113 - let persistedSteps = swiftApi().getSteps() 109 + saveSteps(startTracking: false)
114 - if (persistedSteps > 0) {
115 -
116 - let currentDateTime = Date()
117 - let dateFormatter = DateFormatter()
118 - dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
119 - let dateString = dateFormatter.string(from: currentDateTime)
120 -
121 - swiftApi().setPacingDetailsAsync(persistedSteps, dateString, setPacingDetailsAsyncCallback, failureCallback: {errorCode in })
122 - }
123 110
124 - func setPacingDetailsAsyncCallback (_ responseData: swiftApi.GenericResponseModel?) -> Void { 111 + removeApplicationObservers()
125 - if (responseData != nil) {
126 - DispatchQueue.main.async {
127 - if (responseData?.getStatus == 1) {
128 - // print("===== steps sent on back button press ====")
129 - }
130 - }
131 - } else {
132 - }
133 - }
134 } 112 }
135 113
136 // public override func viewDidLoad() { 114 // public override func viewDidLoad() {
...@@ -154,8 +132,77 @@ var timer2: DispatchSourceTimer? ...@@ -154,8 +132,77 @@ var timer2: DispatchSourceTimer?
154 // 132 //
155 // webview.configuration.userContentController.add(self, name: "Cosmote") 133 // webview.configuration.userContentController.add(self, name: "Cosmote")
156 // } 134 // }
135 +
136 + deinit {
137 + // Remove observers to avoid memory leaks
138 + removeApplicationObservers()
139 + }
140 +
141 + @objc func applicationWillEnterForeground() {
142 + // Handle code when the application is about to enter the foreground
143 +
144 + sendWebviewDidFocus()
145 + }
146 +
147 + @objc func applicationDidEnterBackground() {
148 + // Handle code when the application enters the background
149 +
150 + saveSteps(startTracking: false)
151 + }
157 152
158 // MARK: - Functions 153 // MARK: - Functions
154 + func sendWebviewDidFocus() {
155 + let scriptSource2 = "webviewDidFocus(\(self.webViewLoaded),\(swiftApi().getTrackingStepsEnabled()));"
156 +
157 + webView.evaluateJavaScript(scriptSource2, completionHandler: { (object, error) in
158 + // print("==== webviewDidFocus object ====")
159 + // print(object)
160 + // print("==== webviewDidFocus error ====")
161 + // print(error)
162 + })
163 + }
164 +
165 + func addApplicationObservers() {
166 + NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
167 + NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
168 + }
169 +
170 + func removeApplicationObservers() {
171 + NotificationCenter.default.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil)
172 + NotificationCenter.default.removeObserver(self, name: UIApplication.didEnterBackgroundNotification, object: nil)
173 + }
174 +
175 + func saveSteps(startTracking: Bool) {
176 + let persistedSteps = swiftApi().getSteps()
177 + if (persistedSteps > 0) {
178 +
179 + let currentDateTime = Date()
180 + let dateFormatter = DateFormatter()
181 + dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
182 + let dateString = dateFormatter.string(from: currentDateTime)
183 +
184 + swiftApi().setPacingDetailsAsync(persistedSteps, dateString, setPacingDetailsAsyncCallback, failureCallback: {errorCode in })
185 + }
186 +
187 + func setPacingDetailsAsyncCallback (_ responseData: swiftApi.GenericResponseModel?) -> Void {
188 + if (responseData != nil) {
189 + DispatchQueue.main.async {
190 + if (responseData?.getStatus == 1) {
191 + if (startTracking) {
192 + // TODO: DELETE LOGS
193 + // print("===== startTrackingSteps after save persisted steps ====")
194 +
195 + self.sendWebviewDidFocus()
196 +
197 + swiftApi().startTrackingSteps(self.startTrackingStepsCallback)
198 + }
199 + }
200 + }
201 + } else {
202 + }
203 + }
204 + }
205 +
159 func startTimer() { 206 func startTimer() {
160 // print("========= Webview Timer Started! =========") 207 // print("========= Webview Timer Started! =========")
161 208
...@@ -340,7 +387,14 @@ var timer2: DispatchSourceTimer? ...@@ -340,7 +387,14 @@ var timer2: DispatchSourceTimer?
340 387
341 // MARK: - API Calls 388 // MARK: - API Calls
342 func startTrackingSteps() { 389 func startTrackingSteps() {
343 - swiftApi().startTrackingSteps(startTrackingStepsCallback) 390 + let persistedSteps = swiftApi().getSteps()
391 + if (persistedSteps > 0) {
392 + saveSteps(startTracking: true)
393 + } else {
394 + swiftApi().startTrackingSteps(startTrackingStepsCallback)
395 + }
396 +
397 +// swiftApi().startTrackingSteps(startTrackingStepsCallback)
344 } 398 }
345 399
346 func startTrackingStepsCallback (_ steps: Int) -> Void { 400 func startTrackingStepsCallback (_ steps: Int) -> Void {
...@@ -365,14 +419,14 @@ var timer2: DispatchSourceTimer? ...@@ -365,14 +419,14 @@ var timer2: DispatchSourceTimer?
365 } 419 }
366 }) 420 })
367 421
368 - let scriptSource2 = "webviewDidFocus(\(self.webViewLoaded));" 422 +// let scriptSource2 = "webviewDidFocus(\(self.webViewLoaded),\(swiftApi().getTrackingStepsEnabled()));"
369 - 423 +//
370 - webView.evaluateJavaScript(scriptSource2, completionHandler: { (object, error) in 424 +// webView.evaluateJavaScript(scriptSource2, completionHandler: { (object, error) in
371 -// print("==== object webView ====") 425 +// print("==== webviewDidFocus object didFinish ====")
372 // print(object) 426 // print(object)
373 -// print("==== error webView ====") 427 +// print("==== webviewDidFocus error didFinish ====")
374 // print(error) 428 // print(error)
375 - }) 429 +// })
376 } 430 }
377 431
378 public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { 432 public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
......