Jonas Björkert 2 лет назад
Родитель
Сommit
372755960e

+ 12 - 6
LoopFollow/Remote/RemoteView.swift

@@ -142,13 +142,16 @@ struct RemoteView: View {
 
     private func enactTempTarget() {
         isLoading = true
+        print("Enacting Temp Target with target: \(newHKTarget) and duration: \(duration)")
         sendTempTarget(newHKTarget, duration) { success in
             DispatchQueue.main.async {
-                isLoading = false
+                self.isLoading = false
                 if success {
-                    statusMessage.value = "Target successfully enacted."
+                    print("Target successfully enacted.")
+                    self.statusMessage.value = "Target successfully enacted."
                 } else {
-                    statusMessage.value = "Failed to enact target."
+                    print("Failed to enact target.")
+                    self.statusMessage.value = "Failed to enact target."
                 }
             }
         }
@@ -156,13 +159,16 @@ struct RemoteView: View {
 
     private func cancelTempTarget() {
         isLoading = true
+        print("Cancelling Temp Target...")
         onCancelExistingTarget() { success in
             DispatchQueue.main.async {
-                isLoading = false
+                self.isLoading = false
                 if success {
-                    statusMessage.value = "Temp target successfully cancelled."
+                    print("Temp target successfully cancelled.")
+                    self.statusMessage.value = "Temp target successfully cancelled."
                 } else {
-                    statusMessage.value = "Failed to cancel temp target."
+                    print("Failed to cancel temp target.")
+                    self.statusMessage.value = "Failed to cancel temp target."
                 }
             }
         }

+ 4 - 0
LoopFollow/Remote/RemoteViewController.swift

@@ -61,8 +61,10 @@ class RemoteViewController: UIViewController {
         ]
 
         DispatchQueue.global(qos: .userInitiated).async {
+            print("Executing cancelExistingTarget on thread: \(Thread.current)")
             NightscoutUtils.executePostRequest(eventType: .treatments, body: tempTargetBody) { (result: Result<[TreatmentCancelResponse], Error>) in
                 DispatchQueue.main.async {
+                    print("Handling cancelExistingTarget result on thread: \(Thread.current)")
                     switch result {
                     case .success(let response):
                         print("Success: \(response)")
@@ -90,8 +92,10 @@ class RemoteViewController: UIViewController {
         ]
 
         DispatchQueue.global(qos: .userInitiated).async {
+            print("Executing sendTempTarget on thread: \(Thread.current)")
             NightscoutUtils.executePostRequest(eventType: .treatments, body: tempTargetBody) { (result: Result<[TreatmentResponse], Error>) in
                 DispatchQueue.main.async {
+                    print("Handling sendTempTarget result on thread: \(Thread.current)")
                     switch result {
                     case .success(let response):
                         print("Success: \(response)")

+ 15 - 9
LoopFollow/Storage/ObservableUserDefaultsValue.swift

@@ -9,8 +9,6 @@
 import Foundation
 import Combine
 
-/// An observable value holder that keeps its internal data (the value) synchronized with its UserDefaults value.
-/// The value is read from UserDefaults on initialization (if exists, otherwise a default value is used) and written when the value changes.
 class ObservableUserDefaultsValue<T: AnyConvertible & Equatable>: ObservableObject, UserDefaultsAnyValue {
     // user defaults key (UserDefaultsAnyValue protocol implementation)
     let key: String
@@ -24,7 +22,9 @@ class ObservableUserDefaultsValue<T: AnyConvertible & Equatable>: ObservableObje
 
             if let validation = validation {
                 guard let validatedValue = validation(value) else {
-                    value = oldValue
+                    DispatchQueue.main.async {
+                        self.value = oldValue
+                    }
                     return
                 }
                 value = validatedValue
@@ -34,13 +34,17 @@ class ObservableUserDefaultsValue<T: AnyConvertible & Equatable>: ObservableObje
             ObservableUserDefaultsValue.defaults.setValue(value.toAny(), forKey: key)
 
             // Execute custom closure
-            onChange?(value)
+            DispatchQueue.main.async {
+                self.onChange?(self.value)
 
-            // Notify observers
-            observers.values.forEach { $0(value) }
+                // Notify observers
+                self.observers.values.forEach { $0(self.value) }
 
-            // Notify UserDefaultsValueGroups that value has changed
-            UserDefaultsValueGroups.valueChanged(self)
+                // Notify UserDefaultsValueGroups that value has changed
+                UserDefaultsValueGroups.valueChanged(self)
+
+                print("Value for \(self.key) changed to \(self.value)")  // Logging
+            }
         }
     }
 
@@ -53,7 +57,9 @@ class ObservableUserDefaultsValue<T: AnyConvertible & Equatable>: ObservableObje
             guard let newValue = T.fromAny(newValue) as T? else {
                 return
             }
-            self.value = newValue
+            DispatchQueue.main.async {
+                self.value = newValue
+            }
         }
     }
 

+ 1 - 0
LoopFollow/Storage/ObservableValue.swift

@@ -20,6 +20,7 @@ class ObservableValue<T>: ObservableObject {
 
     func set(_ newValue: T) {
         DispatchQueue.main.async {
+            print("Setting new value: \(newValue)")  // Logging
             self.value = newValue
         }
     }

+ 1 - 8
LoopFollow/helpers/NightscoutUtils.swift

@@ -280,14 +280,7 @@ class NightscoutUtils {
                     }
                     return
                 }
-                // Print the JSON string for debugging
-                /*
-                if let jsonString = String(data: data, encoding: .utf8) {
-                    print("JSON Response: \(jsonString)")
-                } else {
-                    print("Failed to convert data to JSON string")
-                }
-                */
+                print("Network response received on thread: \(Thread.current)")
                 let decoder = JSONDecoder()
                 do {
                     let decodedObject = try decoder.decode(T.self, from: data)