NetNewsWire/Multiplatform/Shared/Sidebar/SidebarItem.swift

99 lines
2.1 KiB
Swift
Raw Normal View History

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)
}
public enum RepresentedType {
2020-07-15 23:52:57 +02:00
case smartFeedController, webFeed, folder, pseudoFeed, account, unknown
}
2020-06-29 13:16:48 +02:00
struct SidebarItem: Identifiable {
var id: SidebarItemIdentifier
var represented: Any
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
var feed: Feed? {
represented as? Feed
}
var containerID: ContainerIdentifier? {
return (represented as? ContainerIdentifiable)?.containerID
}
var representedType: RepresentedType {
switch type(of: represented) {
2020-07-15 23:52:57 +02:00
case is SmartFeedsController.Type:
return .smartFeedController
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
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) {
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
}