Browse Source

Lazy Loading Event Store!

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

20
Clocker/Events and Reminders/CalendarHandler.swift

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

12
Clocker/Events and Reminders/RemindersHandler.swift

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

Loading…
Cancel
Save