Jelajahi Sumber

Working enact/cancel

Jonas Björkert 2 tahun lalu
induk
melakukan
a74897dba7

+ 5 - 16
LoopFollow/Remote/RemoteView.swift

@@ -97,7 +97,6 @@ struct RemoteView: View {
                             }
                         }
                     }
-                    .disabled(isLoading)
 
                     if isLoading {
                         ProgressView("Please wait...")
@@ -144,44 +143,34 @@ struct RemoteView: View {
 
     private var isButtonDisabled: Bool {
         return newHKTarget.doubleValue(for: UserDefaultsRepository.getPreferredUnit()) == 0 ||
-        duration.doubleValue(for: HKUnit.minute()) == 0
+        duration.doubleValue(for: HKUnit.minute()) == 0 || isLoading
     }
 
     private func enactTempTarget() {
         isLoading = true
-        print("Enacting Temp Target with target: \(newHKTarget) and duration: \(duration)")
         sendTempTarget(newHKTarget, duration) { success in
             self.isLoading = false
             if success {
-                print("Target successfully enacted.")
                 self.statusMessage = "Target successfully enacted."
             } else {
-                print("Failed to enact target.")
                 self.statusMessage = "Failed to enact target."
             }
-            DispatchQueue.main.async {
-                self.alertType = .status
-                self.showAlert = true
-            }
+            self.alertType = .status
+            self.showAlert = true
         }
     }
 
     private func cancelTempTarget() {
         isLoading = true
-        print("Cancelling Temp Target...")
         onCancelExistingTarget() { success in
             self.isLoading = false
             if success {
-                print("Temp target successfully cancelled.")
                 self.statusMessage = "Temp target successfully cancelled."
             } else {
-                print("Failed to cancel temp target.")
                 self.statusMessage = "Failed to cancel temp target."
             }
-            DispatchQueue.main.async {
-                self.alertType = .status
-                self.showAlert = true
-            }
+            self.alertType = .status
+            self.showAlert = true
         }
     }
 }

+ 4 - 0
LoopFollow/Remote/RemoteViewController.swift

@@ -48,6 +48,8 @@ class RemoteViewController: UIViewController {
             do {
                 let response: [TreatmentCancelResponse] = try await NightscoutUtils.executePostRequest(eventType: .treatments, body: tempTargetBody)
                 print("Success: \(response)")
+                Observable.shared.tempTarget.value = nil
+                NotificationCenter.default.post(name: NSNotification.Name("refresh"), object: nil)
                 completion(true)
             } catch {
                 print("Error: \(error)")
@@ -71,6 +73,8 @@ class RemoteViewController: UIViewController {
             do {
                 let response: [TreatmentResponse] = try await NightscoutUtils.executePostRequest(eventType: .treatments, body: tempTargetBody)
                 print("Success: \(response)")
+                Observable.shared.tempTarget.value = newTarget
+                NotificationCenter.default.post(name: NSNotification.Name("refresh"), object: nil)
                 completion(true)
             } catch {
                 print("Error: \(error)")

+ 12 - 2
LoopFollow/helpers/NightscoutUtils.swift

@@ -266,7 +266,12 @@ class NightscoutUtils {
             throw NightscoutError.invalidURL
         }
 
-        let (data, response) = try await URLSession.shared.data(for: request)
+        let sessionConfig = URLSessionConfiguration.default
+        sessionConfig.waitsForConnectivity = true
+        sessionConfig.networkServiceType = .responsiveData
+        let session = URLSession(configuration: sessionConfig)
+
+        let (data, response) = try await session.data(for: request)
 
         guard let httpResponse = response as? HTTPURLResponse else {
             throw NightscoutError.networkError
@@ -303,7 +308,12 @@ class NightscoutUtils {
 
         request.httpBody = try JSONSerialization.data(withJSONObject: body, options: [])
 
-        let (data, response) = try await URLSession.shared.data(for: request)
+        let sessionConfig = URLSessionConfiguration.default
+        sessionConfig.waitsForConnectivity = true
+        sessionConfig.networkServiceType = .responsiveData
+        let session = URLSession(configuration: sessionConfig)
+
+        let (data, response) = try await session.data(for: request)
 
         guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
             throw NightscoutError.networkError