2

In my app, I'm trying to add ability to create a new event with CalendarKit when I press an empty space, but I can't find how to do that.

I know it's possible because you can do it in the demo app:

I've tried adding this to my app's code:

override func create(event: EventDescriptor, animated: Bool = false) {
    self.events.append(event) // self.events is my events data source
}

But it didn't worked, in fact, it doesn't even get called when I long press an empty space.

I also tried to look in the source code, but I found nothing. How can I do that? thanks in advance

Daisy the cat
  • 372
  • 3
  • 11
  • Your question isn't clear. Are you modifying an existing demo app? Have you created your own app from scratch? How is `create(event:)` supposed to be called? – Paulw11 Dec 23 '20 at 22:22
  • @Paulw11 I'm trying to add ability to create a new event by long-pressing an empty space (like in the iOS Calendar) **in my app**. I mentioned the demo app to show that it's possible, but unfortunately I don't have the source code of the demo app. – Daisy the cat Dec 24 '20 at 11:05

1 Answers1

2
override func dayView(dayView: DayView, didLongPressTimelineAt date: Date) {
    let newEvent = Event()
    
    newEvent.startDate = date
    newEvent.endDate = date.addingTimeInterval(3600)
    
    // Customize your event...
    newEvent.text = randomName() // A function that generates a new random name that haven't been used before.
    
    self.create(event: newEvent)
}

override func create(event: EventDescriptor, animated: Bool = false) {
    super.create(event: event, animated: animated)
    self.events.append(event)
}

override func dayView(dayView: DayView, didUpdate event: EventDescriptor) {
    for (index, eventFromList) in events.enumerated() {
        if eventFromList.text == event.text {
            events[index] = event
        }
    }
    self.endEventEditing()
    self.reloadData()
}

Please make sure that every Event have it's own unique name, otherwise it won't work

אורי orihpt
  • 2,358
  • 2
  • 16
  • 41
  • There are still bugs in this answer... after adding new event and scrolling the event stays in it's place.. – Daisy the cat Dec 24 '20 at 12:14
  • 1
    Thanks for answering! Overall, looks good, but please keep in mind the need to add a logic to store the events in the persistent storage you're using, e.g. instead of this line `self.events.append(event)`. Also, to better identify events, you can either work with `userInfo` dictionary in the `Event` class or pass your own object conforming to the `EventDescriptor` protocol. – Richard Topchii Dec 27 '20 at 01:56