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

CalendarView Selecting dates out of a month strange behavior #600

Closed
AndreiVataselu opened this issue Oct 23, 2017 · 9 comments
Closed

CalendarView Selecting dates out of a month strange behavior #600

AndreiVataselu opened this issue Oct 23, 2017 · 9 comments
Assignees

Comments

@AndreiVataselu
Copy link

AndreiVataselu commented Oct 23, 2017

Hey patchcode! I want to start off by thanking you for sharing this library with us.

I have a slightly small issue. Whenever I select a range of dates outside of my month let's say 1 Oct - 1 Nov, the weekends are not selected but if I scroll forward a few months and go back ,they select themselves. What the issue might be? I'm leaving you here a recording of the bug and my code.

https://i.imgur.com/tPbP4i1.mp4

//
//  CalendarVC.swift
//  Expense Manager
//
//  Created by Andrei Vataselu on 10/23/17.
//  Copyright © 2017 Andrei Vataselu. All rights reserved.
//

import UIKit
import JTAppleCalendar

class CalendarVC: UIViewController {

    @IBOutlet weak var calendarView: JTAppleCalendarView!
    @IBOutlet weak var monthLabel : UILabel!
    @IBOutlet weak var yearLabel : UILabel!
    
    let formatter = DateFormatter()
    var firstDate : Date?
    var endDate : Date?
    var dateToDeselect : Date?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupCalendarView()
        
        let tapMonth = UITapGestureRecognizer(target: self, action: #selector(monthTap))
        let tapYear = UITapGestureRecognizer(target: self, action: #selector(yearTap))
        
        monthLabel.addGestureRecognizer(tapMonth)
        yearLabel.addGestureRecognizer(tapYear)
        // Do any additional setup after loading the view.
    }

    @objc func monthTap (gestureRecognizer: UITapGestureRecognizer){
        print("month")
    }
    
    @objc func yearTap (gestureRecognizer: UITapGestureRecognizer){
        print("year")
    }
    
    func setupCalendarView(){
        calendarView.minimumLineSpacing = 0
        calendarView.minimumInteritemSpacing = 0
        calendarView.allowsMultipleSelection = true
        calendarView.isRangeSelectionUsed = true
        
        calendarView.scrollToDate(Date(), animateScroll: false)

        
        calendarView.visibleDates { (visibleDates) in
            self.setupCalendarViews(from: visibleDates)
       
        }
    }
    
    
    func handleCellSelected(view: JTAppleCell?, cellState: CellState) {
        guard let validCell = view as? CalendarCell else {return}
        if validCell.isSelected {
            validCell.selectedView.isHidden = false
        } else {
            validCell.selectedView.isHidden = true
        }
    }
    
    func handleCellTextColor(view: JTAppleCell?, cellState: CellState){
        guard let validCell = view as? CalendarCell else {return}
        if validCell.isSelected {
            validCell.dateLabel.textColor = UIColor.white
        } else {
            if cellState.dateBelongsTo == .thisMonth {
                validCell.dateLabel.textColor = UIColor.black
            } else {
                validCell.dateLabel.textColor = UIColor.lightGray
            }
        }
    }
    
    func setupCalendarViews(from visibleDates: DateSegmentInfo){
        let date = visibleDates.monthDates.first!.date
        self.formatter.dateFormat = "MMMM"
        self.monthLabel.text = self.formatter.string(from: date)
        
        self.formatter.dateFormat = "yyyy"
        self.yearLabel.text = self.formatter.string(from: date)
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func backBtnPressed(_ sender: Any) {
        print("SENDER: CalendarVC")
        dismissViewController()
    }
    
}

extension CalendarVC: JTAppleCalendarViewDataSource {
    func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {

        handleCellSelected(view: cell, cellState: cellState)
        handleCellTextColor(view: cell, cellState: cellState)
    
    }
    
    func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
        formatter.dateFormat = "yyyy MM dd"
        formatter.timeZone = Calendar.current.timeZone
        formatter.locale = Calendar.current.locale
        
        let startDate = formatter.date(from: "2017 01 01")
        let endDate = formatter.date(from: "2099 12 31")
        let parameters = ConfigurationParameters(startDate: startDate!, endDate: endDate!, firstDayOfWeek: .monday)
        return parameters
    }
    
}

extension CalendarVC: JTAppleCalendarViewDelegate {
    
    func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
        let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CalendarCell", for: indexPath) as! CalendarCell
        
        cell.dateLabel.text = cellState.text
        
        handleCellSelected(view: cell, cellState: cellState)
        handleCellTextColor(view: cell, cellState: cellState)
        
        return cell
    }
    
    
    func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {

        
        if firstDate != nil {
            if date < firstDate! {
                 calendarView.selectDates(from: date, to: firstDate!, triggerSelectionDelegate: false, keepSelectionIfMultiSelectionAllowed: true)
                endDate = firstDate
                dateToDeselect = date
                firstDate = nil
                
            } else {
            calendarView.selectDates(from: firstDate!, to: date, triggerSelectionDelegate: false, keepSelectionIfMultiSelectionAllowed: true)
                
                endDate = date
                dateToDeselect = firstDate
                firstDate = nil
            }
         
        } else {
            if endDate != nil {
            calendarView.deselectDates(from: dateToDeselect!, to: endDate!, triggerSelectionDelegate: false)
                endDate = nil
            }
            firstDate = date
        }
        
        handleCellSelected(view: cell, cellState: cellState)
        handleCellTextColor(view: cell, cellState: cellState)

    }
    
    func calendar(_ calendar: JTAppleCalendarView, didDeselectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
        
        if endDate != nil {
            calendarView.deselectDates(from: dateToDeselect!, to: endDate!, triggerSelectionDelegate: false)
            endDate = nil
            firstDate = nil
        }

        handleCellSelected(view: cell, cellState: cellState)
        handleCellTextColor(view: cell, cellState: cellState)
        
    }
    
    func calendar(_ calendar: JTAppleCalendarView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) {
            self.setupCalendarViews(from: visibleDates)
        
    }
}
@patchthecode
Copy link
Owner

patchthecode commented Oct 24, 2017

I cannot help much if you do not tell me which version of the library you are using.
is it 7.1.1?

If you are using version 7.1.1, is it a new error you are experiencing with the new update?

@AndreiVataselu
Copy link
Author

AndreiVataselu commented Oct 24, 2017

I had 7.1.0, I updated to 7.1.1 now and I still get this bug, so no, it's an old bug.

@patchthecode
Copy link
Owner

ok looking into this

@patchthecode
Copy link
Owner

I looked at your video.
And i looked into this.
I cannot re-create this on my sample app.
Do you have a sample zipped project i can try?
You can zip it and drag the zip file here.

@AndreiVataselu
Copy link
Author

AndreiVataselu commented Nov 2, 2017

// link removed

@patchthecode
Copy link
Owner

thank you. checking it out now that i have downloaded it.

@patchthecode patchthecode self-assigned this Nov 2, 2017
@patchthecode patchthecode added bug and removed bug labels Nov 2, 2017
@patchthecode
Copy link
Owner

patchthecode commented Nov 3, 2017

@AndreiVataselu The problem is here
->

    func handleCellSelected(view: JTAppleCell?, cellState: CellState) {
        guard let validCell = view as? CalendarCell else {return}
        if validCell.isSelected {
            validCell.selectedView.isHidden = false
        } else {
            validCell.selectedView.isHidden = true
        }
    }

Please change your code to this

    func handleCellSelected(view: JTAppleCell?, cellState: CellState) {
        guard let validCell = view as? CalendarCell else {return}
        if cellState.isSelected {
            validCell.selectedView.isHidden = false
        } else {
            validCell.selectedView.isHidden = true
        } 
    }

You should use the cellState.
Take look at Problem # 2 on this link --> #553

It explains why you should use cellState.

@patchthecode
Copy link
Owner

This should fix your issue.

@AndreiVataselu
Copy link
Author

Indeed it works! Thank you!

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

2 participants