StatusComparisonViewModel.swift
3.63 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
//
// StatusComparisonViewModel.swift
// hippo-watch Watch App
//
// Created by shihao on 2025/7/8.
//
import Foundation
import SwiftUI
import ClockKit
//import SDWebImage
import Combine
class StatusComparisonViewModel: ObservableObject {
@Published var currentInteractionAction: InteractionActionType?
@Published var myWatchTheme: WatchLocalTheme?
@Published var otherWatchTheme: WatchLocalTheme?
init() {
refreshTheme()
}
func onInteractionClick() {
Task {
do {
setupCurrerntInteractionAnimation()
let _ = try await InteractionService().sendInteraction(actionType: WatchUserinfoManager.share.interactionActionType)
} catch {
print(error.getToastErrorDescription())
}
}
}
private func setupCurrerntInteractionAnimation() {
currentInteractionAction = WatchUserinfoManager.share.interactionActionType
Task {
try? await Task.sleep(nanoseconds: 4 * 1_000_000_000)
await MainActor.run {
self.currentInteractionAction = nil
}
}
}
func refreshTheme() {
getMyWatchTheme()
getOtherWatchTheme()
}
private func getMyWatchTheme() {
let defaults = AppGroupConstants.defaults
if let data = defaults?.data(forKey: AppGroupConstants.Key.myWatchTheme),
let cachedTheme = try? JSONDecoder().decode(WatchLocalTheme.self, from: data) {
myWatchTheme = cachedTheme
}
Task {
if let result = try? await ThemeService().getCurrentTheme(isOther: false) {
let localTheme = await WatchThemeLocalBuilder.makeLocalTheme(from: result)
await MainActor.run {
self.myWatchTheme = localTheme
}
await MainActor.run {
reloadAllComplications()
}
do {
let data = try JSONEncoder().encode(localTheme)
defaults?.set(data, forKey: AppGroupConstants.Key.myWatchTheme)
}catch {
print(error.getToastErrorDescription())
}
}
}
}
private func getOtherWatchTheme() {
guard WatchUserinfoManager.share.myUserinfo?.isPaired == true else { return }
let defaults = AppGroupConstants.defaults
if let data = defaults?.data(forKey: AppGroupConstants.Key.otherWatchTheme),
let cachedTheme = try? JSONDecoder().decode(WatchLocalTheme.self, from: data) {
otherWatchTheme = cachedTheme
}
Task {
if let result = try? await ThemeService().getCurrentTheme(isOther: true) {
let localTheme = await WatchThemeLocalBuilder.makeLocalTheme(from: result)
await MainActor.run {
self.otherWatchTheme = localTheme
}
do {
let data = try JSONEncoder().encode(localTheme)
defaults?.set(data, forKey: AppGroupConstants.Key.otherWatchTheme)
}catch {
print(error.getToastErrorDescription())
}
}
}
}
private func reloadAllComplications() {
#if os(watchOS)
let server = CLKComplicationServer.sharedInstance()
if let complications = server.activeComplications {
for complication in complications {
server.reloadTimeline(for: complication)
}
}
#endif
}
}