Files
NetNewsWire/Modules/Account/Sources/Account/Folder.swift
T

229 lines
5.4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// Folder.swift
// NetNewsWire
//
// Created by Brent Simmons on 7/1/17.
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import Articles
import RSCore
public final class Folder: SidebarItem, Renamable, Container, Hashable {
nonisolated public let accountID: String
public weak var account: Account?
public var defaultReadFilterType: ReadFilterType {
return .read
}
public var containerID: ContainerIdentifier? {
ContainerIdentifier.folder(accountID, nameForDisplay)
}
public var sidebarItemID: SidebarItemIdentifier? {
SidebarItemIdentifier.folder(accountID, nameForDisplay)
}
public var topLevelFeeds: Set<Feed> = Set<Feed>()
public var folders: Set<Folder>? // subfolders are not supported, so this is always nil
public var name: String? {
didSet {
postDisplayNameDidChangeNotification()
}
}
static let untitledName = NSLocalizedString("Untitled ƒ", comment: "Folder name")
nonisolated public let folderID: Int // not saved: per-run only
public var externalID: String?
static var incrementingID = 0
// MARK: - DisplayNameProvider
public var nameForDisplay: String {
return name ?? Folder.untitledName
}
// MARK: - UnreadCountProvider
public var unreadCount = 0 {
didSet {
if unreadCount != oldValue {
postUnreadCountDidChangeNotification()
}
}
}
// MARK: - Renamable
public func rename(to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
guard let account else {
return
}
Task { @MainActor in
do {
try await account.renameFolder(self, to: name)
completion(.success(()))
} catch {
completion(.failure(error))
}
}
}
// MARK: - Init
init(account: Account, name: String?) {
self.accountID = account.accountID
self.account = account
self.name = name
let folderID = Folder.incrementingID
Folder.incrementingID += 1
self.folderID = folderID
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(childrenDidChange(_:)), name: .ChildrenDidChange, object: self)
}
// MARK: - Notifications
@objc func unreadCountDidChange(_ note: Notification) {
if let object = note.object {
if objectIsChild(object as AnyObject) {
CoalescingQueue.standard.add(self, #selector(updateUnreadCount))
}
}
}
@objc func childrenDidChange(_ note: Notification) {
updateUnreadCount()
}
@objc func updateUnreadCount() {
var updatedUnreadCount = 0
for feed in topLevelFeeds {
updatedUnreadCount += feed.unreadCount
}
unreadCount = updatedUnreadCount
}
// MARK: Container
public func flattenedFeeds() -> Set<Feed> {
// Since sub-folders are not supported, it’s always the top-level feeds.
return topLevelFeeds
}
public func objectIsChild(_ object: AnyObject) -> Bool {
// Folders contain Feed objects only, at least for now.
guard let feed = object as? Feed else {
return false
}
return topLevelFeeds.contains(feed)
}
public func addFeedToTreeAtTopLevel(_ feed: Feed) {
topLevelFeeds.insert(feed)
postChildrenDidChangeNotification()
}
public func addFeeds(_ feeds: Set<Feed>) {
guard !feeds.isEmpty else {
return
}
topLevelFeeds.formUnion(feeds)
postChildrenDidChangeNotification()
}
public func removeFeedFromTreeAtTopLevel(_ feed: Feed) {
topLevelFeeds.remove(feed)
postChildrenDidChangeNotification()
}
public func removeFeedsFromTreeAtTopLevel(_ feeds: Set<Feed>) {
guard !feeds.isEmpty else {
return
}
topLevelFeeds.subtract(feeds)
postChildrenDidChangeNotification()
}
/// Replace the entire top-level feed set in one shot, posting a single change notification.
public func replaceTopLevelFeeds(_ feeds: Set<Feed>) {
topLevelFeeds = feeds
postChildrenDidChangeNotification()
}
// MARK: - Hashable
public func hash(into hasher: inout Hasher) {
hasher.combine(folderID)
}
// MARK: - Equatable
static public func ==(lhs: Folder, rhs: Folder) -> Bool {
return lhs === rhs
}
}
// MARK: - Private
private extension Folder {
func childrenContain(_ feed: Feed) -> Bool {
return topLevelFeeds.contains(feed)
}
}
// MARK: - OPMLRepresentable
extension Folder: OPMLRepresentable {
public func OPMLString(indentLevel: Int, allowCustomAttributes: Bool) -> String {
let attrExternalID: String = {
if allowCustomAttributes, let externalID = externalID {
return " nnw_externalID=\"\(externalID.escapingSpecialXMLCharacters)\""
} else {
return ""
}
}()
let escapedTitle = nameForDisplay.escapingSpecialXMLCharacters
var s = "<outline text=\"\(escapedTitle)\" title=\"\(escapedTitle)\"\(attrExternalID)>\n"
s = s.prepending(tabCount: indentLevel)
var hasAtLeastOneChild = false
for feed in topLevelFeeds.sorted() {
s += feed.OPMLString(indentLevel: indentLevel + 1, allowCustomAttributes: allowCustomAttributes)
hasAtLeastOneChild = true
}
if !hasAtLeastOneChild {
s = "<outline text=\"\(escapedTitle)\" title=\"\(escapedTitle)\"\(attrExternalID)/>\n"
s = s.prepending(tabCount: indentLevel)
return s
}
s = s + String(repeating: "\t", count: indentLevel) + "</outline>\n"
return s
}
}
// MARK: Set
@MainActor extension Set where Element == Folder {
func sorted() -> [Folder] {
return sorted(by: { (folder1, folder2) -> Bool in
return folder1.nameForDisplay.localizedStandardCompare(folder2.nameForDisplay) == .orderedAscending
})
}
}