Files
NetNewsWire/Modules/Account/Sources/Account/AccountManager.swift
T

719 lines
20 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// AccountManager.swift
// NetNewsWire
//
// Created by Brent Simmons on 7/18/15.
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import os
import RSCore
import RSWeb
import Articles
import ArticlesDatabase
import ErrorLog
import ActivityLog
@MainActor public final class AccountManager: UnreadCountProvider {
public static var shared = AccountManager()
public static let netNewsWireNewsURL = "https://netnewswire.blog/feed.xml"
private static let jsonNetNewsWireNewsURL = "https://netnewswire.blog/feed.json"
public let defaultAccount: Account
public let errorLogDatabase: ErrorLogDatabase
private let accountsFolder: String
private var accountsDictionary = [String: Account]()
private let defaultAccountFolderName = "OnMyMac"
private let defaultAccountIdentifier = "OnMyMac"
private var lastStatusRepairDate: Date?
private static let statusRepairInterval: TimeInterval = 1 * 60 * 60
public var isSuspended = false
nonisolated static let syncArticleContentForUnreadArticlesKey = "iCloudSyncArticleContentForUnreadArticles"
public var syncArticleContentForUnreadArticles: Bool {
get {
assert(Thread.isMainThread)
return UserDefaults.standard.bool(forKey: Self.syncArticleContentForUnreadArticlesKey)
}
set {
assert(Thread.isMainThread)
UserDefaults.standard.set(newValue, forKey: Self.syncArticleContentForUnreadArticlesKey)
if Platform.deviceHasiCloudAccount {
NSUbiquitousKeyValueStore.default.set(newValue, forKey: Self.syncArticleContentForUnreadArticlesKey)
}
}
}
private static let logger = Logger(subsystem: Logger.nnwSubsystem, category: "AccountManager")
public var areUnreadCountsInitialized: Bool {
for account in activeAccounts {
if !account.areUnreadCountsInitialized {
return false
}
}
return true
}
public var unreadCount = 0 {
didSet {
if unreadCount != oldValue {
postUnreadCountDidChangeNotification()
}
}
}
public var accounts: [Account] {
Array(accountsDictionary.values)
}
public var sortedAccounts: [Account] {
sortByName(accounts)
}
public var iCloudAccount: Account? {
accounts.first(where: { $0.type == .cloudKit })
}
public var hasiCloudAccount: Bool {
iCloudAccount != nil
}
public var activeAccounts: [Account] {
assert(Thread.isMainThread)
return Array(accountsDictionary.values.filter { $0.isActive })
}
/// Repair article statuses in all active accounts, at most once per interval.
public func repairStatusesIfNeeded() {
if let lastStatusRepairDate, Date().timeIntervalSince(lastStatusRepairDate) < Self.statusRepairInterval {
return
}
lastStatusRepairDate = Date()
for account in activeAccounts {
account.repairStatuses()
}
}
public var sortedActiveAccounts: [Account] {
sortByName(activeAccounts)
}
public var lastRefreshCompletedDate: Date? {
var lastRefreshCompletedDate: Date?
for account in activeAccounts {
if let accountLastArticleFetchEndTime = account.lastRefreshCompletedDate {
if lastRefreshCompletedDate == nil || lastRefreshCompletedDate! < accountLastArticleFetchEndTime {
lastRefreshCompletedDate = accountLastArticleFetchEndTime
}
}
}
return lastRefreshCompletedDate
}
public func existingActiveAccount(forDisplayName displayName: String) -> Account? {
AccountManager.shared.activeAccounts.first(where: { $0.nameForDisplay == displayName })
}
public var refreshInProgress: Bool {
for account in activeAccounts {
if account.refreshInProgress {
return true
}
}
return false
}
private var isActive = false
public init() {
self.accountsFolder = AppConfig.dataSubfolder(named: "Accounts").path
// The local "On My Mac" account must always exist, even if it's empty.
let localAccountFolder = (accountsFolder as NSString).appendingPathComponent("OnMyMac")
do {
try FileManager.default.createDirectory(atPath: localAccountFolder, withIntermediateDirectories: true, attributes: nil)
} catch {
assertionFailure("Could not create folder for OnMyMac account.")
abort()
}
let errorLogDatabasePath = AppConfig.dataFolder.appendingPathComponent("Errors.db").path
self.errorLogDatabase = ErrorLogDatabase(databasePath: errorLogDatabasePath)
defaultAccount = Account(dataFolder: localAccountFolder, type: .onMyMac, accountID: defaultAccountIdentifier)
accountsDictionary[defaultAccount.accountID] = defaultAccount
readAccountsFromDisk()
NotificationCenter.default.addObserver(self, selector: #selector(handleUbiquitousKeyValueStoreDidChangeExternally(_:)), name: NSUbiquitousKeyValueStore.didChangeExternallyNotification, object: NSUbiquitousKeyValueStore.default)
if Platform.deviceHasiCloudAccount {
NSUbiquitousKeyValueStore.default.synchronize()
}
migrateSyncArticleContentForUnreadArticlesSetting(hasiCloudAccount: hasiCloudAccount)
seedSyncArticleContentForUnreadArticlesInUbiquitousKeyValueStore()
}
public func start() {
guard !isActive else {
assertionFailure("start called when isActive is already true")
return
}
isActive = true
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidInitialize(_:)), name: .UnreadCountDidInitialize, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(accountStateDidChange(_:)), name: .AccountStateDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleAppDidGoToBackground(_:)), name: .appDidGoToBackground, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleLowMemory(_:)), name: .lowMemory, object: nil)
DispatchQueue.main.async {
self.updateUnreadCount()
}
}
// MARK: - API
public func createAccount(type: AccountType) -> Account {
if type == .cloudKit {
if let existingiCloudAccount = iCloudAccount {
return existingiCloudAccount
}
}
let accountID = type == .cloudKit ? "iCloud" : UUID().uuidString
let accountFolder = (accountsFolder as NSString).appendingPathComponent("\(type.rawValue)_\(accountID)")
do {
try FileManager.default.createDirectory(atPath: accountFolder, withIntermediateDirectories: true, attributes: nil)
} catch {
assertionFailure("Could not create folder for \(accountID) account.")
abort()
}
let account = Account(dataFolder: accountFolder, type: type, accountID: accountID)
accountsDictionary[accountID] = account
var userInfo = [String: Any]()
userInfo[Account.UserInfoKey.account] = account
NotificationCenter.default.post(name: .UserDidAddAccount, object: self, userInfo: userInfo)
return account
}
public func deleteAccount(_ account: Account) {
guard !account.refreshInProgress else {
return
}
account.prepareForDeletion()
account.deleteSettings()
accountsDictionary.removeValue(forKey: account.accountID)
account.isDeleted = true
do {
try FileManager.default.removeItem(atPath: account.dataFolder)
} catch let error as CocoaError where error.code == .fileNoSuchFile {
// Already doesn’t exist.
} catch {
// TODO: add logging and/or reporting to user. No need to crash here.
}
updateUnreadCount()
var userInfo = [String: Any]()
userInfo[Account.UserInfoKey.account] = account
NotificationCenter.default.post(name: .UserDidDeleteAccount, object: self, userInfo: userInfo)
}
public func duplicateServiceAccount(type: AccountType, username: String?, endpoint: URL? = nil) -> Bool {
guard type != .onMyMac else {
return false
}
for account in accounts {
if account.type == type && username == account.username {
// Self-hosted services can have the same username on different servers.
if let endpoint, let existingEndpoint = account.endpointURL, endpoint != existingEndpoint {
continue
}
return true
}
}
return false
}
public func existingAccount(accountID: String) -> Account? {
return accountsDictionary[accountID]
}
public func existingContainer(with containerID: ContainerIdentifier) -> Container? {
switch containerID {
case .account(let accountID):
return existingAccount(accountID: accountID)
case .folder(let accountID, let folderName):
return existingAccount(accountID: accountID)?.existingFolder(with: folderName)
default:
break
}
return nil
}
public func existingFeed(with sidebarItemID: SidebarItemIdentifier) -> SidebarItem? {
switch sidebarItemID {
case .folder(let accountID, let folderName):
if let account = existingAccount(accountID: accountID) {
return account.existingFolder(with: folderName)
}
case .feed(let accountID, let feedID):
if let account = existingAccount(accountID: accountID) {
return account.existingFeed(withFeedID: feedID)
}
default:
break
}
return nil
}
public func suspendNetworkAll() {
isSuspended = true
for account in accounts {
account.suspendNetwork()
}
}
public func resumeAll() {
isSuspended = false
for account in accounts {
account.resumeDelegate()
}
for account in accounts {
account.resume()
}
}
public func receiveRemoteNotification(userInfo: [AnyHashable: Any]) async {
for account in activeAccounts {
await account.receiveRemoteNotification(userInfo: userInfo)
}
}
public typealias ErrorHandlerCallback = @Sendable (Error) -> Void
public func refreshAllWithoutWaiting(errorHandler: ErrorHandlerCallback? = nil) {
Task {
await refreshAll(errorHandler: errorHandler)
}
}
/// Returns `true` if the refresh ran, `false` if it was skipped
/// due to no network connection.
@discardableResult
public func refreshAll(errorHandler: ErrorHandlerCallback? = nil) async -> Bool {
guard NetworkMonitor.shared.isConnected else {
Self.logger.info("AccountManager: skipping refreshAll — not connected to internet.")
return false
}
CombinedRefreshProgress.shared.start()
defer {
CombinedRefreshProgress.shared.stop()
}
await withTaskGroup(of: Void.self, isolation: MainActor.shared) { group in
for account in activeAccounts {
group.addTask {
do {
try await account.refreshAll()
} catch {
errorHandler?(error)
}
}
}
}
return true
}
public func sendArticleStatusAll() async {
await withTaskGroup(of: Void.self, isolation: MainActor.shared) { group in
for account in activeAccounts {
group.addTask {
try? await account.sendArticleStatus()
}
}
}
}
public func syncArticleStatusAllWithoutWaiting() {
Task {
await syncArticleStatusAll()
}
}
/// Returns `true` if any account reported meaningful work this round;
/// `false` only if every account was idle.
@discardableResult
public func syncArticleStatusAll() async -> Bool {
await withTaskGroup(of: Bool.self, isolation: MainActor.shared) { group in
for account in activeAccounts {
group.addTask {
(try? await account.syncArticleStatus()) ?? false
}
}
var anyWork = false
for await didWork in group {
if didWork {
anyWork = true
}
}
return anyWork
}
}
public func saveAll() {
for account in accounts {
account.save()
}
}
public func saveAllIfNeeded() {
for account in accounts {
account.saveIfNeeded()
}
}
public func anyAccountHasAtLeastOneFeed() -> Bool {
for account in activeAccounts {
if account.hasAtLeastOneFeed() {
return true
}
}
return false
}
public func anyAccountHasNetNewsWireNewsSubscription() -> Bool {
anyAccountHasFeedWithURL(Self.netNewsWireNewsURL) || anyAccountHasFeedWithURL(Self.jsonNetNewsWireNewsURL)
}
public func anyAccountHasFeedWithURL(_ urlString: String) -> Bool {
for account in activeAccounts {
if account.existingFeed(withURL: urlString) != nil {
return true
}
}
return false
}
// MARK: - Fetching Articles
// These fetch articles from active accounts and return a merged Set<Article>.
public func fetchArticles(_ fetchType: FetchType) -> Set<Article> {
precondition(Thread.isMainThread)
var articles = Set<Article>()
for account in activeAccounts {
articles.formUnion(account.fetchArticles(fetchType))
}
return articles
}
public func fetchArticlesAsync(_ fetchType: FetchType) async -> Set<Article> {
precondition(Thread.isMainThread)
guard activeAccounts.count > 0 else {
return Set<Article>()
}
var allFetchedArticles = Set<Article>()
for account in activeAccounts {
let articles = await account.fetchArticlesAsync(fetchType)
allFetchedArticles.formUnion(articles)
}
return allFetchedArticles
}
/// Fetch a single article (synchronously) by accountID and articleID.
public func fetchArticle(accountID: String, articleID: String) -> Article? {
precondition(Thread.isMainThread)
guard let account = existingAccount(accountID: accountID) else {
return nil
}
let articles = account.fetchArticles(.articleIDs(Set([articleID])))
return articles.first
}
// MARK: - Fetching Article Counts
public func fetchCountForStarredArticles() -> Int {
precondition(Thread.isMainThread)
var count = 0
for account in activeAccounts {
count += account.fetchCountForStarredArticles()
}
return count
}
public func fetchCountForStarredArticlesAsync() async -> Int {
precondition(Thread.isMainThread)
var count = 0
for account in activeAccounts {
count += await account.fetchCountForStarredArticlesAsync()
}
return count
}
public func fetchCountForTodayArticlesAsync() async -> Int {
precondition(Thread.isMainThread)
var count = 0
for account in activeAccounts {
count += await account.fetchCountForTodayArticlesAsync()
}
return count
}
public func fetchUnreadCountForTodayAsync() async -> Int {
precondition(Thread.isMainThread)
var count = 0
for account in activeAccounts {
count += await account.fetchUnreadCountForTodayAsync()
}
return count
}
// MARK: - Vacuum
public func vacuumAccountDatabases() async {
for account in accounts {
await account.vacuumDatabases()
}
}
// MARK: - Caches
/// Empty caches that can reasonably be emptied — when the app moves to the background, for instance.
public func emptyCaches() {
for account in accounts {
account.emptyCaches()
}
}
// MARK: - Notifications
@objc func unreadCountDidInitialize(_ notification: Notification) {
guard notification.object is Account else {
return
}
if areUnreadCountsInitialized {
postUnreadCountDidInitializeNotification()
}
}
@objc func unreadCountDidChange(_ notification: Notification) {
guard notification.object is Account else {
return
}
updateUnreadCount()
}
@objc func accountStateDidChange(_ notification: Notification) {
updateUnreadCount()
}
@objc func handleLowMemory(_ notification: Notification) {
emptyCaches()
}
@objc func handleAppDidGoToBackground(_ notification: Notification) {
emptyCaches()
}
}
// MARK: - Private
private extension AccountManager {
@objc nonisolated func handleUbiquitousKeyValueStoreDidChangeExternally(_ note: Notification) {
guard !Platform.isRunningUnitTests else {
return
}
// Extract only Sendable primitives before hopping to the MainActor.
let changeReason = note.userInfo?[NSUbiquitousKeyValueStoreChangeReasonKey] as? Int
let changedKeys = note.userInfo?[NSUbiquitousKeyValueStoreChangedKeysKey] as? [String]
Task { @MainActor in
assert(Thread.isMainThread)
guard let changeReason, changeReason == NSUbiquitousKeyValueStoreServerChange || changeReason == NSUbiquitousKeyValueStoreInitialSyncChange else {
return
}
guard let changedKeys, changedKeys.contains(Self.syncArticleContentForUnreadArticlesKey) else {
return
}
// Only apply if the store actually has a value for the key.
guard NSUbiquitousKeyValueStore.default.object(forKey: Self.syncArticleContentForUnreadArticlesKey) != nil else {
return
}
let newValue = NSUbiquitousKeyValueStore.default.bool(forKey: Self.syncArticleContentForUnreadArticlesKey)
UserDefaults.standard.set(newValue, forKey: Self.syncArticleContentForUnreadArticlesKey)
}
}
func migrateSyncArticleContentForUnreadArticlesSetting(hasiCloudAccount: Bool) {
assert(Thread.isMainThread)
// syncArticleContentForUnreadArticles should be set to false unless
// the user already has an iCloud account.
guard UserDefaults.standard.object(forKey: Self.syncArticleContentForUnreadArticlesKey) == nil else {
return
}
guard !Platform.isRunningUnitTests else {
return
}
guard Platform.deviceHasiCloudAccount else {
syncArticleContentForUnreadArticles = false
return
}
// Check if another device already set a value via iCloud key-value store.
if NSUbiquitousKeyValueStore.default.object(forKey: Self.syncArticleContentForUnreadArticlesKey) != nil {
let iCloudValue = NSUbiquitousKeyValueStore.default.bool(forKey: Self.syncArticleContentForUnreadArticlesKey)
UserDefaults.standard.set(iCloudValue, forKey: Self.syncArticleContentForUnreadArticlesKey)
return
}
syncArticleContentForUnreadArticles = hasiCloudAccount
}
func seedSyncArticleContentForUnreadArticlesInUbiquitousKeyValueStore() {
assert(Thread.isMainThread)
guard !Platform.isRunningUnitTests else {
return
}
guard Platform.deviceHasiCloudAccount else {
return
}
guard NSUbiquitousKeyValueStore.default.object(forKey: Self.syncArticleContentForUnreadArticlesKey) == nil else {
return
}
NSUbiquitousKeyValueStore.default.set(syncArticleContentForUnreadArticles, forKey: Self.syncArticleContentForUnreadArticlesKey)
}
func updateUnreadCount() {
unreadCount = calculateUnreadCount(activeAccounts)
}
func loadAccount(_ accountSpecifier: AccountSpecifier) -> Account? {
Account(dataFolder: accountSpecifier.folderPath, type: accountSpecifier.type, accountID: accountSpecifier.identifier)
}
func loadAccount(_ filename: String) -> Account? {
let folderPath = (accountsFolder as NSString).appendingPathComponent(filename)
if let accountSpecifier = AccountSpecifier(folderPath: folderPath) {
return loadAccount(accountSpecifier)
}
return nil
}
func readAccountsFromDisk() {
var filenames: [String]?
do {
filenames = try FileManager.default.contentsOfDirectory(atPath: accountsFolder)
} catch {
print("Error reading Accounts folder: \(error)")
return
}
guard let filenames = filenames?.sorted() else {
return
}
for oneFilename in filenames {
guard oneFilename != defaultAccountFolderName else {
continue
}
if let oneAccount = loadAccount(oneFilename) {
if !duplicateServiceAccount(oneAccount) {
accountsDictionary[oneAccount.accountID] = oneAccount
}
}
}
}
func duplicateServiceAccount(_ account: Account) -> Bool {
duplicateServiceAccount(type: account.type, username: account.username, endpoint: account.endpointURL)
}
func sortByName(_ accounts: [Account]) -> [Account] {
// LocalAccount is first.
return accounts.sorted { (account1, account2) -> Bool in
if account1 === defaultAccount {
return true
}
if account2 === defaultAccount {
return false
}
return (account1.nameForDisplay as NSString).localizedStandardCompare(account2.nameForDisplay) == .orderedAscending
}
}
}
private struct AccountSpecifier {
let type: AccountType
let identifier: String
let folderPath: String
let folderName: String
let dataFilePath: String
init?(folderPath: String) {
if !FileManager.default.isFolder(atPath: folderPath) {
return nil
}
let name = NSString(string: folderPath).lastPathComponent
if name.hasPrefix(".") {
return nil
}
let nameComponents = name.components(separatedBy: "_")
guard nameComponents.count == 2, let rawType = Int(nameComponents[0]), let accountType = AccountType(rawValue: rawType) else {
return nil
}
self.folderPath = folderPath
self.folderName = name
self.type = accountType
self.identifier = nameComponents[1]
self.dataFilePath = AccountSpecifier.accountFilePathWithFolder(self.folderPath)
}
private static let accountDataFileName = "AccountData.plist"
private static func accountFilePathWithFolder(_ folderPath: String) -> String {
return NSString(string: folderPath).appendingPathComponent(accountDataFileName)
}
}