2019-09-09 00:35:38 +02:00
|
|
|
//
|
|
|
|
// ShareViewController.swift
|
|
|
|
// NetNewsWire iOS Share Extension
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 9/8/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import MobileCoreServices
|
|
|
|
import Account
|
2020-02-09 22:08:11 +01:00
|
|
|
import Social
|
2019-09-09 00:35:38 +02:00
|
|
|
import RSCore
|
|
|
|
import RSTree
|
|
|
|
|
2019-09-12 19:33:05 +02:00
|
|
|
class ShareViewController: SLComposeServiceViewController, ShareFolderPickerControllerDelegate {
|
2019-09-09 00:35:38 +02:00
|
|
|
|
|
|
|
private var url: URL?
|
2020-02-09 22:08:11 +01:00
|
|
|
private var extensionContainers: ExtensionContainers?
|
|
|
|
private var flattenedContainers: [ExtensionContainer]!
|
|
|
|
private var selectedContainer: ExtensionContainer?
|
2019-09-22 23:36:28 +02:00
|
|
|
private var folderItem: SLComposeSheetConfigurationItem!
|
2019-09-09 00:35:38 +02:00
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
|
2020-02-09 22:08:11 +01:00
|
|
|
extensionContainers = ExtensionContainersFile.read()
|
|
|
|
flattenedContainers = extensionContainers?.flattened ?? [ExtensionContainer]()
|
|
|
|
if let extensionContainers = extensionContainers {
|
|
|
|
selectedContainer = ShareDefaultContainer.defaultContainer(containers: extensionContainers)
|
|
|
|
}
|
2019-09-12 18:24:43 +02:00
|
|
|
|
2019-09-09 00:35:38 +02:00
|
|
|
title = "NetNewsWire"
|
|
|
|
placeholder = "Feed Name (Optional)"
|
|
|
|
if let button = navigationController?.navigationBar.topItem?.rightBarButtonItem {
|
|
|
|
button.title = "Add Feed"
|
|
|
|
button.isEnabled = true
|
|
|
|
}
|
2020-02-09 22:08:11 +01:00
|
|
|
|
2019-09-09 00:35:38 +02:00
|
|
|
// Hack the bottom table rows to be smaller since the controller itself doesn't have enough sense to size itself correctly
|
|
|
|
if let nav = self.children.first as? UINavigationController, let tableView = nav.children.first?.view.subviews.first as? UITableView {
|
|
|
|
tableView.rowHeight = 38
|
|
|
|
}
|
|
|
|
|
|
|
|
var provider: NSItemProvider? = nil
|
2020-02-09 22:08:11 +01:00
|
|
|
|
2019-09-09 00:35:38 +02:00
|
|
|
// Try to get any HTML that is maybe passed in
|
|
|
|
for item in self.extensionContext!.inputItems as! [NSExtensionItem] {
|
|
|
|
for itemProvider in item.attachments! {
|
|
|
|
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypePropertyList as String) {
|
|
|
|
provider = itemProvider
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-09 22:08:11 +01:00
|
|
|
|
2019-09-09 00:35:38 +02:00
|
|
|
if provider != nil {
|
|
|
|
provider!.loadItem(forTypeIdentifier: kUTTypePropertyList as String, options: nil, completionHandler: { [weak self] (pList, error) in
|
|
|
|
if error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let dataGraph = pList as? NSDictionary else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let results = dataGraph["NSExtensionJavaScriptPreprocessingResultsKey"] as? NSDictionary else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if let url = URL(string: results["url"] as! String) {
|
|
|
|
self?.url = url
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2020-02-09 22:08:11 +01:00
|
|
|
|
2019-09-09 00:35:38 +02:00
|
|
|
// Try to get the URL if it is passed in
|
|
|
|
for item in self.extensionContext!.inputItems as! [NSExtensionItem] {
|
|
|
|
for itemProvider in item.attachments! {
|
|
|
|
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeURL as String) {
|
|
|
|
provider = itemProvider
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-09 22:08:11 +01:00
|
|
|
|
2019-09-09 00:35:38 +02:00
|
|
|
if provider != nil {
|
|
|
|
provider!.loadItem(forTypeIdentifier: kUTTypeURL as String, options: nil, completionHandler: { [weak self] (urlCoded, error) in
|
|
|
|
if error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let url = urlCoded as? URL else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self?.url = url
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override func isContentValid() -> Bool {
|
2020-02-09 22:08:11 +01:00
|
|
|
return url != nil && selectedContainer != nil
|
2019-09-09 00:35:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func didSelectPost() {
|
2020-02-09 22:08:11 +01:00
|
|
|
guard let url = url, let selectedContainer = selectedContainer, let containerID = selectedContainer.containerID else {
|
2020-01-30 21:59:15 +01:00
|
|
|
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
|
|
|
|
return
|
|
|
|
}
|
2020-02-09 22:08:11 +01:00
|
|
|
|
|
|
|
let name = contentText.isEmpty ? nil : contentText
|
|
|
|
let request = ExtensionFeedAddRequest(name: name, feedURL: url, destinationContainerID: containerID)
|
|
|
|
ExtensionFeedAddRequestFile.save(request)
|
2020-01-09 01:46:22 +01:00
|
|
|
|
|
|
|
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
|
2019-09-09 00:35:38 +02:00
|
|
|
}
|
|
|
|
|
2020-02-09 22:08:11 +01:00
|
|
|
func shareFolderPickerDidSelect(_ container: ExtensionContainer) {
|
|
|
|
ShareDefaultContainer.saveDefaultContainer(container)
|
|
|
|
self.selectedContainer = container
|
2019-11-17 02:44:01 +01:00
|
|
|
updateFolderItemValue()
|
2019-09-22 23:36:28 +02:00
|
|
|
self.popConfigurationViewController()
|
2019-09-12 19:33:05 +02:00
|
|
|
}
|
|
|
|
|
2019-09-09 00:35:38 +02:00
|
|
|
override func configurationItems() -> [Any]! {
|
|
|
|
|
|
|
|
// To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
|
|
|
|
guard let urlItem = SLComposeSheetConfigurationItem() else { return nil }
|
|
|
|
urlItem.title = "URL"
|
|
|
|
urlItem.value = url?.absoluteString ?? ""
|
|
|
|
|
2019-09-22 23:36:28 +02:00
|
|
|
folderItem = SLComposeSheetConfigurationItem()
|
2019-09-09 00:35:38 +02:00
|
|
|
folderItem.title = "Folder"
|
2019-11-17 02:44:01 +01:00
|
|
|
updateFolderItemValue()
|
2019-09-09 00:35:38 +02:00
|
|
|
|
2019-09-12 19:33:05 +02:00
|
|
|
folderItem.tapHandler = {
|
|
|
|
|
|
|
|
let folderPickerController = ShareFolderPickerController()
|
|
|
|
|
|
|
|
folderPickerController.navigationController?.title = NSLocalizedString("Folder", comment: "Folder")
|
|
|
|
folderPickerController.delegate = self
|
2020-02-09 22:08:11 +01:00
|
|
|
folderPickerController.containers = self.flattenedContainers
|
|
|
|
folderPickerController.selectedContainerID = self.selectedContainer?.containerID
|
2019-09-12 19:33:05 +02:00
|
|
|
|
|
|
|
self.pushConfigurationViewController(folderPickerController)
|
|
|
|
|
|
|
|
}
|
2019-09-09 00:35:38 +02:00
|
|
|
|
2019-09-22 23:36:28 +02:00
|
|
|
return [folderItem!, urlItem]
|
2019-09-12 19:33:05 +02:00
|
|
|
|
2019-09-09 00:35:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-11-17 02:44:01 +01:00
|
|
|
|
|
|
|
private extension ShareViewController {
|
|
|
|
|
|
|
|
func updateFolderItemValue() {
|
2020-02-09 22:08:11 +01:00
|
|
|
if let account = selectedContainer as? ExtensionAccount {
|
|
|
|
self.folderItem.value = account.name
|
|
|
|
} else if let folder = selectedContainer as? ExtensionFolder {
|
|
|
|
self.folderItem.value = "\(folder.accountName) / \(folder.name)"
|
2019-11-17 02:44:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|