-
Notifications
You must be signed in to change notification settings - Fork 872
/
Copy pathAppDelegate.swift
79 lines (61 loc) · 2.83 KB
/
AppDelegate.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
/*
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information
Abstract:
The application delegate.
*/
import Cocoa
import ListerKit
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
// MARK: Properties
@IBOutlet weak var todayListMenuItem: NSMenuItem!
var ubiquityIdentityDidChangeNotificationToken: NSObjectProtocol?
// MARK: NSApplicationDelegate
func applicationDidFinishLaunching(notification: NSNotification) {
AppConfiguration.sharedConfiguration.runHandlerOnFirstLaunch {
// If iCloud is enabled and it's the first launch, we'll show the Today document initially.
if AppConfiguration.sharedConfiguration.isCloudAvailable {
// Make sure that no other documents are visible except for the Today document.
NSDocumentController.sharedDocumentController().closeAllDocumentsWithDelegate(nil, didCloseAllSelector: nil, contextInfo: nil)
self.openTodayDocument()
}
}
// Update the menu item at app launch.
updateTodayListMenuItemForCloudAvailability()
ubiquityIdentityDidChangeNotificationToken = NSNotificationCenter.defaultCenter().addObserverForName(NSUbiquityIdentityDidChangeNotification, object: nil, queue: nil) { [weak self] _ in
// Update the menu item once the iCloud account changes.
self?.updateTodayListMenuItemForCloudAvailability()
return
}
}
// MARK: IBActions
/**
Note that there are two possibile callers for this method. The first is the application delegate if
it's the first launch. The other possibility is if you use the keyboard shortcut (Command-T) to open
your Today document.
*/
@IBAction func openTodayDocument(_: AnyObject? = nil) {
TodayListManager.fetchTodayDocumentURLWithCompletionHandler { url in
if let url = url {
dispatch_async(dispatch_get_main_queue()) {
let documentController = NSDocumentController.sharedDocumentController()
documentController.openDocumentWithContentsOfURL(url, display: true) { _ in
// Configuration of the document can go here...
}
}
}
}
}
// MARK: Convenience
func updateTodayListMenuItemForCloudAvailability() {
if AppConfiguration.sharedConfiguration.isCloudAvailable {
todayListMenuItem.action = #selector(AppDelegate.openTodayDocument(_:))
todayListMenuItem.target = self
}
else {
todayListMenuItem.action = nil
todayListMenuItem.target = nil
}
}
}