106 lines
2.0 KiB
Swift
106 lines
2.0 KiB
Swift
import Foundation
|
|
|
|
final class Post: ObservableObject {
|
|
|
|
unowned let content: Content
|
|
|
|
@Published
|
|
var id: String
|
|
|
|
@Published
|
|
var isDraft: Bool
|
|
|
|
@Published
|
|
var createdDate: Date
|
|
|
|
@Published
|
|
var startDate: Date
|
|
|
|
@Published
|
|
var hasEndDate: Bool
|
|
|
|
@Published
|
|
var endDate: Date
|
|
|
|
@Published
|
|
var tags: [Tag]
|
|
|
|
@Published
|
|
var german: LocalizedPost
|
|
|
|
@Published
|
|
var english: LocalizedPost
|
|
|
|
/// The page linked to by this post
|
|
@Published
|
|
var linkedPage: Page?
|
|
|
|
init(content: Content,
|
|
id: String,
|
|
isDraft: Bool,
|
|
createdDate: Date,
|
|
startDate: Date,
|
|
endDate: Date?,
|
|
tags: [Tag],
|
|
german: LocalizedPost,
|
|
english: LocalizedPost,
|
|
linkedPage: Page? = nil) {
|
|
self.content = content
|
|
self.id = id
|
|
self.isDraft = isDraft
|
|
self.createdDate = createdDate
|
|
self.startDate = startDate
|
|
self.hasEndDate = endDate != nil
|
|
self.endDate = endDate ?? startDate
|
|
self.tags = tags
|
|
self.german = german
|
|
self.english = english
|
|
self.linkedPage = linkedPage
|
|
}
|
|
|
|
func localized(in language: ContentLanguage) -> LocalizedPost {
|
|
switch language {
|
|
case .english: return english
|
|
case .german: return german
|
|
}
|
|
}
|
|
|
|
func isValid(id: String) -> Bool {
|
|
!id.isEmpty &&
|
|
content.isValidIdForTagOrPageOrPost(id) &&
|
|
content.isNewIdForPost(id)
|
|
}
|
|
|
|
@discardableResult
|
|
func update(id newId: String) -> Bool {
|
|
guard content.storage.move(post: id, to: newId) else {
|
|
print("Failed to move file of post \(id)")
|
|
return false
|
|
}
|
|
id = newId
|
|
return true
|
|
}
|
|
}
|
|
|
|
extension Post: Identifiable {
|
|
|
|
}
|
|
|
|
extension Post: Equatable {
|
|
|
|
static func == (lhs: Post, rhs: Post) -> Bool {
|
|
lhs.id == rhs.id
|
|
}
|
|
}
|
|
|
|
extension Post: Hashable {
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
hasher.combine(id)
|
|
}
|
|
}
|
|
|
|
extension Post: DateItem {
|
|
|
|
}
|