Browse Source

Lazy Loading Event Store!

pull/92/head
Abhishek 5 years ago
parent
commit
cf0061c3cb
  1. 24
      Clocker/Events and Reminders/CalendarHandler.swift
  2. 2
      Clocker/Events and Reminders/EventCenter.swift
  3. 12
      Clocker/Events and Reminders/RemindersHandler.swift

24
Clocker/Events and Reminders/CalendarHandler.swift

@ -20,7 +20,7 @@ extension EventCenter {
var sourcesAndCalendars: [Any] = []
// Fetch array of user's calendars sorted first by source title and then by calendar title
let calendars = store.calendars(for: .event).sorted { (cal1, cal2) -> Bool in
let calendars = eventStore.calendars(for: .event).sorted { (cal1, cal2) -> Bool in
if cal1.source.sourceIdentifier == cal2.source.sourceIdentifier {
return cal1.title < cal2.title
@ -109,8 +109,16 @@ extension EventCenter {
return filteredAllDayEvent?.event
}
private func initializeStoreIfNeccesary() {
if eventStore == nil {
eventStore = EKEventStore()
}
}
func requestAccess(to entity: EKEntityType, completionHandler: @escaping (_ granted: Bool) -> Void) {
store.requestAccess(to: entity) { [weak self] granted, _ in
initializeStoreIfNeccesary()
eventStore.requestAccess(to: entity) { [weak self] granted, _ in
// On successful granting of calendar permission, we default to showing events from all calendars
if let self = self, entity == .event, granted {
@ -161,7 +169,7 @@ extension EventCenter {
}
func retrieveAllCalendarIdentifiers() -> [String] {
return store.calendars(for: .event).map { (calendar) -> String in
return eventStore.calendars(for: .event).map { (calendar) -> String in
calendar.calendarIdentifier
}
}
@ -193,19 +201,21 @@ extension EventCenter {
return
}
initializeStoreIfNeccesary()
let calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)
let startDate = createDateComponents(with: calendar, start)
let endDate = createDateComponents(with: calendar, end)
// Passing in nil for calendars to search all calendars
let predicate = store.predicateForEvents(withStart: startDate,
end: endDate,
calendars: nil)
let predicate = eventStore.predicateForEvents(withStart: startDate,
end: endDate,
calendars: nil)
var eventsForDateMapper: [Date: [EventInfo]] = [:]
let events = store.events(matching: predicate)
let events = eventStore.events(matching: predicate)
// Populate our cache with events that match our startDate and endDate.
// We map eachDate to array of events happening on that day

2
Clocker/Events and Reminders/EventCenter.swift

@ -6,7 +6,7 @@ import EventKit
class EventCenter: NSObject {
private static var shared = EventCenter()
let store = EKEventStore()
var eventStore: EKEventStore!
var calendar: EKCalendar?

12
Clocker/Events and Reminders/RemindersHandler.swift

@ -7,7 +7,7 @@ extension EventCenter {
private func retrieveCalendar() -> EKCalendar? {
if calendar == nil {
let calendars = store.calendars(for: .reminder)
let calendars = eventStore.calendars(for: .reminder)
let calendarTitle = "Clocker Reminders"
let predicate = NSPredicate(format: "title matches %@", calendarTitle)
let filtered = calendars.filter { predicate.evaluate(with: $0) }
@ -15,14 +15,14 @@ extension EventCenter {
if !filtered.isEmpty {
calendar = filtered.first
} else {
calendar = EKCalendar(for: .reminder, eventStore: store)
calendar = EKCalendar(for: .reminder, eventStore: eventStore)
calendar?.title = "Clocker Reminders"
calendar?.source = store.defaultCalendarForNewReminders()?.source
calendar?.source = eventStore.defaultCalendarForNewReminders()?.source
guard let calendar = calendar else { return nil }
do {
try store.saveCalendar(calendar, commit: true)
try eventStore.saveCalendar(calendar, commit: true)
} catch {
assertionFailure("Unable to store calendar")
}
@ -60,7 +60,7 @@ extension EventCenter {
from: reminderDate)) else { return false }
reminderComponents.timeZone = TimeZone(identifier: timezone)
let reminderEvent = EKReminder(eventStore: store)
let reminderEvent = EKReminder(eventStore: eventStore)
reminderEvent.calendar = retrieveCalendar()
reminderEvent.title = "\(title) - Clocker"
reminderEvent.startDateComponents = reminderComponents
@ -70,7 +70,7 @@ extension EventCenter {
// Commit the event
do {
try store.save(reminderEvent, commit: true)
try eventStore.save(reminderEvent, commit: true)
} catch {
Logger.log(object: ["Error": error.localizedDescription],
for: "Error saving reminder")

Loading…
Cancel
Save