Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
4.8 kB
1
Indexable
Never
func AddToCalender(store:EKEventStore){
        DispatchQueue.main.async {

        let event = EKEvent.init(eventStore: store)
        if self.arrAgendaDetail.count > 0 {
            let model = self.arrAgendaDetail[0]
            event.title = String(format: "%@ - %@", App_Name, model.Heading ?? "")
            event.location = model.custom_location
            
            var strStartDate: String = ""
            var strEndDate: String = ""
            var strStartTime: String = ""
            var strEndTime: String = ""

            strStartDate = model.Start_date_cal ?? ""
            strEndDate = model.End_date_cal ?? ""
            
            strStartTime = model.Start_time ?? ""
            strEndTime = model.End_time ?? ""
            
            var startDate: Date = Date()
            var endDate: Date = Date()
            let startDateFormatter: DateFormatter = DateFormatter()
            let endDateFormatter: DateFormatter = DateFormatter()
            
            startDate = Date()
            endDate = Date()
            startDateFormatter.dateFormat = "dd/MM/yyyy HH:mm"
            endDateFormatter.dateFormat = "dd/MM/yyyy HH:mm"
            startDateFormatter.locale = Utility.setLocalDateToEnglishDefault()
            endDateFormatter.locale = Utility.setLocalDateToEnglishDefault()
            
            let formatter = DateFormatter()
            formatter.dateFormat = "MM/dd/yyyy HH:mm"
            formatter.calendar = Calendar(identifier: Calendar.Identifier.iso8601)
            formatter.locale = Locale(identifier: "en_US_POSIX")
//                    startDate = startDateFormatter.date(from: String(format: "%@ %@", strStartDate, strStartTime)) ?? Date()
//                    endDate = endDateFormatter.date(from: String(format: "%@ %@", strEndDate, strEndTime)) ?? Date()
            
            let temp1: String = UserDefaults.standard.value(forKey: USER_TIME_ZONE_LABEL) as? String ?? ""
            let temp2: String = UserDefaults.standard.value(forKey: USER_TIME_ZONE) as? String ?? ""

            if (temp1.count > 0) && (temp2.count > 0) {
                let t11: String = self.set12HoursFormat(time: strStartTime)
                let t22: String = self.set12HoursFormat(time: strEndTime)
                
                let dicAgenda = self.agendaModel.agenda?[0]
                let t1: String = self.setTimeInLocalUsingDateForAddToCalender(date: String(format: "%@ %@", dicAgenda?.Start_date_cal ?? "", t11), timeZone: UserDefaults.standard.value(forKey: USER_TIME_ZONE) as? String ?? "")
                
                let t2: String = self.setTimeInLocalUsingDateForAddToCalender(date: String(format: "%@ %@", dicAgenda?.End_date_cal ?? "", t22), timeZone: UserDefaults.standard.value(forKey: USER_TIME_ZONE) as? String ?? "")
                startDate = startDateFormatter.date(from: t1) ?? Date()
                endDate = startDateFormatter.date(from: t2) ?? Date()
            } else {
                let stringDate = "\(strStartDate) \(strStartTime)"
                let stringEndDate = "\(strEndDate) \(strEndTime)"
                var strDate = Utility.change(toDateFormat: "dd/MM/yyyy HH:mm", date: formatter.date(from: stringDate) ?? Date()) ?? ""
                var strEnDate = Utility.change(toDateFormat: "dd/MM/yyyy HH:mm", date: formatter.date(from: stringEndDate) ?? Date()) ?? ""
                startDate = startDateFormatter.date(from: strDate)!
                endDate = startDateFormatter.date(from: strEnDate)!
//                startDate = startDateFormatter.date(from: String(format: "%@ %@", strStartDate, strStartTime)) ?? Date()
//                endDate = startDateFormatter.date(from: String(format: "%@ %@", strEndDate, strEndTime)) ?? Date()
            }
            
            
            event.startDate = startDate //today
            event.endDate = endDate  //set 1 hour meeting
            event.calendar = store.defaultCalendarForNewEvents
            
            let aInterval: TimeInterval = -1 * 60 * 60
            let alarm: EKAlarm = EKAlarm(relativeOffset: aInterval)
            event.addAlarm(alarm)
            
            if (Utility.fetchExistedEventState(forStart: startDate, andEnd: endDate, andEvent: event)) {
                DispatchQueue.main.async {

                Utility.showAlert(withTitle: App_Name, andMessage: CALENDAR_EVENT_ALREADY_EXIST_MSG)
                }
            } else {
                do {
                    try store.save(event, span: EKSpan.thisEvent)
                } catch {}
                
                DispatchQueue.main.async {
                    Utility.showAlert(withTitle: App_Name, andMessage: CALENDAR_EVENT_SAVED_MSG)
                }
            }
            
        }
    }
    }
Leave a Comment