CalendarKit Implementation Basics

859 views Asked by At

I'm working on an app that needs a Calendar section, and CalendarKit looks really great from the preview images. However, I'm new to Swift and not sure exactly how to work with it. I've installed the CocoaPod fine, but the code listed on the CalendarKit website under the Usage section here is giving me two unexpected errors when I try to add it to my project. Those errors are "'override' can only be specified on class members", and "Cannot convert return expression of type '[Event]' to return type '[EventDescriptor]'"

Here's a screenshot of the code with the errors shown, and EDIT: here's the text of the code if you don't want to go to either link

// Return an array of EventDescriptors for particular date
override func eventsForDate(_ date: Date) -> [EventDescriptor] {

let eventStore = EKEventStore()

var models = // Get events (models) from the storage / API

var events = [Event]()

for model in models {
    // Create new EventView
    let event = Event()
    // Specify StartDate and EndDate
    event.startDate = model.startDate
    event.endDate = model.endDate
    // Add info: event title, subtitle, location to the array of Strings
    var info = [model.title, model.location]
    info.append("\(datePeriod.beginning!.format(with: "HH:mm")) - \(datePeriod.end!.format(with: "HH:mm"))")
    // Set "text" value of event by formatting all the information needed for display
    event.text = info.reduce("", {$0 + $1 + "\n"})
    events.append(event)
}
return events
}

Since this code was written exactly as specified in the Usage section, I'm at a loss of how to fix the errors - I don't want to break anything by just assuming the code is wrong and changing it as XCode suggests.

Additionally, I don't understand what exactly I'm supposed to put after "var models = " (which is followed by the note // Get events (models) from the storage / API). Do I use EKEventStore() or something like that? Could anyone link me to example code or explain what exactly is supposed to go here?

Finally, is there any documentation of how I would link the events returned by this function into a Day View or Month View / implement those views? I'd like to have both a Day View and a Month View that I switch between within the app.

Thanks so much for your time, and if there's any documentation I'm missing or example code that would explain these kinds of basic questions, I'm sorry for missing it - please link it and I'll check it out!

1

There are 1 answers

0
Richard Topchii On

Please, take a look at the CalendarApp, a template repository serving as a starting point for experiments with CalendarKit. It's a sample calendar app for iOS built with CalendarKit and EventKit. It displays events stored in the EKEventStore similarly to the default calendar app.

Here is the relevant piece of code, but your implementation might be slightly different:

    // This is the `DayViewDataSource` method that the client app has to implement in order to display events with CalendarKit
    override func eventsForDate(_ date: Date) -> [EventDescriptor] {
        // The `date` always has it's Time components set to 00:00:00 of the day requested
        let startDate = date
        var oneDayComponents = DateComponents()
        oneDayComponents.day = 1
        // By adding one full `day` to the `startDate`, we're getting to the 00:00:00 of the *next* day
        let endDate = calendar.date(byAdding: oneDayComponents, to: startDate)!
        
        let predicate = eventStore.predicateForEvents(withStart: startDate, // Start of the current day
                                                      end: endDate, // Start of the next day
                                                      calendars: nil) // Search in all calendars
        
        let eventKitEvents = eventStore.events(matching: predicate) // All events happening on a given day
        
        // The `eventKitEvents` has a type of `[EKEvent]`, we need to convert them to CalendarKit Events
        let calendarKitEvents = eventKitEvents.map { ekEvent -> Event in
            let ckEvent = Event() // Creating a new CalendarKit.Event
            ckEvent.startDate = ekEvent.startDate // Copying all of the properties of the `EKEvent` to the newly created CalendarKit.Event
            ckEvent.endDate = ekEvent.endDate
            ckEvent.isAllDay = ekEvent.isAllDay
            ckEvent.text = ekEvent.title
            if let eventColor = ekEvent.calendar.cgColor {
                ckEvent.color = UIColor(cgColor: eventColor)
            }
            return ckEvent
        }

        return calendarKitEvents
    }

If something is still unclear, feel free to watch my full tutorial on getting started with CalendarKit, video link.