2020-06-29 13:16:48 +02:00
|
|
|
//
|
|
|
|
// SidebarItem.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 6/29/20.
|
|
|
|
// Copyright © 2020 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2020-07-21 10:05:31 +02:00
|
|
|
import SwiftUI
|
2020-06-29 13:16:48 +02:00
|
|
|
import RSCore
|
|
|
|
import Account
|
|
|
|
|
|
|
|
public enum SidebarItemIdentifier: Hashable, Equatable {
|
|
|
|
case smartFeedController
|
|
|
|
case account(String)
|
|
|
|
case feed(FeedIdentifier)
|
|
|
|
}
|
|
|
|
|
2020-06-30 06:37:29 +02:00
|
|
|
public enum RepresentedType {
|
2020-07-15 23:52:57 +02:00
|
|
|
case smartFeedController, webFeed, folder, pseudoFeed, account, unknown
|
2020-06-30 06:37:29 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 13:16:48 +02:00
|
|
|
struct SidebarItem: Identifiable {
|
|
|
|
|
|
|
|
var id: SidebarItemIdentifier
|
|
|
|
var represented: Any
|
2020-07-01 03:23:22 +02:00
|
|
|
var children: [SidebarItem] = [SidebarItem]()
|
2020-06-29 13:16:48 +02:00
|
|
|
|
|
|
|
var unreadCount: Int
|
2020-07-18 22:24:48 +02:00
|
|
|
var nameForDisplay: String
|
2020-06-29 13:16:48 +02:00
|
|
|
|
2020-06-30 06:37:29 +02:00
|
|
|
var feed: Feed? {
|
|
|
|
represented as? Feed
|
|
|
|
}
|
|
|
|
|
2020-07-01 03:23:22 +02:00
|
|
|
var containerID: ContainerIdentifier? {
|
|
|
|
return (represented as? ContainerIdentifiable)?.containerID
|
|
|
|
}
|
|
|
|
|
2020-06-30 06:37:29 +02:00
|
|
|
var representedType: RepresentedType {
|
|
|
|
switch type(of: represented) {
|
2020-07-15 23:52:57 +02:00
|
|
|
case is SmartFeedsController.Type:
|
|
|
|
return .smartFeedController
|
2020-06-30 06:37:29 +02:00
|
|
|
case is SmartFeed.Type:
|
|
|
|
return .pseudoFeed
|
|
|
|
case is UnreadFeed.Type:
|
|
|
|
return .pseudoFeed
|
|
|
|
case is WebFeed.Type:
|
2020-07-04 19:16:21 +02:00
|
|
|
return .webFeed
|
|
|
|
case is Folder.Type:
|
|
|
|
return .folder
|
2020-06-30 06:37:29 +02:00
|
|
|
case is Account.Type:
|
|
|
|
return .account
|
|
|
|
default:
|
|
|
|
return .unknown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 13:16:48 +02:00
|
|
|
init(_ smartFeedsController: SmartFeedsController) {
|
|
|
|
self.id = .smartFeedController
|
|
|
|
self.represented = smartFeedsController
|
|
|
|
self.unreadCount = 0
|
2020-07-18 22:24:48 +02:00
|
|
|
self.nameForDisplay = smartFeedsController.nameForDisplay
|
2020-06-29 13:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
init(_ account: Account) {
|
|
|
|
self.id = .account(account.accountID)
|
|
|
|
self.represented = account
|
|
|
|
self.unreadCount = account.unreadCount
|
2020-07-18 22:24:48 +02:00
|
|
|
self.nameForDisplay = account.nameForDisplay
|
2020-06-29 13:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
init(_ feed: Feed, unreadCount: Int) {
|
|
|
|
self.id = .feed(feed.feedID!)
|
|
|
|
self.represented = feed
|
|
|
|
self.unreadCount = unreadCount
|
2020-07-18 22:24:48 +02:00
|
|
|
self.nameForDisplay = feed.nameForDisplay
|
2020-06-29 13:16:48 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 22:24:59 +02:00
|
|
|
/// Add a sidebar item to the child list
|
2020-06-29 13:16:48 +02:00
|
|
|
mutating func addChild(_ sidebarItem: SidebarItem) {
|
2020-07-01 03:23:22 +02:00
|
|
|
children.append(sidebarItem)
|
2020-06-29 13:16:48 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 22:24:59 +02:00
|
|
|
/// Recursively visits each sidebar item. Return true when done visiting.
|
|
|
|
@discardableResult
|
|
|
|
func visit(_ block: (SidebarItem) -> Bool) -> Bool {
|
|
|
|
let stop = block(self)
|
|
|
|
if !stop {
|
|
|
|
for child in children {
|
|
|
|
if child.visit(block) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stop
|
|
|
|
}
|
2020-06-29 13:16:48 +02:00
|
|
|
}
|