2017-05-27 19:43:27 +02:00
|
|
|
|
//
|
|
|
|
|
// AccountManager.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
|
// NetNewsWire
|
2017-05-27 19:43:27 +02:00
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 7/18/15.
|
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import RSCore
|
2018-07-24 03:29:08 +02:00
|
|
|
|
import Articles
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2019-07-07 23:05:27 +02:00
|
|
|
|
// Main thread only.
|
|
|
|
|
|
2017-09-17 21:34:10 +02:00
|
|
|
|
public final class AccountManager: UnreadCountProvider {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2019-09-12 17:59:26 +02:00
|
|
|
|
public static var shared: AccountManager!
|
|
|
|
|
|
2019-05-01 12:53:18 +02:00
|
|
|
|
public let defaultAccount: Account
|
2019-05-26 18:54:32 +02:00
|
|
|
|
|
2019-09-12 17:59:26 +02:00
|
|
|
|
private let accountsFolder: String
|
2017-05-27 19:43:27 +02:00
|
|
|
|
private var accountsDictionary = [String: Account]()
|
2017-09-17 21:20:32 +02:00
|
|
|
|
|
2019-07-09 07:58:19 +02:00
|
|
|
|
private let defaultAccountFolderName = "OnMyMac"
|
|
|
|
|
private let defaultAccountIdentifier = "OnMyMac"
|
|
|
|
|
|
2019-04-27 23:16:46 +02:00
|
|
|
|
public var isUnreadCountsInitialized: Bool {
|
2019-05-02 13:01:30 +02:00
|
|
|
|
for account in activeAccounts {
|
2019-04-27 23:16:46 +02:00
|
|
|
|
if !account.isUnreadCountsInitialized {
|
2019-04-27 20:54:52 +02:00
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-17 21:34:10 +02:00
|
|
|
|
public var unreadCount = 0 {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
didSet {
|
2017-10-07 20:56:22 +02:00
|
|
|
|
if unreadCount != oldValue {
|
|
|
|
|
postUnreadCountDidChangeNotification()
|
|
|
|
|
}
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-24 21:24:44 +02:00
|
|
|
|
public var accounts: [Account] {
|
2018-02-14 22:14:25 +01:00
|
|
|
|
return Array(accountsDictionary.values)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
2017-09-17 21:20:32 +02:00
|
|
|
|
|
2017-09-17 21:54:08 +02:00
|
|
|
|
public var sortedAccounts: [Account] {
|
2019-05-02 13:01:30 +02:00
|
|
|
|
return sortByName(accounts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public var activeAccounts: [Account] {
|
2019-07-07 23:05:27 +02:00
|
|
|
|
assert(Thread.isMainThread)
|
2019-05-02 13:01:30 +02:00
|
|
|
|
return Array(accountsDictionary.values.filter { $0.isActive })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public var sortedActiveAccounts: [Account] {
|
|
|
|
|
return sortByName(activeAccounts)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 01:49:17 +02:00
|
|
|
|
public func findActiveAccount(forDisplayName displayName: String) -> Account? {
|
|
|
|
|
return AccountManager.shared.activeAccounts.first(where: { $0.nameForDisplay == displayName })
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-17 21:34:10 +02:00
|
|
|
|
public var refreshInProgress: Bool {
|
2019-05-02 13:01:30 +02:00
|
|
|
|
for account in activeAccounts {
|
2018-02-14 22:14:25 +01:00
|
|
|
|
if account.refreshInProgress {
|
|
|
|
|
return true
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-14 22:14:25 +01:00
|
|
|
|
return false
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
2018-02-14 22:14:25 +01:00
|
|
|
|
|
2017-10-08 02:20:19 +02:00
|
|
|
|
public var combinedRefreshProgress: CombinedRefreshProgress {
|
2019-05-02 13:01:30 +02:00
|
|
|
|
let downloadProgressArray = activeAccounts.map { $0.refreshProgress }
|
2018-02-14 22:14:25 +01:00
|
|
|
|
return CombinedRefreshProgress(downloadProgressArray: downloadProgressArray)
|
2017-10-08 02:20:19 +02:00
|
|
|
|
}
|
2018-02-14 22:14:25 +01:00
|
|
|
|
|
2019-09-22 23:00:06 +02:00
|
|
|
|
public convenience init() {
|
|
|
|
|
let appGroup = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as! String
|
|
|
|
|
let accountsURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)
|
|
|
|
|
let accountsFolder = accountsURL!.appendingPathComponent("Accounts").absoluteString
|
|
|
|
|
let accountsFolderPath = accountsFolder.suffix(from: accountsFolder.index(accountsFolder.startIndex, offsetBy: 7))
|
|
|
|
|
self.init(accountsFolder: String(accountsFolderPath))
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 17:59:26 +02:00
|
|
|
|
public init(accountsFolder: String) {
|
|
|
|
|
self.accountsFolder = accountsFolder
|
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
|
// 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()
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 17:28:13 +02:00
|
|
|
|
defaultAccount = Account(dataFolder: localAccountFolder, type: .onMyMac, accountID: defaultAccountIdentifier)!
|
2019-05-01 12:53:18 +02:00
|
|
|
|
accountsDictionary[defaultAccount.accountID] = defaultAccount
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2019-05-01 20:13:53 +02:00
|
|
|
|
readAccountsFromDisk()
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
|
2019-05-02 15:41:22 +02:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(accountStateDidChange(_:)), name: .AccountStateDidChange, object: nil)
|
|
|
|
|
|
2017-10-19 03:37:45 +02:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self.updateUnreadCount()
|
|
|
|
|
}
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 07:58:19 +02:00
|
|
|
|
// MARK: - API
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2019-05-13 14:17:50 +02:00
|
|
|
|
public func createAccount(type: AccountType) -> Account {
|
2019-05-01 18:05:55 +02:00
|
|
|
|
let accountID = UUID().uuidString
|
2019-05-01 20:13:53 +02:00
|
|
|
|
let accountFolder = (accountsFolder as NSString).appendingPathComponent("\(type.rawValue)_\(accountID)")
|
2019-05-01 18:05:55 +02:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2019-09-08 16:43:51 +02:00
|
|
|
|
var userInfo = [String: Any]()
|
|
|
|
|
userInfo[Account.UserInfoKey.account] = account
|
|
|
|
|
NotificationCenter.default.post(name: .UserDidAddAccount, object: self, userInfo: userInfo)
|
2019-05-01 19:37:13 +02:00
|
|
|
|
|
|
|
|
|
return account
|
2019-05-01 18:05:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 02:22:07 +02:00
|
|
|
|
public func deleteAccount(_ account: Account) {
|
2019-05-02 13:50:35 +02:00
|
|
|
|
guard !account.refreshInProgress else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 02:22:07 +02:00
|
|
|
|
accountsDictionary.removeValue(forKey: account.accountID)
|
2019-05-31 15:05:26 +02:00
|
|
|
|
account.isDeleted = true
|
2019-05-02 02:22:07 +02:00
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
try FileManager.default.removeItem(atPath: account.dataFolder)
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
assertionFailure("Could not create folder for OnMyMac account.")
|
|
|
|
|
abort()
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 15:41:22 +02:00
|
|
|
|
updateUnreadCount()
|
2019-09-08 16:43:51 +02:00
|
|
|
|
|
|
|
|
|
var userInfo = [String: Any]()
|
|
|
|
|
userInfo[Account.UserInfoKey.account] = account
|
|
|
|
|
NotificationCenter.default.post(name: .UserDidDeleteAccount, object: self, userInfo: userInfo)
|
2019-05-02 02:22:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 02:56:04 +02:00
|
|
|
|
public func existingAccount(with accountID: String) -> Account? {
|
2017-09-17 21:20:32 +02:00
|
|
|
|
return accountsDictionary[accountID]
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-02 23:41:32 +02:00
|
|
|
|
public func refreshAll(errorHandler: @escaping (Error) -> Void, completion: (() ->Void)? = nil) {
|
|
|
|
|
let group = DispatchGroup()
|
|
|
|
|
|
2019-05-26 18:54:32 +02:00
|
|
|
|
activeAccounts.forEach { account in
|
2019-10-02 23:41:32 +02:00
|
|
|
|
group.enter()
|
2019-05-26 18:54:32 +02:00
|
|
|
|
account.refreshAll() { result in
|
2019-10-02 23:41:32 +02:00
|
|
|
|
group.leave()
|
2019-05-26 18:54:32 +02:00
|
|
|
|
switch result {
|
|
|
|
|
case .success:
|
|
|
|
|
break
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
errorHandler(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-02 23:41:32 +02:00
|
|
|
|
|
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
|
|
|
|
completion?()
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 20:51:08 +02:00
|
|
|
|
public func syncArticleStatusAll(completion: (() -> Void)? = nil) {
|
|
|
|
|
let group = DispatchGroup()
|
|
|
|
|
|
|
|
|
|
activeAccounts.forEach {
|
|
|
|
|
group.enter()
|
|
|
|
|
$0.syncArticleStatus() {
|
|
|
|
|
group.leave()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-13 00:28:54 +02:00
|
|
|
|
group.notify(queue: DispatchQueue.global(qos: .background)) {
|
2019-05-20 20:51:08 +02:00
|
|
|
|
completion?()
|
|
|
|
|
}
|
2019-05-16 00:21:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-02 22:32:34 +02:00
|
|
|
|
public func saveAll() {
|
|
|
|
|
accounts.forEach { $0.save() }
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-02 22:15:07 +02:00
|
|
|
|
public func anyAccountHasAtLeastOneFeed() -> Bool {
|
2019-05-02 13:01:30 +02:00
|
|
|
|
for account in activeAccounts {
|
2017-10-19 03:45:09 +02:00
|
|
|
|
if account.hasAtLeastOneFeed() {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 07:00:35 +02:00
|
|
|
|
public func anyAccountHasFeedWithURL(_ urlString: String) -> Bool {
|
2019-05-02 13:01:30 +02:00
|
|
|
|
for account in activeAccounts {
|
2017-09-17 21:20:32 +02:00
|
|
|
|
if let _ = account.existingFeed(withURL: urlString) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2019-07-06 05:06:31 +02:00
|
|
|
|
|
|
|
|
|
// 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, _ callback: @escaping ArticleSetBlock) {
|
|
|
|
|
precondition(Thread.isMainThread)
|
|
|
|
|
|
|
|
|
|
var allFetchedArticles = Set<Article>()
|
|
|
|
|
let numberOfAccounts = activeAccounts.count
|
|
|
|
|
var accountsReporting = 0
|
|
|
|
|
|
|
|
|
|
for account in activeAccounts {
|
|
|
|
|
account.fetchArticlesAsync(fetchType) { (articles) in
|
|
|
|
|
allFetchedArticles.formUnion(articles)
|
|
|
|
|
accountsReporting += 1
|
|
|
|
|
if accountsReporting == numberOfAccounts {
|
|
|
|
|
callback(allFetchedArticles)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-13 00:06:21 +02:00
|
|
|
|
// 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()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 07:58:19 +02:00
|
|
|
|
// MARK: - Notifications
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2017-09-17 21:20:32 +02:00
|
|
|
|
@objc dynamic func unreadCountDidChange(_ notification: Notification) {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
guard let _ = notification.object as? Account else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
updateUnreadCount()
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 15:41:22 +02:00
|
|
|
|
@objc func accountStateDidChange(_ notification: Notification) {
|
|
|
|
|
updateUnreadCount()
|
|
|
|
|
}
|
2019-07-09 07:58:19 +02:00
|
|
|
|
}
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2019-07-09 07:58:19 +02:00
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
|
|
|
|
private extension AccountManager {
|
|
|
|
|
|
|
|
|
|
func updateUnreadCount() {
|
|
|
|
|
unreadCount = calculateUnreadCount(activeAccounts)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 07:58:19 +02:00
|
|
|
|
func loadAccount(_ accountSpecifier: AccountSpecifier) -> Account? {
|
|
|
|
|
return Account(dataFolder: accountSpecifier.folderPath, type: accountSpecifier.type, accountID: accountSpecifier.identifier)
|
|
|
|
|
}
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2019-07-09 07:58:19 +02:00
|
|
|
|
func loadAccount(_ filename: String) -> Account? {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
let folderPath = (accountsFolder as NSString).appendingPathComponent(filename)
|
|
|
|
|
if let accountSpecifier = AccountSpecifier(folderPath: folderPath) {
|
2019-05-01 20:13:53 +02:00
|
|
|
|
return loadAccount(accountSpecifier)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 07:58:19 +02:00
|
|
|
|
func readAccountsFromDisk() {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
var filenames: [String]?
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
filenames = try FileManager.default.contentsOfDirectory(atPath: accountsFolder)
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
print("Error reading Accounts folder: \(error)")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filenames?.forEach { (oneFilename) in
|
2019-05-01 17:28:13 +02:00
|
|
|
|
guard oneFilename != defaultAccountFolderName else {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-05-01 20:13:53 +02:00
|
|
|
|
if let oneAccount = loadAccount(oneFilename) {
|
2017-09-17 21:20:32 +02:00
|
|
|
|
accountsDictionary[oneAccount.accountID] = oneAccount
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 07:58:19 +02:00
|
|
|
|
func sortByName(_ accounts: [Account]) -> [Account] {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
// LocalAccount is first.
|
|
|
|
|
|
|
|
|
|
return accounts.sorted { (account1, account2) -> Bool in
|
2019-05-01 12:53:18 +02:00
|
|
|
|
if account1 === defaultAccount {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
return true
|
|
|
|
|
}
|
2019-05-01 12:53:18 +02:00
|
|
|
|
if account2 === defaultAccount {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
return false
|
|
|
|
|
}
|
2017-10-19 03:21:36 +02:00
|
|
|
|
return (account1.nameForDisplay as NSString).localizedStandardCompare(account2.nameForDisplay) == .orderedAscending
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private struct AccountSpecifier {
|
|
|
|
|
|
2019-05-01 20:13:53 +02:00
|
|
|
|
let type: AccountType
|
2017-05-27 19:43:27 +02:00
|
|
|
|
let identifier: String
|
|
|
|
|
let folderPath: String
|
|
|
|
|
let folderName: String
|
|
|
|
|
let dataFilePath: String
|
|
|
|
|
|
|
|
|
|
|
2019-07-09 07:58:19 +02:00
|
|
|
|
init?(folderPath: String) {
|
2017-10-09 03:58:15 +02:00
|
|
|
|
if !FileManager.default.rs_fileIsFolder(folderPath) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-05-01 20:13:53 +02:00
|
|
|
|
|
2017-10-09 03:58:15 +02:00
|
|
|
|
let name = NSString(string: folderPath).lastPathComponent
|
|
|
|
|
if name.hasPrefix(".") {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-05-01 20:13:53 +02:00
|
|
|
|
|
|
|
|
|
let nameComponents = name.components(separatedBy: "_")
|
|
|
|
|
|
2019-07-09 07:58:19 +02:00
|
|
|
|
guard nameComponents.count == 2, let rawType = Int(nameComponents[0]), let accountType = AccountType(rawValue: rawType) else {
|
2017-05-27 19:43:27 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-09 03:58:15 +02:00
|
|
|
|
self.folderPath = folderPath
|
|
|
|
|
self.folderName = name
|
2019-07-09 07:58:19 +02:00
|
|
|
|
self.type = accountType
|
2017-05-27 19:43:27 +02:00
|
|
|
|
self.identifier = nameComponents[1]
|
|
|
|
|
|
2019-07-09 07:58:19 +02:00
|
|
|
|
self.dataFilePath = AccountSpecifier.accountFilePathWithFolder(self.folderPath)
|
2017-05-27 19:43:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 07:58:19 +02:00
|
|
|
|
private static let accountDataFileName = "AccountData.plist"
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
2019-07-09 07:58:19 +02:00
|
|
|
|
private static func accountFilePathWithFolder(_ folderPath: String) -> String {
|
|
|
|
|
return NSString(string: folderPath).appendingPathComponent(accountDataFileName)
|
|
|
|
|
}
|
|
|
|
|
}
|