Blame view

HDFwear/Motion/MotionViewController.swift 13.4 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
  //
  //  MotionViewController.swift
  //  Twear
  //
  //  Created by yangbin on 2021/11/16.
  //
  
  import UIKit
  import SnapKit
  import MBProgressHUD
  
  class MotionViewController: UIViewController {
      
      @IBOutlet weak var mapBackView: UIView!
      
      @IBOutlet weak var underLine: UIImageView!
      @IBOutlet weak var climbBtn: UIButton!
      @IBOutlet weak var rideBtn: UIButton!
      @IBOutlet weak var walkBtn: UIButton!
      @IBOutlet weak var runBtn: UIButton!
      @IBOutlet weak var typeImageView: UIImageView!
      @IBOutlet weak var distanceLabel: UILabel!
      @IBOutlet weak var detailLabel: UILabel!
      @IBOutlet weak var distanceUnitLabel: UILabel!
      @IBOutlet weak var totalDistanceLabel: UILabel!
      @IBOutlet weak var detailButton: UIButton!
      
      @IBOutlet weak var runModeView: UIView!
      @IBOutlet weak var outdoorBtn: UIButton!
      @IBOutlet weak var indoorBtn: UIButton!
      var selectedMode: TrainType = .running
      
      private lazy var mapView: MAMapView = MAMapView()
      
      var user = UserInfo
      
      
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
072a11e2   daifengyi   feat:setting page UI
40
          self.navigationController?.setNavigationBarHidden(true, animated: true)
f2cf74c7   yangbin   1.0.20(4)
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
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
262
263
264
265
266
267
268
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
314
315
316
          user = UserInfo
          getMotionHistory(selectedMode.rawValue)
          
      }
      
      
      override func viewDidLoad() {
          super.viewDidLoad()
          initNav()
          SystemAuth.authLocation {isAllow, isFirst in
  //            if !isAllow {
  //                self?.showAlert(title: "提示", message: "请打开定位服务以获取您的运动轨迹")
  //            }
          }
          
          outdoorBtn.setTitle(LocString("户外"), for: .normal)
          indoorBtn.setTitle(LocString("室内"), for: .normal)
          
          initMapView()
  //        getMotionHistory()
      }
      
      func initNav() {
          let setButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 28))
          setButton.setTitle(LocString("运动"), for: .normal)
          setButton.titleLabel?.font = BoldFont(18)
          setButton.setTitleColor(.black, for: .normal)
          navigationItem.leftBarButtonItem =  UIBarButtonItem(customView: setButton)
      }
      
      func initMapView() {
          MAMapView.updatePrivacyShow(.didShow, privacyInfo: .didContain)
          MAMapView.updatePrivacyAgree(.didAgree)
          
          mapView.showsUserLocation = false
          mapView.userTrackingMode = .follow
          mapView.showsCompass = false
          mapView.zoomLevel = 16
          
          mapView.mapLanguage = (AppSettings.shared.language == .Chinese) ? 0 : 1
          
          let currentLocation = MAUserLocationRepresentation()
          currentLocation.showsAccuracyRing = true//精度圈是否显示
          currentLocation.fillColor = UIColor(white: 1, alpha: 0)//精度圈填充颜色
          currentLocation.strokeColor = UIColor(white: 1, alpha: 0)//调整精度圈边线颜色
          currentLocation.showsHeadingIndicator = false//是否显示蓝点方向指向
  //        currentLocation.image = UIImage(named: "selected") //定位图标, 与蓝色原点互斥
          currentLocation.locationDotBgColor = .clear
          currentLocation.locationDotFillColor = .clear
          currentLocation.enablePulseAnnimation = false
  //        currentLocation.a
          mapView.update(currentLocation)
          mapView.frame = mapBackView.bounds
          mapBackView.addSubview(mapView)
          
          
    
      }
      
      private func getMotionHistory(_ type: Int = 0) {
          guard let array = RealmTools.objectsWithPredicateAndSorted(object: MotionModel.self, predicate: NSPredicate(format: "typeRaw = %@", argumentArray:[type]), sortedKey: "date") as? Array<MotionModel>, array.count > 0 else {
              distanceLabel.text = "0"
              if user.distanceUnit == 0 {
                  distanceUnitLabel.text = LocString("公里")
                  if selectedMode == .mountaineering {
                      let str = NSMutableAttributedString(string: "0"+LocString("米"))
                      str.addAttributes([.font: BoldFont(40)], range: NSMakeRange(0, str.length-LocString("米").count))
                      str.addAttributes([.font: RegularFont(15)], range: NSMakeRange(str.length-LocString("米").count, LocString("米").count))
                      totalDistanceLabel.attributedText = str
                  } else {
                      let str = NSMutableAttributedString(string: "0"+LocString("公里"))
                      str.addAttributes([.font: BoldFont(40)], range: NSMakeRange(0, str.length-LocString("公里").count))
                      str.addAttributes([.font: RegularFont(15)], range: NSMakeRange(str.length-LocString("公里").count, LocString("公里").count))
                      totalDistanceLabel.attributedText = str
                  }
              } else {
                  distanceUnitLabel.text = LocString("英里")
                  if selectedMode == .mountaineering {
                      let str = NSMutableAttributedString(string: "0"+LocString("英尺"))
                      str.addAttributes([.font: BoldFont(40)], range: NSMakeRange(0, str.length-LocString("英尺").count))
                      str.addAttributes([.font: RegularFont(15)], range: NSMakeRange(str.length-LocString("英尺").count, LocString("英尺").count))
                      totalDistanceLabel.attributedText = str
                  } else {
                      let str = NSMutableAttributedString(string: "0"+LocString("英里"))
                      str.addAttributes([.font: BoldFont(40)], range: NSMakeRange(0, str.length-LocString("英里").count))
                      str.addAttributes([.font: RegularFont(15)], range: NSMakeRange(str.length-LocString("英里").count, LocString("英里").count))
                      totalDistanceLabel.attributedText = str
                  }
                  
              }
       
              return
          }
  //        var distanceStr = ""
          if user.distanceUnit == 0 {
              let distanceStr = String(format:"%.2f", Float(array.sum(\.distance))/1000)
              distanceLabel.text = distanceStr
              distanceUnitLabel.text = LocString("公里")
              if selectedMode == .mountaineering {
                  let altitudeStr = String(format:"%.2f", Float(array.sum(\.altitude)))
                  let str = NSMutableAttributedString(string: "\(altitudeStr)\(LocString("米"))")
                  str.addAttributes([.font: BoldFont(40)], range: NSMakeRange(0, str.length-LocString("米").count))
                  str.addAttributes([.font: RegularFont(15)], range: NSMakeRange(str.length-LocString("米").count, LocString("米").count))
                  totalDistanceLabel.attributedText = str
              } else {
                  let str = NSMutableAttributedString(string: "\(distanceStr)\(LocString("公里"))")
                  str.addAttributes([.font: BoldFont(40)], range: NSMakeRange(0, str.length-LocString("公里").count))
                  str.addAttributes([.font: RegularFont(15)], range: NSMakeRange(str.length-LocString("公里").count, LocString("公里").count))
                  totalDistanceLabel.attributedText = str
              }
          } else {
              let distanceStr = (Float(array.sum(\.distance))/1000).mileString()
              distanceLabel.text = distanceStr
              distanceUnitLabel.text = LocString("英里")
              if selectedMode == .mountaineering {
                  let altitudeStr = Float(array.sum(\.altitude)).footString()
                  let str = NSMutableAttributedString(string: "\(altitudeStr)\(LocString("英尺"))")
                  str.addAttributes([.font: BoldFont(40)], range: NSMakeRange(0, str.length-LocString("英尺").count))
                  str.addAttributes([.font: RegularFont(15)], range: NSMakeRange(str.length-LocString("英尺").count, LocString("英尺").count))
                  totalDistanceLabel.attributedText = str
              } else {
                  let str = NSMutableAttributedString(string: "\(distanceStr)\(LocString("英里"))")
                  str.addAttributes([.font: BoldFont(40)], range: NSMakeRange(0, str.length-LocString("英里").count))
                  str.addAttributes([.font: RegularFont(15)], range: NSMakeRange(str.length-LocString("英里").count, LocString("英里").count))
                  totalDistanceLabel.attributedText = str
              }
          }
         
      }
      
      
      
      
      @IBAction func selectMotionMode(_ sender: UIButton) {
          climbBtn.titleLabel?.font = RegularFont(16)
          rideBtn.titleLabel?.font = RegularFont(16)
          walkBtn.titleLabel?.font = RegularFont(16)
          runBtn.titleLabel?.font = RegularFont(16)
          sender.titleLabel?.font = BoldFont(16)
          switch sender.tag {
          case 0:
              selectedMode = .running
              typeImageView.image = UIImage(named: "run_bg")
              detailButton.setTitle(LocString("跑步总里程"), for: .normal)
              runModeView.isHidden = false
              if outdoorBtn.titleColor(for: .normal)! == TintColor {
                  selectedMode = .running
                  detailLabel.text = LocString("户外跑步总距离")
              } else {
                  selectedMode = .run_indoor
                  detailLabel.text = LocString("室内跑步总距离")
              }
          case 1:
              selectedMode = .walking
              typeImageView.image = UIImage(named: "walk_bg")
              detailLabel.text = LocString("步行总距离")
              detailButton.setTitle(LocString("步行总里程"), for: .normal)
              runModeView.isHidden = true
          case 2:
              selectedMode = .bicycle
              typeImageView.image = UIImage(named: "ride_bg")
              detailLabel.text = LocString("骑行总距离")
              detailButton.setTitle(LocString("骑行总里程"), for: .normal)
              runModeView.isHidden = true
          case 3:
              selectedMode = .mountaineering
              typeImageView.image = UIImage(named: "climb_bg")
              detailLabel.text = LocString("爬山总距离")
              detailButton.setTitle(LocString("爬升总高度"), for: .normal)
              runModeView.isHidden = true
          default:
              break
          }
          getMotionHistory(selectedMode.rawValue)
          
          UIView.animate(withDuration: 0.2) {
              self.underLine.center.x = sender.center.x
          }
      }
      
      
      @IBAction func selectRunMode(_ sender: UIButton) {
          if sender.tag == 0 {
              outdoorBtn.setTitleColor(TintColor, for: .normal)
              indoorBtn.setTitleColor(.black, for: .normal)
              detailLabel.text = LocString("户外跑步总距离")
              selectedMode = .running
          } else {
              outdoorBtn.setTitleColor(.black, for: .normal)
              indoorBtn.setTitleColor(TintColor, for: .normal)
              detailLabel.text = LocString("室内跑步总距离")
              selectedMode = .run_indoor
          }
          getMotionHistory(selectedMode.rawValue)
      }
      
      
      @IBAction func startMotion(_ sender: Any) {
          SystemAuth.authLocation {[weak self] isAllow, isFirst in
              if !isAllow {
                  self?.showAlert(title: LocString("提示"), message: LocString("请打开定位服务以获取您的运动轨迹"))
              } else {
                  let vc = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Motion", identifier: "StartMotionVC") as! StartMotionVC
                  vc.motionType = (self?.selectedMode)!
                  vc.modalPresentationStyle = .fullScreen
                  self?.present(vc, animated: false, completion: nil)
              }
          }
        
      }
      
      @IBAction func showDetailInfo(_ sender: Any) {
          let vc = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Motion", identifier: "RecordDetailVC") as! RecordDetailVC
          vc.motionType = selectedMode
          navigationController?.pushViewController(vc, animated: true)
      }
      
      deinit {
          print("deinit\(NSStringFromClass(type(of: self)))!!!!!!!")
      }
  
  }
  
  
  extension MotionViewController: MAMapViewDelegate {
      func mapView(_ mapView: MAMapView!, didUpdate userLocation: MAUserLocation!, updatingLocation: Bool) {
          if !updatingLocation {
              return
          }
          
          let location: CLLocation? = userLocation.location
          
          if location == nil {
              return
          }
          
  //        if isRecording {
  //            // filter the result
  //            if userLocation.location.horizontalAccuracy < 100.0 {
  //
  //                addLocation(location: userLocation.location)
  //            }
  //        }
  //
  //        var speed = location!.speed
  //        if speed < 0.0 {
  //            speed = 0.0
  //        }
  //
  //        let infoArray: [(String, String)] = [
  //            ("coordinate", NSString(format: "<%.4f, %.4f>", location!.coordinate.latitude, location!.coordinate.longitude) as String),
  //            ("speed", NSString(format: "%.2fm/s(%.2fkm/h)", speed, speed * 3.6) as String),
  //            ("accuracy", "\(location!.horizontalAccuracy)m"),
  //            ("altitude", NSString(format: "%.2fm", location!.altitude) as String)]
  //
  //        statusView!.showStatusInfo(info: infoArray)
          
      }
      func mapViewDidFinishLoadingMap(_ mapView: MAMapView!) {
          print("1111")
      }
      
      func mapViewDidFailLoadingMap(_ mapView: MAMapView!, withError error: Error!) {
          print("111231232111")
      }
  
      func mapView(_ mapView: MAMapView, didChange mode: MAUserTrackingMode, animated: Bool) {
  //        if mode == MAUserTrackingMode.none {
  //            locationButton?.setImage(imageNotLocate, for: UIControlState.normal)
  //        }
  //        else {
  //            locationButton?.setImage(imageLocated, for: UIControlState.normal)
  //        }
      }
  
  }