Files
NetNewsWire/Shared/ShareExtension/ExtensionFeedAddRequestFile.swift
T

189 lines
4.9 KiB
Swift
Raw Normal View History

//
// ExtensionFeedAddRequestFile.swift
// NetNewsWire-iOS
//
// Created by Maurice Parker on 2/11/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import Foundation
import RSCore
2026-03-23 11:23:15 -07:00
import os
import Account
2025-11-18 13:25:47 -08:00
final class ExtensionFeedAddRequestFile: NSObject, NSFilePresenter, Sendable {
static let shared = ExtensionFeedAddRequestFile()
2025-10-02 11:27:39 -07:00
static private let logger = Logger(subsystem: Logger.nnwSubsystem, category: "ExtensionFeedAddRequestFile")
2025-11-18 13:25:47 -08:00
private static let filePath: String = {
let appGroup = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as! String
let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)
return containerURL!.appendingPathComponent("extension_feed_add_request.plist").path
}()
private let operationQueue = {
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
return queue
}()
var presentedItemURL: URL? {
URL(fileURLWithPath: ExtensionFeedAddRequestFile.filePath)
}
var presentedItemOperationQueue: OperationQueue {
operationQueue
}
2026-03-29 21:31:02 -07:00
private let didStart = OSAllocatedUnfairLock(initialState: false)
func start() {
2026-03-29 21:31:02 -07:00
let alreadyStarted = didStart.withLock { didStart -> Bool in
2025-11-18 13:25:47 -08:00
if didStart {
2026-03-29 21:31:02 -07:00
return true
2025-11-18 13:25:47 -08:00
}
didStart = true
2026-03-29 21:31:02 -07:00
return false
2025-11-18 13:25:47 -08:00
}
2026-03-29 21:31:02 -07:00
if alreadyStarted {
assertionFailure("start called when already did start")
return
}
NSFileCoordinator.addFilePresenter(self)
2025-11-13 17:11:34 -08:00
Task { @MainActor in
process()
}
}
func presentedItemDidChange() {
2025-11-13 17:11:34 -08:00
Task { @MainActor in
process()
}
}
func resume() {
2025-11-18 13:25:47 -08:00
didStart.withLock { didStart in
assert(didStart)
}
NSFileCoordinator.addFilePresenter(self)
2025-11-13 17:11:34 -08:00
Task { @MainActor in
process()
}
}
func suspend() {
2025-11-18 13:25:47 -08:00
didStart.withLock { didStart in
assert(didStart)
}
NSFileCoordinator.removeFilePresenter(self)
}
static func save(_ feedAddRequest: ExtensionFeedAddRequest) {
let decoder = PropertyListDecoder()
let encoder = PropertyListEncoder()
encoder.outputFormat = .binary
let errorPointer: NSErrorPointer = nil
let fileCoordinator = NSFileCoordinator()
let fileURL = URL(fileURLWithPath: ExtensionFeedAddRequestFile.filePath)
fileCoordinator.coordinate(writingItemAt: fileURL, options: [.forMerging], error: errorPointer, byAccessor: { url in
do {
var requests: [ExtensionFeedAddRequest]
if let fileData = try? Data(contentsOf: url),
let decodedRequests = try? decoder.decode([ExtensionFeedAddRequest].self, from: fileData) {
requests = decodedRequests
} else {
requests = [ExtensionFeedAddRequest]()
}
requests.append(feedAddRequest)
let data = try encoder.encode(requests)
try data.write(to: url)
} catch let error as NSError {
2025-10-02 11:27:39 -07:00
Self.logger.error("Save to disk failed: \(error.localizedDescription)")
}
})
if let error = errorPointer?.pointee {
2025-10-02 11:27:39 -07:00
Self.logger.error("Save to disk coordination failed: \(error.localizedDescription)")
}
}
}
2025-11-13 17:11:34 -08:00
@MainActor private extension ExtensionFeedAddRequestFile {
func process() {
let decoder = PropertyListDecoder()
let encoder = PropertyListEncoder()
encoder.outputFormat = .binary
let errorPointer: NSErrorPointer = nil
let fileCoordinator = NSFileCoordinator(filePresenter: self)
let fileURL = URL(fileURLWithPath: ExtensionFeedAddRequestFile.filePath)
2026-01-01 15:50:20 -08:00
var requests: [ExtensionFeedAddRequest]?
fileCoordinator.coordinate(writingItemAt: fileURL, options: [.forMerging], error: errorPointer, byAccessor: { url in
do {
if let fileData = try? Data(contentsOf: url),
let decodedRequests = try? decoder.decode([ExtensionFeedAddRequest].self, from: fileData) {
requests = decodedRequests
}
let data = try encoder.encode([ExtensionFeedAddRequest]())
try data.write(to: url)
} catch let error as NSError {
2025-10-02 11:27:39 -07:00
Self.logger.error("Save to disk failed: \(error.localizedDescription)")
}
})
if let error = errorPointer?.pointee {
2025-10-02 11:27:39 -07:00
Self.logger.error("Save to disk coordination failed: \(error.localizedDescription)")
}
if let requests {
for request in requests {
processRequest(request)
}
}
}
func processRequest(_ request: ExtensionFeedAddRequest) {
2026-01-01 15:50:20 -08:00
var destinationAccountID: String?
switch request.destinationContainerID {
case .account(let accountID):
destinationAccountID = accountID
case .folder(let accountID, _):
destinationAccountID = accountID
default:
break
}
2025-11-17 20:16:41 -08:00
guard let accountID = destinationAccountID, let account = AccountManager.shared.existingAccount(accountID: accountID) else {
return
}
2026-01-01 15:38:51 -08:00
var destinationContainer: Container?
if account.containerID == request.destinationContainerID {
destinationContainer = account
} else {
destinationContainer = account.folders?.first(where: { $0.containerID == request.destinationContainerID })
}
guard let container = destinationContainer else { return }
2025-11-02 15:40:05 -08:00
account.createFeed(url: request.feedURL.absoluteString, name: request.name, container: container, validateFeed: true) { _ in }
}
}