Procházet zdrojové kódy

requestState onAppear

Ivan Valkou před 4 roky
rodič
revize
b2818061d9

+ 3 - 0
FreeAPSWatch WatchKit Extension/Views/MainView.swift

@@ -26,6 +26,9 @@ struct MainView: View {
         }
         .frame(maxHeight: .infinity)
         .padding()
+        .onAppear {
+            state.requestState()
+        }
     }
 
     var header: some View {

+ 7 - 3
FreeAPSWatch WatchKit Extension/Views/WatchStateModel.swift

@@ -60,6 +60,12 @@ class WatchStateModel: NSObject, ObservableObject {
         }
     }
 
+    func requestState() {
+        session.sendMessage(["stateRequest": true], replyHandler: nil) { error in
+            print("WatchStateModel error: " + error.localizedDescription)
+        }
+    }
+
     private func completionHandler(_ reply: [String: Any]) {
         if let ok = reply["confirmation"] as? Bool {
             DispatchQueue.main.async {
@@ -104,9 +110,7 @@ class WatchStateModel: NSObject, ObservableObject {
 extension WatchStateModel: WCSessionDelegate {
     func session(_: WCSession, activationDidCompleteWith state: WCSessionActivationState, error _: Error?) {
         print("WCSession activated: \(state == .activated)")
-        session.sendMessage(["stateRequest": true], replyHandler: nil) { error in
-            print("WatchStateModel error: " + error.localizedDescription)
-        }
+        requestState()
     }
 
     func session(_: WCSession, didReceiveMessage _: [String: Any]) {}