//
|
// YardDetailDateTimeVC.swift
|
// WanPai
|
//
|
// Created by 无故事王国 on 2023/6/19.
|
//
|
|
import UIKit
|
import JQTools
|
import RxRelay
|
import RxSwift
|
|
|
let UpdateDetailDate_Noti = Notification.Name.init("UpdateDetailDate_Noti")
|
|
class YardDetailDateTimeVC: BaseVC {
|
|
private var collectionView:UICollectionView!
|
private var clouse:((Double,Int)->Void)?
|
private(set) var date:Date!
|
private var id:Int!
|
private var currentPage:Int!
|
private var items = [SiteDetailDateTimeModel]()
|
|
var drop1:Int?
|
var drop2:Int?
|
var selects = [SiteDetailDateTimeModel]()
|
private let cellW = (JQ_ScreenW - 32.0) / 3.0
|
private let cellH = (JQ_ScreenW - 32.0) / 3.0 * 0.3167
|
|
private var halfName:String?
|
private var siteName:String?
|
|
/// 缓存高度
|
private(set) var innerHeight:Double = 0
|
|
required init(id:Int,date:Date,currentPage:Int,halfName:String?,siteName:String?) {
|
super.init(nibName: nil, bundle: nil)
|
self.date = date
|
self.id = id
|
self.currentPage = currentPage
|
self.halfName = halfName
|
self.siteName = siteName
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
|
let layout = UICollectionViewFlowLayout()
|
layout.itemSize = CGSize(width: cellW, height: cellH)
|
layout.minimumLineSpacing = 1
|
layout.minimumInteritemSpacing = 1
|
collectionView = UICollectionView(frame: .zero,collectionViewLayout: layout)
|
collectionView.delegate = self
|
collectionView.dataSource = self
|
collectionView.isScrollEnabled = false
|
collectionView.register(UINib(nibName: "CalendarDateTimeTCell", bundle: nil), forCellWithReuseIdentifier: "_CalendarDateTimeTCell")
|
view.addSubview(collectionView)
|
collectionView.backgroundColor = .white
|
collectionView.snp.makeConstraints { make in
|
make.edges.equalToSuperview()
|
}
|
|
querySiteTimes(halfName: halfName, siteName: siteName)
|
}
|
|
override func setRx() {
|
NotificationCenter.default.rx.notification(UpdateDetailDate_Noti, object: nil).take(until: self.rx.deallocated).subscribe(onNext: {[weak self] data in
|
guard let weakSelf = self else { return }
|
self?.clearALL()
|
self?.querySiteTimes(halfName: weakSelf.halfName, siteName: weakSelf.siteName)
|
}).disposed(by: disposeBag)
|
}
|
|
//清除水
|
public func clearALL(){
|
drop1 = nil
|
drop2 = nil
|
selects.removeAll()
|
var val = YardDetailDateManager.shared().dates.value
|
val.removeAll()
|
YardDetailDateManager.shared().dates.accept(val)
|
|
collectionView.reloadData()
|
}
|
|
public func querySiteTimes(halfName:String?,siteName:String?){
|
self.halfName = halfName
|
self.siteName = siteName
|
Services.querySiteTimes(id: id, day: date.string(withFormat: "yyyy-MM-dd"),halfName: halfName,siteName:siteName).subscribe(onNext: { [weak self] data in
|
if let models = data.data{
|
guard let weakSelf = self else { return }
|
weakSelf.items = models
|
for item in weakSelf.items {
|
if item.selectable == 1{
|
let d = weakSelf.date.jq_format("yyyy-MM-dd ") + item.time.components(separatedBy: "-").last!
|
let interval = Date.jq_StringToTimeInterval(d, "yyyy-MM-dd HH:mm")
|
let canSelect = Date().timeIntervalSince1970 < interval
|
item.selectable = canSelect ? 1:0
|
}
|
}
|
weakSelf.collectionView.reloadData()
|
weakSelf.innerHeight = ceil(Double(models.count) / 3.0) * weakSelf.cellH + floor(Double(models.count) / 3.0) * 1.0
|
weakSelf.clouse?(weakSelf.innerHeight,weakSelf.currentPage)
|
}
|
}).disposed(by: disposeBag)
|
}
|
|
|
/// 获得请求后,更新的高度,来使父更新并缓存,滑动时获取
|
func updateInnerHeight(clouse:@escaping (Double,Int)->Void){
|
self.clouse = clouse
|
}
|
|
private func dropDate(_ item:Int){
|
var tempSelects = [SiteDetailDateTimeModel]()
|
///临时变量,用于恢复
|
let tempDrop1 = drop1
|
let tempDrop2 = drop2
|
|
|
if drop1 == nil{
|
drop1 = item
|
}else if drop1 == item && drop2 == nil{
|
drop1 = nil
|
}else if drop1 == item && drop2 != nil {
|
drop1 = drop2;drop2 = nil
|
}else{
|
drop2 = item
|
}
|
|
if let f = drop1{
|
tempSelects.append(items[f])
|
}
|
|
if let f = drop2{
|
tempSelects.append(items[f])
|
}
|
|
|
if let f = drop1,let l = drop2{
|
let first = min(f, l)
|
let last = max(f, l)
|
tempSelects = Array(items[first...last])
|
}
|
|
if tempSelects.filter({$0.selectable == 0}).count > 0{
|
alertError(msg: "不能跨时间段选择")
|
drop1 = tempDrop1
|
drop2 = tempDrop2
|
return
|
}
|
|
selects = tempSelects
|
YardDetailDateManager.shared().dates.accept(["\(self.date.jq_format("yyyy-MM-dd"))":selects])
|
collectionView.reloadData()
|
|
}
|
}
|
|
extension YardDetailDateTimeVC:UICollectionViewDelegate{
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
let keys = YardDetailDateManager.shared().dates.value.jq_sortKey().filter({$0 != date.jq_format("yyyy-MM-dd")})
|
var hasOther:Bool = false
|
for key in keys{
|
if (YardDetailDateManager.shared().dates.value[key]?.count ?? 0) > 0{
|
hasOther = true;break
|
}
|
}
|
|
guard !hasOther else {
|
alertError(msg: "不能跨天选择");return
|
}
|
|
let m = items[indexPath.row]
|
guard m.selectable == 1 else {return}
|
dropDate(indexPath.row)
|
}
|
}
|
|
extension YardDetailDateTimeVC:UICollectionViewDataSource{
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_CalendarDateTimeTCell", for: indexPath) as! CalendarDateTimeTCell
|
let m = items[indexPath.row]
|
cell.label_content.text = m.time
|
// let d = date.jq_format("yyyy-MM-dd ") + m.time.components(separatedBy: "-").last!
|
// let interval = Date.jq_StringToTimeInterval(d, "yyyy-MM-dd HH:mm")
|
//
|
// let canSelect = Date().timeIntervalSince1970 < interval
|
|
if m.selectable == 1{
|
cell.contentView.backgroundColor = UIColor.white
|
cell.label_content.textColor = UIColor(hexStr: "#323232")
|
}else{
|
cell.contentView.backgroundColor = UIColor(hexStr: "#EC0808").withAlphaComponent(0.16)
|
cell.label_content.textColor = UIColor(hexStr: "#EC0808")
|
}
|
|
// if m.selectable == 0{
|
// cell.contentView.backgroundColor = UIColor(hexStr: "#EC0808").withAlphaComponent(0.16)
|
// cell.label_content.textColor = UIColor(hexStr: "#EC0808")
|
// }else if m.selectable == 1{
|
// cell.contentView.backgroundColor = UIColor.white
|
// cell.label_content.textColor = UIColor(hexStr: "#323232")
|
// }
|
|
if indexPath.row == drop2 || indexPath.row == drop1{
|
cell.contentView.backgroundColor = UIColor(hexStr: "#256D0F").withAlphaComponent(0.24)
|
cell.label_content.textColor = UIColor(hexStr: "#256D0F")
|
}
|
|
if selects.contains(where: {$0 == m}){
|
cell.contentView.backgroundColor = UIColor(hexStr: "#256D0F").withAlphaComponent(0.24)
|
cell.label_content.textColor = UIColor(hexStr: "#256D0F")
|
}
|
|
|
|
|
|
return cell
|
}
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
return items.count
|
}
|
}
|