From a041d1f048131d6483990b64296ee6b8037260cf Mon Sep 17 00:00:00 2001 From: Marco Filetti Date: Wed, 10 Feb 2016 00:43:19 +0200 Subject: [PATCH] Data mining calendars (incomplete) --- JustUsed/Base.lproj/Main.storyboard | 145 ++++++++++++++++-- JustUsed/Model/CalendarTracker.swift | 33 +++- .../UI/DiMePreferencesViewController.swift | 19 +++ 3 files changed, 180 insertions(+), 17 deletions(-) diff --git a/JustUsed/Base.lproj/Main.storyboard b/JustUsed/Base.lproj/Main.storyboard index e7ea5b3..c2ea5a2 100644 --- a/JustUsed/Base.lproj/Main.storyboard +++ b/JustUsed/Base.lproj/Main.storyboard @@ -1,7 +1,7 @@ - + - + @@ -759,7 +759,7 @@ - + @@ -1047,11 +1047,11 @@ - + - + @@ -1059,7 +1059,7 @@ - + @@ -1067,7 +1067,7 @@ - + @@ -1075,7 +1075,7 @@ - + @@ -1086,7 +1086,7 @@ - + @@ -1097,7 +1097,7 @@ - + @@ -1108,7 +1108,7 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -1180,7 +1289,7 @@ + + + + + + @@ -1310,7 +1425,6 @@ - @@ -1328,6 +1442,7 @@ + @@ -1352,7 +1467,7 @@ - + diff --git a/JustUsed/Model/CalendarTracker.swift b/JustUsed/Model/CalendarTracker.swift index 2514c29..cb89784 100644 --- a/JustUsed/Model/CalendarTracker.swift +++ b/JustUsed/Model/CalendarTracker.swift @@ -52,19 +52,26 @@ public class CalendarTracker { private let store = EKEventStore() + /// If the user granted access to the calendar, this becomes true + private(set) var hasAccess: Bool = false + /// All events currently in dime private var dimeEvents = [CalendarEvent]() /// Where are new events fetched from or sent var calendarDelegate: CalendarHistoryDelegate + static var sharedInstance: CalendarTracker? + /// Creates a new calendar tracker, which uses the given object to fetch / update /// calendar events. init(calendarDelegate: CalendarHistoryDelegate) { self.calendarDelegate = calendarDelegate store.requestAccessToEntityType(.Event) { - result in - if result.0 { + (granted, error) in + if granted { + self.hasAccess = true + // check calendar when an event is modified NSNotificationCenter.defaultCenter().addObserver(self, selector: "getCurrentEvents:", name: EKEventStoreChangedNotification, object: self.store) // check calendar regularly @@ -78,9 +85,31 @@ public class CalendarTracker { self.getCurrentEvents(nil) } } + if let err = error { + AppSingleton.log.error("Error while asking permission to access calendars:\n\(err)") + } } + CalendarTracker.sharedInstance = self } + // MARK: - Accessors + + /// Returns an array containing all calendars residing on the user's device. + /// Nil if we don't have permission to access them. + func calendarNames() -> [String]? { + if hasAccess { + var retVal = [String]() + for cal in store.calendarsForEntityType(.Event) { + retVal.append(cal.title) + } + return retVal + } else { + return nil + } + } + + // MARK: - Private + /// Get events which just-passed. Current event is defined as the latest event that started from kBackLook seconds ago until now, in all calendars. /// - parameter hitObject: Whatever is calling this (notification or timer) @objc private func getCurrentEvents(hitObject: AnyObject?) { diff --git a/JustUsed/UI/DiMePreferencesViewController.swift b/JustUsed/UI/DiMePreferencesViewController.swift index 27c70cc..e5bac6d 100644 --- a/JustUsed/UI/DiMePreferencesViewController.swift +++ b/JustUsed/UI/DiMePreferencesViewController.swift @@ -35,9 +35,14 @@ class DiMePreferencesViewController: NSViewController { @IBOutlet weak var logsPathLabel: NSTextField! + @IBOutlet weak var calendarExcludeTable: NSTableView! + let calendarExcludeDelegate = CalendarExcludeDelegate() + /// Create view and programmatically set-up bindings override func viewDidLoad() { super.viewDidLoad() + calendarExcludeTable.setDataSource(calendarExcludeDelegate) + calendarExcludeTable.setDelegate(calendarExcludeDelegate) let options: [String: AnyObject] = ["NSContinuouslyUpdatesValue": true] @@ -81,3 +86,17 @@ class DiMePreferencesViewController: NSViewController { } } } + +class CalendarExcludeDelegate: NSObject, NSTableViewDataSource, NSTableViewDelegate { + @objc func numberOfRowsInTableView(tableView: NSTableView) -> Int { + return CalendarTracker.sharedInstance!.calendarNames()!.count + } + + @objc func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn?, row: Int) -> AnyObject? { + if tableColumn!.identifier == "calExclTableCheck" { + return true + } else { + return CalendarTracker.sharedInstance!.calendarNames()![row] + } + } +}