DFWatchFigmaHomeViews.swift
4.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
import SwiftUI
import UIKit
struct DFDefaultWatchHomeView: View {
@State private var batteryLevel: Float = UIDevice.current.batteryLevel
@State private var batteryState: UIDevice.BatteryState = UIDevice.current.batteryState
@State private var weekday: String = ""
@State private var day: String = ""
var hasData: Bool = false
var hrvValue: String = "38ms"
var hrvTime: String = "19:04"
var batteryColor: Color{
if batteryLevel < 0.1{
// 0
return Color(hex: "#FF0000")
}else if batteryLevel < 40{
// 25
return Color(hex: "#FF9A6E")
}else if batteryLevel < 60{
// 50
return Color(hex: "#7B9BFB")
}else if batteryLevel < 80{
// 75
return Color(hex: "#3BD49D")
}else{
// 100
return Color(hex: "#3BD49D")
}
}
var batteryImageName: String{
if batteryLevel < 0.1{
// 0
return "battery.0percent"
}else if batteryLevel < 40{
// 25
return "battery.25percent"
}else if batteryLevel < 60{
// 50
return "battery.50percent"
}else if batteryLevel < 80{
// 75
return "battery.75percent"
}else{
// 100
return "battery.100percent"
}
}
var body: some View {
VStack{
HStack{
Image(systemName: batteryImageName)
.frame(height: 20)
.tint(batteryColor)
Spacer()
Text(weekday)
.font(.system(size: 16, weight: .medium))
.foregroundColor(DFWatchFigmaColor.purple)
Spacer().frame(width: 4)
Text(day)
.font(.system(size: 16, weight: .medium))
.foregroundColor(.white)
}
Text(.now, style: .time)
.font(.system(size: 52, weight: .medium))
.foregroundColor(.white)
.frame(height: 50)
HStack(alignment: .center){
HStack{}
.frame(width: 44, height: 44)
.background(Color(hex: "#FF0000"))
Spacer().frame(width: 14)
VStack(alignment: .leading, spacing: 0) {
Text(hasData ? "状态优秀" : "暂无数据")
.font(.system(size: 16, weight: .semibold))
.foregroundColor(hasData ? DFWatchFigmaColor.green : DFWatchFigmaColor.grayText)
Text(hasData ? "\(hrvValue)·\(hrvTime)" : "--ms·--:--")
.font(.system(size: 12, weight: .medium))
.foregroundColor(.white)
DFWatchFigmaSegmentBar(selected: nil)
.frame(height: 14)
}
}
HStack{
DFWatchHeartBeatCircle(progress: 0, value: nil)
.frame(width: 45, height: 44)
Spacer()
DFWatchCloseCircle(outerProgress: 0, middleProgress: 0, innerProgress: 0)
.frame(width: 44, height: 44)
Spacer()
DFWatchStepCountCircle(progress: 0, value: 0)
.frame(width: 44, height: 44)
}
}
.padding(.all, 15)
.onAppear {
updateDate()
updateBattery()
NotificationCenter.default.addObserver(
forName: UIDevice.batteryLevelDidChangeNotification,
object: nil,
queue: .main
) { _ in
updateBattery()
}
NotificationCenter.default.addObserver(
forName: UIDevice.batteryStateDidChangeNotification,
object: nil,
queue: .main
) { _ in
updateBattery()
}
}
}
private func updateBattery() {
UIDevice.current.isBatteryMonitoringEnabled = true
batteryLevel = UIDevice.current.batteryLevel
batteryState = UIDevice.current.batteryState
}
private func updateDate(){
let calendar = Calendar.current
let component = calendar.component(.weekday, from: .now)
weekday = calendar.shortWeekdaySymbols[component].uppercased()
let formatter = DateFormatter()
formatter.dateFormat = "dd"
day = formatter.string(from: .now)
}
}