宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//
//  TaxiOrderDetailsVC.swift
//  OKProject
//
//  Created by alvin_y on 2020/6/13.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
import RxSwift
import RxCocoa
/// 出租车订单详情
class TaxiOrderDetailsVC: YYViewController {
    
    /// 地图
    private let mapView = MAMapView()
    
    /// viewModel
    let viewModel = TravelServiceViewModel()
    
    /// 用户点数据
    private var pointArray: [PointAnnotation] = []
    
    /// 折线数据
    private var lineCoordinates: [CLLocationCoordinate2D] = []
    
    /// 评价信息
    private let view_info = TaxiOrderDetailsView.instance()
    
    init(orderId: Int,orderType: OrderType) {
        super.init(nibName: nil, bundle: nil)
        viewModel.orderId.accept(orderId)
        viewModel.orderType.accept(orderType)
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        // Do any additional setup after loading the view.
        viewModel.queryTrack()
        viewModel.queryOrderInfo()
    }
    
    //MARK: - UI
    override func setupViews() {
        super.setupViews()
        navigationItem.title = "订单详情"
        navigationItem.rightBarButtonItem = UIBarButtonItem.yy_creat(title: "开发票", target: self, action: #selector(invoice)).item
        mapView.showsUserLocation = false // YES 为打开定位,NO为关闭定位
        mapView.isRotateEnabled = false
        mapView.isRotateCameraEnabled = false
        mapView.showsCompass = false
        mapView.showsScale = false
        mapView.delegate = self
        mapView.zoomLevel = 17
        mapView.mapType = .standard
        view.addSubview(mapView)
        view.addSubview(view_info)
        
    }
    
    //MARK: - Layouts
    override func defineLayouts() {
        super.defineLayouts()
        mapView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        view_info.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    /// 开发票
    @objc func invoice()
    {
//        let vc = InvoiceApplyViewController(orderId: viewModel.orderId.value)
        let vc = InvoiceViewController()
        yy_push(vc: vc)
    }
    
    //MARK: - Rx
    override func bindRx() {
        super.bindRx()
        
        view_info.didPressEvaluation.delegate(on: self) { (self, arge) in
            let (score,tags) = arge
            self.viewModel.fraction.accept(Int(score))
            self.viewModel.content.accept(tags)
            self.viewModel.orderEvaluate()
        }
        
        view_info.didPressMore.delegate(on: self) { (self, _) in
            let vc = YYCostDetailViewController.init(orderId: self.viewModel.orderId.value, orderType: self.viewModel.orderType.value)
            self.yy_push(vc: vc)
        }
        
        // 评价订单
        viewModel.evaluateSubject
        .subscribeOn(MainScheduler.instance)
            .subscribe(onNext: { [unowned self](status) in
                switch status{
                case .loading:
                    self.show()
                    break
                case .success(_ ):
                    self.hide()
                    alert(text: "评价成功")
                    NotificationCenter.default.post(name: NSNotification.Name.init(YYOrderListRefresh), object: nil)
                    self.viewModel.queryOrderInfo()
                    // 查询是否有红包
                    self.viewModel.queryRedMoney()
                    break
                case .error(let error):
                    self.hide()
                    alert(text: error.localizedDescription)
                    break
                }
        }).disposed(by: disposeBag)
        
        // 查询是否有红包
        viewModel.queryRedMoneySubject
        .subscribeOn(MainScheduler.instance)
            .subscribe(onNext: { [unowned self](status) in
                switch status{
                case .loading:
                    self.show()
                    break
                case .success(let model):
                    self.hide()
                    if let data = model as? CancleAmountModel{
                        if data.amount != 0{
                            alert(popup: .double, title: "提示", text: "分享APP给好友可以获得\(data.amount)元打车红包", submitTitle: "去分享", cancelTitle: "不分享", submitClick: {[unowned self] in
                                // 弹出分享框
                                ShareView.share(controller: self, title: nil, content: "新版“宽窄优行”隆重上线,下载注册即送打车券。", url: "\(share_url)/share/shareIndex.html?uid=\(app.userInfo.id)&type=1",hiddenMessage: true).show(success: {
                                    self.viewModel.shareRedEnvelope()
                                })
                            }) {}
                        }
                    }
                    break
                case .error(let error):
                    self.hide()
                    alert(text: error.localizedDescription)
                    break
                }
        }).disposed(by: disposeBag)
        
        
        viewModel.track
            .observeOn(MainScheduler.instance)
            .subscribe(onNext: { (data) in
                for item in data{
                    let point = CLLocationCoordinate2D.init(latitude: item.lat, longitude: item.lon)
                    self.lineCoordinates.append(point)
                }
                if self.lineCoordinates.count > 0{
                    let polyline = MAPolyline(coordinates: &self.lineCoordinates, count: UInt(self.lineCoordinates.count))!
                    self.mapView.add(polyline)
                    self.mapView.showOverlays([polyline], animated: true)
                }
        }).disposed(by: disposeBag)
        
        
        /// 更新行程信息
               viewModel.data
                   .subscribeOn(MainScheduler.instance)
                   .subscribe(onNext: {[unowned self] (r) in
                   guard let model = r else {return}
                    self.view_info.configure(data: model)
                    /// 加入起点
                    let startPointAnnotation = PointAnnotation()
                    startPointAnnotation.coordinate = CLLocationCoordinate2DMake(model.startLat, model.startLon)
                    startPointAnnotation.title = nil
                    startPointAnnotation.type = 3
                    self.pointArray.append(startPointAnnotation)
                    
                    /// 加入终点
                    let destinationAnnotation = PointAnnotation()
                    destinationAnnotation.coordinate = CLLocationCoordinate2DMake(model.endLat, model.endLon)
                    destinationAnnotation.title = nil
                    destinationAnnotation.type = 4
                    self.pointArray.append(destinationAnnotation)
                    self.mapView.addAnnotations(self.pointArray)
                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                        self.mapView.showAnnotations(self.pointArray, edgePadding: UIEdgeInsets(top: navH + 20, left: 10, bottom: 350, right: 10), animated: true)
                    }
               }).disposed(by: disposeBag)
    }
}
//MARK: - MAMapViewDelegate
extension TaxiOrderDetailsVC: MAMapViewDelegate
{
    func mapView(_ mapView: MAMapView!, regionDidChangeAnimated animated: Bool) {
        
    }
    
    func mapView(_ mapView: MAMapView!, mapDidMoveByUser wasUserAction: Bool) {
        
    }
    
    func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
        if annotation.isKind(of: PointAnnotation.classForCoder()) {
            let annotation = annotation as! PointAnnotation
            var imageName: String = ""
            let identifier = "annotation"
            var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? UserAnnotationView
            if annotationView == nil{
                annotationView = UserAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            }
            switch annotation.type {
            // 开始位置
            case 1:
                imageName = "icon_location"
                annotationView?.yy_tintColor = UIColor.color(hexString: "#E30B0B")
                annotationView?.yy_isTintColor = true
                annotationView?.yy_font = UIFont.init(name: Medium, size: 14)!
            case 2:// 车辆位置
                imageName = "car"
            case 3:// 起点位置
                imageName = "starting_point"
            case 4:// 终点位置
                imageName = "end_point"
            case 5: // 修改起点位置
                imageName = "starting_point"
                annotationView?.yy_font = UIFont.init(name: Medium, size: 14)!
            case 6://司机位置
                imageName = "bus_location"
            default:
                return nil
            }
            let image = UIImage(named: imageName)
            annotationView?.image = image
            if annotation.title != nil{
                annotationView?.title = annotation.title
            }
            return annotationView
            
        }
        return nil
    }
    
    func mapView(_ mapView: MAMapView!, rendererFor overlay: MAOverlay!) -> MAOverlayRenderer! {
        let renderer: MAPolylineRenderer = MAPolylineRenderer(overlay: overlay)
        renderer.lineWidth = 6.0
        renderer.strokeColor = UIColor.color(hexString: "#00C47A")
        return renderer
    }
    
    
}