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 shows multiple/glitched selections with single row view, starting on wrong day of week #196

Closed
Croge32 opened this issue Nov 14, 2016 · 14 comments

Comments

@Croge32
Copy link

Croge32 commented Nov 14, 2016

Calendar works great for the most part! Very informative tutorials.

So I'm making a calendar that has a button that expands it from 1 to 6 rows. With 6 rows I have it working and looking great, but with 1 row I have a couple issues:

  1. When the calendar starts on the 1 row view and I set the inDates to .off and outDates to .tillEndOfRow, the days of the week don't align properly with my settings. I have Sunday set as my first day of the week and this occurs:

screen shot 2016-11-14 at 9 49 40 am

When I set outDates to .off, the view starts off incorrect, but corrects itself on selection, which is a bit jarring.

  1. Occasionally there seems to be some sort of graphical selection related glitch with the single row calendarView. Example:

screen shot 2016-11-14 at 9 52 03 am

This happens when I select a date near the end of my row (i.e. the right side), scroll towards that direction, and select another date on the opposite (i.e. left) side. Scrolling back towards the first selection will still show a selected date. Then if you make another selection near it, it may show the duplicate on the same row.

Relevant code:

// calendar button:

func calendarPressed(sender: Any) {
    calendarViewHeight.constant = calendarOut ? 60 : 200
    calendarOut = !calendarOut
    calendarView.reloadData()

    UIView.animate(withDuration: 0.2) {
      self.view.layoutIfNeeded()
    }
}

    ...

override func viewDidLoad() {
    super.viewDidLoad()

    calendarView.dataSource = self
    calendarView.delegate = self
    calendarView.registerCellViewXib(file: "MealsMealPlanCalendarCell")
    calendarView.scrollToDate(Date(), triggerScrollToDateDelegate: false, animateScroll: false)

    ...
  }

    ...

extension MealsMealPlanViewController: JTAppleCalendarViewDataSource, JTAppleCalendarViewDelegate {
  func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy MM dd"

    let rows = calendarOut ? 6 : 1
    let inDates: InDateCellGeneration =  calendarOut ? .forAllMonths : .off
    let outDates: OutDateCellGeneration = calendarOut ? .tillEndOfGrid : .tillEndOfRow

    let startDate = Calendar.current.date(byAdding: .year, value: -1, to: Date())!
    let endDate = Date()
    let parameters = ConfigurationParameters(
      startDate: startDate,
      endDate: endDate,
      numberOfRows: rows,
      calendar: Calendar.current,
      generateInDates: inDates,
      generateOutDates: outDates,
      firstDayOfWeek: .sunday)

    return parameters
  }

  func calendar(_ calendar: JTAppleCalendarView, willDisplayCell cell: JTAppleDayCellView, date: Date, cellState: CellState) {
    let myCustomCell = cell as! MealsMealPlanCalendarCell

    myCustomCell.dayLabel.text = cellState.text

    if cellState.dateBelongsTo == .thisMonth {
      myCustomCell.dayLabel.textColor = UIColor.white
      cell.isHidden = false
    } else {
      cell.isHidden = true
    }

    handleCellSelection(view: cell, cellState: cellState)
  }

  func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleDayCellView?, cellState: CellState) {
    handleCellSelection(view: cell, cellState: cellState)
    let formatter = DateFormatter()
    formatter.dateFormat = "EEEE, MMMM dd, yyyy"
    calendarDate?.text = formatter.string(from: date)
  }

  func calendar(_ calendar: JTAppleCalendarView, didDeselectDate date: Date, cell: JTAppleDayCellView?, cellState: CellState) {
    handleCellSelection(view: cell, cellState: cellState)
  }

  func handleCellSelection(view: JTAppleDayCellView?, cellState: CellState) {
    guard let myCustomCell = view as? MealsMealPlanCalendarCell  else {
      return
    }
    if cellState.isSelected {
      myCustomCell.selectedView.layer.cornerRadius = myCustomCell.selectedView.frame.size.width / 2.0
      myCustomCell.selectedView.isHidden = false
    } else {
      myCustomCell.selectedView.isHidden = true
    }
  }
}

Is there anything I'm doing wrong? Thanks!

@patchthecode patchthecode self-assigned this Nov 14, 2016
@patchthecode
Copy link
Owner

patchthecode commented Nov 14, 2016

OK. your for your issues.

  1. Your first one is expected. It seems that you have an external header [s|m|t|w|t|f|s] that is outside of the calendar. Therefore your in-dates/out-dates will offset the positioning of your dates. It seems that the setting that you want is

in-Dates = .forFirstMonthOnly and out-dates = .off.

Since you have an external header (and not an internal one), this setting will ensure that the dates are offset correctly. The in-date setting will start of the offset and the out-date setting being set to off will make the dates start immediately in its correct position.

Let me know if this works for you. I am looking into your 2nd issue now.

@patchthecode
Copy link
Owner

patchthecode commented Nov 14, 2016

Another thing to note for issue#1, you said that it auto-corrected it self on selection. Ok there is a bug here. The bug happens on single-row calendar.
It happens with the scrollToDate() function. On single row view, it scrolls to the incorrect position. Therefore when you select the date, it readjusts it self to what it should be.

I have now created a new issue here to track this -->> #197

@Croge32
Copy link
Author

Croge32 commented Nov 14, 2016

Good to go regarding issue#1 👍

issue#2 still occurring. Were you able to replicate this?

@patchthecode
Copy link
Owner

@Croge32 i'll get it it in a bit. I'm currently working on issue#1.

but i'm pretty sure that the 2nd issue is nothing serious. I'll look at it in a sec.
I'll let you know if I can't replicate it. Just a min.

@patchthecode
Copy link
Owner

patchthecode commented Nov 14, 2016

@Croge32 hey I think I have fixed the first issue.
can you test it?

put this in your pod file (to test the masterBranch). And run pod install

pod 'JTAppleCalendar', :git => 'https://github.com/patchthecode/JTAppleCalendar.git'

@Croge32
Copy link
Author

Croge32 commented Nov 14, 2016

I'll get that tested for you tomorrow!

@patchthecode
Copy link
Owner

I have now replicated the 2nd issue. Looking into it now.

@patchthecode
Copy link
Owner

I have now fixed the second issue.
Let me know if the issues are resolved when you have tested them

patchthecode added a commit that referenced this issue Nov 15, 2016
Fixed double selection because of pre-fetching
@patchthecode patchthecode reopened this Nov 15, 2016
@Croge32
Copy link
Author

Croge32 commented Nov 15, 2016

Worked for me! 👍

@Croge32
Copy link
Author

Croge32 commented Nov 16, 2016

@patchthecode I'm noticing another issue that may or may not be related to your latest updates.

When I start my application I'm in one row mode at the current date. If I click my expand button I am in the same month. If I retract the calendar back to 1 row, the dates shown are the 24th - 30th of January, so almost 1 entire year back. Code above is basically the same.

Also calendarView.scrollToNextSegment() and calendarView.scrollToPreviousSegment() seem broken with 1 row. ScrollToPrevious goes back by entire months even when set to 1 row and scrollToNext doesn't scroll at all.

@patchthecode
Copy link
Owner

Ok. Looking into it

@patchthecode
Copy link
Owner

Lets track this here -> #202

@mertayD
Copy link

mertayD commented Apr 1, 2021

I am experiencing the same issue, where Calendar glitches on selected dates. And my configure cell function is exactly the same on tutorials on the JT website.

@patchthecode
Copy link
Owner

have you looked at the discussion on the related issue? #202 ?
this issue was being tracked there.
other information there was given.

Repository owner locked as off-topic and limited conversation to collaborators Apr 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants