Files
NetNewsWire/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift
T

224 lines
6.5 KiB
Swift

//
// LocalAccountDelegate.swift
// NetNewsWire
//
// Created by Brent Simmons on 9/16/17.
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import RSCore
import RSParser
import Articles
import ArticlesDatabase
import FeedFinder
import RSWeb
import Secrets
@MainActor final class LocalAccountDelegate: AccountDelegate {
weak var account: Account?
let behaviors: AccountBehaviors = []
let isOPMLImportInProgress = false
var progressInfo = ProgressInfo() {
didSet {
if progressInfo != oldValue {
postProgressInfoDidChangeNotification()
}
}
}
let server: String? = nil
var credentials: Credentials?
var accountSettings: AccountSettings?
private lazy var refresher: LocalAccountRefresher = {
let refresher = LocalAccountRefresher()
refresher.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(progressInfoDidChange(_:)), name: .progressInfoDidChange, object: refresher)
return refresher
}()
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable: Any]) async {
}
@MainActor func refreshAll(for account: Account) async throws {
guard progressInfo.isComplete, !Platform.isRunningUnitTests else {
return
}
let feeds = account.flattenedFeeds()
refresher.accountID = account.accountID
await refresher.refreshFeeds(feeds)
account.lastRefreshCompletedDate = Date()
}
@MainActor func syncArticleStatus(for account: Account) async throws {
}
@MainActor func sendArticleStatus(for account: Account) async throws {
}
@MainActor func refreshArticleStatus(for account: Account) async throws {
}
@MainActor func importOPML(for account: Account, opmlFile: URL) async throws {
try account.logActivity(kind: .importOPML, detail: opmlFile.lastPathComponent) {
let opmlData = try Data(contentsOf: opmlFile)
let parserData = ParserData(url: opmlFile.absoluteString, data: opmlData)
let opmlDocument = try OPMLParser.parseOPML(with: parserData)
// TODO: throw appropriate error for empty OPML
guard let children = opmlDocument.children else {
return
}
BatchUpdate.shared.perform {
account.loadOPMLItems(children)
}
}
}
@MainActor func createFeed(for account: Account, url urlString: String, name: String?, container: Container, validateFeed: Bool) async throws -> Feed {
guard let url = URL(string: urlString) else {
throw AccountError.invalidParameter
}
return try await account.logActivity(kind: .subscribeFeed, detail: urlString) {
try await createFeed(account: account, url: url, editedName: name, container: container)
}
}
@MainActor func renameFeed(for account: Account, with feed: Feed, to name: String) async throws {
feed.editedName = name
}
@MainActor func removeFeed(account: Account, feed: Feed, container: Container) async throws {
container.removeFeedFromTreeAtTopLevel(feed)
}
@MainActor func moveFeed(account: Account, feed: Feed, sourceContainer: Container, destinationContainer: Container) async throws {
sourceContainer.removeFeedFromTreeAtTopLevel(feed)
destinationContainer.addFeedToTreeAtTopLevel(feed)
}
@MainActor func addFeed(account: Account, feed: Feed, container: Container) async throws {
container.addFeedToTreeAtTopLevel(feed)
}
@MainActor func restoreFeed(for account: Account, feed: Feed, container: Container) async throws {
container.addFeedToTreeAtTopLevel(feed)
}
@MainActor func createFolder(for account: Account, name: String) async throws -> Folder {
guard let folder = account.ensureFolder(with: name) else {
throw AccountError.invalidParameter
}
return folder
}
@MainActor func renameFolder(for account: Account, with folder: Folder, to name: String) async throws {
folder.name = name
}
@MainActor func removeFolder(for account: Account, with folder: Folder) async throws {
account.removeFolderFromTree(folder)
}
@MainActor func restoreFolder(for account: Account, folder: Folder) async throws {
account.addFolderToTree(folder)
}
@MainActor func markArticles(for account: Account, articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async throws {
try await account.updateAsync(articles: articles, statusKey: statusKey, flag: flag)
}
func accountDidInitialize(_ account: Account) {
self.account = account
}
func accountWillBeDeleted(_ account: Account) {
}
static func validateCredentials(transport: Transport, credentials: Credentials, endpoint: URL?) async throws -> Credentials? {
nil
}
func vacuumDatabases(for account: Account) async {
}
// MARK: Suspend and Resume (for iOS)
@MainActor func suspendNetwork() {
refresher.suspend()
}
@MainActor func suspendDatabase() {
// Nothing to do
}
@MainActor func resume(account: Account) {
refresher.resume()
}
// MARK: - Notifications
@objc func progressInfoDidChange(_ notification: Notification) {
progressInfo = refresher.progressInfo
}
}
extension LocalAccountDelegate: LocalAccountRefresherDelegate {
func localAccountRefresher(_ refresher: LocalAccountRefresher, articleChanges: ArticleChanges) {
}
}
private extension LocalAccountDelegate {
@MainActor func createFeed(account: Account, url: URL, editedName: String?, container: Container) async throws -> Feed {
// We need to use a batch update here because we need to assign add the feed to the
// container before the name has been downloaded. This will put it in the sidebar
// with an Untitled name if we don't delay it being added to the sidebar.
BatchUpdate.shared.start()
defer {
BatchUpdate.shared.end()
}
let feedSpecifiers = try await FeedFinder.find(url: url)
guard let bestFeedSpecifier = FeedSpecifier.bestFeed(in: feedSpecifiers),
let url = URL(string: bestFeedSpecifier.urlString) else {
throw AccountError.createErrorNotFound
}
guard !account.hasFeed(withURL: bestFeedSpecifier.urlString) else {
throw AccountError.createErrorAlreadySubscribed
}
let (parsedFeed, response) = try await InitialFeedDownloader.download(url)
guard let parsedFeed else {
throw AccountError.createErrorNotFound
}
let feed = account.createFeed(with: nil, url: url.absoluteString, feedID: url.absoluteString, homePageURL: nil)
feed.lastCheckDate = Date()
// Save conditional GET info so that first refresh uses conditional GET.
if let httpResponse = response as? HTTPURLResponse,
let conditionalGetInfo = HTTPConditionalGetInfo(urlResponse: httpResponse) {
feed.conditionalGetInfo = conditionalGetInfo
}
feed.editedName = editedName
container.addFeedToTreeAtTopLevel(feed)
Task {
try? await account.updateAsync(feed: feed, parsedFeed: parsedFeed)
}
return feed
}
}