Blame view

Twear/Motion(运动)/MotionViewController.swift 9.38 KB
75d24c15   yangbin   123
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
230
231
232
233
234
235
236
237
238
239
240
  //
  //  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 totalDistanceLabel: UILabel!
      @IBOutlet weak var detailButton: UIButton!
      
      var selectedMode: TrainType = .running
      
      private lazy var mapView: MAMapView = MAMapView()
      
      
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
          self.navigationController?.setNavigationBarHidden(false, animated: true)
          getMotionHistory(selectedMode.rawValue)
      }
      
      
      override func viewDidLoad() {
          super.viewDidLoad()
          initNav()
          SystemAuth.authLocation {isAllow, isFirst in
  //            if !isAllow {
  //                self?.showAlert(title: "提示", message: "请打开定位服务以获取您的运动轨迹")
  //            }
          }
          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 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-2))
                  str.addAttributes([.font: RegularFont(15)], range: NSMakeRange(str.length-2, 2))
                  totalDistanceLabel.attributedText = str
              }
              return
          }
          let distanceStr = String(format:"%.2f", Float(array.sum(\.distance))/1000)
          distanceLabel.text = distanceStr
          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-2))
              str.addAttributes([.font: RegularFont(15)], range: NSMakeRange(str.length-2, 2))
              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")
              detailLabel.text = LocString("户外跑步总距离")
              detailButton.setTitle(LocString("跑步总里程"), for: .normal)
          case 1:
              selectedMode = .walking
              typeImageView.image = UIImage(named: "walk_bg")
              detailLabel.text = LocString("步行总距离")
              detailButton.setTitle(LocString("步行总里程"), for: .normal)
          case 2:
              selectedMode = .bicycle
              typeImageView.image = UIImage(named: "ride_bg")
              detailLabel.text = LocString("骑行总距离")
              detailButton.setTitle(LocString("骑行总里程"), for: .normal)
          case 3:
              selectedMode = .mountaineering
              typeImageView.image = UIImage(named: "climb_bg")
              detailLabel.text = LocString("爬山总距离")
              detailButton.setTitle(LocString("爬升总高度"), for: .normal)
          default:
              break
          }
          getMotionHistory(selectedMode.rawValue)
          
          UIView.animate(withDuration: 0.2) {
              self.underLine.center.x = sender.center.x
          }
          
      }
      @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)
  //        }
      }
  
  }