宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-04-06 a1ae6802080a22e6e6ce6d0935e95facb1daca5c
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
//
//  SwitchCityVC.swift
//  OKProject
//
//  Created by alvin_y on 2020/5/28.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
import MJRefresh
import RxCocoa
import RxSwift
import SwifterSwift
 
/// 切换城市
class SwitchCityVC: YYTableViewController {
    
    /// 搜索容器
    private let search_container: UIView = {
       let view = UIView()
        view.backgroundColor = UIColor.color(light: UIColor.color(hexString: "#FFFFFF"), dark: UIColor.color(hexString: "#FFFFFF"))
        view.layer.borderColor = UIColor.color(light: UIColor.color(hexString: "#F3F4F5"), dark: UIColor.color(hexString: "#F3F4F5")).cgColor
        view.layer.borderWidth = 1
        return view
    }()
    
    /// 搜索框
    private let textField_sreach: YYTextField = {
       let textField = YYTextField()
        textField.placeholder = "请输入城市名字"
        textField.maximumOffset = 9
        textField.font = UIFont.systemFont(ofSize: 13)
        textField.backgroundColor = UIColor.color(light: UIColor.color(hexString: "#F6F6F7"), dark: UIColor.color(hexString: "#F6F6F7"))
        textField.layer.cornerRadius = 4
        textField.layer.masksToBounds = true
        textField.tintColor = ThemeColor
        textField.returnKeyType = .done
        return textField
    }()
    
    /// 取消按钮
    private let button_cancel: YYButton = {
       let button = YYButton()
        button.setTitle("取消", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.color(light: UIColor.color(hexString: "#666666"), dark: UIColor.color(hexString: "#666666")), for: .normal)
        return button
    }()
    
    
    /// 当前城市容器
    private let view_currentCity: YYView = {
       let view = YYView()
        view.backgroundColor = UIColor.color(light: UIColor.color(hexString: "#F3F4F5"), dark: UIColor.color(hexString: "#F3F4F5"))
        return view
    }()
    
    /// 当前城市
    private let label_currentCity: YYLabel = {
       let label = YYLabel()
        label.textColor = UIColor.color(light: UIColor.color(hexString: "#333333"), dark: UIColor.color(hexString: "#333333"))
        label.font = UIFont.systemFont(ofSize: 14)
        label.text = "当前选择:\(YYLocationManager.shared.currentCity)"
        return label
    }()
    
    /// 是否是全局
    var global = true
    
    /// 临时存贮数据
    var tempData: [SwitchCityModel] = []
    
    private let viewModel = SwitchCityViewModel()
    
    /// 侧边标题
    private var sectionTitles: [String] = []
    
    /// 刷新回调
    var refresh:((_ model: SwitchCityModel?,_ global: Bool) ->Void)?
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        yy_isHiddenNavBarLine = true
    }
    
    //MARK: - UI
    override func setupViews() {
        super.setupViews()
        navigationItem.title = "切换城市"
        view.addSubview(search_container)
        view.addSubview(view_currentCity)
        view_currentCity.addSubview(label_currentCity)
        search_container.addSubview(button_cancel)
        search_container.addSubview(textField_sreach)
        tableView.sectionIndexColor = ThemeColor
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorColor = UIColor.color(hexString: "#E5E5E5")
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "item")
        tableView.register(headerFooterViewClassWith: YYTitleWithButtonHeaderView.self)
        tableView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(onRefresh))
        tableView.mj_header?.beginRefreshing()
    }
    
    /// 刷新
    @objc func onRefresh(){
        viewModel.queryOpenCity()
    }
 
    
    //MARK: - Layouts
    override func defineLayouts() {
        super.defineLayouts()
        search_container.snp.makeConstraints { (make) in
            if #available(iOS 11.0, *) {
                make.top.equalTo(view.safeAreaLayoutGuide)
            } else {
                make.top.equalToSuperview()
            }
            make.left.right.equalToSuperview()
            make.height.equalTo(45)
        }
        
        view_currentCity.snp.makeConstraints { (make) in
            make.top.equalTo(search_container.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(39)
        }
        
        label_currentCity.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(14)
            make.right.equalToSuperview().offset(-14)
        }
        
        textField_sreach.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-60)
            make.height.equalTo(29)
        }
        
        button_cancel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-16)
        }
        
        tableView.snp.remakeConstraints { (make) in
            make.top.equalTo(view_currentCity.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }
 
    //MARK: - Rx
    override func bindRx() {
        super.bindRx()
        button_cancel.rx.tap.subscribe(onNext: {[unowned self] (_) in
            self.yy_pop()
        }).disposed(by: disposeBag)
        
        textField_sreach.rx.text.subscribe(onNext: { (text) in
            if let string = text,string != ""{
                let model = self.tempData.filter { (model) -> Bool in
                    return model.name.contains(string)
                }
                let citySections: [SwitchCitySection] = Dictionary(grouping: model, by: { self.getFirstLetter(from: self.getPinYin(from: $0.name)) }).reduce([], { (result, arg1) -> [SwitchCitySection] in
                    let (key, value) = arg1
                    return result + [SwitchCitySection(letter: key, items: value)]
                }).sorted(by: \SwitchCitySection.letter)
                self.viewModel.cities.accept(citySections)
                self.sectionTitles = citySections.map{$0.letter}
                self.tableView.reloadData()
            }else{
                let model = self.tempData
                let citySections: [SwitchCitySection] = Dictionary(grouping: model, by: { self.getFirstLetter(from: self.getPinYin(from: $0.name)) }).reduce([], { (result, arg1) -> [SwitchCitySection] in
                    let (key, value) = arg1
                    return result + [SwitchCitySection(letter: key, items: value)]
                }).sorted(by: \SwitchCitySection.letter)
                self.viewModel.cities.accept(citySections)
                self.sectionTitles = citySections.map{$0.letter}
                self.tableView.reloadData()
            }
        }).disposed(by: disposeBag)
        
        viewModel.requestSubject
            .observeOn(MainScheduler.instance)
            .subscribe(onNext: { [unowned self](status) in
                self.tableView.mj_header?.endRefreshing()
                switch status{
                case .loading:
                    self.show()
                    break
                case .success(let model):
                    self.hide()
                    guard let model = (model as? [SwitchCityModel])?.filter({ (model) -> Bool in
                        return model.name != ""
                    }) else {return}
                    self.tempData = model
                    let citySections: [SwitchCitySection] = Dictionary(grouping: model, by: { self.getFirstLetter(from: self.getPinYin(from: $0.name)) }).reduce([], { (result, arg1) -> [SwitchCitySection] in
                        let (key, value) = arg1
                        return result + [SwitchCitySection(letter: key, items: value)]
                    }).sorted(by: \SwitchCitySection.letter)
                    self.viewModel.cities.accept(citySections)
                    self.sectionTitles = citySections.map{$0.letter}
                    self.tableView.reloadData()
                    break
                case .error(let error):
                    self.hide()
                    alert(text: error.localizedDescription)
                    break
                }
        }).disposed(by: disposeBag)
    }
    
    func getPinYin(from aString: String) -> String {
        //转变成可变字符串
        let mutableString = NSMutableString.init(string: aString)
        
        //将中文转换成带声调的拼音
        CFStringTransform(mutableString as CFMutableString, nil, kCFStringTransformToLatin, false)
        
        //去掉声调
        let pinyinString = mutableString.folding(options: String.CompareOptions.diacriticInsensitive, locale: NSLocale.current)
        
        return pinyinString
    }
    
    func getFirstLetter(from aString: String) -> String {
        //截取大写首字母
        let firstString = aString.prefix(1).uppercased()
        
        //判断首字母是否为大写
        let regexA = "^[A-Z]$"
        let predA = NSPredicate.init(format: "SELF MATCHES %@", regexA)
        return predA.evaluate(with: firstString) ? firstString : "#"
    }
}
 
 
// MARK: - UITableViewDelegate
extension SwitchCityVC:UITableViewDelegate{
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }
    
    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return sectionTitles
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let model = self.viewModel.cities.value[indexPath.section].items[indexPath.row]
        if global{
            YYLocationManager.shared.currentCity = model.name
            YYLocationManager.shared.currentCityCode = model.content
            YYLocationManager.shared.id = model.id
            YYMapViewManager.share.setCenter(center: CLLocationCoordinate2D.init(latitude: CLLocationDegrees(CGFloat(model.lat)), longitude: CLLocationDegrees(CGFloat(model.lon))))
            yy_pop()
            NotificationCenter.default.post(Notification.init(name: Notification.Name(rawValue: SwitchCityNotification)))
        }else{
            yy_pop()
            self.refresh?(model,global)
        }
        
    }
}
 
// MARK: - UITableViewDelegate
extension SwitchCityVC:UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.viewModel.cities.value.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.cities.value[section].items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "item", for: indexPath)
        cell.textLabel?.text = viewModel.cities.value[indexPath.section].items[indexPath.row].name
        cell.textLabel?.font = UIFont.systemFont(ofSize: 14)
        cell.textLabel?.textColor = UIColor.color(light: UIColor.color(hexString: "#333333"), dark: UIColor.color(hexString: "#333333"))
        cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = tableView.dequeueReusableHeaderFooterView(withClass: YYTitleWithButtonHeaderView.self)
        view.label_title.text = viewModel.cities.value[section].letter
        return view
    }
}