mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-03 10:47:21 +00:00
Create FeedSettings to replace FeedMetadata. Delete FeedMetadataFile since it's been replaced by FeedSettingsDatabase.
This commit is contained in:
1 parent
8c948bd63e
commit
a34a62ae41
3 files changed
+193
-252
No files matched your search
@@ -1,173 +0,0 @@
|
||||
//
|
||||
// FeedMetadata.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Brent Simmons on 3/12/19.
|
||||
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import RSWeb
|
||||
import Articles
|
||||
|
||||
@MainActor protocol FeedMetadataDelegate: AnyObject {
|
||||
func valueDidChange(_ feedMetadata: FeedMetadata, key: FeedMetadata.CodingKeys)
|
||||
}
|
||||
|
||||
@MainActor final class FeedMetadata: Codable {
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case feedID
|
||||
case homePageURL
|
||||
case iconURL
|
||||
case faviconURL
|
||||
case editedName
|
||||
case authors
|
||||
case contentHash
|
||||
case isNotifyAboutNewArticles
|
||||
case isArticleExtractorAlwaysOn
|
||||
case conditionalGetInfo
|
||||
case conditionalGetInfoDate
|
||||
case cacheControlInfo
|
||||
case externalID = "subscriptionID"
|
||||
case folderRelationship
|
||||
case lastCheckDate
|
||||
}
|
||||
|
||||
var feedID: String {
|
||||
didSet {
|
||||
if feedID != oldValue {
|
||||
valueDidChange(.feedID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var homePageURL: String? {
|
||||
didSet {
|
||||
if homePageURL != oldValue {
|
||||
valueDidChange(.homePageURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var iconURL: String? {
|
||||
didSet {
|
||||
if iconURL != oldValue {
|
||||
valueDidChange(.iconURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var faviconURL: String? {
|
||||
didSet {
|
||||
if faviconURL != oldValue {
|
||||
valueDidChange(.faviconURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var editedName: String? {
|
||||
didSet {
|
||||
if editedName != oldValue {
|
||||
valueDidChange(.editedName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var contentHash: String? {
|
||||
didSet {
|
||||
if contentHash != oldValue {
|
||||
valueDidChange(.contentHash)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var isNotifyAboutNewArticles: Bool? {
|
||||
didSet {
|
||||
if isNotifyAboutNewArticles != oldValue {
|
||||
valueDidChange(.isNotifyAboutNewArticles)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var isArticleExtractorAlwaysOn: Bool? {
|
||||
didSet {
|
||||
if isArticleExtractorAlwaysOn != oldValue {
|
||||
valueDidChange(.isArticleExtractorAlwaysOn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var authors: [Author]? {
|
||||
didSet {
|
||||
if authors != oldValue {
|
||||
valueDidChange(.authors)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var conditionalGetInfo: HTTPConditionalGetInfo? {
|
||||
didSet {
|
||||
if conditionalGetInfo != oldValue {
|
||||
valueDidChange(.conditionalGetInfo)
|
||||
if conditionalGetInfo == nil {
|
||||
conditionalGetInfoDate = nil
|
||||
} else {
|
||||
conditionalGetInfoDate = Date()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var conditionalGetInfoDate: Date? {
|
||||
didSet {
|
||||
if conditionalGetInfoDate != oldValue {
|
||||
valueDidChange(.conditionalGetInfoDate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var cacheControlInfo: CacheControlInfo? {
|
||||
didSet {
|
||||
if cacheControlInfo != oldValue {
|
||||
valueDidChange(.cacheControlInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var externalID: String? {
|
||||
didSet {
|
||||
if externalID != oldValue {
|
||||
valueDidChange(.externalID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Folder Name: Sync Service Relationship ID
|
||||
var folderRelationship: [String: String]? {
|
||||
didSet {
|
||||
if folderRelationship != oldValue {
|
||||
valueDidChange(.folderRelationship)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Last time an attempt was made to read the feed.
|
||||
/// (Not necessarily a successful attempt.)
|
||||
var lastCheckDate: Date? {
|
||||
didSet {
|
||||
if lastCheckDate != oldValue {
|
||||
valueDidChange(.lastCheckDate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
weak var delegate: FeedMetadataDelegate?
|
||||
|
||||
init(feedID: String) {
|
||||
self.feedID = feedID
|
||||
}
|
||||
|
||||
func valueDidChange(_ key: CodingKeys) {
|
||||
delegate?.valueDidChange(self, key: key)
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
//
|
||||
// FeedMetadataFile.swift
|
||||
// Account
|
||||
//
|
||||
// Created by Maurice Parker on 9/13/19.
|
||||
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import os.log
|
||||
import RSCore
|
||||
|
||||
@MainActor final class FeedMetadataFile {
|
||||
private let fileURL: URL
|
||||
private let account: Account
|
||||
|
||||
@MainActor private var isDirty = false {
|
||||
didSet {
|
||||
queueSaveToDiskIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
private let saveQueue = CoalescingQueue(name: "Save Queue", interval: 0.5)
|
||||
static private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "FeedMetadataFile")
|
||||
|
||||
init(filename: String, account: Account) {
|
||||
self.fileURL = URL(fileURLWithPath: filename)
|
||||
self.account = account
|
||||
}
|
||||
|
||||
@MainActor func markAsDirty() {
|
||||
isDirty = true
|
||||
}
|
||||
|
||||
func load() {
|
||||
if let fileData = try? Data(contentsOf: fileURL) {
|
||||
let decoder = PropertyListDecoder()
|
||||
account.feedMetadata = (try? decoder.decode(Account.FeedMetadataDictionary.self, from: fileData)) ?? Account.FeedMetadataDictionary()
|
||||
}
|
||||
account.feedMetadata.values.forEach { $0.delegate = account }
|
||||
}
|
||||
|
||||
func save() {
|
||||
guard !account.isDeleted else { return }
|
||||
|
||||
let feedMetadata = metadataForOnlySubscribedToFeeds()
|
||||
|
||||
let encoder = PropertyListEncoder()
|
||||
encoder.outputFormat = .binary
|
||||
|
||||
do {
|
||||
let data = try encoder.encode(feedMetadata)
|
||||
try data.write(to: fileURL)
|
||||
} catch let error as NSError {
|
||||
Self.logger.error("Save FeedMetadataFile file to disk failed: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension FeedMetadataFile {
|
||||
|
||||
@MainActor func queueSaveToDiskIfNeeded() {
|
||||
saveQueue.add(self, #selector(saveToDiskIfNeeded))
|
||||
}
|
||||
|
||||
@MainActor @objc func saveToDiskIfNeeded() {
|
||||
if isDirty {
|
||||
isDirty = false
|
||||
save()
|
||||
}
|
||||
}
|
||||
|
||||
private func metadataForOnlySubscribedToFeeds() -> Account.FeedMetadataDictionary {
|
||||
let feedIDs = account.idToFeedDictionary.keys
|
||||
return account.feedMetadata.filter { (_, metadata) -> Bool in
|
||||
feedIDs.contains(metadata.feedID)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
//
|
||||
// FeedSettings.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Brent Simmons on 3/12/19.
|
||||
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import RSWeb
|
||||
import Articles
|
||||
|
||||
@MainActor final class FeedSettings {
|
||||
private let feedURL: String
|
||||
private let database: FeedSettingsDatabase
|
||||
weak var feed: Feed?
|
||||
|
||||
var feedID: String {
|
||||
didSet {
|
||||
if feedID != oldValue {
|
||||
database.setString(feedID, for: feedURL, column: .feedID)
|
||||
postSettingDidChange(.feedID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var homePageURL: String? {
|
||||
didSet {
|
||||
if homePageURL != oldValue {
|
||||
database.setString(homePageURL, for: feedURL, column: .homePageURL)
|
||||
postSettingDidChange(.homePageURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var iconURL: String? {
|
||||
didSet {
|
||||
if iconURL != oldValue {
|
||||
database.setString(iconURL, for: feedURL, column: .iconURL)
|
||||
postSettingDidChange(.iconURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var faviconURL: String? {
|
||||
didSet {
|
||||
if faviconURL != oldValue {
|
||||
database.setString(faviconURL, for: feedURL, column: .faviconURL)
|
||||
postSettingDidChange(.faviconURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var editedName: String? {
|
||||
didSet {
|
||||
if editedName != oldValue {
|
||||
database.setString(editedName, for: feedURL, column: .editedName)
|
||||
postSettingDidChange(.editedName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var contentHash: String? {
|
||||
didSet {
|
||||
if contentHash != oldValue {
|
||||
database.setString(contentHash, for: feedURL, column: .contentHash)
|
||||
postSettingDidChange(.contentHash)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var newArticleNotificationsEnabled = false {
|
||||
didSet {
|
||||
if newArticleNotificationsEnabled != oldValue {
|
||||
database.setBool(newArticleNotificationsEnabled, for: feedURL, column: .newArticleNotificationsEnabled)
|
||||
postSettingDidChange(.newArticleNotificationsEnabled)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var readerViewAlwaysEnabled = false {
|
||||
didSet {
|
||||
if readerViewAlwaysEnabled != oldValue {
|
||||
database.setBool(readerViewAlwaysEnabled, for: feedURL, column: .readerViewAlwaysEnabled)
|
||||
postSettingDidChange(.readerViewAlwaysEnabled)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var authors: [Author]? {
|
||||
didSet {
|
||||
if authors != oldValue {
|
||||
database.setAuthors(authors, for: feedURL)
|
||||
postSettingDidChange(.authors)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var conditionalGetInfo: HTTPConditionalGetInfo? {
|
||||
didSet {
|
||||
if conditionalGetInfo != oldValue {
|
||||
database.setConditionalGetInfo(conditionalGetInfo, for: feedURL)
|
||||
postSettingDidChange(.conditionalGetInfo)
|
||||
if conditionalGetInfo == nil {
|
||||
conditionalGetInfoDate = nil
|
||||
} else {
|
||||
conditionalGetInfoDate = Date()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var conditionalGetInfoDate: Date? {
|
||||
didSet {
|
||||
if conditionalGetInfoDate != oldValue {
|
||||
database.setDate(conditionalGetInfoDate, for: feedURL, column: .conditionalGetInfoDate)
|
||||
postSettingDidChange(.conditionalGetInfoDate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var cacheControlInfo: CacheControlInfo? {
|
||||
didSet {
|
||||
if cacheControlInfo != oldValue {
|
||||
database.setCacheControlInfo(cacheControlInfo, for: feedURL)
|
||||
postSettingDidChange(.cacheControlInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var externalID: String? {
|
||||
didSet {
|
||||
if externalID != oldValue {
|
||||
database.setString(externalID, for: feedURL, column: .externalID)
|
||||
postSettingDidChange(.externalID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Folder Name: Sync Service Relationship ID
|
||||
var folderRelationship: [String: String]? {
|
||||
didSet {
|
||||
if folderRelationship != oldValue {
|
||||
database.setFolderRelationship(folderRelationship, for: feedURL)
|
||||
postSettingDidChange(.folderRelationship)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Last time an attempt was made to read the feed.
|
||||
/// (Not necessarily a successful attempt.)
|
||||
var lastCheckDate: Date? {
|
||||
didSet {
|
||||
if lastCheckDate != oldValue {
|
||||
database.setDate(lastCheckDate, for: feedURL, column: .lastCheckDate)
|
||||
postSettingDidChange(.lastCheckDate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Create from database row (bulk load at startup).
|
||||
init(feedURL: String, row: FeedSettingsDatabase.Row, database: FeedSettingsDatabase) {
|
||||
self.feedURL = feedURL
|
||||
self.database = database
|
||||
self.feedID = row.feedID
|
||||
self.homePageURL = row.homePageURL
|
||||
self.iconURL = row.iconURL
|
||||
self.faviconURL = row.faviconURL
|
||||
self.editedName = row.editedName
|
||||
self.contentHash = row.contentHash
|
||||
self.newArticleNotificationsEnabled = row.newArticleNotificationsEnabled
|
||||
self.readerViewAlwaysEnabled = row.readerViewAlwaysEnabled
|
||||
self.authors = row.authors
|
||||
self.conditionalGetInfo = row.conditionalGetInfo
|
||||
self.conditionalGetInfoDate = row.conditionalGetInfoDate
|
||||
self.cacheControlInfo = row.cacheControlInfo
|
||||
self.externalID = row.externalID
|
||||
self.folderRelationship = row.folderRelationship
|
||||
self.lastCheckDate = row.lastCheckDate
|
||||
}
|
||||
|
||||
/// Create for a new feed not yet in the database.
|
||||
init(feedURL: String, feedID: String, database: FeedSettingsDatabase) {
|
||||
self.feedURL = feedURL
|
||||
self.database = database
|
||||
self.feedID = feedID
|
||||
database.ensureFeedExists(feedURL, feedID: feedID)
|
||||
}
|
||||
|
||||
private func postSettingDidChange(_ key: Feed.SettingKey) {
|
||||
feed?.postFeedSettingDidChangeNotification(key)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user