From a94eee5233675e001b3b135621f4a8f035ae6c4d Mon Sep 17 00:00:00 2001 From: Snesnopic Date: Mon, 27 May 2024 15:58:45 +0200 Subject: [PATCH] Safety methods --- Morser/Model/VibrationEngine.swift | 3 +++ Morser/Model/WatchConnectivityProvider.swift | 20 ++++++++++--------- .../WatchCommunicationManager.swift | 6 +++++- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Morser/Model/VibrationEngine.swift b/Morser/Model/VibrationEngine.swift index 9570408..74f745e 100644 --- a/Morser/Model/VibrationEngine.swift +++ b/Morser/Model/VibrationEngine.swift @@ -144,6 +144,9 @@ class VibrationEngine: ObservableObject { // Function to stop reading Morse code func stopReading() { + #if os(watchOS) + WatchCommunicationManager.shared.sendStopRequest() + #endif morseCodeIndex = 0 morseCodeString = "" vibrationTimer?.invalidate() diff --git a/Morser/Model/WatchConnectivityProvider.swift b/Morser/Model/WatchConnectivityProvider.swift index 87af715..02b8e58 100644 --- a/Morser/Model/WatchConnectivityProvider.swift +++ b/Morser/Model/WatchConnectivityProvider.swift @@ -50,17 +50,19 @@ class WatchConnectivityProvider: NSObject, ObservableObject, WCSessionDelegate { } @MainActor func session(_ session: WCSession, didReceiveUserInfo userInfo: [String: Any] = [:]) { if let action = userInfo["action"] as? String, action == "vibrate", let message = userInfo["message"] as? String { - print("\(message)") - DispatchQueue.main.async { - if VibrationEngine.shared.engine == nil { - do { - VibrationEngine.shared.engine = try CHHapticEngine() - } catch { - print(error) + let maxDate = userInfo["maxDate"] as? Date + if maxDate! >= .now { + DispatchQueue.main.async { + if VibrationEngine.shared.engine == nil { + do { + VibrationEngine.shared.engine = try CHHapticEngine() + } catch { + print(error) + } } + VibrationEngine.shared.readMorseCode(morseCode: message.morseCode()) } - VibrationEngine.shared.readMorseCode(morseCode: message.morseCode()) - } + } } } diff --git a/MorserWatch Watch App/WatchCommunicationManager.swift b/MorserWatch Watch App/WatchCommunicationManager.swift index 6331254..c5dd6c3 100644 --- a/MorserWatch Watch App/WatchCommunicationManager.swift +++ b/MorserWatch Watch App/WatchCommunicationManager.swift @@ -22,7 +22,11 @@ class WatchCommunicationManager: NSObject, WCSessionDelegate, ObservableObject { } func sendVibrationRequest(_ message: String) { - WCSession.default.transferUserInfo(["action": "vibrate", "message": message]) + WCSession.default.transferUserInfo(["action": "vibrate", "message": message, "maxDate": Date.now.addingTimeInterval(3)]) + } + + func sendStopRequest() { + WCSession.default.transferUserInfo(["action": "stop"]) } func session(_ session: WCSession, didReceiveUserInfo userInfo: [String: Any] = [:]) { print(userInfo)