-
Notifications
You must be signed in to change notification settings - Fork 872
/
Copy pathListDocument.swift
106 lines (77 loc) · 3.86 KB
/
ListDocument.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information
Abstract:
The `ListDocument` class is an `NSDocument` subclass that represents a list. It manages the serialization / deserialization of the list object, presentation of window controllers, a list presenter, and more.
*/
import Cocoa
public class ListDocument: NSDocument {
// MARK: Types
private struct StoryboardConstants {
static let listWindowControllerStoryboardIdentifier = "ListWindowControllerStoryboardIdentifier"
}
// MARK: Properties
private var makesCustomWindowControllers = true
public var listPresenter: ListPresenterType? {
didSet {
if let unarchivedList = unarchivedList {
listPresenter?.setList(unarchivedList)
}
}
}
public var unarchivedList: List?
// MARK: Initializers
public convenience init(contentsOfURL URL: NSURL, makesCustomWindowControllers: Bool) throws {
try self.init(contentsOfURL: URL, ofType: AppConfiguration.listerFileExtension)
self.makesCustomWindowControllers = makesCustomWindowControllers
}
// MARK: Auto Save and Versions
override public class func autosavesInPlace() -> Bool {
return true
}
// MARK: NSDocument Overrides
/**
Create window controllers from a storyboard, if desired (based on `makesWindowControllers`).
The window controller that's used is the initial controller set in the storyboard.
*/
override public func makeWindowControllers() {
super.makeWindowControllers()
if makesCustomWindowControllers {
let storyboard = NSStoryboard(name: "Main", bundle: nil)
let windowController = storyboard.instantiateControllerWithIdentifier(StoryboardConstants.listWindowControllerStoryboardIdentifier) as! NSWindowController
addWindowController(windowController)
}
}
override public func defaultDraftName() -> String {
return AppConfiguration.defaultListerDraftName
}
// MARK: Serialization / Deserialization
override public func readFromData(data: NSData, ofType typeName: String) throws {
unarchivedList = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? List
if let unarchivedList = unarchivedList {
listPresenter?.setList(unarchivedList)
return
}
throw NSError(domain: NSCocoaErrorDomain, code: NSFileReadCorruptFileError, userInfo: [
NSLocalizedDescriptionKey: NSLocalizedString("Could not read file.", comment: "Read error description"),
NSLocalizedFailureReasonErrorKey: NSLocalizedString("File was in an invalid format.", comment: "Read failure reason")
])
}
override public func dataOfType(typeName: String) throws -> NSData {
if let archiveableList = listPresenter?.archiveableList {
return NSKeyedArchiver.archivedDataWithRootObject(archiveableList)
}
throw NSError(domain: "ListDocumentDomain", code: -1, userInfo: [
NSLocalizedDescriptionKey: NSLocalizedString("Could not archive list", comment: "Archive error description"),
NSLocalizedFailureReasonErrorKey: NSLocalizedString("No list presenter was available for the document", comment: "Archive failure reason")
])
}
// MARK: Handoff
override public func updateUserActivityState(userActivity: NSUserActivity) {
super.updateUserActivityState(userActivity)
// Store the list's color in the user activity to be able to quickly present a list when it's viewed.
userActivity.addUserInfoEntriesFromDictionary([
AppConfiguration.UserActivity.listColorUserInfoKey: listPresenter!.color.rawValue
])
}
}