1

I've been having issues with using JT Apple Calendar to select a date to display in CalendarKit. The label will display the proper date, but the date selector above it, does not reflect the date that the label shows.

Is there an easy way to fix this? I've been told that it may be a bug.

The date scroll bar seems to take the current date, instead of the date that's been selected. I selected January 19th, 2017, but the date scroll bar shows December 2, 2017 which is today's date.

This is the package home for reference: https://github.com/richardtop/CalendarKit

enter image description here

Here is the relevant part of the code, where selectedDate is the date being displayed in the label and passed from JTAppleCalendar:

var selectedDate: Date! var selectedEvent: Event! var newEventDate: Date!

override func viewDidLoad() {
    super.viewDidLoad()

    dayView.autoScrollToFirstEvent = true
    navigationController?.navigationBar.backgroundColor = .white
    reloadData()
    loadForSelectedDate(date: selectedDate)
}


func loadForSelectedDate(date: Date) {
    dayView.state = DayViewState(date: date)
    dayView.state?.move(to: date)
    dayView.reloadData()
}
user3628240
  • 877
  • 1
  • 23
  • 41
  • So are you using JTAppleCalendar? or are you using CalendarKit ? – Just a coder Nov 27 '17 at 02:35
  • Sorry for the confusion, I was using JTAppleCalendar to pass the date to the CalendarKit page, but the date scroll bar does not reflect the date that was passed – user3628240 Nov 27 '17 at 05:44
  • the date that was passed is one date earlier? one day later? to totally wrong? If it is one day off, then maybe this has something to do with it? -> https://github.com/patchthecode/JTAppleCalendar/issues/252 – Just a coder Nov 27 '17 at 07:03
  • It just so happened that the date I clicked was close to the current date. The scroll bar corresponds to the current date, while the label corresponds to the date that I select on my JTAppleCalendar. I couldn't find the setting to set the date for the date scroll bar though. – user3628240 Nov 27 '17 at 08:19
  • Im not quite sure what you mean by the scroll bar. Can you summarize the problem? Are you saying that the date you click on JTAppleCalendar is incorrect? – Just a coder Nov 27 '17 at 09:07
  • Sorry, so at the very top there is a bar with the 19 in a red circle. This is what I was referring to as the scroll bar. I'm trying to figure out if there's a way to set the date for it, because I couldn't find one. JTAppleCalendar allows me to select the date and it displays it correctly, like the Wednesday, January 18, 2017 example, but the bar on top refers to the current date, not the date that JTAppleCalendar passed in – user3628240 Nov 28 '17 at 09:41
  • OK, then it looks like what you are trying to solve is related to CalendarKit.. on this, I do not have an answer for you. I thought the problem was with JTApplecalendar. – Just a coder Nov 28 '17 at 15:16
  • The issue should now be fixed. – Richard Topchii Apr 04 '18 at 18:42

2 Answers2

3

I think that you are changing between views, because i am doing the same, and i did this: in your DayViewController use viewDidAppear instead of viewDidLoad something like this:

override func viewDidAppear(_ animated: Bool) {
    let dateJT = //your date variable from JTAppleCalendar
    dayView.state?.move(to: dateJT!)
}
  • Thanks @Luis, your solution works, I am able to go on specific date when i opend DayViewController and also able to see the events on that date if any which was not happening when i put my dayView.state?.move(to: date) code in viewDidLoad method – Sachin Tanpure May 10 '18 at 06:01
0

CalendarKit uses a separate object, called DayViewState to update all of its views in a synchronized manner.

The reason why the labels on the scrollbar are not updates is that the corresponding methods on the DayViewState has not been called. Most likely, you're trying to call move(from oldDate: Date, to newDate: Date) only on one of the CalendarKit's components.

The correct way to change the date on all the elements of the DayView (Timeline, Scrollbar and TimeLabel) is to invoke this method on the DayViewState:

let date = // Get Date from JTAppleCalendar
// Set date to all the elements of the CalendarKit
dayView.state?.move(to: date)

Update on the code example

I've found a couple of cases where CalendarKit is used not the way it was intended.

I've made comments along your code snippet, to better understand how to change it:

func loadForSelectedDate(date: Date) {
    // No need to create a new state. DayView creates and configures its own state, and automatically links all the components together.
    // dayView.state = DayViewState(date: date)

    // Just call this method to move to the needed date
    dayView.state?.move(to: date)

    // No need to call `reloadData()` as the DayView will ask the DataSource automatically when the selected date is reached.
    // dayView.reloadData()
}

After all the modifications, it could be shortened to just a single line:

func loadForSelectedDate(date: Date) {
    // Just call this method to move to the needed date
    dayView.state?.move(to: date)
}

Still, your code should have worked (even when you're creating a new state), so you've definitely uncovered a bug in the library.

Richard Topchii
  • 7,075
  • 8
  • 48
  • 115
  • Thank you for your answer. I added the relevant part of my code to show what I did, and it seems like it should work based on the example you gave. selectedDate is the date from the JTAppleCalendar and that seems to work for the label, but not the scroll bar. – user3628240 Dec 02 '17 at 22:50
  • I've updated my answer, could you try the snippet I've provided and see if it works correctly? – Richard Topchii Dec 03 '17 at 05:33
  • I tried your snippet, and the scroll bar is still not reflecting the label date, but rather the current date. Is there a way to change the library itself to reflect the correct date? – user3628240 Dec 03 '17 at 21:25
  • Or do you have another suggestion for an hourly calendar? – user3628240 Dec 03 '17 at 21:25
  • @user3628240 then it seems, you've discovered a bug in the library, I will fix it as soon as I can. Up to my knowledge, there is no other library like this. The closest one is Tapku, however, it's no longer maintained. https://github.com/devinross/tapkulibrary – Richard Topchii Dec 04 '17 at 04:06
  • 1
    Ok, thank you very much for your help, I really appreciate it – user3628240 Dec 04 '17 at 05:29