From 2819403d625e979fa72aa51158262bcfb4d93c2b Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 27 Mar 2019 22:10:14 -0700 Subject: [PATCH] Store the account name in a settings file. Set a default name based on the account type. --- Frameworks/Account/Account.swift | 66 ++++++++++++++++++- Frameworks/Account/AccountSettings.swift | 3 +- .../LocalAccount/LocalAccountDelegate.swift | 2 - 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index d11b5c88b..b935c4268 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -44,11 +44,28 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, public let accountID: String public let type: AccountType - public var nameForDisplay = "" - public var name = "" + public var nameForDisplay: String { + guard let name = name, !name.isEmpty else { + return defaultName + } + return name + } + + public var name: String? { + get { + return settings.name + } + set { + if newValue != settings.name { + settings.name = newValue + + } + } + } + public let defaultName: String + public var topLevelFeeds = Set() public var folders: Set? = Set() - private var feedDictionaryNeedsUpdate = true private var _idToFeedDictionary = [String: Feed]() var idToFeedDictionary: [String: Feed] { @@ -74,6 +91,11 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, private let settingsPath: String private var settings = AccountSettings() + private var settingsDirty = false { + didSet { + queueSaveSettingsIfNeeded() + } + } private let feedMetadataPath: String private typealias FeedMetadataDictionary = [String: FeedMetadata] @@ -140,6 +162,19 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, self.feedMetadataPath = (dataFolder as NSString).appendingPathComponent("FeedMetadata.plist") self.settingsPath = (dataFolder as NSString).appendingPathComponent("Settings.plist") + switch type { + case .onMyMac: + defaultName = NSLocalizedString("On My Mac", comment: "Account name") + case .feedly: + defaultName = "Feedly" + case .feedbin: + defaultName = "Feedbin" + case .feedWrangler: + defaultName = "FeedWrangler" + case .newsBlur: + defaultName = "NewsBlur" + } + NotificationCenter.default.addObserver(self, selector: #selector(downloadProgressDidChange(_:)), name: .DownloadProgressDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) @@ -555,6 +590,12 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, } } + @objc func saveSettingsIfNeeded() { + if settingsDirty { + saveSettings() + } + } + // MARK: - Hashable public func hash(into hasher: inout Hasher) { @@ -689,6 +730,25 @@ private extension Account { assertionFailure(error.localizedDescription) } } + + func queueSaveSettingsIfNeeded() { + Account.saveQueue.add(self, #selector(saveSettingsIfNeeded)) + } + + func saveSettings() { + settingsDirty = false + + let encoder = PropertyListEncoder() + encoder.outputFormat = .binary + let url = URL(fileURLWithPath: settingsPath) + do { + let data = try encoder.encode(settings) + try data.write(to: url) + } + catch { + assertionFailure(error.localizedDescription) + } + } } // MARK: - Private diff --git a/Frameworks/Account/AccountSettings.swift b/Frameworks/Account/AccountSettings.swift index 25db6f618..1e61aa323 100644 --- a/Frameworks/Account/AccountSettings.swift +++ b/Frameworks/Account/AccountSettings.swift @@ -9,5 +9,6 @@ import Foundation final class AccountSettings: Codable { - + + var name: String? } diff --git a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift index f981e0c22..259d437a5 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift @@ -24,8 +24,6 @@ final class LocalAccountDelegate: AccountDelegate { } func accountDidInitialize(_ account: Account) { - - account.nameForDisplay = NSLocalizedString("On My Mac", comment: "Local Account Name") } // MARK: Disk