You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
235 lines
10 KiB
235 lines
10 KiB
// |
|
// DateSwitchHeaderView.swift |
|
// HPlusFit |
|
// |
|
// Created by lemo. on 2019/9/17. |
|
// Copyright © 2019 lemo. All rights reserved. |
|
// |
|
|
|
import UIKit |
|
|
|
enum HPSegControlType { |
|
case day |
|
case week |
|
case month |
|
} |
|
|
|
typealias SegControlValueChangeClosure = (HPSegControlType) -> Void |
|
typealias DateSeletedClosure = (String) -> Void |
|
class DateSwitchHeaderView: UIView { |
|
|
|
var segControl: DateSegmentView! |
|
var dateSelectedView: DateSelectView! |
|
var segControlType: HPSegControlType = .day |
|
var segControlValueChangeClosure: SegControlValueChangeClosure? |
|
var dateSelectedClosure: DateSeletedClosure? |
|
|
|
|
|
var selectedDateStr: String = "" |
|
|
|
override init(frame: CGRect) { |
|
super.init(frame: frame) |
|
let segBackView = UIView(frame: CGRect(x: 0, y: 0, width: self.width, height: kScaleHeight(45.0))) |
|
segBackView.backgroundColor = UIColor.white |
|
self.addSubview(segBackView) |
|
//创建segment组件 |
|
segControl = DateSegmentView(frame: CGRect.init(x: 0, y: kScaleHeight(15), width: kScaleWidth(258), height: kScaleWidth(30))) |
|
segControl.centerX = segBackView.center.x |
|
segControl.titles = ["日".localized, "周".localized, "月".localized]; |
|
segControl.selectedClosure = { [weak self] index in |
|
guard let `self` = self else { return } |
|
self.segControlValueChange(index: index) |
|
} |
|
segBackView.addSubview(segControl) |
|
|
|
//创建日期选择组件 |
|
dateSelectedView = DateSelectView(frame: CGRect(x: 0, y: segBackView.height, width: self.width, height: self.height - segBackView.height)) |
|
dateSelectedView.backgroundColor = UIColor.white |
|
dateSelectedView.dateStr = DateClass.todayString().pregReplace(pattern: "-", with: "/") |
|
dateSelectedView.dateSelectClosure = { [weak self] (dateStr, isforward) in |
|
guard let `self` = self else { return } |
|
self.dateSelectChange(dateStr: dateStr, isforward: isforward) |
|
} |
|
self.addSubview(dateSelectedView) |
|
} |
|
|
|
required init?(coder aDecoder: NSCoder) { |
|
fatalError("init(coder:) has not been implemented") |
|
} |
|
|
|
fileprivate func segControlValueChange(index: Int) { |
|
if index == 0 { |
|
segControlType = .day |
|
let dateFormat = DateFormatter() |
|
dateFormat.dateFormat = "yyyy/MM/dd" |
|
self.dateSelectedView.dateStr = dateFormat.string(from: Date()) |
|
}else if index == 1 { |
|
segControlType = .week |
|
self.dateSelectedView.dateStr = DateClass.todayString().weekRegion() |
|
}else { |
|
segControlType = .month |
|
let monthStr = DateClass.todayString().monthRegion().prefix(7) |
|
self.dateSelectedView.dateStr = monthStr.description |
|
} |
|
|
|
if let segControlClosure = self.segControlValueChangeClosure { |
|
segControlClosure(segControlType) |
|
} |
|
} |
|
|
|
fileprivate func dateSelectChange(dateStr: String, isforward: Bool) { |
|
|
|
if segControlType == .day { |
|
let dateString = dateStr.pregReplace(pattern: "/", with: "-") |
|
selectedDateStr = isforward ? DateClass.dateStringOffset(from: dateString, offset: 1) : DateClass.dateStringOffset(from: dateString, offset: -1) |
|
if selectedDateStr > DateClass.todayString() { |
|
SVProgressHUD.showInfo(withStatus: "该日期还没到呢!".localized) |
|
return |
|
} |
|
|
|
if selectedDateStr < "2019-01-01" { |
|
SVProgressHUD.showInfo(withStatus: "日期不能再往前了!".localized) |
|
return |
|
} |
|
self.dateSelectedView.dateStr = selectedDateStr.pregReplace(pattern: "-", with: "/") |
|
} |
|
|
|
if segControlType == .week { |
|
var dateString = "" |
|
let weekRegionComps = dateStr.components(separatedBy: "-") |
|
if weekRegionComps.count == 2 { |
|
selectedDateStr = "" |
|
if isforward { //下一周 |
|
let nextDateStr = weekRegionComps[1].pregReplace(pattern: "/", with: "-") |
|
selectedDateStr = DateClass.dateStringOffset(from: nextDateStr, offset: 1) |
|
if selectedDateStr > DateClass.todayString() { |
|
SVProgressHUD.showInfo(withStatus: "该日期还没到呢!".localized) |
|
return |
|
} |
|
}else { //前一周 |
|
let lastDateStr = weekRegionComps[0].pregReplace(pattern: "/", with: "-") |
|
selectedDateStr = DateClass.dateStringOffset(from: lastDateStr, offset: -7) |
|
if selectedDateStr < "2019-01-01" { |
|
SVProgressHUD.showInfo(withStatus: "日期不能再往前了!".localized) |
|
return |
|
} |
|
} |
|
dateString = selectedDateStr.weekRegion() |
|
} |
|
self.dateSelectedView.dateStr = dateString |
|
} |
|
|
|
if segControlType == .month { |
|
var dateString = "" |
|
selectedDateStr = "" |
|
let monthComps = dateStr.components(separatedBy: "/") |
|
let dateFormatter = DateFormatter() |
|
dateFormatter.dateFormat = "yyyy-MM-dd" |
|
if monthComps.count == 2 { |
|
let year = (monthComps[0] as NSString).integerValue |
|
let month = (monthComps[1] as NSString).integerValue |
|
if isforward { //下个月 |
|
let monthEnd = DateClass.endOfMonth(year: year, month: month) |
|
let nextDateStr = dateFormatter.string(from: monthEnd) |
|
selectedDateStr = DateClass.dateStringOffset(from: nextDateStr, offset: 1) |
|
if selectedDateStr > DateClass.todayString() { |
|
SVProgressHUD.showInfo(withStatus: "该日期还没到呢!".localized) |
|
return |
|
} |
|
}else { //上个月 |
|
let monthStart = DateClass.startOfMonth(year: year, month: month) |
|
let lastDateStr = dateFormatter.string(from: monthStart) |
|
selectedDateStr = DateClass.dateStringOffset(from: lastDateStr, offset: -1) |
|
if selectedDateStr < "2019-01-01" { |
|
SVProgressHUD.showInfo(withStatus: "日期不能再往前了!".localized) |
|
return |
|
} |
|
} |
|
dateString = selectedDateStr.monthRegion() |
|
} |
|
self.dateSelectedView.dateStr = dateString.prefix(7).description |
|
} |
|
if let dateSelectedClosure = self.dateSelectedClosure { |
|
dateSelectedClosure(selectedDateStr) |
|
} |
|
} |
|
|
|
/// 外部调用切换日期 |
|
func switchDate(isforward: Bool) { |
|
if isforward { |
|
dateSelectedView.forwardAction() |
|
return |
|
} |
|
dateSelectedView.backwardAction() |
|
} |
|
|
|
|
|
/// 是否允许向前或者向后滚动日期 |
|
/// |
|
/// - Returns: isEnable |
|
func isforwardEnable(isforward: Bool) -> Bool { |
|
let dateStr = dateSelectedView.dateStr! |
|
switch segControlType { |
|
case .day: |
|
let dateString = dateStr.pregReplace(pattern: "/", with: "-") |
|
let selectedDateStr = isforward ? DateClass.dateStringOffset(from: dateString, offset: 1) : DateClass.dateStringOffset(from: dateString, offset: -1) |
|
if selectedDateStr > DateClass.todayString() { |
|
SVProgressHUD.showInfo(withStatus: "该日期还没到呢!".localized) |
|
return false |
|
} |
|
if selectedDateStr < "2019-01-01" { |
|
SVProgressHUD.showInfo(withStatus: "日期不能再往前了!".localized) |
|
return false |
|
} |
|
return true |
|
case .week: |
|
let weekRegionComps = dateStr.components(separatedBy: "-") |
|
if weekRegionComps.count == 2 { |
|
if isforward { //下一周 |
|
let nextDateStr = weekRegionComps[1].pregReplace(pattern: "/", with: "-") |
|
let selectedDateStr = DateClass.dateStringOffset(from: nextDateStr, offset: 1) |
|
if selectedDateStr > DateClass.todayString() { |
|
SVProgressHUD.showInfo(withStatus: "该日期还没到呢!".localized) |
|
return false |
|
} |
|
}else { //前一周 |
|
let lastDateStr = weekRegionComps[0].pregReplace(pattern: "/", with: "-") |
|
let selectedDateStr = DateClass.dateStringOffset(from: lastDateStr, offset: -7) |
|
if selectedDateStr < "2019-01-01" { |
|
SVProgressHUD.showInfo(withStatus: "日期不能再往前了!".localized) |
|
return false |
|
} |
|
} |
|
} |
|
return true |
|
case .month: |
|
let monthComps = dateStr.components(separatedBy: "/") |
|
let dateFormatter = DateFormatter() |
|
dateFormatter.dateFormat = "yyyy-MM-dd" |
|
if monthComps.count == 2 { |
|
let year = (monthComps[0] as NSString).integerValue |
|
let month = (monthComps[1] as NSString).integerValue |
|
if isforward { //下个月 |
|
let monthEnd = DateClass.endOfMonth(year: year, month: month) |
|
let nextDateStr = dateFormatter.string(from: monthEnd) |
|
let selectedDateStr = DateClass.dateStringOffset(from: nextDateStr, offset: 1) |
|
if selectedDateStr > DateClass.todayString() { |
|
SVProgressHUD.showInfo(withStatus: "该日期还没到呢!".localized) |
|
return false |
|
} |
|
}else { //上个月 |
|
let monthStart = DateClass.startOfMonth(year: year, month: month) |
|
let lastDateStr = dateFormatter.string(from: monthStart) |
|
let selectedDateStr = DateClass.dateStringOffset(from: lastDateStr, offset: -1) |
|
if selectedDateStr < "2019-01-01" { |
|
SVProgressHUD.showInfo(withStatus: "日期不能再往前了!".localized) |
|
return false |
|
} |
|
} |
|
} |
|
return true |
|
} |
|
} |
|
|
|
} |
|
|
|
|