NetNewsWire/iOS/ShareExtension/ShareViewController.swift

176 lines
4.9 KiB
Swift
Raw Normal View History

//
// 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 Social
import Account
import Articles
import RSCore
import RSTree
2019-09-12 19:33:05 +02:00
class ShareViewController: SLComposeServiceViewController, ShareFolderPickerControllerDelegate {
private var url: URL?
2019-09-12 19:33:05 +02:00
private var container: Container?
private var folderItem: SLComposeSheetConfigurationItem!
override func viewDidLoad() {
2019-09-22 23:00:06 +02:00
AccountManager.shared = AccountManager()
2019-09-22 20:09:06 +02:00
container = AddWebFeedDefaultContainer.defaultContainer
2019-09-12 18:24:43 +02:00
title = "NetNewsWire"
placeholder = "Feed Name (Optional)"
if let button = navigationController?.navigationBar.topItem?.rightBarButtonItem {
button.title = "Add Feed"
button.isEnabled = true
}
// 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
// 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
}
}
}
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
}
// 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
}
}
}
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 {
2019-09-12 19:33:05 +02:00
return url != nil && container != nil
}
override func didSelectPost() {
2019-09-12 19:33:05 +02:00
var account: Account?
if let containerAccount = container as? Account {
account = containerAccount
} else if let containerFolder = container as? Folder, let containerAccount = containerFolder.account {
account = containerAccount
}
if let urlString = url?.absoluteString, account!.hasWebFeed(withURL: urlString) {
2019-09-12 19:33:05 +02:00
presentError(AccountError.createErrorAlreadySubscribed)
return
}
let feedName = contentText.isEmpty ? nil : contentText
account!.createWebFeed(url: url!.absoluteString, name: feedName, container: container!) { result in
switch result {
2019-09-12 18:24:43 +02:00
case .success:
account!.save()
2019-09-12 18:24:43 +02:00
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
case .failure(let error):
2019-09-12 18:24:43 +02:00
self.presentError(error) {
self.extensionContext!.cancelRequest(withError: error)
}
}
}
}
func shareFolderPickerDidSelect(_ container: Container) {
AddWebFeedDefaultContainer.saveDefaultContainer(container)
2019-09-12 19:33:05 +02:00
self.container = container
updateFolderItemValue()
self.popConfigurationViewController()
2019-09-12 19:33:05 +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 ?? ""
folderItem = SLComposeSheetConfigurationItem()
folderItem.title = "Folder"
updateFolderItemValue()
2019-09-12 19:33:05 +02:00
folderItem.tapHandler = {
let folderPickerController = ShareFolderPickerController()
folderPickerController.navigationController?.title = NSLocalizedString("Folder", comment: "Folder")
folderPickerController.delegate = self
folderPickerController.selectedContainer = self.container
self.pushConfigurationViewController(folderPickerController)
}
return [folderItem!, urlItem]
2019-09-12 19:33:05 +02:00
}
}
private extension ShareViewController {
func updateFolderItemValue() {
if let containerName = (container as? DisplayNameProvider)?.nameForDisplay {
if container is Folder {
self.folderItem.value = "\(container?.account?.nameForDisplay ?? "") / \(containerName)"
} else {
self.folderItem.value = containerName
}
}
}
}