Skip to content

Commit

Permalink
Safety methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Snesnopic committed May 27, 2024
1 parent 047e75d commit a94eee5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Morser/Model/VibrationEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 11 additions & 9 deletions Morser/Model/WatchConnectivityProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion MorserWatch Watch App/WatchCommunicationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a94eee5

Please sign in to comment.