2025-02-05 12:24:33 +01:00

84 lines
1.6 KiB
Swift

import Foundation
import Combine
class Item: ObservableContentItem, Identifiable {
unowned let content: Content
/// A dummy property to force views to update when properties change
@Published
private var changeToggle = false
@Published
var id: String
var cancellables = Set<AnyCancellable>()
init(content: Content, id: String) {
self.content = content
self.id = id
observeChanges()
}
// MARK: Change observation
func didChange() {
DispatchQueue.main.async {
self.changeToggle.toggle()
}
}
// MARK: Paths
func makeCleanAbsolutePath(_ path: String) -> String {
"/" + makeCleanRelativePath(path)
}
func makeCleanRelativePath(_ path: String) -> String {
path.components(separatedBy: "/").filter { !$0.isEmpty }.joined(separator: "/")
}
func title(in language: ContentLanguage) -> String {
fatalError()
}
func absoluteUrl(in language: ContentLanguage) -> String {
fatalError()
}
var itemType: ItemType {
fatalError()
}
var itemReference: ItemReference {
fatalError()
}
var itemId: ItemId {
.init(type: itemType, id: id)
}
}
extension Item: Equatable {
static func == (lhs: Item, rhs: Item) -> Bool {
lhs.id == rhs.id && lhs.itemType == rhs.itemType
}
}
extension Item: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(id)
hasher.combine(itemType)
}
}
extension Item: Comparable {
static func < (lhs: Item, rhs: Item) -> Bool {
lhs.id < rhs.id && lhs.itemType < rhs.itemType
}
}