Skip to content

Commit

Permalink
Now at every chance, the iphone sends saved sentences to watch, which…
Browse files Browse the repository at this point in the history
… in turns deletes all previous sentences and uses the new ones
  • Loading branch information
Snesnopic committed May 26, 2024
1 parent 22331d9 commit b3c90c1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
19 changes: 13 additions & 6 deletions Morser/Model/WatchConnectivityProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)")
}
}
3 changes: 3 additions & 0 deletions Morser/Views/MorserApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ struct MorserApp: App {
WindowGroup {
ParentView()
.environment(\.managedObjectContext, dataController.container.viewContext)
.onAppear {
WatchConnectivityProvider.shared.managedContext = dataController.container.viewContext
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Morser/Views/QuickTranslateView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ struct QuickTranslateView: View {
})
do {
try moc.save()
WatchConnectivityProvider.sendSentencesToWatch()
} catch {
print("Error: \(error)")
}
Expand All @@ -72,6 +73,7 @@ struct QuickTranslateView: View {
}
do {
try moc.save()
WatchConnectivityProvider.sendSentencesToWatch()
} catch {
print("Error: \(error)")
}
Expand All @@ -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 {
Expand All @@ -112,6 +114,7 @@ struct QuickTranslateView: View {
}
.onAppear {
ensureSentencesExist(sentences, moc)
WatchConnectivityProvider.sendSentencesToWatch()
}
.environment(\.editMode, $mode)
}
Expand Down
3 changes: 3 additions & 0 deletions MorserWatch Watch App/MorserWatchApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct MorserWatch_Watch_AppApp: App {
WindowGroup {
ParentView()
.environment(\.managedObjectContext, dataController.container.viewContext)
.onAppear {
WatchCommunicationManager.shared.managedContext = dataController.container.viewContext
}
}
}
}
26 changes: 25 additions & 1 deletion MorserWatch Watch App/WatchCommunicationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down

0 comments on commit b3c90c1

Please sign in to comment.