-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathInitialViewController.swift
136 lines (106 loc) · 5.73 KB
/
InitialViewController.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import UIKit
import Shared
import Storage
private let LastUsedShareDestinationsKey = "LastUsedShareDestinations"
@objc(InitialViewController)
class InitialViewController: UIViewController, ShareControllerDelegate {
var shareDialogController: ShareDialogController!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(white: 0.0, alpha: 0.66) // TODO: Is the correct color documented somewhere?
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
ExtensionUtils.extractSharedItemFromExtensionContext(self.extensionContext, completionHandler: { (item, error) -> Void in
if let item = item, error == nil {
DispatchQueue.main.async {
guard item.isShareable else {
let alert = UIAlertController(title: Strings.SendToErrorTitle, message: Strings.SendToErrorMessage, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: Strings.SendToErrorOKButton, style: .default) { _ in self.finish() })
self.present(alert, animated: true, completion: nil)
return
}
self.presentShareDialog(item)
}
} else {
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
}
})
}
//
func shareControllerDidCancel(_ shareController: ShareDialogController) {
UIView.animate(withDuration: 0.25, animations: { () -> Void in
self.shareDialogController.view.alpha = 0.0
}, completion: { (Bool) -> Void in
self.dismissShareDialog()
self.finish()
})
}
func finish() {
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
}
func shareController(_ shareController: ShareDialogController, didShareItem item: ShareItem, toDestinations destinations: NSSet) {
setLastUsedShareDestinations(destinations)
UIView.animate(withDuration: 0.25, animations: { () -> Void in
self.shareDialogController.view.alpha = 0.0
}, completion: { (Bool) -> Void in
self.dismissShareDialog()
let profile = BrowserProfile(localName: "profile")
if destinations.contains(ShareDestinationReadingList) {
profile.readingList?.createRecordWithURL(item.url, title: item.title ?? "", addedBy: UIDevice.current.name)
}
if destinations.contains(ShareDestinationBookmarks) {
_ = profile.bookmarks.shareItem(item).value // Blocks until database has settled
}
profile.shutdown()
self.finish()
})
}
//
// TODO: use Set.
func getLastUsedShareDestinations() -> NSSet {
if let destinations = UserDefaults.standard.object(forKey: LastUsedShareDestinationsKey) as? NSArray {
return NSSet(array: destinations as [AnyObject])
}
return NSSet(object: ShareDestinationBookmarks)
}
func setLastUsedShareDestinations(_ destinations: NSSet) {
UserDefaults.standard.set(destinations.allObjects, forKey: LastUsedShareDestinationsKey)
UserDefaults.standard.synchronize()
}
func presentShareDialog(_ item: ShareItem) {
shareDialogController = ShareDialogController()
shareDialogController.delegate = self
shareDialogController.item = item
shareDialogController.initialShareDestinations = getLastUsedShareDestinations()
self.addChildViewController(shareDialogController)
shareDialogController.view.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(shareDialogController.view)
shareDialogController.didMove(toParentViewController: self)
// Setup constraints for the dialog. We keep the dialog centered with 16 points of padding on both
// sides. The dialog grows to max 380 points wide so that it does not look too big on landscape or
// iPad devices.
let views: NSDictionary = ["dialog": shareDialogController.view]
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(16@751)-[dialog(<=380@1000)]-(16@751)-|",
options: NSLayoutFormatOptions(), metrics: nil, views: (views as? [String : AnyObject])!))
let cx = NSLayoutConstraint(item: shareDialogController.view, attribute: NSLayoutAttribute.centerX,
relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0)
cx.priority = 1000 // TODO: Why does UILayoutPriorityRequired give a linker error? SDK Bug?
view.addConstraint(cx)
view.addConstraint(NSLayoutConstraint(item: shareDialogController.view, attribute: NSLayoutAttribute.centerY,
relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: 0))
// Fade the dialog in
shareDialogController.view.alpha = 0.0
UIView.animate(withDuration: 0.25, animations: { () -> Void in
self.shareDialogController.view.alpha = 1.0
}, completion: nil)
}
func dismissShareDialog() {
shareDialogController.willMove(toParentViewController: nil)
shareDialogController.view.removeFromSuperview()
shareDialogController.removeFromParentViewController()
}
}