From f7bb58ea4d925f4f9d2591fbf9f09c418be4d525 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Fri, 13 Sep 2019 15:46:22 -0500 Subject: [PATCH] Modify OPMLFile to use the new ManagedResourceFile utility class --- Frameworks/Account/OPMLFile.swift | 68 ++++++++----------------------- submodules/RSCore | 2 +- 2 files changed, 18 insertions(+), 52 deletions(-) diff --git a/Frameworks/Account/OPMLFile.swift b/Frameworks/Account/OPMLFile.swift index 3d78192c7..2932e7fab 100644 --- a/Frameworks/Account/OPMLFile.swift +++ b/Frameworks/Account/OPMLFile.swift @@ -11,81 +11,49 @@ import os.log import RSCore import RSParser -final class OPMLFile: NSObject, NSFilePresenter { +final class OPMLFile { private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "opmlFile") - private var isDirty = false { - didSet { - queueSaveToDiskIfNeeded() - } - } - - private var isLoading = false private let fileURL: URL private let account: Account - private let operationQueue: OperationQueue - - var presentedItemURL: URL? { - return fileURL - } - - var presentedItemOperationQueue: OperationQueue { - return operationQueue - } + private lazy var managedFile = ManagedResourceFile(fileURL: fileURL, load: loadCallback, reload: reloadCallback, save: saveCallback) init(filename: String, account: Account) { self.fileURL = URL(fileURLWithPath: filename) self.account = account - operationQueue = OperationQueue() - operationQueue.maxConcurrentOperationCount = 1 - - super.init() - - NSFileCoordinator.addFilePresenter(self) - } - - func presentedItemDidChange() { - DispatchQueue.main.async { - self.reload() - } } func markAsDirty() { - if !isLoading { - isDirty = true - } + managedFile.markAsDirty() } func queueSaveToDiskIfNeeded() { - Account.saveQueue.add(self, #selector(saveToDiskIfNeeded)) + managedFile.queueSaveToDiskIfNeeded() } func load() { - isLoading = true - guard let opmlItems = parsedOPMLItems() else { return } - BatchUpdate.shared.perform { - account.loadOPMLItems(opmlItems, parentFolder: nil) - } - isLoading = false + managedFile.load() } } private extension OPMLFile { - - @objc func saveToDiskIfNeeded() { - if isDirty && !account.isDeleted { - isDirty = false - save() + + func loadCallback() { + guard let opmlItems = parsedOPMLItems() else { return } + BatchUpdate.shared.perform { + account.loadOPMLItems(opmlItems, parentFolder: nil) } } - - func save() { + + func saveCallback() { + guard !account.isDeleted else { return } + let opmlDocumentString = opmlDocument() let errorPointer: NSErrorPointer = nil - let fileCoordinator = NSFileCoordinator(filePresenter: self) + let fileCoordinator = NSFileCoordinator(filePresenter: managedFile) fileCoordinator.coordinate(writingItemAt: fileURL, options: .forReplacing, error: errorPointer, byAccessor: { writeURL in do { @@ -100,21 +68,19 @@ private extension OPMLFile { } } - func reload() { - isLoading = true + func reloadCallback() { guard let opmlItems = parsedOPMLItems() else { return } BatchUpdate.shared.perform { account.topLevelFeeds.removeAll() account.loadOPMLItems(opmlItems, parentFolder: nil) } - isLoading = false } func parsedOPMLItems() -> [RSOPMLItem]? { var fileData: Data? = nil let errorPointer: NSErrorPointer = nil - let fileCoordinator = NSFileCoordinator(filePresenter: self) + let fileCoordinator = NSFileCoordinator(filePresenter: managedFile) fileCoordinator.coordinate(readingItemAt: fileURL, options: [], error: errorPointer, byAccessor: { readURL in do { diff --git a/submodules/RSCore b/submodules/RSCore index d640a2310..1adcf4087 160000 --- a/submodules/RSCore +++ b/submodules/RSCore @@ -1 +1 @@ -Subproject commit d640a2310b96a0a3d4d34c49c08c7bce195d0762 +Subproject commit 1adcf4087b6cb5806c81d8cdfd68d73858408ae5