iOS Development

ios – I am seeking to combine CalendarKit library with SwiftUI Representable, dealing with concern with occasions dealing with

Spread the love


I am trying to implement CalendarKit utilizing SwiftUI’s UIViewControllerRepresentable. The instance supplied by the library is in UIKit. I’ve efficiently displayed the UI, however when I attempt to go dummy occasions information to show occasions in CalendarKit, the DayViewDelegate and EventDataSource strategies contained in the coordinator class will not be being known as, and no information is being displayed.
What am I doing fallacious, or how can I obtain this? Any type of assist can be appreciated.

My SwiftUI Representable Code:

import SwiftUI
import CalendarKit


struct DayViewControllerWrapper: UIViewControllerRepresentable {
    
    @Binding var selectedEvent: Occasion?
    @Binding var occasions: [EventDescriptor]
    
    // Callbacks for coordinator delegates
    var didSelectEventView: ((EventView) -> Void)?
    var didLongPressEventView: ((EventView) -> Void)?
    var didTapTimelineAt: ((Date) -> Void)?
    var didLongPressTimelineAt: ((Date) -> Void)?
    var didBeginDragging: (() -> Void)?
    var didTransitionCancel: (() -> Void)?
    var willMoveTo: ((Date) -> Void)?
    var didMoveTo: ((Date) -> Void)?
    var didUpdateEvent: ((EventDescriptor) -> Void)?
    
    func makeUIViewController(context: Context) -> DayViewController {
        let viewController = DayViewController()
        
        // Arrange bindings for delegates and information sources
        viewController.dataSource = context.coordinator
        viewController.delegate = context.coordinator
        
        return viewController
    }
    
    func updateUIViewController(_ uiViewController: DayViewController, context: Context) {
        
        uiViewController.reloadData()
    }
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }
    
    class Coordinator: NSObject, EventDataSource, DayViewDelegate {
        
        var guardian: DayViewControllerWrapper
        
        init(_ guardian: DayViewControllerWrapper) {
            self.guardian = guardian
        }
        
        // MARK: - EventDataSource
        
        func eventsForDate(_ date: Date) -> [EventDescriptor] {
            return guardian.occasions
        }
        
        // MARK: - DayViewDelegate
        
        func dayViewDidSelectEventView(_ eventView: EventView) {
            guardian.selectedEvent = eventView.descriptor as? Occasion
            guardian.didSelectEventView?(eventView)
        }
        
        func dayViewDidLongPressEventView(_ eventView: EventView) {
            guardian.didLongPressEventView?(eventView)
        }
        
        func dayView(dayView: DayView, didTapTimelineAt date: Date) {
            guardian.didTapTimelineAt?(date)
        }
        
        func dayView(dayView: DayView, didLongPressTimelineAt date: Date) {
            guardian.didLongPressTimelineAt?(date)
        }
        
        func dayViewDidBeginDragging(dayView: DayView) {
            guardian.didBeginDragging?()
        }
        
        func dayViewDidTransitionCancel(dayView: DayView) {
            guardian.didTransitionCancel?()
        }
        
        func dayView(dayView: DayView, willMoveTo date: Date) {
            guardian.willMoveTo?(date)
        }
        
        func dayView(dayView: DayView, didMoveTo date: Date) {
            guardian.didMoveTo?(date)
        }
        
        func dayView(dayView: DayView, didUpdate occasion: EventDescriptor) {
            guardian.didUpdateEvent?(occasion)
        }
    }
}

struct DayViewControllerWrapper_Previews: PreviewProvider {
    static var previews: some View {
        DayViewControllerWrapper(selectedEvent: .fixed(nil), occasions: .fixed([]))
    }
}

My ContentView code, how i am utilizing it.

import SwiftUI
import CalendarKit

struct ContentView: View {
    
    @State personal var selectedEvent: Occasion?
    @State personal var occasions: [EventDescriptor] = []
    
    var physique: some View {
        VStack {
            Textual content("My Customized Calendar Instance")
            DayViewControllerWrapper(
                selectedEvent: $selectedEvent,
                occasions: $occasions,
                didSelectEventView: { eventView in
                    // Deal with occasion choice
                    print("Chosen occasion: (eventView.descriptor)")
                },
                didLongPressEventView: { eventView in
                    // Deal with lengthy press occasion
                    print("Lengthy pressed occasion: (eventView.descriptor)")
                },
                didTapTimelineAt: { date in
                    // Deal with faucet on timeline
                    print("Tapped timeline at: (date)")
                },
                didLongPressTimelineAt: { date in
                    // Deal with lengthy press on timeline
                    print("Lengthy pressed timeline at: (date)")
                },
                didBeginDragging: {
                    // Deal with start dragging
                    print("Started dragging")
                },
                didTransitionCancel: {
                    // Deal with transition cancel
                    print("Transition canceled")
                },
                willMoveTo: { date in
                    // Deal with will transfer thus far
                    print("Will transfer thus far: (date)")
                },
                didMoveTo: { date in
                    // Deal with did transfer thus far
                    print("Did transfer thus far: (date)")
                },
                didUpdateEvent: { occasion in
                    // Deal with updating occasion
                    print("Up to date occasion: (occasion)")
                }
            )
            .body(top: 400) // Alter body top as wanted
            .onAppear {
                // Populate occasions with dummy information
                generateDummyData()
            }
        }
    }
    
    personal func generateDummyData() {
        occasions = [
            createEvent(title: "Breakfast at Tiffany's", location: "New York, 5th avenue"),
            createEvent(title: "Workout", location: "Tufteparken"),
            createEvent(title: "Meeting with Alex", location: "Home, Oslo, Tjuvholmen"),
            createEvent(title: "Beach Volleyball", location: "Ipanema Beach, Rio De Janeiro"),
            createEvent(title: "WWDC", location: "Moscone West Convention Center, 747 Howard St"),
            createEvent(title: "Google I/O", location: "Shoreline Amphitheatre, One Amphitheatre Parkway"),
            createEvent(title: "✈️️ to Svalbard ❄️️❄️️❄️️❤️️", location: "Oslo Gardermoen"),
            createEvent(title: "💻📲 Developing CalendarKit", location: "🌍 Worldwide"),
            createEvent(title: "Software Development Lecture", location: "Mikpoli MB310, Craig Federighi")
        ]
    }
    
    personal func createEvent(title: String, location: String) -> EventDescriptor {
        let occasion = Occasion()
        occasion.startDate = Date()
        occasion.endDate = Date().addingTimeInterval(3600)
        occasion.textual content = "(title)n(location)"
        occasion.coloration = .blue
        occasion.isAllDay = false
        occasion.userInfo = nil
        return occasion
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Library supplied UIKit instance code:
https://github.com/richardtop/CalendarKit/blob/grasp/CalendarKitDemo/CalendarApp/CustomCalendarExampleController.swift

One thought on “ios – I am seeking to combine CalendarKit library with SwiftUI Representable, dealing with concern with occasions dealing with

  1. The amazing speed at which the website loads—it almost looks as though you are pulling off some special trick—and the superb job that you have done with the contents truly demonstrate your talent as a webmaster.

Leave a Reply

Your email address will not be published. Required fields are marked *