1

Having trouble adding events, using CalendarKit. Trying to plot a testing event and it does not display. Docs say that CalendarKit requires an CalendarKit requires EventDataSource to return an array of objects conforming to EventDescriptor protocol, ive tried that no luck.

import SwiftUI
import CalendarKit
import UIKit


struct TestingCalendarController: UIViewControllerRepresentable {
    @Binding var eventDates: [Date]
    
    func makeCoordinator() -> Coordinator {
        return Coordinator()
    }
    func makeUIViewController(context: Context) ->  DayViewController {
        var dayView = DayViewController()
        dayView.delegate = context.coordinator
        
        var style = CalendarStyle()
        style.timeline.timeIndicator.color = .systemBlue
        style.header.daySelector.todayActiveTextColor = .white
        style.header.daySelector.selectedBackgroundColor = UIColor(.primary)
        style.header.daySelector.todayInactiveTextColor = .systemBlue
        style.header.daySelector.todayActiveBackgroundColor = .systemBlue
        style.header.swipeLabel.font = .systemFont(ofSize: 0.0)
       
        dayView.updateStyle(style)
        var testingEvents:  [Event] =  [Event]()
        var event = Event()
        event.startDate = Date()
        event.endDate = Date()
        event.isAllDay = true
        event.text = "Testing Event"
        dayView.create(event: event)
       
        return dayView
        
    }
    
  
    

    func updateUIViewController(_ dayView: DayViewController, context: Context) {
        dayView.delegate = context.coordinator
    }
    
    class Coordinator: NSObject, DayViewDelegate {
        
      
        func dayViewDidSelectEventView(_ eventView: EventView) {
            
        }
        
        func dayViewDidLongPressEventView(_ eventView: EventView) {
            
        }
        
        func dayView(dayView: DayView, didTapTimelineAt date: Date) {
            
        }
        
        func dayView(dayView: DayView, didLongPressTimelineAt date: Date) {
            
        }
        
        func dayViewDidBeginDragging(dayView: DayView) {
            
        }
        
        func dayViewDidTransitionCancel(dayView: DayView) {
            
        }
        
        func dayView(dayView: DayView, willMoveTo date: Date) {
            print("Date \(date)")
        }
        
        func dayView(dayView: DayView, didMoveTo date: Date) {
            
        }
        
        func dayView(dayView: DayView, didUpdate event: EventDescriptor) {
          
        }
        
        
      
    }
}

Any help is greatly appreciated thanks!

Carterman
  • 219
  • 1
  • 7

1 Answers1

1

Looks like you're using CalendarKit with SwiftUI. As of right now, SwiftUI is not supported out-of-the box.

Moreover, this API's purpose is to provide animations / gesture handling for the user, it doesn't actually creates events in the datasource.

        dayView.updateStyle(style)
        var testingEvents:  [Event] =  [Event]()
        var event = Event()
        event.startDate = Date()
        event.endDate = Date()
        event.isAllDay = true
        event.text = "Testing Event"
        dayView.create(event: event)

In order to solve your issue, I suggest making the following changes:

  1. Implement DayViewDataSource and any other CalendarKit interaction in the domain of UIKit.
  2. Bridge your custom subclass of DayViewController to SwiftUI and use that subclass in the SwiftUI domain (similar to the way you've provided in your code snippet)
Richard Topchii
  • 7,075
  • 8
  • 48
  • 115