2019-09-13 23:35:53 +02:00
|
|
|
//
|
2019-11-15 03:11:41 +01:00
|
|
|
// WebFeedMetadataFile.swift
|
2019-09-13 23:35:53 +02:00
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 9/13/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import os.log
|
|
|
|
import RSCore
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
final class WebFeedMetadataFile {
|
2019-09-13 23:35:53 +02:00
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "webFeedMetadataFile")
|
2019-09-13 23:35:53 +02:00
|
|
|
|
|
|
|
private let fileURL: URL
|
|
|
|
private let account: Account
|
|
|
|
private lazy var managedFile = ManagedResourceFile(fileURL: fileURL, load: loadCallback, save: saveCallback)
|
|
|
|
|
|
|
|
init(filename: String, account: Account) {
|
|
|
|
self.fileURL = URL(fileURLWithPath: filename)
|
|
|
|
self.account = account
|
|
|
|
}
|
|
|
|
|
|
|
|
func markAsDirty() {
|
|
|
|
managedFile.markAsDirty()
|
|
|
|
}
|
|
|
|
|
|
|
|
func load() {
|
|
|
|
managedFile.load()
|
|
|
|
}
|
|
|
|
|
2019-10-02 22:32:34 +02:00
|
|
|
func save() {
|
2019-09-23 18:09:40 +02:00
|
|
|
managedFile.saveIfNecessary()
|
|
|
|
}
|
|
|
|
|
2019-12-02 00:16:52 +01:00
|
|
|
func suspend() {
|
|
|
|
managedFile.suspend()
|
|
|
|
}
|
|
|
|
|
|
|
|
func resume() {
|
|
|
|
managedFile.resume()
|
|
|
|
}
|
|
|
|
|
2019-09-13 23:35:53 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
private extension WebFeedMetadataFile {
|
2019-09-13 23:35:53 +02:00
|
|
|
|
|
|
|
func loadCallback() {
|
|
|
|
|
|
|
|
let errorPointer: NSErrorPointer = nil
|
|
|
|
let fileCoordinator = NSFileCoordinator(filePresenter: managedFile)
|
|
|
|
|
|
|
|
fileCoordinator.coordinate(readingItemAt: fileURL, options: [], error: errorPointer, byAccessor: { readURL in
|
|
|
|
if let fileData = try? Data(contentsOf: readURL) {
|
|
|
|
let decoder = PropertyListDecoder()
|
2019-11-15 03:11:41 +01:00
|
|
|
account.webFeedMetadata = (try? decoder.decode(Account.WebFeedMetadataDictionary.self, from: fileData)) ?? Account.WebFeedMetadataDictionary()
|
2019-09-24 13:56:39 +02:00
|
|
|
}
|
2019-11-15 03:11:41 +01:00
|
|
|
account.webFeedMetadata.values.forEach { $0.delegate = account }
|
2019-09-24 13:56:39 +02:00
|
|
|
if !account.startingUp {
|
2019-11-15 03:11:41 +01:00
|
|
|
account.resetWebFeedMetadataAndUnreadCounts()
|
2019-09-13 23:35:53 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if let error = errorPointer?.pointee {
|
|
|
|
os_log(.error, log: log, "Read from disk coordination failed: %@.", error.localizedDescription)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveCallback() {
|
|
|
|
guard !account.isDeleted else { return }
|
|
|
|
|
|
|
|
let feedMetadata = metadataForOnlySubscribedToFeeds()
|
|
|
|
|
|
|
|
let encoder = PropertyListEncoder()
|
|
|
|
encoder.outputFormat = .binary
|
|
|
|
|
|
|
|
let errorPointer: NSErrorPointer = nil
|
|
|
|
let fileCoordinator = NSFileCoordinator(filePresenter: managedFile)
|
|
|
|
|
2019-09-13 23:58:10 +02:00
|
|
|
fileCoordinator.coordinate(writingItemAt: fileURL, options: [], error: errorPointer, byAccessor: { writeURL in
|
2019-09-13 23:35:53 +02:00
|
|
|
do {
|
|
|
|
let data = try encoder.encode(feedMetadata)
|
|
|
|
try data.write(to: writeURL)
|
|
|
|
} catch let error as NSError {
|
|
|
|
os_log(.error, log: log, "Save to disk failed: %@.", error.localizedDescription)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if let error = errorPointer?.pointee {
|
|
|
|
os_log(.error, log: log, "Save to disk coordination failed: %@.", error.localizedDescription)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
private func metadataForOnlySubscribedToFeeds() -> Account.WebFeedMetadataDictionary {
|
|
|
|
let webFeedIDs = account.idToWebFeedDictionary.keys
|
|
|
|
return account.webFeedMetadata.filter { (feedID: String, metadata: WebFeedMetadata) -> Bool in
|
|
|
|
return webFeedIDs.contains(metadata.webFeedID)
|
2019-09-13 23:35:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|