Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Select Range - FSCalendar in Swift?

I am trying to use FSCalendar in one of my project to selecting range of dates.

What I found is this library have Swift version however range selection version is only available with Objective-C. So what I tried to make is using bridging, however I am unable to use the RangePickerViewController in Swift.

Did anyone implemented this library for Swift for using date range? (e.x. I want to select 2 dates as range for flight app where I am select Departure & Return flight dates.)

like image 729
Fahim Parkar Avatar asked Apr 16 '18 11:04

Fahim Parkar


People also ask

How do I change the selected date color in FSCalendar in Swift?

Can anyone suggest me how change color of selected dates in FSCalendar in swift 3? import FSCalendar fileprivate weak var calendar: FSCalendar! override func viewDidLoad() { super. viewDidLoad() // In loadView or viewDidLoad let calendar = FSCalendar(frame: CGRect(x: 0, y: 0, width: 320, height: 300)) calendar.


1 Answers

Although FSCalendar does not directly supports range selection, it does allow multiple selection, which means that at some point you would be able to handle the range selection by your self.

So, in the viewDidLoad() you have to make sure that you set the calendar allowsMultipleSelection property to true:

private weak var calendar: FSCalendar!

override func viewDidLoad() {
    super.viewDidLoad()

    calendar.allowsMultipleSelection = true
}

The view controller should conforms to FSCalendarDelegate protocol for handling the logic of selecting/deselecting a range.

What we need so far is declare tow dates for the range (the staring date and the ending date):

// first date in the range
private var firstDate: Date?
// last date in the range
private var lastDate: Date?

also an array of dates to hold value dates between firstDate and lastDate:

private var datesRange: [Date]?

and then implement the didSelect date and the didDeselect date delegate methods as:

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
    // nothing selected:
    if firstDate == nil {
        firstDate = date
        datesRange = [firstDate!]

        print("datesRange contains: \(datesRange!)")

        return
    }

    // only first date is selected:
    if firstDate != nil && lastDate == nil {
        // handle the case of if the last date is less than the first date:
        if date <= firstDate! {
            calendar.deselect(firstDate!)
            firstDate = date
            datesRange = [firstDate!]

            print("datesRange contains: \(datesRange!)")

            return
        }

        let range = datesRange(from: firstDate!, to: date)

        lastDate = range.last

        for d in range {
            calendar.select(d)
        }

        datesRange = range

        print("datesRange contains: \(datesRange!)")

        return
    }

    // both are selected:
    if firstDate != nil && lastDate != nil {
        for d in calendar.selectedDates {
            calendar.deselect(d)
        }

        lastDate = nil
        firstDate = nil

        datesRange = []

        print("datesRange contains: \(datesRange!)")
    }
}

func calendar(_ calendar: FSCalendar, didDeselect date: Date, at monthPosition: FSCalendarMonthPosition) {
    // both are selected:

    // NOTE: the is a REDUANDENT CODE:
    if firstDate != nil && lastDate != nil {
        for d in calendar.selectedDates {
            calendar.deselect(d)
        }

        lastDate = nil
        firstDate = nil

        datesRange = []
        print("datesRange contains: \(datesRange!)")
    }
}

What about datesRange method?

I did not mentioned it in my answer for the purpose of making it shorter, all you have to do is to copy-paste it from this answer.

Output:

enter image description here

like image 170
Ahmad F Avatar answered Oct 20 '22 00:10

Ahmad F