From df79da291fde8735f6c82e7491fba1f03c318e7f Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Tue, 14 Apr 2020 22:33:05 -0500 Subject: [PATCH] Change how ExtensionPoint titles are handled. --- .../Twitter/TwitterFeedProvider.swift | 19 ++++++++++++++-- .../ExtensionPoints/ExtensionPointDetail.xib | 16 ++++++++------ .../ExtensionPointDetailViewController.swift | 14 ++++++------ ...ExtensionPointEnableWindowController.swift | 5 ++--- ...ensionPointPreferencesViewController.swift | 22 +++++++++---------- Shared/ExtensionPoints/ExtensionPoint.swift | 9 ++++++-- .../ExtensionPointIdentifer.swift | 17 -------------- .../ExtensionPointManager.swift | 15 ++++++++----- .../SendToMarsEditCommand.swift | 4 ++++ .../SendToMicroBlogCommand.swift | 4 ++++ .../TwitterFeedProvider+Extensions.swift | 6 ++++- 11 files changed, 76 insertions(+), 55 deletions(-) diff --git a/Frameworks/FeedProvider/Twitter/TwitterFeedProvider.swift b/Frameworks/FeedProvider/Twitter/TwitterFeedProvider.swift index b82f05b41..4a8236894 100644 --- a/Frameworks/FeedProvider/Twitter/TwitterFeedProvider.swift +++ b/Frameworks/FeedProvider/Twitter/TwitterFeedProvider.swift @@ -12,10 +12,25 @@ import OAuthSwift public struct TwitterFeedProvider { - public var username: String + public var userID: String + public var screenName: String + + public init(tokenSuccess: OAuthSwift.TokenSuccess) { + // TODO: beef this up + userID = tokenSuccess.parameters["user_id"] as? String ?? "" + screenName = tokenSuccess.parameters["screen_name"] as? String ?? "" + + // let token = tokenSuccess.credential.oauthToken + // let secret = tokenSuccess.credential.oauthTokenSecret + + // TODO: save credentials here + } public init(username: String) { - self.username = username + self.userID = username + self.screenName = "Stored Somewhere" + + // TODO: load credentials here } } diff --git a/Mac/Preferences/ExtensionPoints/ExtensionPointDetail.xib b/Mac/Preferences/ExtensionPoints/ExtensionPointDetail.xib index ef4f72148..a5740062d 100644 --- a/Mac/Preferences/ExtensionPoints/ExtensionPointDetail.xib +++ b/Mac/Preferences/ExtensionPoints/ExtensionPointDetail.xib @@ -26,20 +26,20 @@ - + - + - - + + - + - + @@ -55,7 +55,7 @@ - + @@ -70,7 +70,9 @@ + + diff --git a/Mac/Preferences/ExtensionPoints/ExtensionPointDetailViewController.swift b/Mac/Preferences/ExtensionPoints/ExtensionPointDetailViewController.swift index d7829b4d2..da5749f82 100644 --- a/Mac/Preferences/ExtensionPoints/ExtensionPointDetailViewController.swift +++ b/Mac/Preferences/ExtensionPoints/ExtensionPointDetailViewController.swift @@ -15,11 +15,11 @@ class ExtensionPointDetailViewController: NSViewController { @IBOutlet weak var descriptionLabel: NSTextField! private var extensionPointWindowController: NSWindowController? - private var extensionPointID: ExtensionPointIdentifer? + private var extensionPoint: ExtensionPoint? - init(extensionPointID: ExtensionPointIdentifer) { + init(extensionPoint: ExtensionPoint) { super.init(nibName: "ExtensionPointDetail", bundle: nil) - self.extensionPointID = extensionPointID + self.extensionPoint = extensionPoint } public required init?(coder: NSCoder) { @@ -28,10 +28,10 @@ class ExtensionPointDetailViewController: NSViewController { override func viewDidLoad() { super.viewDidLoad() - guard let extensionPointID = extensionPointID else { return } - imageView.image = extensionPointID.templateImage - titleLabel.stringValue = extensionPointID.title - descriptionLabel.attributedStringValue = extensionPointID.description + guard let extensionPoint = extensionPoint else { return } + imageView.image = extensionPoint.templateImage + titleLabel.stringValue = extensionPoint.title + descriptionLabel.attributedStringValue = extensionPoint.description } } diff --git a/Mac/Preferences/ExtensionPoints/ExtensionPointEnableWindowController.swift b/Mac/Preferences/ExtensionPoints/ExtensionPointEnableWindowController.swift index 62a682abf..3b222b9ea 100644 --- a/Mac/Preferences/ExtensionPoints/ExtensionPointEnableWindowController.swift +++ b/Mac/Preferences/ExtensionPoints/ExtensionPointEnableWindowController.swift @@ -116,13 +116,12 @@ private extension ExtensionPointEnableWindowController { oauth1.authorizeURLHandler = self oauth1.authorize(withCallbackURL: callbackURL) { [weak self] result in - guard let self = self else { return } + guard let self = self, let extensionPointType = self.extensionPointType else { return } switch result { case .success(let tokenSuccess): - // let token = tokenSuccess.credential.oauthToken - // let secret = tokenSuccess.credential.oauthTokenSecret + ExtensionPointManager.shared.activateExtensionPoint(extensionPointType, tokenSuccess: tokenSuccess) let screenName = tokenSuccess.parameters["screen_name"] as? String ?? "" print("******************* \(screenName)") self.hostWindow!.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK) diff --git a/Mac/Preferences/ExtensionPoints/ExtensionPointPreferencesViewController.swift b/Mac/Preferences/ExtensionPoints/ExtensionPointPreferencesViewController.swift index 8445a2ddc..25038d720 100644 --- a/Mac/Preferences/ExtensionPoints/ExtensionPointPreferencesViewController.swift +++ b/Mac/Preferences/ExtensionPoints/ExtensionPointPreferencesViewController.swift @@ -14,7 +14,7 @@ final class ExtensionPointPreferencesViewController: NSViewController { @IBOutlet weak var detailView: NSView! @IBOutlet weak var deleteButton: NSButton! - private var activeExtensionPointIDs = [ExtensionPointIdentifer]() + private var activeExtensionPoints = [ExtensionPoint]() override func viewDidLoad() { super.viewDidLoad() @@ -42,8 +42,8 @@ final class ExtensionPointPreferencesViewController: NSViewController { return } - let extensionPointID = activeExtensionPointIDs[tableView.selectedRow] - ExtensionPointManager.shared.deactivateExtensionPoint(extensionPointID) + let extensionPoint = activeExtensionPoints[tableView.selectedRow] + ExtensionPointManager.shared.deactivateExtensionPoint(extensionPoint.extensionPointID) showController(ExtensionPointAddViewController()) } @@ -54,11 +54,11 @@ final class ExtensionPointPreferencesViewController: NSViewController { extension ExtensionPointPreferencesViewController: NSTableViewDataSource { func numberOfRows(in tableView: NSTableView) -> Int { - return activeExtensionPointIDs.count + return activeExtensionPoints.count } func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? { - return activeExtensionPointIDs[row] + return activeExtensionPoints[row] } } @@ -70,9 +70,9 @@ extension ExtensionPointPreferencesViewController: NSTableViewDelegate { func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"), owner: nil) as? NSTableCellView { - let extensionPointID = activeExtensionPointIDs[row] - cell.textField?.stringValue = extensionPointID.title - cell.imageView?.image = extensionPointID.templateImage + let extensionPoint = activeExtensionPoints[row] + cell.textField?.stringValue = extensionPoint.title + cell.imageView?.image = extensionPoint.templateImage return cell } return nil @@ -88,8 +88,8 @@ extension ExtensionPointPreferencesViewController: NSTableViewDelegate { deleteButton.isEnabled = true } - let extensionPointID = activeExtensionPointIDs[selectedRow] - let controller = ExtensionPointDetailViewController(extensionPointID: extensionPointID) + let extensionPoint = activeExtensionPoints[selectedRow] + let controller = ExtensionPointDetailViewController(extensionPoint: extensionPoint) showController(controller) } @@ -105,7 +105,7 @@ private extension ExtensionPointPreferencesViewController { } func showDefaultView() { - activeExtensionPointIDs = Array(ExtensionPointManager.shared.activeExtensionPoints.keys).sorted(by: { $0.title < $1.title }) + activeExtensionPoints = Array(ExtensionPointManager.shared.activeExtensionPoints.values).sorted(by: { $0.title < $1.title }) tableView.reloadData() showController(ExtensionPointAddViewController()) } diff --git a/Shared/ExtensionPoints/ExtensionPoint.swift b/Shared/ExtensionPoints/ExtensionPoint.swift index 44d5deb9f..fede4fbfe 100644 --- a/Shared/ExtensionPoints/ExtensionPoint.swift +++ b/Shared/ExtensionPoints/ExtensionPoint.swift @@ -16,16 +16,21 @@ protocol ExtensionPoint { static var templateImage: RSImage { get } static var description: NSAttributedString { get } + var title: String { get } var extensionPointID: ExtensionPointIdentifer { get } } extension ExtensionPoint { - var title: String { - return extensionPointID.title + var templateImage: RSImage { + return extensionPointID.type.templateImage } + var description: NSAttributedString { + return extensionPointID.type.description + } + static func makeAttrString(_ text: String) -> NSMutableAttributedString { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center diff --git a/Shared/ExtensionPoints/ExtensionPointIdentifer.swift b/Shared/ExtensionPoints/ExtensionPointIdentifer.swift index 87cc5deac..099b4196d 100644 --- a/Shared/ExtensionPoints/ExtensionPointIdentifer.swift +++ b/Shared/ExtensionPoints/ExtensionPointIdentifer.swift @@ -15,23 +15,6 @@ enum ExtensionPointIdentifer: Hashable { case marsEdit case microblog case twitter(String) - - var title: String { - switch self { - case .twitter(let username): - return "\(type.title) (\(username)" - default: - return type.title - } - } - - var templateImage: RSImage { - return type.templateImage - } - - var description: NSAttributedString { - return type.description - } var type: ExtensionPoint.Type { switch self { diff --git a/Shared/ExtensionPoints/ExtensionPointManager.swift b/Shared/ExtensionPoints/ExtensionPointManager.swift index d0dd4453a..e62db219c 100644 --- a/Shared/ExtensionPoints/ExtensionPointManager.swift +++ b/Shared/ExtensionPoints/ExtensionPointManager.swift @@ -9,6 +9,7 @@ import Foundation import FeedProvider import RSCore +import OAuthSwift public extension Notification.Name { static let ActiveExtensionPointsDidChange = Notification.Name(rawValue: "ActiveExtensionPointsDidChange") @@ -62,8 +63,8 @@ final class ExtensionPointManager { loadExtensionPoints() } - func activateExtensionPoint(_ extensionPointType: ExtensionPoint.Type) { - if let extensionPoint = self.extensionPoint(for: extensionPointType) { + func activateExtensionPoint(_ extensionPointType: ExtensionPoint.Type, tokenSuccess: OAuthSwift.TokenSuccess? = nil) { + if let extensionPoint = self.extensionPoint(for: extensionPointType, tokenSuccess: tokenSuccess) { activeExtensionPoints[extensionPoint.extensionPointID] = extensionPoint saveExtensionPointIDs() } @@ -93,14 +94,18 @@ private extension ExtensionPointManager { NotificationCenter.default.post(name: .ActiveExtensionPointsDidChange, object: nil, userInfo: nil) } - func extensionPoint(for extensionPointType: ExtensionPoint.Type) -> ExtensionPoint? { + func extensionPoint(for extensionPointType: ExtensionPoint.Type, tokenSuccess: OAuthSwift.TokenSuccess?) -> ExtensionPoint? { switch extensionPointType { case is SendToMarsEditCommand.Type: return SendToMarsEditCommand() case is SendToMicroBlogCommand.Type: return SendToMicroBlogCommand() -// case is TwitterFeedProvider.Type: -// return TwitterFeedProvider(username: username) + case is TwitterFeedProvider.Type: + if let tokenSuccess = tokenSuccess { + return TwitterFeedProvider(tokenSuccess: tokenSuccess) + } else { + return nil + } default: assertionFailure("Unrecognized Extension Point Type.") } diff --git a/Shared/ExtensionPoints/SendToMarsEditCommand.swift b/Shared/ExtensionPoints/SendToMarsEditCommand.swift index a53811de9..820ace4e2 100644 --- a/Shared/ExtensionPoints/SendToMarsEditCommand.swift +++ b/Shared/ExtensionPoints/SendToMarsEditCommand.swift @@ -27,6 +27,10 @@ final class SendToMarsEditCommand: ExtensionPoint, SendToCommand { let extensionPointID = ExtensionPointIdentifer.marsEdit + var title: String { + return extensionPointID.type.title + } + var image: NSImage? { return appToUse()?.icon ?? nil } diff --git a/Shared/ExtensionPoints/SendToMicroBlogCommand.swift b/Shared/ExtensionPoints/SendToMicroBlogCommand.swift index 739554615..a48eca60e 100644 --- a/Shared/ExtensionPoints/SendToMicroBlogCommand.swift +++ b/Shared/ExtensionPoints/SendToMicroBlogCommand.swift @@ -30,6 +30,10 @@ final class SendToMicroBlogCommand: ExtensionPoint, SendToCommand { let extensionPointID = ExtensionPointIdentifer.microblog + var title: String { + return extensionPointID.type.title + } + var image: NSImage? { return microBlogApp.icon } diff --git a/Shared/ExtensionPoints/TwitterFeedProvider+Extensions.swift b/Shared/ExtensionPoints/TwitterFeedProvider+Extensions.swift index cebcca7e8..162326492 100644 --- a/Shared/ExtensionPoints/TwitterFeedProvider+Extensions.swift +++ b/Shared/ExtensionPoints/TwitterFeedProvider+Extensions.swift @@ -28,7 +28,11 @@ extension TwitterFeedProvider: ExtensionPoint { }() var extensionPointID: ExtensionPointIdentifer { - return ExtensionPointIdentifer.twitter(username) + return ExtensionPointIdentifer.twitter(userID) + } + + var title: String { + return "@\(screenName)" } }