Skip to content

Commit

Permalink
swift 3 update + Sierra compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Filetti committed Apr 26, 2017
1 parent 59c9b17 commit 149c9b9
Show file tree
Hide file tree
Showing 40 changed files with 1,298 additions and 4,467 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "XCGLogger"]
path = XCGLogger
url = https://github.com/DaveWoodCom/XCGLogger.git
[submodule "SwiftyJSON"]
path = SwiftyJSON
url = https://github.com/SwiftyJSON/SwiftyJSON.git
229 changes: 130 additions & 99 deletions JustUsed.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

73 changes: 22 additions & 51 deletions JustUsed/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions JustUsed/DiMe Data/CalendarEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ import Contacts
/// Represents an calendar event, as understood by dime
class CalendarEvent: Event {

private(set) var participants: [Person] = [Person]()
private(set) var name: String
private(set) var calendar: String
private(set) var location: Location?
private(set) var locString: String?
private(set) var notes: String?
fileprivate(set) var participants: [Person] = [Person]()
fileprivate(set) var name: String
fileprivate(set) var calendar: String
fileprivate(set) var location: Location?
fileprivate(set) var locString: String?
fileprivate(set) var notes: String?
let id: String

override var hash: Int { get {
Expand Down Expand Up @@ -62,7 +62,7 @@ class CalendarEvent: Event {
self.calendar = event.calendar.compositeName
self.notes = event.notes
if #available(OSX 10.11, *) {
if let structLoc = event.structuredLocation, clloc = structLoc.geoLocation {
if let structLoc = event.structuredLocation, let clloc = structLoc.geoLocation {
location = Location(fromCLLocation: clloc)
}
}
Expand All @@ -72,14 +72,14 @@ class CalendarEvent: Event {

if event.hasAttendees, let attendees = event.attendees {
for attendee in attendees {
if let name = attendee.name, part = Person(fromString: name) {
if let name = attendee.name, let part = Person(fromString: name) {
// if possible, fetch more data for this person
if #available(OSX 10.11, *) {
if CNContactStore.authorizationStatusForEntityType(.Contacts) == .Authorized {
if CNContactStore.authorizationStatus(for: .contacts) == .authorized {
do {
let store: CNContactStore = AppSingleton.contactStore as! CNContactStore
let predicate = attendee.contactPredicate
let contacts = try store.unifiedContactsMatchingPredicate(predicate, keysToFetch: [CNContactEmailAddressesKey,CNContactMiddleNameKey, CNContactGivenNameKey, CNContactFamilyNameKey])
let contacts = try store.unifiedContacts(matching: predicate, keysToFetch: [CNContactEmailAddressesKey as CNKeyDescriptor,CNContactMiddleNameKey as CNKeyDescriptor, CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor])
// put in data from the first returned contact
if contacts.count >= 1 {
part.firstName = contacts[0].givenName
Expand All @@ -88,7 +88,7 @@ class CalendarEvent: Event {
if midName != "" {
part.middleNames = [midName]
}
part.email = (contacts[0].emailAddresses[0].value as! String)
part.email = (contacts[0].emailAddresses[0].value as String)
}
} catch {
AppSingleton.log.error("Error while fetching an individual contact for \(self.name):\n\(error)")
Expand Down Expand Up @@ -117,7 +117,7 @@ class CalendarEvent: Event {
if participants.count > 0 {
self.participants = [Person]()
for participant in participants {
self.participants.append(Person(fromJson: participant))
self.participants.append(Person(fromDime: participant))
}
}
}
Expand All @@ -128,22 +128,22 @@ class CalendarEvent: Event {

super.init()

let start = NSDate(timeIntervalSince1970: NSTimeInterval(json["start"].intValue / 1000))
let end = NSDate(timeIntervalSince1970: NSTimeInterval(json["end"].intValue / 1000))
let start = Date(timeIntervalSince1970: TimeInterval(json["start"].intValue / 1000))
let end = Date(timeIntervalSince1970: TimeInterval(json["end"].intValue / 1000))
setStart(start)
setEnd(end)

}

/// getDict for calendar is overridden to update return value with internal state.
override func getDict() -> [String : AnyObject] {
override func getDict() -> [String : Any] {
var retDict = theDictionary // fetch current values

// update values
retDict["calendar"] = calendar
retDict["name"] = name
if participants.count > 0 {
var partArray = [[String: AnyObject]]()
var partArray = [[String: Any]]()
for participant in participants {
partArray.append(participant.getDict())
}
Expand All @@ -170,7 +170,7 @@ class CalendarEvent: Event {
}

/// Compares two calendar events, which are equal only if all their fields are equal
override func isEqual(object: AnyObject?) -> Bool {
override func isEqual(_ object: Any?) -> Bool {
if let otherEvent = object as? CalendarEvent {
if name != otherEvent.name || calendar != otherEvent.calendar || notes != otherEvent.notes {
return false
Expand Down
24 changes: 12 additions & 12 deletions JustUsed/DiMe Data/DesktopEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@ import Foundation
/// Used to send the first-time file opening event
class DesktopEvent: Event {

init(infoElem: DocumentInformationElement, ofType type: TrackingType, withDate date: NSDate, andLocation location: Location?) {
init(infoElem: DocumentInformationElement, ofType type: TrackingType, withDate date: Date, andLocation location: Location?) {
super.init()

theDictionary["targettedResource"] = infoElem.getDict()
theDictionary["targettedResource"] = infoElem.getDict() as AnyObject
switch type {
case .Spotlight:
theDictionary["actor"] = "JustUsed_Spotlight"
case let .Browser(browser):
theDictionary["actor"] = "JustUsed_\(browser)"
case .spotlight:
theDictionary["actor"] = "JustUsed_Spotlight" as AnyObject
case let .browser(browser):
theDictionary["actor"] = "JustUsed_\(browser)" as AnyObject
}
theDictionary["start"] = JustUsedConstants.diMeDateFormatter.stringFromDate(date)
theDictionary["start"] = JustUsedConstants.diMeDateFormatter.string(from: date)

if let loc = location {
theDictionary["location"] = loc.getDict()
theDictionary["location"] = loc.getDict() as AnyObject
}

theDictionary["@type"] = "DesktopEvent"
theDictionary["type"] = "http://www.hiit.fi/ontologies/dime/#DesktopEvent"
theDictionary["@type"] = "DesktopEvent" as AnyObject
theDictionary["type"] = "http://www.hiit.fi/ontologies/dime/#DesktopEvent" as AnyObject
}
}

enum TrackingType {
case Browser(BrowserType)
case Spotlight
case browser(BrowserType)
case spotlight
}
76 changes: 65 additions & 11 deletions JustUsed/DiMe Data/DiMeData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,83 @@

import Foundation

/// Marks classes and structs that can return themselves in a dictionary
/// where all keys are strings and values can be used in a JSON
protocol Dictionariable {

/// Returns itself in a JSON-Serializable dict
func getDict() -> [String: AnyObject]
}
import Foundation

/// This class is made for subclassing. It represents data common to all dime objects (see /dime-server/src/main/java/fi/hiit/dime/data/DiMeData.java in the dime project).
class DiMeBase: NSObject, Dictionariable {

/// Main dictionary storing all data
///
/// **Important**: all sublasses must set these two keys, in order to be decoded by dime:
/// **Important**: endpoint classes (Event, InformationElement as subclasses) must set these two keys, in order to be decoded by dime:
/// - @type
/// - type
var theDictionary = [String: AnyObject]()
var theDictionary = [String : Any]()

override init() {
super.init()
}

func getDict() -> [String : AnyObject] {
/// Simply returns the dictionary. Can be overridden by subclasses that want
/// to edit the dictionary before sending it.
func getDict() -> [String : Any] {
return theDictionary
}
}
}

/// Represents a simple range with a start and end value
struct DiMeRange: Dictionariable, Equatable {
var min: NSNumber
var max: NSNumber

/// Returns min and max in a dict
func getDict() -> [String : Any] {
var retDict = [String : Any]()
retDict["min"] = min
retDict["max"] = max
return retDict
}
}

func == (lhs: DiMeRange, rhs: DiMeRange) -> Bool {
return lhs.max == rhs.max &&
lhs.min == rhs.min
}

/// Marks classes and structs that can return themselves in a dictionary
/// where all keys are strings and values can be used in a JSON
protocol Dictionariable {

/// Returns itself in a dict
func getDict() -> [String : Any]
}

/// Allows collections of dictionariable types to return themselves as array of dicts
extension Sequence where Iterator.Element: Dictionariable {

/// Returns itself as an array of dicts
func asDictArray() -> [[String : Any]] {
return self.reduce([[String : Any]](), {$0 + [$1.getDict()]})
}
}

extension NSSize: Dictionariable {
/// Returns width and height in a dictionary with their values as
/// numbers (both as JSONableItem enums).
func getDict() -> [String : Any] {
var retDict = [String : Any]()
retDict["height"] = self.height
retDict["width"] = self.width
return retDict
}
}

extension NSPoint: Dictionariable {
/// Returns x and y in a dictionary with their values as
/// numbers (both as JSONableItem enums).
func getDict() -> [String : Any] {
var retDict = [String : Any]()
retDict["x"] = self.x
retDict["y"] = self.y
return retDict
}
}
Loading

0 comments on commit 149c9b9

Please sign in to comment.