Blame view

HDFwear/Home/MotionRecordVC.swift 8.51 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  //
  //  MotionRecordVC.swift
  //  Twear
  //
  //  Created by yangbin on 2021/11/25.
  //
  
  import UIKit
  import SwiftDate
  import MJRefresh
  
  struct MotionRecord {
      var isShow: Bool
      var motionArray: [MotionModel]
      var date: String
  }
  
  class MotionRecordVC: UIViewController {
      
      @IBOutlet weak var tableView: UITableView!
      
      private var page: Int = 1
      private let nowDate = DateInRegion()
      private var motionList: [MotionRecord] = [] {
          didSet {
  //            points = []
  //            if trainArray.count == 0 {
  //                resetLabel()
  //            }
  //            let max = trainArray.max{$0.calorie < $1.calorie}?.calorie
  //            barChartView.leftAxis.axisMaximum = Double(max ?? 100) * 1.1
  //            self.tableView.reloadData()
          }
      }
      var user = UserInfo
      
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
          navigationController?.setNavigationBarHidden(false, animated: true)
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
          title = LocString("运动记录")
          
  
  //        tableView.reloadData()
          tableView.register(UINib.init(nibName: "MotionRecordCell", bundle: Bundle.main), forCellReuseIdentifier: "MotionRecordCell")
          tableView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 10, right: 0)
          
          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)
  //        self.tableView.estimatedRowHeight = 50;
  //        self.tableView.estimatedSectionHeaderHeight = 50;
  //        self.tableView.estimatedSectionFooterHeight = 10;
  //        tableView.rowHeight = UITableView.automaticDimension;
  //        tableView.sectionHeaderHeight = UITableView.automaticDimension;
  //        tableView.sectionFooterHeight = UITableView.automaticDimension
          /*
          let header = MJRefreshNormalHeader(refreshingBlock: { [weak self] in
              self?.page = 1
              self?.getMotionRecordList(page: 1)
          })
          header.lastUpdatedTimeLabel?.isHidden = true
          tableView.mj_header = header
          tableView.mj_header?.isHidden = true
          
          
          let footer = MJRefreshAutoNormalFooter.init {[weak self] in
              if !(self?.tableView.mj_header?.isRefreshing)! {
                  self?.getMotionRecordList(page: (self?.page)!)
              } else {
                  self?.tableView.mj_footer?.endRefreshing()
              }
          }
          footer.setTitle("", for: .noMoreData)
          tableView.mj_footer = footer
          tableView.mj_footer?.isHidden = true
           
           */
          
          getMotionRecordList()
      }
      
      @objc func share() {
          let shareView = ShareView(view.captureImage)
          shareView.show()
      }
      
      private func getMotionRecordList(page: Int = 1) {
          if page == 1 {
              motionList = []
          }
          for i in 12*(page-1)..<12*(page-1)+12 {
              let motionArray = MotionModel.getMotionByMonth((nowDate-i.months).date)
  //            if 12-i == 10 {
  //                let t1 = TrainModel(type: .running, date: DateInRegion().date-100, length: 100, calorie: 200, mileage: 300, steps: 500)
  //                let t2 = TrainModel(type: .steppers, date: DateInRegion().date-300.minutes, length: 66, calorie: 1000, mileage: 300, steps: 500)
  //                let t3 = TrainModel(type: .steppers, date: DateInRegion().date+7.hours, length: 1000, calorie: 1000, mileage: 300, steps: 500)
  //                trainArray = [t2, t1, t3]
  //            }
              if motionArray.count > 0 {
                  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")))
              motionList.append(motion)
          } else {
  //            motionList[0].isShow = true
  //            tableView.reloadData()
          }
      }
      
      deinit {
          print("MotionRecordVC dealloc")
      }
  
  }
  
  extension MotionRecordVC: UITableViewDelegate, UITableViewDataSource {
      func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
          if section == motionList.count-1 {
              return 0.1
          }
          return 10
      }
      
      func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
          let footerView = UIView()
          footerView.backgroundColor = BackgroundColor
          return footerView
      }
      
      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.section = section
  //        headerView.isShow = motionArray[section].isShow
          
          headerView.headerClosure = {[weak self] section in
              self?.motionList[section].isShow = !(self?.motionList[section].isShow)!
  //            UIView.performWithoutAnimation {
                  self?.tableView.reloadSections(IndexSet.init(integer: section), with: .none)
  //            }
              
  //            headerView.rotateArrow((self?.motionArray[section].isShow)!)
          }
          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
          cell.user = user
          if motionList[indexPath.section].isShow {
              cell.motion = motionList[indexPath.section].motionArray[indexPath.row]
          }
          
          return cell
      }
      
      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)
      }
  }