NetNewsWire/Frameworks/Account/FeedProvider/FeedProviderManager.swift

43 lines
994 B
Swift
Raw Normal View History

2020-04-16 22:06:56 +02:00
//
// FeedProviderManager.swift
// Account
//
// Created by Maurice Parker on 4/16/20.
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
//
import Foundation
public protocol FeedProviderManagerDelegate: class {
var activeFeedProviders: [FeedProvider] { get }
}
public final class FeedProviderManager {
public static let shared = FeedProviderManager()
public weak var delegate: FeedProviderManagerDelegate?
public func best(for offered: URLComponents) -> FeedProvider? {
if let owner = feedProviderMatching(offered, ability: .owner) {
2020-04-16 22:06:56 +02:00
return owner
}
return feedProviderMatching(offered, ability: .available)
2020-04-16 22:06:56 +02:00
}
}
private extension FeedProviderManager {
func feedProviderMatching(_ offered: URLComponents, ability: FeedProviderAbility) -> FeedProvider? {
2020-04-16 22:06:56 +02:00
if let delegate = delegate {
for feedProvider in delegate.activeFeedProviders {
if feedProvider.ability(offered) == ability {
2020-04-16 22:06:56 +02:00
return feedProvider
}
}
}
return nil
}
}