2020-06-28 21:21:43 +02:00
|
|
|
//
|
|
|
|
// SidebarModel.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 6/28/20.
|
|
|
|
// Copyright © 2020 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2020-06-29 23:58:10 +02:00
|
|
|
import RSCore
|
2020-06-29 00:43:20 +02:00
|
|
|
import Account
|
|
|
|
|
|
|
|
protocol SidebarModelDelegate: class {
|
|
|
|
func sidebarSelectionDidChange(_: SidebarModel, feeds: [Feed]?)
|
2020-06-29 13:16:48 +02:00
|
|
|
func unreadCount(for: Feed) -> Int
|
2020-06-29 00:43:20 +02:00
|
|
|
}
|
2020-06-28 21:21:43 +02:00
|
|
|
|
|
|
|
class SidebarModel: ObservableObject {
|
|
|
|
|
2020-06-29 00:43:20 +02:00
|
|
|
weak var delegate: SidebarModelDelegate?
|
|
|
|
|
2020-06-29 13:16:48 +02:00
|
|
|
@Published var sidebarItems = [SidebarItem]()
|
|
|
|
|
2020-06-29 23:58:10 +02:00
|
|
|
init() {
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidInitialize(_:)), name: .UnreadCountDidInitialize, object: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: API
|
|
|
|
|
2020-06-29 13:16:48 +02:00
|
|
|
func rebuildSidebarItems() {
|
|
|
|
guard let delegate = delegate else { return }
|
|
|
|
var items = [SidebarItem]()
|
|
|
|
|
|
|
|
var smartFeedControllerItem = SidebarItem(SmartFeedsController.shared)
|
|
|
|
for feed in SmartFeedsController.shared.smartFeeds {
|
|
|
|
smartFeedControllerItem.addChild(SidebarItem(feed, unreadCount: delegate.unreadCount(for: feed)))
|
|
|
|
}
|
|
|
|
items.append(smartFeedControllerItem)
|
|
|
|
|
2020-06-29 22:03:17 +02:00
|
|
|
for account in AccountManager.shared.sortedActiveAccounts {
|
|
|
|
var accountItem = SidebarItem(account)
|
|
|
|
|
2020-06-29 23:58:10 +02:00
|
|
|
for webFeed in sort(account.topLevelWebFeeds) {
|
2020-06-29 22:03:17 +02:00
|
|
|
accountItem.addChild(SidebarItem(webFeed, unreadCount: delegate.unreadCount(for: webFeed)))
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:58:10 +02:00
|
|
|
for folder in sort(account.folders ?? Set<Folder>()) {
|
2020-06-29 22:03:17 +02:00
|
|
|
var folderItem = SidebarItem(folder, unreadCount: delegate.unreadCount(for: folder))
|
2020-06-29 23:58:10 +02:00
|
|
|
for webFeed in sort(folder.topLevelWebFeeds) {
|
2020-06-29 22:03:17 +02:00
|
|
|
folderItem.addChild(SidebarItem(webFeed, unreadCount: delegate.unreadCount(for: webFeed)))
|
|
|
|
}
|
|
|
|
accountItem.addChild(folderItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
items.append(accountItem)
|
|
|
|
}
|
|
|
|
|
2020-06-29 13:16:48 +02:00
|
|
|
sidebarItems = items
|
|
|
|
}
|
|
|
|
|
2020-06-28 21:21:43 +02:00
|
|
|
}
|
2020-06-29 23:58:10 +02:00
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
private extension SidebarModel {
|
|
|
|
|
|
|
|
@objc func unreadCountDidInitialize(_ notification: Notification) {
|
|
|
|
guard notification.object is AccountManager else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rebuildSidebarItems()
|
|
|
|
}
|
|
|
|
|
|
|
|
func sort(_ folders: Set<Folder>) -> [Folder] {
|
|
|
|
return folders.sorted(by: { $0.nameForDisplay.localizedStandardCompare($1.nameForDisplay) == .orderedAscending })
|
|
|
|
}
|
|
|
|
|
|
|
|
func sort(_ feeds: Set<WebFeed>) -> [Feed] {
|
|
|
|
return feeds.sorted(by: { $0.nameForDisplay.localizedStandardCompare($1.nameForDisplay) == .orderedAscending })
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|