NetNewsWire/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift

110 lines
3.1 KiB
Swift
Raw Normal View History

2017-09-17 20:32:58 +02:00
//
// LocalAccountDelegate.swift
// Account
//
// Created by Brent Simmons on 9/16/17.
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import RSWeb
2017-09-17 20:32:58 +02:00
2019-05-09 00:41:19 +02:00
public enum LocalAccountDelegateError: String, Error {
case invalidParameter = "An invalid parameter was used."
}
final class LocalAccountDelegate: AccountDelegate {
2017-09-28 22:16:47 +02:00
let supportsSubFolders = false
let server: String? = nil
2019-05-05 10:25:21 +02:00
var credentials: Credentials?
var accountMetadata: AccountMetadata?
2019-05-09 00:41:19 +02:00
private weak var account: Account?
private var feedFinder: FeedFinder?
private var createFeedCompletion: ((Result<AccountCreateFeedResult, Error>) -> Void)?
2017-10-02 22:15:07 +02:00
private let refresher = LocalAccountRefresher()
var refreshProgress: DownloadProgress {
return refresher.progress
}
2019-05-05 22:41:20 +02:00
// LocalAccountDelegate doesn't wait for completion before calling the completion block
2019-05-06 17:53:20 +02:00
func refreshAll(for account: Account, completion: (() -> Void)? = nil) {
refresher.refreshFeeds(account.flattenedFeeds())
2019-05-05 22:41:20 +02:00
completion?()
2017-09-17 20:32:58 +02:00
}
2019-05-07 00:34:41 +02:00
func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
2019-05-06 17:53:20 +02:00
folder.name = name
completion(.success(()))
}
2019-05-07 00:34:41 +02:00
func deleteFolder(for account: Account, with folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
account.deleteFolder(folder)
completion(.success(()))
}
2019-05-09 00:41:19 +02:00
func createFeed(for account: Account, with name: String?, url urlString: String, completion: @escaping (Result<AccountCreateFeedResult, Error>) -> Void) {
guard let url = URL(string: urlString) else {
completion(.failure(LocalAccountDelegateError.invalidParameter))
return
}
self.account = account
createFeedCompletion = completion
feedFinder = FeedFinder(url: url, delegate: self)
}
func renameFeed(for account: Account, with feed: Feed, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
feed.editedName = name
completion(.success(()))
}
func accountDidInitialize(_ account: Account) {
}
2019-05-06 17:53:20 +02:00
static func validateCredentials(transport: Transport, credentials: Credentials, completion: (Result<Bool, Error>) -> Void) {
return completion(.success(false))
}
2017-09-17 20:32:58 +02:00
}
2019-05-09 00:41:19 +02:00
extension LocalAccountDelegate: FeedFinderDelegate {
// MARK: FeedFinderDelegate
public func feedFinder(_ feedFinder: FeedFinder, didFindFeeds feedSpecifiers: Set<FeedSpecifier>) {
if let error = feedFinder.initialDownloadError {
if feedFinder.initialDownloadStatusCode == 404 {
createFeedCompletion!(.success(.notFound))
} else {
createFeedCompletion!(.failure(error))
}
return
}
guard let bestFeedSpecifier = FeedSpecifier.bestFeed(in: feedSpecifiers),
let url = URL(string: bestFeedSpecifier.urlString),
let account = account else {
createFeedCompletion!(.success(.notFound))
return
}
let feed = account.createFeed(with: nil, url: url.absoluteString, feedID: url.absoluteString, homePageURL: nil)
InitialFeedDownloader.download(url) { [weak self] parsedFeed in
if let parsedFeed = parsedFeed {
account.update(feed, with: parsedFeed, {})
}
self?.createFeedCompletion!(.success(.created(feed)))
}
}
}