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
|
2019-05-26 23:45:09 +02:00
|
|
|
import RSCore
|
2019-05-11 19:26:23 +02:00
|
|
|
import RSParser
|
2019-05-14 22:34:05 +02:00
|
|
|
import Articles
|
2017-10-08 02:20:19 +02:00
|
|
|
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."
|
|
|
|
}
|
|
|
|
|
2017-10-07 23:40:14 +02:00
|
|
|
final class LocalAccountDelegate: AccountDelegate {
|
2019-05-05 14:21:26 +02:00
|
|
|
|
2017-09-28 22:16:47 +02:00
|
|
|
let supportsSubFolders = false
|
2019-05-29 00:42:19 +02:00
|
|
|
let usesTags = false
|
2019-05-17 17:44:22 +02:00
|
|
|
let opmlImportInProgress = false
|
|
|
|
|
2019-05-04 18:48:48 +02:00
|
|
|
let server: String? = nil
|
2019-05-05 10:25:21 +02:00
|
|
|
var credentials: Credentials?
|
2019-05-05 14:49:59 +02:00
|
|
|
var accountMetadata: AccountMetadata?
|
2019-05-05 14:21:26 +02:00
|
|
|
|
2019-05-09 00:41:19 +02:00
|
|
|
private weak var account: Account?
|
|
|
|
private var feedFinder: FeedFinder?
|
2019-05-28 16:45:02 +02:00
|
|
|
private var createFeedName: String?
|
|
|
|
private var createFeedContainer: Container?
|
2019-05-10 17:14:24 +02:00
|
|
|
private var createFeedCompletion: ((Result<Feed, Error>) -> Void)?
|
2019-05-09 00:41:19 +02:00
|
|
|
|
2017-10-02 22:15:07 +02:00
|
|
|
private let refresher = LocalAccountRefresher()
|
2017-10-07 23:40:14 +02:00
|
|
|
|
2017-10-08 02:20:19 +02:00
|
|
|
var refreshProgress: DownloadProgress {
|
2018-02-14 22:14:25 +01:00
|
|
|
return refresher.progress
|
2017-10-08 02:20:19 +02:00
|
|
|
}
|
|
|
|
|
2019-05-05 22:41:20 +02:00
|
|
|
// LocalAccountDelegate doesn't wait for completion before calling the completion block
|
2019-05-26 18:54:32 +02:00
|
|
|
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-04-26 21:21:17 +02:00
|
|
|
refresher.refreshFeeds(account.flattenedFeeds())
|
2019-05-26 18:54:32 +02:00
|
|
|
completion(.success(()))
|
2017-09-17 20:32:58 +02:00
|
|
|
}
|
2019-05-15 18:52:56 +02:00
|
|
|
|
|
|
|
func sendArticleStatus(for account: Account, completion: @escaping (() -> Void)) {
|
|
|
|
completion()
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshArticleStatus(for account: Account, completion: @escaping (() -> Void)) {
|
|
|
|
completion()
|
|
|
|
}
|
2019-05-11 19:26:23 +02:00
|
|
|
|
|
|
|
func importOPML(for account:Account, opmlFile: URL, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
var fileData: Data?
|
|
|
|
|
|
|
|
do {
|
|
|
|
fileData = try Data(contentsOf: opmlFile)
|
|
|
|
} catch {
|
|
|
|
completion(.failure(error))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let opmlData = fileData else {
|
|
|
|
completion(.success(()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let parserData = ParserData(url: opmlFile.absoluteString, data: opmlData)
|
|
|
|
var opmlDocument: RSOPMLDocument?
|
|
|
|
|
|
|
|
do {
|
|
|
|
opmlDocument = try RSOPMLParser.parseOPML(with: parserData)
|
|
|
|
} catch {
|
|
|
|
completion(.failure(error))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let loadDocument = opmlDocument else {
|
|
|
|
completion(.success(()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use the same mechanism to load local accounts as we do to load the subscription
|
|
|
|
// OPML all accounts.
|
2019-05-26 23:45:09 +02:00
|
|
|
BatchUpdate.shared.perform {
|
|
|
|
account.loadOPML(loadDocument)
|
|
|
|
}
|
2019-05-11 19:26:23 +02:00
|
|
|
completion(.success(()))
|
|
|
|
|
|
|
|
}
|
2019-05-07 00:34:41 +02:00
|
|
|
|
2019-05-28 16:45:02 +02:00
|
|
|
func createFeed(for account: Account, url urlString: String, name: String?, container: Container, completion: @escaping (Result<Feed, Error>) -> Void) {
|
2019-05-09 00:41:19 +02:00
|
|
|
|
|
|
|
guard let url = URL(string: urlString) else {
|
|
|
|
completion(.failure(LocalAccountDelegateError.invalidParameter))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.account = account
|
2019-05-28 16:45:02 +02:00
|
|
|
createFeedName = name
|
|
|
|
createFeedContainer = container
|
2019-05-09 00:41:19 +02:00
|
|
|
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(()))
|
|
|
|
}
|
2019-05-09 14:25:45 +02:00
|
|
|
|
2019-05-30 04:04:44 +02:00
|
|
|
func removeFeed(for account: Account, with feed: Feed, from container: Container?, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-30 03:47:52 +02:00
|
|
|
container?.removeFeed(feed)
|
2019-05-09 14:25:45 +02:00
|
|
|
completion(.success(()))
|
2019-05-09 20:31:18 +02:00
|
|
|
}
|
|
|
|
|
2019-05-30 17:12:34 +02:00
|
|
|
func moveFeed(for account: Account, with feed: Feed, from: Container, to: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
from.removeFeed(feed)
|
|
|
|
to.addFeed(feed)
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
|
2019-05-30 04:04:44 +02:00
|
|
|
func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-30 03:47:52 +02:00
|
|
|
container.addFeed(feed)
|
2019-05-09 20:31:18 +02:00
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:45:02 +02:00
|
|
|
func restoreFeed(for account: Account, feed: Feed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
2019-05-30 04:39:53 +02:00
|
|
|
container.addFeed(feed)
|
|
|
|
completion(.success(()))
|
2019-05-09 23:09:21 +02:00
|
|
|
}
|
|
|
|
|
2019-05-30 21:36:21 +02:00
|
|
|
func addFolder(for account: Account, name: String, completion: @escaping (Result<Folder, Error>) -> Void) {
|
|
|
|
if let folder = account.ensureFolder(with: name) {
|
|
|
|
completion(.success(folder))
|
|
|
|
} else {
|
|
|
|
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
folder.name = name
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeFolder(for account: Account, with folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
account.removeFolder(folder)
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
|
2019-05-09 23:09:21 +02:00
|
|
|
func restoreFolder(for account: Account, folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
account.addFolder(folder)
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
|
2019-05-14 22:34:05 +02:00
|
|
|
func markArticles(for account: Account, articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) -> Set<Article>? {
|
|
|
|
return account.update(articles, statusKey: statusKey, flag: flag)
|
|
|
|
}
|
|
|
|
|
2017-12-20 02:48:30 +01:00
|
|
|
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 {
|
2019-05-10 17:14:24 +02:00
|
|
|
createFeedCompletion!(.failure(AccountError.createErrorNotFound))
|
2019-05-09 00:41:19 +02:00
|
|
|
} else {
|
|
|
|
createFeedCompletion!(.failure(error))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let bestFeedSpecifier = FeedSpecifier.bestFeed(in: feedSpecifiers),
|
|
|
|
let url = URL(string: bestFeedSpecifier.urlString),
|
|
|
|
let account = account else {
|
2019-05-10 17:14:24 +02:00
|
|
|
createFeedCompletion!(.failure(AccountError.createErrorNotFound))
|
2019-05-09 00:41:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:22:28 +02:00
|
|
|
if account.hasFeed(withURL: bestFeedSpecifier.urlString) {
|
|
|
|
createFeedCompletion!(.failure(AccountError.createErrorAlreadySubscribed))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-09 00:41:19 +02:00
|
|
|
let feed = account.createFeed(with: nil, url: url.absoluteString, feedID: url.absoluteString, homePageURL: nil)
|
2019-05-28 16:45:02 +02:00
|
|
|
|
|
|
|
InitialFeedDownloader.download(url) { parsedFeed in
|
|
|
|
|
2019-05-09 00:41:19 +02:00
|
|
|
if let parsedFeed = parsedFeed {
|
|
|
|
account.update(feed, with: parsedFeed, {})
|
|
|
|
}
|
2019-05-28 16:45:02 +02:00
|
|
|
|
|
|
|
feed.editedName = self.createFeedName
|
|
|
|
|
2019-05-30 03:47:52 +02:00
|
|
|
self.createFeedContainer?.addFeed(feed)
|
|
|
|
self.createFeedCompletion?(.success(feed))
|
2019-05-28 16:45:02 +02:00
|
|
|
|
2019-05-09 00:41:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|