2020-04-07 22:25:33 +02:00
|
|
|
//
|
|
|
|
// ExtensionPointManager.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 4/7/20.
|
|
|
|
// Copyright © 2020 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import FeedProvider
|
|
|
|
import RSCore
|
|
|
|
|
2020-04-09 03:22:13 +02:00
|
|
|
public extension Notification.Name {
|
|
|
|
static let ActiveExtensionPointsDidChange = Notification.Name(rawValue: "ActiveExtensionPointsDidChange")
|
|
|
|
}
|
|
|
|
|
2020-04-08 20:46:15 +02:00
|
|
|
final class ExtensionPointManager {
|
2020-04-07 22:25:33 +02:00
|
|
|
|
|
|
|
static let shared = ExtensionPointManager()
|
2020-04-08 20:46:15 +02:00
|
|
|
|
|
|
|
var activeExtensionPoints = [ExtensionPointIdentifer: ExtensionPoint]()
|
2020-04-14 23:47:05 +02:00
|
|
|
let possibleExtensionPointTypes: [ExtensionPoint.Type]
|
|
|
|
var availableExtensionPointTypes: [ExtensionPoint.Type] {
|
2020-04-09 03:22:13 +02:00
|
|
|
|
2020-04-14 23:47:05 +02:00
|
|
|
let activeExtensionPointTypes = activeExtensionPoints.keys.compactMap({ ObjectIdentifier($0.type) })
|
|
|
|
var available = [ExtensionPoint.Type]()
|
2020-04-09 03:22:13 +02:00
|
|
|
for possibleExtensionPointType in possibleExtensionPointTypes {
|
|
|
|
if possibleExtensionPointType.isSinglton {
|
2020-04-14 23:47:05 +02:00
|
|
|
if !activeExtensionPointTypes.contains(ObjectIdentifier(possibleExtensionPointType)) {
|
2020-04-09 03:22:13 +02:00
|
|
|
available.append(possibleExtensionPointType)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
available.append(possibleExtensionPointType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return available
|
|
|
|
}
|
2020-04-08 20:46:15 +02:00
|
|
|
|
|
|
|
var activeSendToCommands: [SendToCommand] {
|
|
|
|
return activeExtensionPoints.values.compactMap({ return $0 as? SendToCommand })
|
|
|
|
}
|
|
|
|
|
|
|
|
var activeFeedProviders: [FeedProvider] {
|
|
|
|
return activeExtensionPoints.values.compactMap({ return $0 as? FeedProvider })
|
|
|
|
}
|
2020-04-07 22:25:33 +02:00
|
|
|
|
|
|
|
init() {
|
|
|
|
#if os(macOS)
|
|
|
|
#if DEBUG
|
2020-04-14 23:47:05 +02:00
|
|
|
possibleExtensionPointTypes = [SendToMarsEditCommand.self, SendToMicroBlogCommand.self, TwitterFeedProvider.self]
|
2020-04-07 22:25:33 +02:00
|
|
|
#else
|
2020-04-14 23:47:05 +02:00
|
|
|
possibleExtensionPointTypes = [SendToMarsEditCommand.self, SendToMicroBlogCommand.self, TwitterFeedProvider.self]
|
2020-04-07 22:25:33 +02:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#if DEBUG
|
2020-04-14 23:47:05 +02:00
|
|
|
possibleExtensionPointTypes = [TwitterFeedProvider.self]
|
2020-04-07 22:25:33 +02:00
|
|
|
#else
|
2020-04-14 23:47:05 +02:00
|
|
|
possibleExtensionPointTypes = [TwitterFeedProvider.self]
|
2020-04-07 22:25:33 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2020-04-09 03:22:13 +02:00
|
|
|
loadExtensionPoints()
|
2020-04-08 20:46:15 +02:00
|
|
|
}
|
|
|
|
|
2020-04-14 23:47:05 +02:00
|
|
|
func activateExtensionPoint(_ extensionPointType: ExtensionPoint.Type) {
|
|
|
|
if let extensionPoint = self.extensionPoint(for: extensionPointType) {
|
|
|
|
activeExtensionPoints[extensionPoint.extensionPointID] = extensionPoint
|
|
|
|
saveExtensionPointIDs()
|
|
|
|
}
|
2020-04-08 20:46:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func deactivateExtensionPoint(_ extensionPointID: ExtensionPointIdentifer) {
|
|
|
|
activeExtensionPoints[extensionPointID] = nil
|
|
|
|
saveExtensionPointIDs()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension ExtensionPointManager {
|
|
|
|
|
2020-04-09 03:22:13 +02:00
|
|
|
func loadExtensionPoints() {
|
2020-04-08 20:46:15 +02:00
|
|
|
if let extensionPointUserInfos = AppDefaults.activeExtensionPointIDs {
|
|
|
|
for extensionPointUserInfo in extensionPointUserInfos {
|
|
|
|
if let extensionPointID = ExtensionPointIdentifer(userInfo: extensionPointUserInfo) {
|
|
|
|
activeExtensionPoints[extensionPointID] = extensionPoint(for: extensionPointID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveExtensionPointIDs() {
|
|
|
|
AppDefaults.activeExtensionPointIDs = activeExtensionPoints.keys.map({ $0.userInfo })
|
2020-04-09 03:22:13 +02:00
|
|
|
NotificationCenter.default.post(name: .ActiveExtensionPointsDidChange, object: nil, userInfo: nil)
|
2020-04-08 20:46:15 +02:00
|
|
|
}
|
|
|
|
|
2020-04-14 23:47:05 +02:00
|
|
|
func extensionPoint(for extensionPointType: ExtensionPoint.Type) -> ExtensionPoint? {
|
|
|
|
switch extensionPointType {
|
|
|
|
case is SendToMarsEditCommand.Type:
|
|
|
|
return SendToMarsEditCommand()
|
|
|
|
case is SendToMicroBlogCommand.Type:
|
|
|
|
return SendToMicroBlogCommand()
|
|
|
|
// case is TwitterFeedProvider.Type:
|
|
|
|
// return TwitterFeedProvider(username: username)
|
|
|
|
default:
|
|
|
|
assertionFailure("Unrecognized Extension Point Type.")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-08 20:46:15 +02:00
|
|
|
func extensionPoint(for extensionPointID: ExtensionPointIdentifer) -> ExtensionPoint {
|
|
|
|
switch extensionPointID {
|
|
|
|
case .marsEdit:
|
|
|
|
return SendToMarsEditCommand()
|
|
|
|
case .microblog:
|
|
|
|
return SendToMicroBlogCommand()
|
|
|
|
case .twitter(let username):
|
|
|
|
return TwitterFeedProvider(username: username)
|
|
|
|
}
|
2020-04-07 22:25:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|