Blame view

HDFwear/Mine/HealthDataVC.swift 6.41 KB
f2cf74c7   yangbin   1.0.20(4)
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
  //
  //  HealthDataVC.swift
  //  Twear
  //
  //  Created by yangbin on 2022/1/1.
  //
  
  import UIKit
  import SnapKit
  import SwiftDate
  
  class HealthDataVC: UIViewController {
      
      @IBOutlet weak var tableView: UITableView!
      
      private var dataArray: [[String]] = [["步数", "距离", "热量"], ["心率", "血压", "血氧", "睡眠", "体重"]]
      private let dataDic = ["步数": "my_data_step", "距离": "my_data_distance", "热量": "my_data_calorie", "心率": "my_data_hr", "血压": "my_data_bp", "血氧": "my_data_bo", "睡眠": "my_data_sleep", "体重": "my_data_weight"]
      
      let device = CurDevice
      let weight = UserInfo.weight
      var totalDistance = "--"
      var totalCalorie = "--"
      var totalSteps = "--"
      var bp = "--"
      var bo = "--"
      var hr = "--"
      var sleep = "--"
      
      override func viewDidLoad() {
          super.viewDidLoad()
          title = LocString("我的数据")
          
fc3a8047   daifengyi   feat:health data ...
33
34
35
36
  //        let shareButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 28))
  //        shareButton.setImage(UIImage(named: "share_btn"), for: .normal)
  //        shareButton.addTarget(self, action: #selector(share), for: .touchUpInside)
  //        navigationItem.rightBarButtonItem =  UIBarButtonItem(customView: shareButton)
f2cf74c7   yangbin   1.0.20(4)
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
          testDate()
          let stepArray = StepModel.getStepsByYear(Date())
          let user = UserInfo
          
          if stepArray.count > 0 {
              if user.distanceUnit == 0 {
                  totalDistance = "\(String(format:"%.2f", stepArray.sum(\.distance)))\(LocString("公里"))"
              } else {
                  totalDistance = "\(stepArray.sum(\.distance).mileString())\(LocString("英里"))"
              }
              totalCalorie = "\(String(format:"%.2f", stepArray.sum(\.calorie)))\(LocString("千卡"))"
              totalSteps = "\(stepArray.sum(\.number))\(LocString("步"))"
          }
          
          for value in dataArray[1] {
              switch value {
              case "心率":
                  if let hrArray = RealmTools.queryObjects(HeartRateModel.self) as? [HeartRateModel], hrArray.count > 0 {
                      hr = "\(hrArray.average(\.value))\(LocString("次/分钟"))"
                  }
              case "血氧":
                  if let boArray = RealmTools.queryObjects(BloodOxygenModel.self) as? [BloodOxygenModel], boArray.count > 0 {
                      bo = "\(boArray.average(\.value))%"
                  }
              case "血压":
                  if let bpArray = RealmTools.queryObjects(BloodPressureModel.self) as? [BloodPressureModel], bpArray.count > 0 {
                      bp = "\(bpArray.average(\.sbp))/\(bpArray.average(\.dbp))mmHg"
                  }
              case "睡眠":
                  let sleepArray = SleepModel.getSleepByYear(Date())
                  if sleepArray.count > 0 {
                      let length = sleepArray.average(\.sleepLength)
                      sleep = "\(String(format:"%02d",length/60))h\( String(format:"%02d",length%60))m"
                  }
              default:
                  break
              }
          }
          
          
          // Do any additional setup after loading the view.
      }
      
      func testDate() {
          if IsTest {
  
              if let i = dataArray[1].firstIndex(of: "血压") {
                  dataArray[1].remove(at: i)
              }
              if let i = dataArray[1].firstIndex(of: "血氧") {
                  dataArray[1].remove(at: i)
              }
              
          }
      }
      
      @objc func share() {
          let shareView = ShareView(view.captureImage)
          shareView.show()
      }
      
      
  }
  
  extension HealthDataVC: UITableViewDataSource, UITableViewDelegate {
      func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
          return 30
      }
      func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
          let view = UIView()
          view.backgroundColor = .clear
          
          let label = UILabel()
          label.font = RegularFont(11)
          if section == 0 {
              label.text = LocString("活动统计")
          } else {
              label.text = LocString("健康状况")
          }
          view.addSubview(label)
          label.snp.makeConstraints { make in
              make.left.equalToSuperview().offset(12)
              make.top.equalToSuperview().offset(7)
          }
          return view
      }
      func numberOfSections(in tableView: UITableView) -> Int {
          return dataArray.count
      }
fc3a8047   daifengyi   feat:health data ...
126
127
128
129
130
131
132
133
134
135
136
      func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
           return 48
      }
      
      func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
          if section == 0 {
              return "活动统计"
          }else {
              return "健康状况"
          }
      }
f2cf74c7   yangbin   1.0.20(4)
137
138
      
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
fc3a8047   daifengyi   feat:health data ...
139
          return 55
f2cf74c7   yangbin   1.0.20(4)
140
141
142
143
144
145
146
147
148
149
150
151
      }
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return dataArray[section].count
      }
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "HealthDataCell", for: indexPath) as! HealthDataCell
          let text = dataArray[indexPath.section][indexPath.row]
          cell.titleLabel.text = LocString(text)
          cell.iconView.image = UIImage(named: dataDic[text]!)
          switch text {
          case "体重":
              cell.detailLabel.text = "\(weight)kg"
fc3a8047   daifengyi   feat:health data ...
152
153
              cell.setCorners(corners: [.bottomLeft, .bottomRight], radio: 10)
              break
f2cf74c7   yangbin   1.0.20(4)
154
155
          case "距离":
              cell.detailLabel.text = totalDistance
fc3a8047   daifengyi   feat:health data ...
156
              break
f2cf74c7   yangbin   1.0.20(4)
157
158
          case "热量":
              cell.detailLabel.text = totalCalorie
fc3a8047   daifengyi   feat:health data ...
159
160
              cell.setCorners(corners: [.bottomLeft, .bottomRight], radio: 10)
              break
f2cf74c7   yangbin   1.0.20(4)
161
162
          case "血氧":
              cell.detailLabel.text = bo
fc3a8047   daifengyi   feat:health data ...
163
              break
f2cf74c7   yangbin   1.0.20(4)
164
165
          case "血压":
              cell.detailLabel.text = bp
fc3a8047   daifengyi   feat:health data ...
166
              break
f2cf74c7   yangbin   1.0.20(4)
167
168
          case "心率":
              cell.detailLabel.text = hr
fc3a8047   daifengyi   feat:health data ...
169
170
              cell.setCorners(corners: [.topLeft, .topRight], radio: 10)
              break
f2cf74c7   yangbin   1.0.20(4)
171
172
          case "睡眠":
              cell.detailLabel.text = sleep
fc3a8047   daifengyi   feat:health data ...
173
              break
f2cf74c7   yangbin   1.0.20(4)
174
175
          case "步数":
              cell.detailLabel.text = totalSteps
fc3a8047   daifengyi   feat:health data ...
176
177
              cell.setCorners(corners: [.topLeft, .topRight], radio: 10)
              break
f2cf74c7   yangbin   1.0.20(4)
178
179
180
181
182
183
          default:
              break
          }
          return cell
      }
  }