TakePulseView.swift
6.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
//
// TakePulseView.swift
// hippo-watch Watch App
//
// Created by shihao on 2025/10/21.
//
import Foundation
import SwiftUI
struct TakePulseView: View {
@StateObject private var pulseMonitor = PulseMonitor()
@State private var showPulseDetailView = false
@State private var showPulseFailedView = false
@State private var displayType: TakePulseViewDisplayType = .waitForTakePulse
enum TakePulseViewDisplayType {
case waitForTakePulse
case takingPulse
case pulseFailed
}
var body: some View {
ZStack(alignment: .top) {
Image(.takePulseBg)
.resizable()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.aspectRatio(contentMode: .fill)
.ignoresSafeArea()
switch displayType {
case .waitForTakePulse:
waitForTakePulseView
case .takingPulse:
takingPulseView
case .pulseFailed:
pulseFailedView
}
HStack {
Image(.takePulseHelp)
.padding(.top, 22)
.padding(.leading, 8)
.onTapGesture {
showPulseFailedView = true
}
Spacer()
}
}
.fullScreenCover(isPresented: $showPulseDetailView, content: {
if let pulseType = pulseMonitor.pulseType {
PulseDetailView(pulseType: pulseType)
}
})
.fullScreenCover(isPresented: $showPulseFailedView, content: {
PulseFailedReasonView(isPresent: $showPulseFailedView)
})
.onAppear {
ThinkSDKUtil.track(eventName: "enter_doublefeel_watch_page",
properties: ["page_type": "诊脉"])
}
}
@ViewBuilder
private var waitForTakePulseView: some View {
VStack(spacing: 0) {
Image(.takePulseIcon)
.padding(.top, 35.x)
Spacer()
Text("请保持静坐,并将手腕放平")
.font(.custom("Songti SC Regular", size: 11))
.foregroundColor(.init(hex: "#896CDC"))
.padding(.top, 10.x)
Button {
withAnimation {
displayType = .takingPulse
pulseMonitor.startDiagnosis { pulseType in
if let pulseType {
showPulseDetailView = true
reset()
Task {
let _ = try? await HealthService().uploadPulse(pulseType: pulseType)
WatchSessionManager.share.notifyPhoneToRefreshPulse()
}
}else {
displayType = .pulseFailed
}
}
}
ThinkSDKUtil.track(eventName: "click_doublefeel_watch_page",
properties: ["page_type": "诊断",
"click_content": "开始诊脉"])
} label: {
Text("立即诊脉")
.font(.system(size: 15, weight: .semibold))
.foregroundColor(.white)
.frame(height: 36, alignment: .center)
.frame(maxWidth: .infinity)
.background(Color(hex: "#896CDC"))
.cornerRadius(18)
}
.padding(.top, 4.x)
.padding(.horizontal, 8)
.padding(.bottom, 10.x)
}
}
@ViewBuilder
private var takingPulseView: some View {
VStack(spacing: 0) {
ZStack(alignment: .center) {
RingProgressView(progress: pulseMonitor.progress, color: .init(hex: "#896CDC"), size: 110)
Image(.takePulseIcon)
}
.padding(.top, 38.x)
Spacer()
Text("正在分析数据\n诊脉中…")
.font(.custom("Songti SC Regular", size: 11))
.foregroundColor(.init(hex: "#896CDC"))
.padding(.top, 19.x)
.padding(.bottom, 24.x)
.multilineTextAlignment(.center)
}
.background (
AnimatedGIFView(gifName: "takePulseBg")
.frame(width: ScreenSize.width, height: ScreenSize.height)
)
}
@ViewBuilder
private var pulseFailedView: some View {
VStack(alignment: .center, spacing: 0) {
Spacer()
Text("诊断失败\n未读取到数据")
.font(.custom("Songti SC Regular", size: 11))
.foregroundColor(.init(hex: "#896CDC"))
.multilineTextAlignment(.center)
Spacer()
Button {
withAnimation {
showPulseFailedView = true
reset()
}
ThinkSDKUtil.track(eventName: "click_doublefeel_watch_page",
properties: ["page_type": "诊断",
"click_content": "为何会诊脉失败"])
} label: {
Text("为何会诊脉失败")
.font(.system(size: 15, weight: .semibold))
.foregroundColor(.white)
.frame(height: 36, alignment: .center)
.frame(maxWidth: .infinity)
.background(Color(hex: "#896CDC"))
.cornerRadius(18)
}
.padding(.horizontal, 8)
.padding(.bottom, 18.x)
}
}
private func reset() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
displayType = .waitForTakePulse
pulseMonitor.progress = 0
}
}
}
fileprivate struct RingProgressView: View {
var progress: Double // 0.0 ~ 1.0
var lineWidth: CGFloat = 4
var color: Color = .blue
var size: CGFloat = 100
var body: some View {
ZStack {
// 背景环
Circle()
.stroke(Color.gray.opacity(0.2), lineWidth: lineWidth)
// 前景进度环
Circle()
.trim(from: 0, to: CGFloat(progress))
.stroke(color, style: StrokeStyle(lineWidth: lineWidth, lineCap: .round))
.rotationEffect(.degrees(-90)) // 从顶部开始
.animation(.easeInOut(duration: 0.3), value: progress)
}
.frame(width: size, height: size)
}
}