WatchUserinfoManager.swift
9.73 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
// WatchUserinfoManager.swift
// hippo-watch Watch App
//
// Created by shihao on 2025/7/8.
//
import Foundation
import WidgetKit
import Combine
@MainActor
final class WatchUserinfoManager: ObservableObject {
static let share = WatchUserinfoManager()
private let service = UserInfoService()
private let themeService = ThemeService()
var isLogin: Bool{
return token?.count ?? 0 > 0
}
var isMeVip: Bool? {
vipInfo?.isAvaiableVip
}
var baseUrl: String{
myUserInfo?.baseUrl ?? "https://api.doublefeel.cn"
}
var token: String?{
myUserInfo?.token
}
private(set) var myUserInfo: UserInfoModel?
@Published var displayFriend: HAppFriend?
@Published var vipInfo: HVipInfo?
@Published var showRealtimeHRV: Bool
@Published var interactionActionType: InteractionActionType = .stick
var myUserinfo: UserInfo? {
nil
}
// 表盘主题
@Published var myTheme: WatchLocalTheme?
@Published var otherTheme: WatchLocalTheme?
private var didSetup = false
var displayType: HealthType{
showRealtimeHRV ? .realtime_stress : .hrv
}
private init(){
showRealtimeHRV = AppGroupConstants.defaults?.bool(
forKey: AppGroupConstants.Key.showRealtimeHRV
) ?? false
guard let data = AppGroupConstants.defaults?.data(forKey: AppGroupConstants.Key.myUserInfo) else{
return
}
do{
self.myUserInfo = try JSONDecoder().decode(UserInfoModel.self, from: data)
}catch{}
}
func setup() {
guard !didSetup else { return }
didSetup = true
Task { @MainActor [weak self] in
await Task.yield()
self?.reloadLocalFriend()
self?.loadLocalWatchTheme()
self?.reloadLogin()
}
}
func login(userInfo: UserInfoModel){
guard let _ = userInfo.token, let _ = userInfo.userId, let _ = userInfo.baseUrl else{
return
}
self.myUserInfo = userInfo
reloadLogin()
}
@MainActor
func reloadLogin(){
// 刷新所有数据
if isLogin{
reloadVip()
reloadFriend()
}
if let id = self.myUserInfo?.userId {
ThinkSDKUtil.login(id)
}
}
@MainActor
func logout(){
myUserInfo = nil
displayFriend = nil
vipInfo = nil
showRealtimeHRV = false
myTheme = nil
otherTheme = nil
AppGroupConstants.clearUserSessionData()
ThinkSDKUtil.logout()
}
@MainActor
func switchHRVType(to realtime: Bool){
showRealtimeHRV = realtime
AppGroupConstants.defaults?.set(showRealtimeHRV, forKey: AppGroupConstants.Key.showRealtimeHRV)
// 刷新数据
WatchDataManager.share.getLatestHealthData()
WatchDataManager.share.getMyLatestData()
if let _ = displayFriend?.friend_user_id{
WatchDataManager.share.getFriendLatestData()
}
}
func reloadTheme(){
loadLocalWatchTheme()
Task {
await loadRemoteWatchTheme()
}
WidgetCenter.shared.reloadTimelines(ofKind: HippoWidgetKind.hrv.rawValue)
}
func applySyncedTheme(_ theme: WatchLocalTheme) {
saveTheme(theme, key: AppGroupConstants.Key.myWatchTheme)
myTheme = theme
objectWillChange.send()
WidgetCenter.shared.reloadTimelines(ofKind: HippoWidgetKind.hrv.rawValue)
}
func deleteLocalTheme(){
deleteTheme(key: AppGroupConstants.Key.myWatchTheme)
myTheme = nil
objectWillChange.send()
WidgetCenter.shared.reloadTimelines(ofKind: HippoWidgetKind.hrv.rawValue)
}
//MARK: - 表盘主题
@MainActor
private func loadLocalWatchTheme(){
myTheme = loadTheme(key: AppGroupConstants.Key.myWatchTheme)
otherTheme = loadTheme(key: AppGroupConstants.Key.otherWatchTheme)
WidgetCenter.shared.reloadTimelines(ofKind: HippoWidgetKind.hrv.rawValue)
}
private func loadRemoteWatchTheme() async{
let myRemoteTheme = try? await themeService.getCurrentTheme(userId: nil)
var otherRemoteTheme: WatchThemeModel?
if let userId = displayFriend?.user_id{
otherRemoteTheme = try? await themeService.getCurrentTheme(userId: userId)
}
if (myTheme?.hasAllImages != true), let myRemoteTheme {
let theme = await WatchThemeLocalBuilder.makeLocalTheme(from: myRemoteTheme)
myTheme = theme
saveTheme(theme, key: AppGroupConstants.Key.myWatchTheme)
} else if myTheme == nil,
let legacyTheme = legacyRemoteTheme(key: AppGroupConstants.Key.myWatchTheme) {
let theme = await WatchThemeLocalBuilder.makeLocalTheme(from: legacyTheme)
myTheme = theme
saveTheme(theme, key: AppGroupConstants.Key.myWatchTheme)
}
if let otherRemoteTheme {
let theme = await WatchThemeLocalBuilder.makeLocalTheme(from: otherRemoteTheme)
if theme != otherTheme {
otherTheme = theme
saveTheme(theme, key: AppGroupConstants.Key.otherWatchTheme)
}
} else if displayFriend?.user_id != nil,
otherTheme == nil,
let legacyTheme = legacyRemoteTheme(key: AppGroupConstants.Key.otherWatchTheme) {
let theme = await WatchThemeLocalBuilder.makeLocalTheme(from: legacyTheme)
otherTheme = theme
saveTheme(theme, key: AppGroupConstants.Key.otherWatchTheme)
} else {
otherTheme = nil
AppGroupConstants.defaults?.removeObject(forKey: AppGroupConstants.Key.otherWatchTheme)
}
WidgetCenter.shared.reloadTimelines(ofKind: HippoWidgetKind.hrv.rawValue)
}
private func loadTheme(key: String) -> WatchLocalTheme? {
guard let data = AppGroupConstants.defaults?.data(forKey: key) else { return nil }
if let theme = try? JSONDecoder().decode(WatchLocalTheme.self, from: data) {
return theme
}
return nil
}
private func legacyRemoteTheme(key: String) -> WatchThemeModel? {
if let data = AppGroupConstants.defaults?.data(forKey: key),
let theme = try? JSONDecoder().decode(WatchThemeModel.self, from: data) {
return theme
}
if let jsonString = AppGroupConstants.defaults?.string(forKey: key),
let data = jsonString.data(using: .utf8),
let theme = try? JSONDecoder().decode(WatchThemeModel.self, from: data) {
return theme
}
return nil
}
private func saveTheme(_ theme: WatchLocalTheme, key: String) {
guard let data = try? JSONEncoder().encode(theme) else { return }
AppGroupConstants.defaults?.set(data, forKey: key)
}
private func deleteTheme(key: String){
AppGroupConstants.defaults?.removeObject(forKey: key)
AppGroupConstants.defaults?.synchronize()
}
//MARK: - 我的信息
// private func reloadUserInfo(){
// guard isLogin else{
// return
// }
// Task{
// myUserInfo = await service.getMyUserInfo()
// guard let myUserInfo else{
// AppGroupConstants.defaults?.removeObject(forKey: AppGroupConstants.Key.myUserInfo)
// return
// }
// do{
// let data = try JSONEncoder().encode(myUserInfo)
// AppGroupConstants.defaults?.set(data, forKey: AppGroupConstants.Key.myUserInfo)
// }catch{}
// }
// }
private func reloadFriend(){
guard isLogin else{
return
}
Task{
let friendList = await service.getFriendInfo()
let friend = friendList?.list?.first(where: { $0.show_in_dial == 1 })
if let _ = friend?.friend_user_id{
WatchDataManager.share.getFriendLatestData()
}
self.saveOtherData(displayFriend: friend)
await loadRemoteWatchTheme()
}
}
func reloadVip(){
guard isLogin else{
return
}
Task{
let vipInfo = await service.getVipInfo()
self.saveVip(info: vipInfo)
}
}
//MARK: - OtherData
private func reloadLocalFriend(){
if let data = AppGroupConstants.defaults?.data(forKey: AppGroupConstants.Key.showOtherUserInfo),
let model = try? JSONDecoder().decode(HAppFriend.self, from: data) {
self.displayFriend = model
}else{
self.displayFriend = nil
}
}
private func saveOtherData(displayFriend: HAppFriend?){
self.displayFriend = displayFriend
guard let displayFriend else{
AppGroupConstants.defaults?.removeObject(forKey: AppGroupConstants.Key.showOtherUserInfo)
return
}
do{
let data = try JSONEncoder().encode(displayFriend)
AppGroupConstants.defaults?.set(data, forKey: AppGroupConstants.Key.showOtherUserInfo)
}catch{}
}
//MARK: - VIP
private func reloadVipInfo(){
if let data = AppGroupConstants.defaults?.data(forKey: AppGroupConstants.Key.vipInfo),
let model = try? JSONDecoder().decode(HVipInfo.self, from: data) {
self.vipInfo = model
}else{
self.vipInfo = nil
}
reloadVip()
}
private func saveVip(info: HVipInfo?){
self.vipInfo = info
if let info{
do{
let data = try JSONEncoder().encode(info)
AppGroupConstants.defaults?.set(data, forKey: AppGroupConstants.Key.vipInfo)
}catch{}
}else{
AppGroupConstants.defaults?.removeObject(forKey: AppGroupConstants.Key.vipInfo)
}
}
}