宽窄优行-由【嘉易行】项目成品而来
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
//
//  YYCarTypeView.swift
//  OKProject
//
//  Created by alvin_y on 2020/9/3.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
import RxSwift
import RxCocoa
 
class YYCarTypeView: UIView {
 
    let view_refreshing = UIView()
    let indicator = UIActivityIndicatorView(style: .gray)
    let label_refreshing = UILabel()
    
    let view_error = UIView()
    let label_error = UILabel()
    let button_error = UIButton(type: .system)
    
    let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        let c = UICollectionView(frame: .zero, collectionViewLayout: layout)
        return c
    }()
    
    var dataSource: [YYCarTypeModel] = [] {
        didSet {
            if selectedModel == nil && dataSource.first != nil{
                selectedModel = dataSource.first
                onCarTypePressed.onNext(selectedModel!)
            }
            collectionView.reloadData()
        }
    }
    
    /// 当前状态
    var style: Style = .none {
        didSet {
            switch style {
            case .none:
                view_refreshing.isHidden = true
                indicator.stopAnimating()
                view_error.isHidden = true
                collectionView.isHidden = false
            case .refreshing:
                view_refreshing.isHidden = false
                indicator.startAnimating()
                view_error.isHidden = true
                collectionView.isHidden = true
            case .nothing:
                view_refreshing.isHidden = true
                indicator.stopAnimating()
                view_error.isHidden = false
                label_error.text = "附近暂无车辆"
                collectionView.isHidden = true
            case .error:
                view_refreshing.isHidden = true
                indicator.stopAnimating()
                view_error.isHidden = false
                label_error.text = "获取车辆类型失败"
                collectionView.isHidden = true
            }
        }
    }
    
    var selectedModel: YYCarTypeModel?
    
    /// 选中车型回调
    let onCarTypePressed = PublishSubject<YYCarTypeModel>()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupViews()
        defineLayouts()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupViews()
        defineLayouts()
    }
    
    func setupViews() {
        
        collectionView.backgroundColor = .clear
        collectionView.register(nibWithCellClass: YYCarTypeCell.self)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        addSubview(collectionView)
        
        view_refreshing.backgroundColor = .white
        addSubview(view_refreshing)
        
        view_refreshing.addSubview(indicator)
        
        label_refreshing.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        label_refreshing.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.6)
        label_refreshing.text = "正在获取车辆类型"
        view_refreshing.addSubview(label_refreshing)
        
        view_error.backgroundColor = .white
        addSubview(view_error)
        
        label_error.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        label_error.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.6)
        label_error.text = "附近暂无车辆"
        view_error.addSubview(label_error)
        
        button_error.setTitle("重新加载", for: .normal)
        button_error.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.6), for: .normal)
        button_error.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        button_error.borderColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.6)
        button_error.borderWidth = 1
        button_error.cornerRadius = 4
        view_error.addSubview(button_error)
        
        
    }
    
    func defineLayouts() {
        
        view_refreshing.snp.makeConstraints { (make) in
            make.edges.equalTo(self).inset(UIEdgeInsets(top: 0, left: 0, bottom: 2, right: 0))
        }
        
        indicator.snp.makeConstraints { (make) in
            make.centerX.equalTo(view_refreshing)
            make.centerY.equalTo(view_refreshing).offset(-10)
        }
        
        label_refreshing.snp.makeConstraints { (make) in
            make.top.equalTo(indicator.snp.bottom).offset(10)
            make.centerX.equalTo(indicator)
        }
        
        view_error.snp.makeConstraints { (make) in
            make.edges.equalTo(self).inset(UIEdgeInsets(top: 0, left: 0, bottom: 2, right: 0))
        }
        
        label_error.snp.makeConstraints { (make) in
            make.centerX.equalTo(view_error)
            make.centerY.equalTo(view_error).offset(-15)
        }
        
        button_error.snp.makeConstraints { (make) in
            make.top.equalTo(label_error.snp.bottom).offset(10)
            make.centerX.equalTo(indicator)
            make.width.equalTo(60)
            make.height.equalTo(30)
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalTo(self).inset(UIEdgeInsets(top: 0, left: 0, bottom: 1, right: 0))
        }
    }
}
extension YYCarTypeView: UICollectionViewDataSource {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let model = dataSource[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withClass: YYCarTypeCell.self, for: indexPath)
        cell.configure(for: model)
        if model.id == selectedModel?.id {
            cell.view_container.backgroundColor = .white
            cell.view_container.image = #imageLiteral(resourceName: "carType")
            cell.label_carType.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.8)
            cell.label_price.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.8)
        } else {
            cell.view_container.image = nil
            cell.view_container.backgroundColor = .white
            cell.label_carType.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4)
            cell.label_price.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4)
        }
        return cell
    }
}
 
extension YYCarTypeView: UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = collectionView.width / 3
        let height = collectionView.height
        return CGSize(width: width, height: height)
    }
}
 
extension YYCarTypeView: UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let model = dataSource[indexPath.row]
        onCarTypePressed.onNext(model)
        self.selectedModel = model
        collectionView.reloadData()
    }
}
 
extension YYCarTypeView {
    
    enum Style {
        case none
        case refreshing
        case nothing
        case error
    }
}