Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I am trying to do date range using FScalendar but this is not working properly with my code. #1451

Open
ratneshrajpathak opened this issue Dec 26, 2024 · 0 comments

Comments

@ratneshrajpathak
Copy link

vFp2U

import FSCalendar

class TransitsCalenderVC: UIViewController, FSCalendarDelegate, FSCalendarDataSource, FSCalendarDelegateAppearance {

@IBOutlet weak var calendarFS: FSCalendar!

var date1: Date?
var date2: Date?

// Initialize the Gregorian calendar and date formatter
let gregorian = Calendar(identifier: .gregorian)
let formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
return formatter
}()

override func viewDidLoad() {
super.viewDidLoad()

calendarFS.delegate = self
calendarFS.dataSource = self
calendarFS.allowsMultipleSelection = true
calendarFS.scrollDirection = .vertical
calendarFS.scope = .month

// Register and set custom cell class
calendarFS.register(DIYCalendarCell.self, forCellReuseIdentifier: "cell")
}

// MARK: - FSCalendar Delegate and DataSource

func calendarCurrentPageDidChange(_ calendar: FSCalendar) {
}

func calendar(_ calendar: FSCalendar, cellFor date: Date, at position: FSCalendarMonthPosition) -> FSCalendarCell {
let cell = calendar.dequeueReusableCell(withIdentifier: "cell", for: date, at: position)
return cell
}

func calendar(_ calendar: FSCalendar, willDisplay cell: FSCalendarCell, for date: Date, at position: FSCalendarMonthPosition) {
self.configureCell(cell, for: date, at: position)
}

func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
return monthPosition == FSCalendarMonthPosition.current
}

func calendar(_ calendar: FSCalendar, shouldDeselect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
return false
}

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
if calendar.swipeToChooseGesture.state == .changed {
// If the selection is caused by swipe gestures
if !(date1 != nil) {
date1 = date
}
else {
if (date2 != nil) {
calendar.deselect(date2!)
}
date2 = date
}
}
else {
if (date2 != nil) {
calendar.deselect(date1!)
calendar.deselect(date2!)
date1 = date
date2 = nil
}
else if !(date1 != nil) {
date1 = date
}
else {
date2 = date
}
}
configureVisibleCells()
}

func calendar(_ calendar: FSCalendar, didDeselect date: Date) {
print("did deselect date (self.formatter.string(from: date))")
configureVisibleCells()
}

func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventDefaultColorsFor date: Date) -> [UIColor]? {
if self.gregorian.isDateInToday(date) {
return [UIColor.orange]
}
return [appearance.eventDefaultColor]
}

private func configureVisibleCells() {
self.calendarFS.visibleCells().forEach { (cell) in
let date = self.calendarFS.date(for: cell)
let position = self.calendarFS.monthPosition(for: cell)
self.configureCell(cell, for: date, at: position)
}
}

func configureCell(_ cell: FSCalendarCell?, for date: Date?, at position: FSCalendarMonthPosition) {
let rangeCell = cell as? DIYCalendarCell
if position != FSCalendarMonthPosition.current {
rangeCell?.middleLayer.isHidden = true
rangeCell?.selectionLayer.isHidden = true
return
}
if (date1 != nil) && (date2 != nil) {
// The date is in the middle of the range
let isMiddle: Bool = date?.compare(date1!) != date?.compare(date2!)
rangeCell?.middleLayer.isHidden = !isMiddle
}
else {
rangeCell?.middleLayer.isHidden = true
}

var isSelected = false

if (date1 != nil) {
    isSelected = gregorian.isDate(date!, inSameDayAs: date1!)
}
else {
    isSelected = false
}

if (date2 != nil) {
    isSelected = gregorian.isDate(date!, inSameDayAs: date2!)
}
else {
    isSelected = false
}

rangeCell?.selectionLayer.isHidden = !isSelected

}
}
// DIY Calendar Cell ========>

enum SelectionType: Int {
case none
case single
case leftBorder
case middle
case rightBorder
}

class DIYCalendarCell: FSCalendarCell {
weak var selectionLayer: CAShapeLayer!
weak var middleLayer: CAShapeLayer!

var selectionType: SelectionType = .none {
didSet {
setNeedsLayout()
}
}

required init!(coder aDecoder: NSCoder!) {
fatalError("init(coder:) has not been implemented")
}

override init(frame: CGRect) {
super.init(frame: frame)

let selectionLayer = CAShapeLayer()
selectionLayer.fillColor = UIColor.green.withAlphaComponent(0.5).cgColor
selectionLayer.actions = ["hidden": NSNull()]
self.contentView.layer.insertSublayer(selectionLayer, below: self.titleLabel!.layer)
self.selectionLayer = selectionLayer

let middleLayer = CAShapeLayer()
middleLayer.fillColor = UIColor.blue.withAlphaComponent(0.3).cgColor
self.contentView.layer.insertSublayer(middleLayer, below: selectionLayer)
self.middleLayer = middleLayer
}

override func layoutSubviews() {
super.layoutSubviews()

self.selectionLayer.frame = self.contentView.bounds.insetBy(dx: 5, dy: 5)
self.middleLayer.frame = self.contentView.bounds.insetBy(dx: 5, dy: 5)

let cornerRadius = self.selectionLayer.frame.height / 2

switch selectionType {
case .middle:
self.middleLayer.isHidden = false
self.selectionLayer.isHidden = true
self.middleLayer.path = UIBezierPath(rect: self.middleLayer.bounds).cgPath
case .leftBorder:
self.middleLayer.isHidden = true
self.selectionLayer.isHidden = false
self.selectionLayer.path = UIBezierPath(roundedRect: self.selectionLayer.bounds,
byRoundingCorners: [.topLeft, .bottomLeft],
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)).cgPath
case .rightBorder:
self.middleLayer.isHidden = true
self.selectionLayer.isHidden = false
self.selectionLayer.path = UIBezierPath(roundedRect: self.selectionLayer.bounds,
byRoundingCorners: [.topRight, .bottomRight],
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)).cgPath
case .single:
self.middleLayer.isHidden = true
self.selectionLayer.isHidden = false
self.selectionLayer.path = UIBezierPath(ovalIn: self.selectionLayer.bounds).cgPath
case .none:
self.middleLayer.isHidden = true
self.selectionLayer.isHidden = true
}
}
}

This code date range is not working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant