Blame view

Twear/Motion/RecordDetailVC.swift 12.5 KB
75d24c15   yangbin   123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  //
  //  RecordDetailVC.swift
  //  Twear
  //
  //  Created by yangbin on 2021/12/30.
  //
  
  import UIKit
  import SwiftDate
  
  
  
  class RecordDetailVC: UIViewController {
  
      @IBOutlet weak var titleTopLayout: NSLayoutConstraint!
      @IBOutlet weak var tableViewHeight: NSLayoutConstraint!
      @IBOutlet weak var tableView: UITableView!
     
be19e595   yangbin   9
19
      @IBOutlet weak var totalTitleLabel: UILabel!
75d24c15   yangbin   123
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
      @IBOutlet weak var timeTitleLabel: UILabel!
      @IBOutlet weak var distanceTitleLabel: UILabel!
      @IBOutlet weak var calorieTitleLabel: UILabel!
      @IBOutlet weak var titleLabel: UILabel!
      @IBOutlet weak var totalDistanceLabel: UILabel!
      @IBOutlet weak var totalCalorieLabel: UILabel!
      @IBOutlet weak var totalTimeLabel: UILabel!
      @IBOutlet weak var totalNumberLabel: UILabel!
  
      @IBOutlet weak var maxDistanceLabel: UILabel!
      @IBOutlet weak var maxCalorieLabel: UILabel!
      @IBOutlet weak var maxTimeLabel: UILabel!
      
      @IBOutlet weak var scrollTopLayout: NSLayoutConstraint!
      
      var motionType: TrainType = .running
      
      private var page: Int = 1
      private let nowDate = DateInRegion()
      private var motionList: [MotionRecord] = []
      
be19e595   yangbin   9
41
      var device = CurDevice
75d24c15   yangbin   123
42
43
44
45
46
47
48
49
50
51
52
      
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
          navigationController?.setNavigationBarHidden(true, animated: true)
      }
      
      override func viewDidLoad() {
          super.viewDidLoad()
          titleTopLayout.constant = StatusBarHeight + 10
          scrollTopLayout.constant = StatusBarHeight
          calorieTitleLabel.text = LocString("单次最高热量(千卡)")
be19e595   yangbin   9
53
54
55
56
57
58
          if device.distanceUnit == 0 {
              totalTitleLabel.text = LocString("累计距离(公里)")
          } else {
              totalTitleLabel.text = LocString("累计距离(英里)")
          }
         
75d24c15   yangbin   123
59
          switch motionType {
be19e595   yangbin   9
60
          case .running, .run_indoor:
75d24c15   yangbin   123
61
62
              titleLabel.text = LocString("跑步记录")
              timeTitleLabel.text = LocString("单次最长用时")
be19e595   yangbin   9
63
64
65
66
67
68
              if device.distanceUnit == 0 {
                  distanceTitleLabel.text = LocString("单次最远距离(公里)")
              } else {
                  distanceTitleLabel.text = LocString("单次最远距离(英里)")
              }
             
75d24c15   yangbin   123
69
70
71
          case .walking:
              titleLabel.text = LocString("步行记录")
              timeTitleLabel.text = LocString("单次最长用时")
be19e595   yangbin   9
72
73
74
75
76
77
              if device.distanceUnit == 0 {
                  distanceTitleLabel.text = LocString("单次最远距离(公里)")
              } else {
                  distanceTitleLabel.text = LocString("单次最远距离(英里)")
              }
              
75d24c15   yangbin   123
78
79
80
          case .bicycle:
              titleLabel.text = LocString("骑行记录")
              timeTitleLabel.text = LocString("单次最长用时")
be19e595   yangbin   9
81
82
83
84
85
86
              if device.distanceUnit == 0 {
                  distanceTitleLabel.text = LocString("单次最远距离(公里)")
              } else {
                  distanceTitleLabel.text = LocString("单次最远距离(英里)")
              }
             
75d24c15   yangbin   123
87
88
          case .mountaineering:
              titleLabel.text = LocString("爬山记录")
be19e595   yangbin   9
89
90
91
92
93
94
95
96
              if device.distanceUnit == 0 {
                  timeTitleLabel.text = LocString("单次最远距离(公里)")
                  distanceTitleLabel.text = LocString("单次最高爬升(米)")
              } else {
                  timeTitleLabel.text = LocString("单次最远距离(英里)")
                  distanceTitleLabel.text = LocString("单次最高爬升(英尺)")
              }
             
75d24c15   yangbin   123
97
98
99
100
101
102
          default:
              break
          }
          
          
          tableView.register(UINib.init(nibName: "MotionRecordCell", bundle: Bundle.main), forCellReuseIdentifier: "MotionRecordCell")
be19e595   yangbin   9
103
          tableView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
75d24c15   yangbin   123
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
          tableView.tableFooterView = UIView(frame: CGRect.zero)
          getMotionRecordList()
          // Do any additional setup after loading the view.
      }
      
      @IBAction func back(_ sender: Any) {
          navigationController?.popViewController(animated: true)
      }
      private func getMotionRecordList(page: Int = 1) {
          if page == 1 {
              motionList = []
          }
          var totalMotions: [MotionModel] = []
          for i in 12*(page-1)..<12*(page-1)+12 {
              let motionArray = MotionModel.getMonthData(motionType.rawValue, date: (nowDate-i.months).date)
  //            if 12-i == 10 || 12-i == 9 || 12-i == 8 || 12-i == 7 {
  //                let t1 = MotionModel(type: .running, date: DateInRegion().date-100, length: 100, calorie: 200, distance: 300, steps: 500, altitude: 2)
  //                let t2 = MotionModel(type: .steppers, date: DateInRegion().date-300.minutes, length: 66, calorie: 1000, distance: 300, steps: 500, altitude: 2)
  //                let t3 = MotionModel(type: .steppers, date: DateInRegion().date+7.hours, length: 1000, calorie: 1000, distance: 300, steps: 500, altitude: 2)
  //                motionArray = [t2, t1, t3]
  //            }
be19e595   yangbin   9
125
126
127
128
129
130
131
132
133
134
              if motionArray.count > 0 {
                  totalMotions.append(contentsOf: motionArray)
                  let motion = MotionRecord(isShow: false, motionArray: motionArray.reversed(), date: (nowDate-i.months).toString(.custom("yyyy-MM")))
                  motionList.append(motion)
              }
              
          }
          if motionList.count == 0 {
  //            let motionArray = MotionModel.getMotionByMonth(nowDate.date)
              let motion = MotionRecord(isShow: false, motionArray: [], date: nowDate.toString(.custom("yyyy-MM")))
75d24c15   yangbin   123
135
              motionList.append(motion)
be19e595   yangbin   9
136
137
138
139
140
141
142
143
          } else {
  //            motionList[0].isShow = true
  //            tableView.reloadData()
          }
          if motionList.count == 1 {
              tableViewHeight.constant = 50
          } else {
              tableViewHeight.constant = CGFloat(60*motionList.count)
75d24c15   yangbin   123
144
          }
be19e595   yangbin   9
145
146
          
          
75d24c15   yangbin   123
147
148
149
150
151
152
          self.tableView.reloadData()
     
          
          if totalMotions.count == 0 {
              return
          }
be19e595   yangbin   9
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
          if device.distanceUnit == 0 {
              if motionType == .mountaineering {
                  if let maxAltitude = totalMotions.max(\.altitude)?.altitude {
                      maxDistanceLabel.text = "\(String(format:"%.2f", Float(maxAltitude)))"
                  }
                  if let maxDistance = totalMotions.max(\.distance)?.distance {
                      maxTimeLabel.text = "\(String(format:"%.2f", Float(maxDistance)/1000))"
                  }
              } else {
                  if let maxDistance = totalMotions.max(\.distance)?.distance {
                      maxDistanceLabel.text = "\(String(format:"%.2f", Float(maxDistance)/1000))"
                  }
                  if let maxTime = totalMotions.max(\.length)?.length {
                      maxTimeLabel.toTimeType3(length: maxTime)
                  }
75d24c15   yangbin   123
168
              }
be19e595   yangbin   9
169
              totalDistanceLabel.text = "\(String(format:"%.2f", Float(totalMotions.sum(\.distance))/1000))"
75d24c15   yangbin   123
170
          } else {
be19e595   yangbin   9
171
172
173
174
175
176
177
178
179
180
181
182
183
184
              if motionType == .mountaineering {
                  if let maxAltitude = totalMotions.max(\.altitude)?.altitude {
                      maxDistanceLabel.text = "\(String(format:"%.2f", Float(maxAltitude*3.281)))"
                  }
                  if let maxDistance = totalMotions.max(\.distance)?.distance {
                      maxTimeLabel.text = "\(String(format:"%.2f", Float(maxDistance)*0.6213/1000))"
                  }
              } else {
                  if let maxDistance = totalMotions.max(\.distance)?.distance {
                      maxDistanceLabel.text = "\(String(format:"%.2f", Float(maxDistance)*0.6213/1000))"
                  }
                  if let maxTime = totalMotions.max(\.length)?.length {
                      maxTimeLabel.toTimeType3(length: maxTime)
                  }
75d24c15   yangbin   123
185
              }
be19e595   yangbin   9
186
              totalDistanceLabel.text = "\(String(format:"%.2f", Float(totalMotions.sum(\.distance))*0.6213/1000))"
75d24c15   yangbin   123
187
          }
be19e595   yangbin   9
188
   
75d24c15   yangbin   123
189
190
191
192
193
194
          if let maxCalorie = totalMotions.max(\.calorie)?.calorie {
              maxCalorieLabel.text = "\(String(format:"%.2f", Float(maxCalorie)))"
          }
     
                  
  //        totalStepsLabel.text = "\(stepArray.sum(\.number))步"
be19e595   yangbin   9
195
196
          
          totalCalorieLabel.text = "\(String(format:"%.2f", Float(totalMotions.sum(\.calorie))))"
75d24c15   yangbin   123
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
          totalTimeLabel.toTimeType3(length: totalMotions.sum(\.length))
          totalNumberLabel.text = "\(totalMotions.count)"
      }
      
      private func reloadSectionCell(_ section: Int) {
          motionList[section].isShow = !motionList[section].isShow
          tableView.reloadSections(IndexSet.init(integer: section), with: .automatic)
          if motionList[section].isShow {
              tableViewHeight.constant += CGFloat(motionList[section].motionArray.count*50)
          } else {
              tableViewHeight.constant -= CGFloat(motionList[section].motionArray.count*50)
          }
          
      }
  
  }
  
  
  extension RecordDetailVC: UITableViewDelegate, UITableViewDataSource {
      
      func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
be19e595   yangbin   9
218
219
220
          if section == motionList.count-1 {
              return 0.1
          }
75d24c15   yangbin   123
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
          return 10
      }
      
      func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
          let footerView = UIView()
          footerView.backgroundColor = UIColor.rgbColorFromHex(0xF2F2F2)
  //        print("???")
          return footerView
      }
  //    func height
      
      func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
          return 50
      }
  
      
      func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
          let headerView = RecordHeaderView(section: section, motion: motionList[section])
  //        headerView.backgroundColor = .red
          headerView.headerClosure = {[weak self] section in
              self?.reloadSectionCell(section)
          }
          return headerView
      }
      
      
      func numberOfSections(in tableView: UITableView) -> Int {
          return motionList.count
      }
      
      
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return 50
      }
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return motionList[section].isShow ? motionList[section].motionArray.count : 0
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "MotionRecordCell", for: indexPath) as! MotionRecordCell
be19e595   yangbin   9
262
          cell.device = device
75d24c15   yangbin   123
263
264
265
266
267
268
          if motionList[indexPath.section].isShow {
              cell.motion = motionList[indexPath.section].motionArray[indexPath.row]
          }
          
          return cell
      }
be19e595   yangbin   9
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
      
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          let vc = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Motion", identifier: "EndMotionVC") as! EndMotionVC
          vc.modalPresentationStyle = .fullScreen
          let motion = motionList[indexPath.section].motionArray[indexPath.row]
          var coordinateArray: [CLLocationCoordinate2D] = []
          let coordinateStrArray = motion.coordinateArray.split(separator: "|")
          for coordinateStr in coordinateStrArray {
              let coordinateSub = String(coordinateStr).split(separator: ",")
              if coordinateSub.count == 2 {
                  coordinateArray.append(CLLocationCoordinate2D(latitude: CLLocationDegrees(Float(coordinateSub[0])!), longitude: CLLocationDegrees(Float(coordinateSub[1])!)))
              }
          }
          
          var speedArray: [Float] = []
          let speedStrArray = motion.speedArray
          for speed in speedStrArray.split(separator: "|") {
              speedArray.append(Float("\(speed)")!)
          }
          
          var stepCadenceArray: [Int] = []
          let stepCadenceStrArray = motion.stepCadenceArray
          for stepCadence in stepCadenceStrArray.split(separator: "|") {
              stepCadenceArray.append(Int("\(stepCadence)")!)
          }
          
          var altitudeArray: [Double] = []
          let altitudeStrArray = motion.altitudeArray
          for altitude in altitudeStrArray.split(separator: "|") {
              altitudeArray.append(Double("\(altitude)")!)
          }
  
          vc.coordinateArray = coordinateArray
          vc.calorie = motion.calorie
          vc.distance = Float(motion.distance)
          vc.length = motion.length
          vc.date = motion.date
          vc.speedArray = speedArray
          vc.isRecordPresent = true
          vc.motionType = motion.type!
          vc.stepCadenceArray = stepCadenceArray
          vc.altitudeArray = altitudeArray
          vc.altitude = motion.altitude
          self.present(vc, animated: true, completion: nil)
      }
75d24c15   yangbin   123
314
  }