|
...
|
...
|
@@ -20,7 +20,9 @@ final class WatchConnectivityService: NSObject { |
|
|
|
guard WCSession.isSupported() else { return false }
|
|
|
|
let session = WCSession.default
|
|
|
|
session.delegate = self
|
|
|
|
session.activate()
|
|
|
|
if session.activationState == .notActivated {
|
|
|
|
session.activate()
|
|
|
|
}
|
|
|
|
refreshState()
|
|
|
|
return true
|
|
|
|
}
|
|
...
|
...
|
@@ -55,72 +57,62 @@ final class WatchConnectivityService: NSObject { |
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendTextMessage(_ message: String) -> Bool {
|
|
|
|
activate()
|
|
|
|
guard WCSession.default.isReachable else { return false }
|
|
|
|
WCSession.default.sendMessage(["message": message], replyHandler: nil) { error in
|
|
|
|
print("Watch message failed: \(error.localizedDescription)")
|
|
|
|
func transferLoginInfo(userInfo: UserInfoModel?){
|
|
|
|
guard activate() else { return }
|
|
|
|
let params = loginPayload(userInfo: userInfo)
|
|
|
|
do{
|
|
|
|
try WCSession.default.updateApplicationContext(params)
|
|
|
|
print("Send Watch updateApplicationContext: \(params)")
|
|
|
|
}catch{
|
|
|
|
print("Error Send Watch updateApplicationContext: \(params)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendCommandMessage(_ message: String) -> Bool {
|
|
|
|
activate()
|
|
|
|
guard WCSession.default.isReachable else { return false }
|
|
|
|
WCSession.default.sendMessage(["command": message], replyHandler: nil) { error in
|
|
|
|
print("Watch message send failed: \(error.localizedDescription)")
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
func sendLoginInfoMessage(userInfo: UserInfoModel?) -> Bool {
|
|
|
|
guard let userInfo else {
|
|
|
|
return sendCommandMessage(AppGroupMessageKey.logout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func syncPayload(jsonPayload: String) -> Bool {
|
|
|
|
activate()
|
|
|
|
guard let data = jsonPayload.data(using: .utf8),
|
|
|
|
let object = try? JSONSerialization.jsonObject(with: data),
|
|
|
|
let payload = object as? [String: Any] else {
|
|
|
|
return false
|
|
|
|
guard let json = userInfo.jsonString else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return sendCommandMessage(AppGroupMessageKey.login, json: json)
|
|
|
|
}
|
|
|
|
|
|
|
|
persistThemeIfPresent(payload: payload, originalJSON: jsonPayload)
|
|
|
|
|
|
|
|
guard WCSession.isSupported(), WCSession.default.isPaired, WCSession.default.isWatchAppInstalled else {
|
|
|
|
return false
|
|
|
|
private func loginPayload(userInfo: UserInfoModel?) -> [String: Any] {
|
|
|
|
guard var params = userInfo?.toJson(), !params.isEmpty else {
|
|
|
|
return ["command": AppGroupMessageKey.logout]
|
|
|
|
}
|
|
|
|
params["command"] = AppGroupMessageKey.login
|
|
|
|
return params
|
|
|
|
}
|
|
|
|
|
|
|
|
WCSession.default.transferUserInfo(payload)
|
|
|
|
try? WCSession.default.updateApplicationContext(payload)
|
|
|
|
sendWatchThemeChangedMessageIfNeeded(payload: payload)
|
|
|
|
func sendTextMessage(_ message: String) -> Bool {
|
|
|
|
activate()
|
|
|
|
guard WCSession.default.isReachable else { return false }
|
|
|
|
WCSession.default.sendMessage(["message": message], replyHandler: nil) { error in
|
|
|
|
print("Watch message failed: \(error.localizedDescription)")
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendWatchThemeChangedMessage() -> Bool {
|
|
|
|
sendCommandMessage(AppGroupMessageKey.reloadTheme)
|
|
|
|
}
|
|
|
|
func sendCommandMessage(_ message: String, json: String? = nil) -> Bool {
|
|
|
|
activate()
|
|
|
|
guard WCSession.default.isReachable else { return false }
|
|
|
|
var params: [String: Any] = [
|
|
|
|
"command": message
|
|
|
|
]
|
|
|
|
params["json"] = json
|
|
|
|
|
|
|
|
private func sendWatchThemeChangedMessageIfNeeded(payload: [String: Any]) {
|
|
|
|
if payload["myWatchTheme"] != nil || payload["watchTheme"] != nil || payload["theme"] != nil {
|
|
|
|
sendWatchThemeChangedMessage()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func persistThemeIfPresent(payload: [String: Any], originalJSON: String) {
|
|
|
|
if let theme = payload["myWatchTheme"] as? String {
|
|
|
|
WatchThemeStore.shared.saveThemeJSONString(theme)
|
|
|
|
return
|
|
|
|
WCSession.default.sendMessage(params, replyHandler: nil) { error in
|
|
|
|
print("Watch message send failed: \(error.localizedDescription)")
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
let themeObject = payload["watchTheme"] ?? payload["theme"]
|
|
|
|
guard let themeObject,
|
|
|
|
JSONSerialization.isValidJSONObject(themeObject),
|
|
|
|
let data = try? JSONSerialization.data(withJSONObject: themeObject),
|
|
|
|
let json = String(data: data, encoding: .utf8) else {
|
|
|
|
if payload["isWatchTheme"] as? Bool == true {
|
|
|
|
WatchThemeStore.shared.saveThemeJSONString(originalJSON)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
func sendWatchThemeChangedMessage(json: String) -> Bool{
|
|
|
|
sendCommandMessage(AppGroupMessageKey.reloadTheme, json: json)
|
|
|
|
}
|
|
|
|
WatchThemeStore.shared.saveThemeJSONString(json)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension WatchConnectivityService: WCSessionDelegate {
|
|
...
|
...
|
@@ -132,6 +124,8 @@ extension WatchConnectivityService: WCSessionDelegate { |
|
|
|
refreshState()
|
|
|
|
if let error {
|
|
|
|
print("WatchConnectivity activation failed: \(error.localizedDescription)")
|
|
|
|
}else{
|
|
|
|
transferLoginInfo(userInfo: AppShared.shared.myUserInfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
...
|
...
|
@@ -146,12 +140,17 @@ extension WatchConnectivityService: WCSessionDelegate { |
|
|
|
|
|
|
|
func sessionReachabilityDidChange(_ session: WCSession) {
|
|
|
|
refreshState()
|
|
|
|
if session.isReachable {
|
|
|
|
_ = sendLoginInfoMessage(userInfo: AppShared.shared.myUserInfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func session(_ session: WCSession, didReceiveMessage message: [String: Any]) {
|
|
|
|
if message["command"] as? String == "statusPulseRefresh" {
|
|
|
|
if message["command"] as? String == AppGroupMessageKey.watchDeviceToken,
|
|
|
|
let token = message["token"] as? String {
|
|
|
|
AppGroupConstants.defaults?.set(token, forKey: AppGroupConstants.Key.watchDeviceToken)
|
|
|
|
Task {
|
|
|
|
await HealthKitService.shared.refreshSharedWatchValues()
|
|
|
|
await AppShared.shared.reportDeviceInfo()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
...
|
...
|
@@ -161,11 +160,22 @@ extension WatchConnectivityService: WCSessionDelegate { |
|
|
|
didReceiveMessage message: [String: Any],
|
|
|
|
replyHandler: @escaping ([String: Any]) -> Void
|
|
|
|
) {
|
|
|
|
if message["command"] as? String == "statusPulseRefresh" {
|
|
|
|
if message["command"] as? String == AppGroupMessageKey.requestLoginState {
|
|
|
|
let userInfo = AppShared.shared.myUserInfo
|
|
|
|
var reply = loginPayload(userInfo: userInfo)
|
|
|
|
if let userInfo, let json = userInfo.jsonString {
|
|
|
|
reply["json"] = json
|
|
|
|
}
|
|
|
|
replyHandler(reply)
|
|
|
|
} else if message["command"] as? String == AppGroupMessageKey.watchDeviceToken,
|
|
|
|
let token = message["token"] as? String {
|
|
|
|
AppGroupConstants.defaults?.set(token, forKey: AppGroupConstants.Key.watchDeviceToken)
|
|
|
|
Task {
|
|
|
|
await HealthKitService.shared.refreshSharedWatchValues()
|
|
|
|
replyHandler(["success": true])
|
|
|
|
await AppShared.shared.reportDeviceInfo()
|
|
|
|
}
|
|
|
|
replyHandler(["success": true])
|
|
|
|
} else if message["command"] as? String == AppGroupMessageKey.statusPulseRefresh {
|
|
|
|
replyHandler(["success": true])
|
|
|
|
} else {
|
|
|
|
replyHandler(["success": true])
|
|
|
|
}
|
...
|
...
|
|