Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stuck #345

Closed
3 tasks done
R35Master opened this issue Aug 18, 2022 · 4 comments
Closed
3 tasks done

Stuck #345

R35Master opened this issue Aug 18, 2022 · 4 comments
Labels

Comments

@R35Master
Copy link

New Issue Checklist

Issue Description

Hi @richardtop I dont like to create tons of topics but my brain is melting and I cant figure it out.
So the reason I am using CalenderKit is basically getting a user calendar data and show it to other related user.

First side everything works great, no dubt. I am capturing 35 days of calendar data and pulling it to firebase with +startdate +endDate +isallDay + color + title with for - in loop.

Second side with same path I am taking that captured user's data with for in loop, append it and keeping it and what happens is when I try to do .map(EKWrapper.init) that variable I am having this issue

public var color: UIColor {
get {
UIColor(cgColor: ekEvent.calendar.cgColor) ==>> error : Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value <<== .calender is red and underlined. Please find the code I use below:
}
}

Code I'm using with CalendarKit
<!-- INSERT CODE HERE -->

// THIS GETS THE EVENTS FROM FIREBASE

    var userEventBox: [EventBox] = []

    func getEvents() {
        
        db.collection(XX).document(XX).collection("Calendar").getDocuments { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for event in querySnapshot!.documents {
                    if let startDate = event["startDate"] as? Date,
                       let endDate = event["endDate"] as? Date,
                       let isAllDay = event["isAllDay"] as? Bool,
                       let title = event["title"] as? String,
                       let colorSpace = event["colorSpace"],
                       let colorComponents = event["colorComponents"]{
                        let newEventBox = EventBox(startDate: startDate, endDate: endDate, isAllDay: isAllDay, title: title, colorSpace: colorSpace as! CGColorSpace, colorComponents: colorComponents as! [CGFloat])
                        self.userEventBox.append(newEventBox)
                    }
                }
            }
        }
        generateEvents().  // THEN TRIGGERS SECOND FUNC 
    }



    var generatedEvents = [EKEvent(eventStore: EKEventStore())]

    func generateEvents() {
        
        for createEvents in self.userEventBox {
            
            if self.userEventBox.isEmpty { } else {
                
                if let event = EKEvent() as? EKEvent {
                    event.title = createEvents.title
                    event.startDate = createEvents.startDate
                    event.endDate = createEvents.endDate
                    event.isAllDay = createEvents.isAllDay
                    event.calendar.cgColor = (UIColor.init(cgColor: CGColor(red: 1, green: 1, blue: 1, alpha: 1)) as! CGColor)
                    self.generatedEvents = [event] } else {return}
            }
        }
    }
 // AND HERE IS OVERRIDE EVENTS FOR DATE

    override func eventsForDate(_ date: Date) -> [EventDescriptor] {
        getEvents()         // << AND HERE I AM TRIGGERING THE FIRST FUNC WHERE I GET DATA FROM FIREBASE
        
        let calenderKitEvents = generatedEvents.map(EKWrapper.init)
        
        return calenderKitEvents
    }

Result I am trying to achieve

StackOverflow Question link

@R35Master
Copy link
Author

I have edited the code and this is how it looks like `

func getEvents() {
    
    self.db.collection(XX).document(XX).collection("Calendar").addSnapshotListener { [self] (querySnapshot, error) in
        
        self.userEventBox = []
        
        if let e = error {
            print("There was an issue retrieving data from Firestore. \(e)")
        } else {
            
            if let snapshotDocuments = querySnapshot?.documents {
                
                for doc in snapshotDocuments {
                    
                    let data = doc.data()
                    if let title = data["title"] as? String ,
                       let startDate = data["startDate"] as? String ,
                       let endDate = data["endDate"] as? String ,
                       let isAllDay = data["isAllDay"] as? Bool
                    {
                        let newEventBox = EventBox(startDate: startDate, endDate: endDate, isAllDay: isAllDay, title: title)
                        self.userEventBox.append(newEventBox)
                        print(newEventBox)
                        self.generate()
                    }
                }
            }
        }
    }
}


func generate() {
    
    for generation in userEventBox {
        
        let isoStartDate = generation.startDate
        let isoEndDate = generation.endDate
        
        let dateFormatter = ISO8601DateFormatter()
        let startDate = dateFormatter.date(from:isoStartDate)!
        let endDate = dateFormatter.date(from:isoEndDate)!
        
        if let event = EKEvent() as? EKEvent {

        event.title = generation.title
        event.isAllDay = generation.isAllDay
        event.startDate = startDate
        event.endDate = endDate
        event.calendar.cgColor = CGColor(red: 1, green: 1, blue: 1, alpha: 1)
            self.generated = [event]
        }
        
    }
}

var generated = [EKEvent()]
override func eventsForDate(_ date: Date) -> [EventDescriptor] {
    
    self.getEvents()
    
    let calenderKitEvents = generated.map(EKWrapper.init)
    
    return calenderKitEvents
}

}
`

Now I have this error Thread 1: "You must use [EKEvent eventWithEventStore:] to create an event"

I will really lose my mind. Please help @richardtop

@richardtop
Copy link
Owner

Please post on stackoverflow first.

@richardtop
Copy link
Owner

Let's continue there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants