宽窄优行-由【嘉易行】项目成品而来
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
//
//  SearchAddressVC.swift
//  OKProject
//
//  Created by alvin_y on 2020/6/4.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
import RxCocoa
import RxSwift
import IQKeyboardManager
 
class SearchAddressVC: YYViewController {
    
    /// 列表底部的View
    @IBOutlet weak var bottomView: UIView!
    
    /// 列表
    @IBOutlet weak var tableView: UITableView!
    
    /// 城市按钮
    @IBOutlet weak var button_city: UIButton!
    
    /// 搜索框
    @IBOutlet weak var textField_search: UITextField!
    
    /// 搜索管理类
    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()
        // Do any additional setup after loading the view.
    }
 
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        textField_search.becomeFirstResponder()
    }
 
    //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.text
            .distinctUntilChanged()
            .subscribe(onNext: {[unowned self] (s) in
                if let string = s {
                    if string != ""{
                        DispatchQueue.main.async {
                            self.manager.startPoiSearch(keyword: string, city: self.cityName)
                            self.bottomView.isHidden = true
                        }
                    }else{
                        DispatchQueue.main.async {
                            self.bottomView.isHidden = false
                            self.items.removeAll()
                            self.items = AddressModelInfoSave.readFile()
                            self.bottomView.isHidden = self.items.count == 0
                            self.tableView.reloadData()
                        }
                    }
                }
            }).disposed(by: 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)
        navigationItem.title = origin.value == true ? "更改出发地" : "设置目的地"
        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)
        
    }
    
    /// 选择城市
    func selectCity() {
        let vc = SwitchCityVC()
        vc.global = false
        vc.refresh = { [unowned self] model,global in
            guard let name = model?.name else {
                return
            }
            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()
    }
    
    /// 清除历史
    ///
    /// - Parameter sender: 按钮
    @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()
        }) {}
    }
    
    /// 切换城市
    ///
    /// - Parameter sender: 按钮
    @IBAction func selectCity(_ sender: UIButton) {
        selectCity()
    }
    
    /// 取消
    ///
    /// - Parameter sender: 按钮
    @IBAction func cancel(_ sender: UIButton) {
        yy_pop()
    }
    
}
// MARK: - LocationSearchDelegate
extension SearchAddressVC:LocationSearchDelegate{
    func onSearchDone(address: [AddressModelInfo]) {
        if textField_search.text != ""{
            DispatchQueue.main.async {
                self.items.removeAll()
                self.items = address
                self.tableView.reloadData()
            }
        }
    }
}
 
 
// MARK: - UITableViewDelegate
extension SearchAddressVC: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 SearchAddressVC: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
    }
    
}