WatchSessionManager.swift
8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// WatchSessionManager.swift
// hippo-watch Watch App
//
// Created by shihao on 2025/7/7.
//
import Foundation
import WatchConnectivity
import Combine
import ClockKit
class WatchSessionManager: NSObject, WCSessionDelegate, ObservableObject {
static let share = WatchSessionManager()
override init() {
super.init()
}
func addListen() {
if WCSession.isSupported() {
WCSession.default.delegate = self
if WCSession.default.activationState == .notActivated {
WCSession.default.activate()
} else if WCSession.default.activationState == .activated {
requestLoginStateFromPhone()
}
}
}
private func handleLoginData(_ data: Data) {
do {
let model = try JSONDecoder().decode(UserInfoModel.self, from: data)
Task { @MainActor in
applyLoginInfo(model, data: data)
}
} catch {
print("decode watch login info failed: \(error.localizedDescription)")
}
}
@MainActor
private func applyLoginInfo(_ model: UserInfoModel, data: Data? = nil) {
guard model.token?.isEmpty == false else {
applyLogout()
return
}
let storeData = data ?? (try? JSONEncoder().encode(model))
if let storeData {
AppGroupConstants.defaults?.set(storeData, forKey: AppGroupConstants.Key.myUserInfo)
}
guard model != WatchUserinfoManager.share.myUserInfo else {
return
}
WatchUserinfoManager.share.login(userInfo: model)
WatchDataManager.share.fetchMyTodayData()
WatchDataManager.share.fetchCoupleLatestHRV()
WatchHealthObserverUploader.shared.performFullUpload()
}
@MainActor
private func applyLogout() {
WatchUserinfoManager.share.logout()
WatchDataManager.share.logout()
}
private func requestLoginStateFromPhone() {
guard WCSession.default.isReachable else { return }
WCSession.default.sendMessage(
["command": AppGroupMessageKey.requestLoginState],
replyHandler: { [weak self] reply in
self?.handleLoginStatePayload(reply)
},
errorHandler: { error in
print("❌ Failed to request login state: \(error.localizedDescription)")
}
)
}
private func handleLoginStatePayload(_ payload: [String: Any]) {
if let command = payload["command"] as? String,
command == AppGroupMessageKey.logout {
Task { @MainActor in
self.applyLogout()
}
return
}
if let json = payload["json"] as? String,
let data = json.data(using: .utf8) {
handleLoginData(data)
return
}
do {
let data = try JSONSerialization.data(withJSONObject: payload)
handleLoginData(data)
} catch {
print("serialize watch login state failed: \(error.localizedDescription)")
}
}
private func handleCommand(command: String, json: String?, payload: [String: Any]? = nil){
if command == AppGroupMessageKey.login{
// 登录, 刷新所有数据
guard let data = json?.data(using: .utf8) else{
return
}
handleLoginData(data)
}else if command == AppGroupMessageKey.reloadTheme {
// 刷新表盘等
Task { @MainActor in
if let payload, let theme = WatchLocalTheme(payload: payload) {
WatchUserinfoManager.share.applySyncedTheme(theme)
} else {
WatchUserinfoManager.share.reloadTheme()
}
}
}else if command == AppGroupMessageKey.deleteTheme{
Task { @MainActor in
WatchUserinfoManager.share.deleteLocalTheme()
}
}else if command == AppGroupMessageKey.reloadAll{
// 刷新数据
Task { @MainActor in
WatchUserinfoManager.share.reloadLogin()
}
}else if command == AppGroupMessageKey.reloadVip{
// 刷新会员
Task { @MainActor in
WatchUserinfoManager.share.reloadVip()
}
}else if command == AppGroupMessageKey.logout{
// 退出登录
Task { @MainActor in
self.applyLogout()
}
}else if command == AppGroupMessageKey.statusPulseRefresh{
Task { @MainActor in
WatchDataManager.share.fetchMyTodayData()
}
}
}
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
print("message-replyHandler: \(message)")
if let command = message["command"] as? String {
handleCommand(
command: command,
json: message["json"] as? String,
payload: message["payload"] as? [String: Any]
)
}
replyHandler(["success": true])
}
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
print("received userInfo transform: \(userInfo)")
if let command = userInfo["command"] as? String {
handleCommand(
command: command,
json: userInfo["json"] as? String,
payload: userInfo["payload"] as? [String: Any]
)
}
}
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
print("message: \(message)")
if let command = message["command"] as? String {
handleCommand(
command: command,
json: message["json"] as? String,
payload: message["payload"] as? [String: Any]
)
}
}
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
print("received applicationContext: \(applicationContext)")
if let command = applicationContext["command"] as? String {
if command != AppGroupMessageKey.login || applicationContext["json"] != nil {
handleCommand(
command: command,
json: applicationContext["json"] as? String,
payload: applicationContext["payload"] as? [String: Any]
)
return
}
}
do{
let data = try JSONSerialization.data(withJSONObject: applicationContext)
let model = try JSONDecoder().decode(UserInfoModel.self, from: data)
Task { @MainActor in
self.applyLoginInfo(model, data: data)
}
}catch{
print("handle applicationContext failed: \(error.localizedDescription)")
}
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: (any Error)?) {
print("Activation state: \(activationState.rawValue)")
if let error = error {
print("Activation error: \(error.localizedDescription)")
} else if activationState == .activated {
requestLoginStateFromPhone()
}
}
#if os(iOS)
func sessionDidBecomeInactive(_ session: WCSession) {
}
func sessionDidDeactivate(_ session: WCSession) {
WCSession.default.activate()
}
#endif
}
extension WatchSessionManager {
func notifyPhoneToRefreshPulse() {
if WCSession.default.isReachable {
WCSession.default.sendMessage(["command": AppGroupMessageKey.statusPulseRefresh],
replyHandler: nil,
errorHandler: { error in
print("❌ Failed to send refresh message: \(error)")
})
} else {
print("📵 iPhone not reachable")
}
}
func sendWatchDeviceToken(token: String){
if WCSession.default.isReachable {
WCSession.default.sendMessage(["command": AppGroupMessageKey.watchDeviceToken,
"token": token
],
replyHandler: nil,
errorHandler: { error in
print("❌ Failed to send token message: \(error)")
})
} else {
print("📵 iPhone not reachable")
}
}
}