From b3c90c108cd3666da2eaee0f0180adc505d27c52 Mon Sep 17 00:00:00 2001 From: Snesnopic Date: Sun, 26 May 2024 20:12:53 +0200 Subject: [PATCH] Now at every chance, the iphone sends saved sentences to watch, which in turns deletes all previous sentences and uses the new ones --- Morser/Model/WatchConnectivityProvider.swift | 19 +++++++++----- Morser/Views/MorserApp.swift | 3 +++ Morser/Views/QuickTranslateView.swift | 5 +++- MorserWatch Watch App/MorserWatchApp.swift | 3 +++ .../WatchCommunicationManager.swift | 26 ++++++++++++++++++- 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/Morser/Model/WatchConnectivityProvider.swift b/Morser/Model/WatchConnectivityProvider.swift index 7759356..55e1c27 100644 --- a/Morser/Model/WatchConnectivityProvider.swift +++ b/Morser/Model/WatchConnectivityProvider.swift @@ -10,9 +10,11 @@ import WatchConnectivity import Combine import CoreHaptics import SwiftUI +import CoreData class WatchConnectivityProvider: NSObject, ObservableObject, WCSessionDelegate { static let shared = WatchConnectivityProvider() + var managedContext: NSManagedObjectContext? private override init() { super.init() @@ -62,11 +64,16 @@ class WatchConnectivityProvider: NSObject, ObservableObject, WCSessionDelegate { } } - private func vibrateDevice() { - // DispatchQueue.main.async { - // let generator = UIImpactFeedbackGenerator(style: .medium) - // generator.impactOccurred() - // } - // } + static func sendSentencesToWatch() { + var message: [String: Any] = ["action": "sentences"] + var fetchRequest = Sentence.fetchRequest() + var sentences = try? WatchConnectivityProvider.shared.managedContext!.fetch(fetchRequest) + var dict: [Int32: String] = [:] + sentences?.forEach({ sentence in + dict[sentence.order] = sentence.sentence! + }) + message["dict"] = dict + WCSession.default.transferUserInfo(message) + print("Mandate al watch sentences: \(message)") } } diff --git a/Morser/Views/MorserApp.swift b/Morser/Views/MorserApp.swift index c50fb10..27f4b65 100644 --- a/Morser/Views/MorserApp.swift +++ b/Morser/Views/MorserApp.swift @@ -16,6 +16,9 @@ struct MorserApp: App { WindowGroup { ParentView() .environment(\.managedObjectContext, dataController.container.viewContext) + .onAppear { + WatchConnectivityProvider.shared.managedContext = dataController.container.viewContext + } } } } diff --git a/Morser/Views/QuickTranslateView.swift b/Morser/Views/QuickTranslateView.swift index c9dbbe8..e6c9065 100644 --- a/Morser/Views/QuickTranslateView.swift +++ b/Morser/Views/QuickTranslateView.swift @@ -51,6 +51,7 @@ struct QuickTranslateView: View { }) do { try moc.save() + WatchConnectivityProvider.sendSentencesToWatch() } catch { print("Error: \(error)") } @@ -72,6 +73,7 @@ struct QuickTranslateView: View { } do { try moc.save() + WatchConnectivityProvider.sendSentencesToWatch() } catch { print("Error: \(error)") } @@ -95,7 +97,7 @@ struct QuickTranslateView: View { .toolbar { ToolbarItem(placement: .topBarLeading) { EditButton() - .disabled(vibrationEngine.isListening || vibrationEngine.isVibrating()) + .disabled(vibrationEngine.isListening || vibrationEngine.isVibrating()) } ToolbarItem(placement: .topBarTrailing) { Button { @@ -112,6 +114,7 @@ struct QuickTranslateView: View { } .onAppear { ensureSentencesExist(sentences, moc) + WatchConnectivityProvider.sendSentencesToWatch() } .environment(\.editMode, $mode) } diff --git a/MorserWatch Watch App/MorserWatchApp.swift b/MorserWatch Watch App/MorserWatchApp.swift index c5d4f2e..70a3611 100644 --- a/MorserWatch Watch App/MorserWatchApp.swift +++ b/MorserWatch Watch App/MorserWatchApp.swift @@ -15,6 +15,9 @@ struct MorserWatch_Watch_AppApp: App { WindowGroup { ParentView() .environment(\.managedObjectContext, dataController.container.viewContext) + .onAppear { + WatchCommunicationManager.shared.managedContext = dataController.container.viewContext + } } } } diff --git a/MorserWatch Watch App/WatchCommunicationManager.swift b/MorserWatch Watch App/WatchCommunicationManager.swift index 370c0a8..6331254 100644 --- a/MorserWatch Watch App/WatchCommunicationManager.swift +++ b/MorserWatch Watch App/WatchCommunicationManager.swift @@ -8,10 +8,11 @@ import Foundation import SwiftUI import WatchConnectivity +import CoreData class WatchCommunicationManager: NSObject, WCSessionDelegate, ObservableObject { static let shared = WatchCommunicationManager() - + var managedContext: NSManagedObjectContext? private override init() { super.init() if WCSession.isSupported() { @@ -36,6 +37,29 @@ class WatchCommunicationManager: NSObject, WCSessionDelegate, ObservableObject { VibrationEngine.shared.updateTimings() print("Ricevute dall'iPhone settings: \(sliderPreference) e \(soundFrequency)") } + case "sentences": + if let dict = userInfo["dict"] as? [Int32: String] { + print(dict) + do { + let fetchRequest = Sentence.fetchRequest() + let items = try? managedContext!.fetch(fetchRequest) + for item in items ?? [] { + managedContext!.delete(item) + } + try managedContext!.save() + + try dict.forEach { order, sentence in + print("Frase attuale: \(sentence)") + let newSentence: Sentence = Sentence(context: managedContext!) + newSentence.order = order + newSentence.sentence = sentence + try managedContext!.save() + } + } catch { + print(error.localizedDescription) + } + } + default: break