宽窄优行-由【嘉易行】项目成品而来
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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//
//  SearchAddressDragVC.swift
//  OKProject
//
//  Created by 无故事王国 on 2022/5/23.
//  Copyright © 2022 yangwang. All rights reserved.
//
 
import UIKit
import RxCocoa
import RxSwift
import IQKeyboardManager
import AMapSearchKit
import QMUIKit
 
class SearchAddressDragVC: YYViewController {
 
    @IBOutlet weak var mapBgView: UIView!
    /// 列表
    @IBOutlet weak var tableView: UITableView!
 
    /// 城市按钮
    @IBOutlet weak var button_city: QMUIButton!
 
    /// 搜索框
    @IBOutlet weak var textField_search: UITextField!
 
    @IBOutlet weak var clearBtn: QMUIButton!
    @IBOutlet weak var clearHeiCons: NSLayoutConstraint!
 
    lazy var label_desc: UILabel = {
        let label = UILabel()
        label.text = "在这里上车"
        label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        label.textColor = ThemeColor
        return label
    }()
 
    /// 中心点
    lazy var view_desc: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.cornerRadius = 10
        view.addSubview(label_desc)
        label_desc.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5))
        }
        return view
    }()
 
    /// 中心点
    private let centerView: UIImageView = {
        let image = UIImageView.init(image: UIImage.init(named: "zuobiao"))
        return image
    }()
 
    let mapView: MAMapView = {
        let view = MAMapView()
        view.mapType = .standard
        view.showsUserLocation = true // YES 为打开定位,NO为关闭定位
        view.zoomLevel = 17
        view.isRotateCameraEnabled = false
        view.isRotateEnabled = false
        view.userTrackingMode = .follow
        view.pausesLocationUpdatesAutomatically = false
        view.allowsBackgroundLocationUpdates = true
        view.isRotateEnabled = false
        view.showsCompass = false
        view.showsScale = false
        return view
    }()
 
    private var manager: LocationSearchManager = LocationSearchManager()
 
    /// 搜索城市
    private var cityName = YYLocationManager.shared.currentCity
 
    /// 数据源
    private var items: [AddressModelInfo] = AddressModelInfoSave.readFile()
 
    /// 是不是开始位置
    let origin = BehaviorRelay<Bool>(value: false)
 
    /// 订单类型  跨城
    let orderType = BehaviorRelay<OrderType>(value: .special)
 
    /// 回调
    let complete = Delegate<(AddressModelInfo,Bool),Void>()
 
 
 
    /// 跨城新增
    var lat = 0.0
    var lon = 0.0
 
    override func viewDidLoad() {
        super.viewDidLoad()
        textField_search.delegate = self
        manager.searchDelegate = self
        view_desc.isHidden = !origin.value
        mapView.delegate = self
    }
 
    //MARK: - Rx
    override func bindRx() {
        super.bindRx()
        textField_search.placeholder = origin.value == true ? "您从哪里上车" : "您要去哪儿"
       if orderType.value == .acrossLogistics {
            self.textField_search.placeholder = "在哪里取货?"
        }else if orderType.value == .cityLogistics{
            self.textField_search.placeholder = "在哪里取货?"
        }
 
        textField_search.rx.controlEvent(.editingDidEnd).subscribe(onNext: {[unowned self] _ in
            if let string = self.textField_search.text {
                if string != ""{
                    DispatchQueue.main.async {
                        self.manager.startPoiSearch(keyword: string, city: self.cityName)
                    }
                }else{
                    DispatchQueue.main.async {
                        self.clearBtn.isHidden = false
                        self.clearHeiCons.constant = 40
                        self.items.removeAll()
                        self.items = AddressModelInfoSave.readFile()
                        self.tableView.reloadData()
                    }
                }
            }
        }).disposed(by: disposeBag)
 
        YYMapViewManager.share.animation.delegate(on: self) { (self, _) in
            DispatchQueue.main.async {
                self.label_desc.text = "在这里上车"
                self.centerView.popJumpAnimation()
            }
        }
 
 
        YYMapViewManager.share.mapWillMoveByUser.delegate(on: self) { (self, _) in
            DispatchQueue.main.async {
                self.label_desc.text = "拖到路边,接驾更快"
            }
        }
 
        YYMapViewManager.share.originInfo.subscribe(onNext: {[unowned self]info in
            if let name = info?.name{
//                self.textField_search.text = name
//                self.manager.startPoiSearch(keyword: name, city: self.cityName)
            }
        }).disposed(by: rx.disposeBag)
    }
 
    //MARK: - UI
    override func setupViews() {
        super.setupViews()
        view.backgroundColor = #colorLiteral(red: 0.9647058824, green: 0.9647058824, blue: 0.9647058824, alpha: 1)
        tableView.backgroundColor = #colorLiteral(red: 0.9647058824, green: 0.9647058824, blue: 0.9647058824, alpha: 1)
        tableView.separatorStyle = .none
        navigationItem.title = origin.value == true ? "更改出发地" : "设置目的地"
 
        button_city.imagePosition = .right
        mapBgView.addSubview(mapView)
        mapBgView.addSubview(view_desc)
        mapBgView.addSubview(centerView)
 
        mapView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
 
        centerView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-13)
        }
 
        view_desc.snp.makeConstraints { (make) in
            make.centerX.equalTo(centerView.snp.centerX)
            make.bottom.equalTo(centerView.snp.top).offset(-15)
        }
 
 
        if orderType.value == .travel {
            navigationItem.title = origin.value == true ? "设置上车点" : "设置下车点"
            if lat == 0.0 || lon == 0.0 {
                button_city.setTitle(YYLocationManager.shared.currentCity, for: .normal)
                self.cityName = YYLocationManager.shared.currentCity
            }else {
                YYLocationManager.shared.searchPOIAroundBy(latitude: CGFloat(lat), longitude: CGFloat(lon))
                YYLocationManager.shared.searchPOISubject.subscribe(onNext: {[unowned  self] (status) in
                    switch status{
                    case .loading:
                        break
                    case .success(let model):
                        guard let data:[YYPOIModel] = model as? [YYPOIModel] else { return  }
                        self.button_city.setTitle(data.first?.city ?? "", for: .normal)
                        self.cityName = data.first?.city ?? ""
                        break
                    case .error( _):
                        break
                    }
                })
                .disposed(by: disposeBag)
            }
        }else if orderType.value == .acrossLogistics {
            // 跨城
            navigationItem.title = "设置领取点"
            self.textField_search.placeholder = "在哪里取货?"
            button_city.setTitle(YYLocationManager.shared.currentCity, for: .normal)
            self.cityName = YYLocationManager.shared.currentCity
        }else if orderType.value == .cityLogistics{
            // 同城
            button_city.setTitle(YYLocationManager.shared.currentCity, for: .normal)
            self.cityName = YYLocationManager.shared.currentCity
            navigationItem.title = "设置领取点"
            self.textField_search.placeholder = "在哪里取货?"
//            button_city.isUserInteractionEnabled = false
        }
        else{
            button_city.setTitle(YYLocationManager.shared.currentCity, for: .normal)
            self.cityName = YYLocationManager.shared.currentCity
        }
 
        manager.searchDelegate = self
        tableView.register(cellName: "SearchAddressCell", identifier: "item")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .singleLine
        tableView.separatorColor = #colorLiteral(red: 0.1529411765, green: 0.1568627451, blue: 0.1764705882, alpha: 0.06)
 
    }
 
    /// 切换城市
    ///
    /// - Parameter sender: 按钮
    @IBAction func selectCity(_ sender: UIButton) {
        selectCity()
    }
 
    /// 选择城市
    func selectCity() {
        let vc = SwitchCityVC()
        vc.global = false
        vc.refresh = { [unowned self] model,global in
            guard let name = model?.name else {
                return
            }
 
            if name == YYMapViewManager.share.currentCity.value{
                self.mapView.setCenter(self.mapView.userLocation.coordinate, animated: true)
            }else{
                let coordinate = CLLocationCoordinate2D(latitude: model!.lat, longitude: model!.lon)
                self.mapView.setCenter(coordinate, animated: true)
            }
 
            self.cityName = name
            self.button_city.setTitle(name, for: .normal)
            self.manager.startPoiSearch(keyword: self.textField_search.text ?? "", city: self.cityName)
        }
        yy_push(vc: vc)
    }
 
    /// 返回
    @objc func cancelActtion() {
        yy_pop()
    }
 
    @IBAction func clear(_ sender: UIButton) {
        alert(popup: .double, title: nil, text: "确定清除历史吗?", submitTitle: nil, cancelTitle: nil, submitClick: { [unowned self] in
            AddressModelInfoSave.clearFile()
            self.items = AddressModelInfoSave.readFile()
            self.tableView.reloadData()
        }) {}
    }
 
}
 
// MARK: - LocationSearchDelegate
extension SearchAddressDragVC:LocationSearchDelegate{
    func onSearchDone(address: [AddressModelInfo]) {
//        if textField_search.text != ""{
        self.clearBtn.isHidden = true
        self.clearHeiCons.constant = 0
            DispatchQueue.main.async {
                self.items.removeAll()
                self.items = address
                self.tableView.reloadData()
            }
//        }
    }
}
 
 
// MARK: - UITableViewDelegate
extension SearchAddressDragVC:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let model = items[indexPath.row]
        AddressModelInfoSave.saveFile(model: model)
        yy_pop()
        complete.call((model,origin.value))
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
}
 
// MARK: - UITableViewDataSource
extension SearchAddressDragVC:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "item", for: indexPath) as! SearchAddressCell
        cell.configure(model: items[indexPath.row])
        cell.separatorInset = UIEdgeInsets.init(top: 0, left: 14, bottom: 0, right: 14)
        return cell
    }
}
 
extension SearchAddressDragVC:UITextFieldDelegate{
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
}
 
    //MARK: - MAMapViewDelegate
extension SearchAddressDragVC: MAMapViewDelegate
{
//    func mapView(_ mapView: MAMapView!, regionDidChangeAnimated animated: Bool) {
//
//    }
 
    func mapView(_ mapView: MAMapView!, mapWillMoveByUser wasUserAction: Bool) {
 
    }
 
    func mapView(_ mapView: MAMapView!, mapDidMoveByUser wasUserAction: Bool) {
        guard wasUserAction else {return}
        let coordinate = mapView.centerCoordinate
        manager.startReverseSearch(lat: coordinate.latitude, lng: coordinate.longitude)
    }
}