//
|
// 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
|
}
|
}
|