-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathActionViewController.swift
79 lines (65 loc) · 3.23 KB
/
ActionViewController.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
/* 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
import SnapKit
/// The ActionViewController is the initial viewcontroller that is presented (full screen) when the share extension
/// is activated. Depending on whether the user is logged in or not, this viewcontroller will present either the
/// InstructionsVC or the ClientPicker VC.
@objc(ActionViewController)
class ActionViewController: UIViewController, ClientPickerViewControllerDelegate, InstructionsViewControllerDelegate {
private var sharedItem: ShareItem?
override func viewDidLoad() {
view.backgroundColor = UIColor.white
super.viewDidLoad()
if !hasAccount() {
let instructionsViewController = InstructionsViewController()
instructionsViewController.delegate = self
let navigationController = UINavigationController(rootViewController: instructionsViewController)
present(navigationController, animated: false, completion: nil)
return
}
ExtensionUtils.extractSharedItemFromExtensionContext(self.extensionContext, completionHandler: { (item, error) -> Void in
guard let item = item, error == nil, 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.sharedItem = item
let clientPickerViewController = ClientPickerViewController()
clientPickerViewController.clientPickerDelegate = self
clientPickerViewController.profile = nil // This means the picker will open and close the default profile
let navigationController = UINavigationController(rootViewController: clientPickerViewController)
self.present(navigationController, animated: false, completion: nil)
})
}
func finish() {
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
}
func clientPickerViewController(_ clientPickerViewController: ClientPickerViewController, didPickClients clients: [RemoteClient]) {
guard let item = sharedItem else {
return finish()
}
let profile = BrowserProfile(localName: "profile")
profile.sendItems([item], toClients: clients).uponQueue(DispatchQueue.main) { result in
profile.shutdown()
self.finish()
}
}
func clientPickerViewControllerDidCancel(_ clientPickerViewController: ClientPickerViewController) {
finish()
}
func instructionsViewControllerDidClose(_ instructionsViewController: InstructionsViewController) {
finish()
}
private func hasAccount() -> Bool {
let profile = BrowserProfile(localName: "profile")
defer {
profile.shutdown()
}
return profile.hasAccount()
}
}