DFWatchFaceEntryView.swift
3.35 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
//
// DFWatchFaceEntryView.swift
// hippo-watch-extension
//
import WidgetKit
import SwiftUI
struct DFWatchFaceEntryView: View {
@Environment(\.widgetFamily) private var family
let entry: DFWatchFaceEntry
var body: some View {
Group {
switch family {
case .accessoryInline:
inlineView
case .accessoryCircular:
circularView
default:
rectangularView
}
}
.containerBackground(DFWatchFaceColor.background, for: .widget)
}
@ViewBuilder
private var rectangularView: some View {
switch entry.face {
case .defaultHome:
DFDefaultHomeFaceView(entry: entry)
case .singleStress:
DFSingleStressFaceView(entry: entry)
case .coupleStress:
DFCoupleStressFaceView(entry: entry)
}
}
private var circularView: some View {
let status = statusForCompactView
return ZStack {
DFWatchFaceColor.background
Circle()
.trim(from: 0.08, to: 0.86)
.stroke(status.color.opacity(0.28), style: StrokeStyle(lineWidth: 5, lineCap: .round))
.rotationEffect(.degrees(110))
Circle()
.trim(from: 0.08, to: compactProgress)
.stroke(status.color, style: StrokeStyle(lineWidth: 5, lineCap: .round))
.rotationEffect(.degrees(110))
VStack(spacing: 0) {
Image(systemName: compactIcon)
.font(.system(size: 13, weight: .semibold))
.foregroundStyle(status.color)
Text(compactValue)
.font(.system(size: 12, weight: .bold, design: .rounded))
.foregroundStyle(.white)
.minimumScaleFactor(0.7)
}
}
}
private var inlineView: some View {
Text("\(inlineTitle) \(compactValue)")
}
private var statusForCompactView: DFStressStatus {
switch entry.face {
case .defaultHome, .singleStress:
return entry.data.myStatus
case .coupleStress:
return entry.data.myStatus
}
}
private var compactIcon: String {
switch entry.face {
case .defaultHome:
return "waveform.path.ecg"
case .singleStress:
return "heart.text.square.fill"
case .coupleStress:
return "person.2.fill"
}
}
private var compactValue: String {
switch entry.face {
case .defaultHome, .singleStress:
return entry.data.hasData ? "\(entry.data.hrvValue)ms" : "--"
case .coupleStress:
return entry.data.hasData ? "\(entry.data.partnerHRV)/\(entry.data.myHRV)" : "--"
}
}
private var compactProgress: CGFloat {
switch entry.face {
case .defaultHome, .singleStress:
return 0.08 + min(CGFloat(entry.data.hrvValue) / 100, 0.78)
case .coupleStress:
return 0.08 + min(CGFloat(entry.data.myHRV) / 100, 0.78)
}
}
private var inlineTitle: String {
switch entry.face {
case .defaultHome:
return "HRV"
case .singleStress:
return entry.data.myStatus.title
case .coupleStress:
return "双人状态"
}
}
}