<commit_before>/* 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 lazy var profile: Profile = { return BrowserProfile(localName: "profile", app: nil) }()
    private var sharedItem: ShareItem?

    override func viewDidLoad() {
        view.backgroundColor = UIColor.whiteColor()

        super.viewDidLoad()

        guard profile.hasAccount() else {
            let instructionsViewController = InstructionsViewController()
            instructionsViewController.delegate = self
            let navigationController = UINavigationController(rootViewController: instructionsViewController)
            presentViewController(navigationController, animated: false, completion: nil)
            return
        }

        ExtensionUtils.extractSharedItemFromExtensionContext(self.extensionContext, completionHandler: { (item, error) -> Void in
            guard let item = item where 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.presentViewController(alert, animated: true, completion: nil)
                return
            }

            let clientPickerViewController = ClientPickerViewController()
            clientPickerViewController.clientPickerDelegate = self
            clientPickerViewController.profile = self.profile
            let navigationController = UINavigationController(rootViewController: clientPickerViewController)
            self.presentViewController(navigationController, animated: false, completion: nil)
        })
    }

    func finish() {
        self.extensionContext!.completeRequestReturningItems(nil, completionHandler: nil)
    }

    func clientPickerViewController(clientPickerViewController: ClientPickerViewController, didPickClients clients: [RemoteClient]) {
        // TODO: hook up Send Tab via Sync.
        // profile?.clients.sendItem(self.sharedItem!, toClients: clients)
        if let item = sharedItem {
            self.profile.sendItems([item], toClients: clients)
        }
        finish()
    }<commit_msg>Bug 1278854 - Added back sharedItem assignment so SendTo can properly send tabs (#1902) r=bnicholson<commit_after>/* 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 lazy var profile: Profile = { return BrowserProfile(localName: "profile", app: nil) }()
    private var sharedItem: ShareItem?

    override func viewDidLoad() {
        view.backgroundColor = UIColor.whiteColor()

        super.viewDidLoad()

        guard profile.hasAccount() else {
            let instructionsViewController = InstructionsViewController()
            instructionsViewController.delegate = self
            let navigationController = UINavigationController(rootViewController: instructionsViewController)
            presentViewController(navigationController, animated: false, completion: nil)
            return
        }

        ExtensionUtils.extractSharedItemFromExtensionContext(self.extensionContext, completionHandler: { (item, error) -> Void in
            guard let item = item where 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.presentViewController(alert, animated: true, completion: nil)
                return
            }

            self.sharedItem = item
            let clientPickerViewController = ClientPickerViewController()
            clientPickerViewController.clientPickerDelegate = self
            clientPickerViewController.profile = self.profile
            let navigationController = UINavigationController(rootViewController: clientPickerViewController)
            self.presentViewController(navigationController, animated: false, completion: nil)
        })
    }

    func finish() {
        self.extensionContext!.completeRequestReturningItems(nil, completionHandler: nil)
    }

    func clientPickerViewController(clientPickerViewController: ClientPickerViewController, didPickClients clients: [RemoteClient]) {
        // TODO: hook up Send Tab via Sync.
        // profile?.clients.sendItem(self.sharedItem!, toClients: clients)
        if let item = sharedItem {
            self.profile.sendItems([item], toClients: clients)
        }
        finish()
    }
{'fraction_non_alphanumeric': 0.07307561885384876, 'fraction_numerical': 0.003221430993557138, 'mean_word_length': 3.3375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '403317', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1320, 'n_words': 425}