NetNewsWire/Frameworks/Account/AccountManager.swift

296 lines
7.0 KiB
Swift
Raw Normal View History

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
import Articles
2017-05-27 19:43:27 +02:00
public extension Notification.Name {
static let AccountsDidChange = Notification.Name(rawValue: "AccountsDidChange")
}
2017-05-27 19:43:27 +02:00
2019-05-01 17:28:13 +02:00
private let defaultAccountFolderName = "OnMyMac"
private let defaultAccountIdentifier = "OnMyMac"
2017-05-27 19:43:27 +02:00
2017-09-17 21:34:10 +02:00
public final class AccountManager: UnreadCountProvider {
2017-05-27 19:43:27 +02:00
2017-09-23 21:17:14 +02:00
public static let shared = AccountManager()
public let defaultAccount: Account
2017-05-27 19:43:27 +02:00
private let accountsFolder = RSDataSubfolder(nil, "Accounts")!
private var accountsDictionary = [String: Account]()
public var isUnreadCountsInitialized: Bool {
for account in activeAccounts {
if !account.isUnreadCountsInitialized {
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 {
if unreadCount != oldValue {
postUnreadCountDidChangeNotification()
}
2017-05-27 19:43:27 +02:00
}
}
2017-09-24 21:24:44 +02:00
public var accounts: [Account] {
return Array(accountsDictionary.values)
2017-05-27 19:43:27 +02:00
}
2017-09-17 21:54:08 +02:00
public var sortedAccounts: [Account] {
return sortByName(accounts)
}
public var activeAccounts: [Account] {
return Array(accountsDictionary.values.filter { $0.isActive })
}
public var sortedActiveAccounts: [Account] {
return sortByName(activeAccounts)
2017-05-27 19:43:27 +02:00
}
2017-09-17 21:34:10 +02:00
public var refreshInProgress: Bool {
for account in activeAccounts {
if account.refreshInProgress {
return true
2017-05-27 19:43:27 +02:00
}
}
return false
2017-05-27 19:43:27 +02:00
}
public var combinedRefreshProgress: CombinedRefreshProgress {
let downloadProgressArray = activeAccounts.map { $0.refreshProgress }
return CombinedRefreshProgress(downloadProgressArray: downloadProgressArray)
}
2017-09-17 21:34:10 +02:00
public init() {
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)!
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)
NotificationCenter.default.addObserver(self, selector: #selector(accountStateDidChange(_:)), name: .AccountStateDidChange, object: nil)
DispatchQueue.main.async {
self.updateUnreadCount()
}
2017-05-27 19:43:27 +02:00
}
// MARK: API
2019-05-13 14:17:50 +02:00
public func createAccount(type: AccountType) -> Account {
let accountID = UUID().uuidString
2019-05-01 20:13:53 +02:00
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
NotificationCenter.default.post(name: .AccountsDidChange, object: self)
2019-05-01 19:37:13 +02:00
return account
}
2019-05-02 02:22:07 +02:00
public func deleteAccount(_ account: Account) {
guard !account.refreshInProgress else {
return
}
2019-05-02 02:22:07 +02:00
accountsDictionary.removeValue(forKey: account.accountID)
do {
try FileManager.default.removeItem(atPath: account.dataFolder)
}
catch {
assertionFailure("Could not create folder for OnMyMac account.")
abort()
}
updateUnreadCount()
NotificationCenter.default.post(name: .AccountsDidChange, object: self)
2019-05-02 02:22:07 +02:00
}
2017-09-18 02:56:04 +02:00
public func existingAccount(with accountID: String) -> Account? {
2017-05-27 19:43:27 +02:00
return accountsDictionary[accountID]
2017-05-27 19:43:27 +02:00
}
public func refreshAll() {
2017-05-27 19:43:27 +02:00
activeAccounts.forEach { $0.refreshAll() }
2017-05-27 19:43:27 +02:00
}
public func syncArticleStatusAll() {
activeAccounts.forEach { $0.syncArticleStatus() }
}
2017-10-02 22:15:07 +02:00
public func anyAccountHasAtLeastOneFeed() -> Bool {
2017-05-27 19:43:27 +02:00
for account in activeAccounts {
if account.hasAtLeastOneFeed() {
2017-05-27 19:43:27 +02:00
return true
}
}
return false
}
public func anyAccountHasFeedWithURL(_ urlString: String) -> Bool {
2017-05-27 19:43:27 +02:00
for account in activeAccounts {
if let _ = account.existingFeed(withURL: urlString) {
2017-05-27 19:43:27 +02:00
return true
}
}
return false
}
func updateUnreadCount() {
unreadCount = calculateUnreadCount(activeAccounts)
2017-05-27 19:43:27 +02:00
}
// MARK: Notifications
@objc dynamic func unreadCountDidChange(_ notification: Notification) {
2017-05-27 19:43:27 +02:00
guard let _ = notification.object as? Account else {
return
}
updateUnreadCount()
}
@objc func accountStateDidChange(_ notification: Notification) {
updateUnreadCount()
}
2017-05-27 19:43:27 +02:00
// MARK: Private
2019-05-01 20:13:53 +02:00
private 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-05-01 20:13:53 +02:00
private 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-05-01 20:13:53 +02:00
private 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) {
accountsDictionary[oneAccount.accountID] = oneAccount
2017-05-27 19:43:27 +02:00
}
}
}
private func sortByName(_ accounts: [Account]) -> [Account] {
2017-05-27 19:43:27 +02:00
// LocalAccount is first.
return accounts.sorted { (account1, account2) -> Bool in
if account1 === defaultAccount {
2017-05-27 19:43:27 +02:00
return true
}
if account2 === defaultAccount {
2017-05-27 19:43:27 +02:00
return false
}
return (account1.nameForDisplay as NSString).localizedStandardCompare(account2.nameForDisplay) == .orderedAscending
2017-05-27 19:43:27 +02:00
}
}
}
private let accountDataFileName = "AccountData.plist"
private func accountFilePathWithFolder(_ folderPath: String) -> String {
return NSString(string: folderPath).appendingPathComponent(accountDataFileName)
}
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
init?(folderPath: String) {
if !FileManager.default.rs_fileIsFolder(folderPath) {
return nil
}
2019-05-01 20:13:53 +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: "_")
guard nameComponents.count == 2, let rawType = Int(nameComponents[0]), let acctType = AccountType(rawValue: rawType) else {
2017-05-27 19:43:27 +02:00
return nil
}
self.folderPath = folderPath
self.folderName = name
2019-05-01 20:13:53 +02:00
self.type = acctType
2017-05-27 19:43:27 +02:00
self.identifier = nameComponents[1]
self.dataFilePath = accountFilePathWithFolder(self.folderPath)
2019-05-01 20:13:53 +02:00
2017-05-27 19:43:27 +02:00
}
}