Add labels to posts
This commit is contained in:
49
CHDataManagement/Model/ContentLabel.swift
Normal file
49
CHDataManagement/Model/ContentLabel.swift
Normal file
@ -0,0 +1,49 @@
|
||||
import Foundation
|
||||
|
||||
final class ContentLabel: ObservableObject {
|
||||
|
||||
@Published
|
||||
var icon: PageIcon
|
||||
|
||||
@Published
|
||||
var value: String
|
||||
|
||||
init(icon: PageIcon, value: String) {
|
||||
self.icon = icon
|
||||
self.value = value
|
||||
}
|
||||
}
|
||||
|
||||
extension ContentLabel: Equatable {
|
||||
|
||||
static func == (lhs: ContentLabel, rhs: ContentLabel) -> Bool {
|
||||
lhs.icon == rhs.icon && lhs.value == rhs.value
|
||||
}
|
||||
}
|
||||
|
||||
extension ContentLabel: Identifiable {
|
||||
|
||||
var id: String {
|
||||
icon.rawValue + value
|
||||
}
|
||||
}
|
||||
|
||||
extension ContentLabel {
|
||||
|
||||
var data: Data {
|
||||
.init(icon: icon.rawValue, value: value)
|
||||
}
|
||||
|
||||
convenience init?(context: LoadingContext, data: Data) {
|
||||
guard let icon = PageIcon(rawValue: data.icon) else {
|
||||
context.error("Unknown label icon '\(data.icon)'")
|
||||
return nil
|
||||
}
|
||||
self.init(icon: icon, value: data.value)
|
||||
}
|
||||
|
||||
struct Data: Codable {
|
||||
let icon: String
|
||||
let value: String
|
||||
}
|
||||
}
|
@ -17,6 +17,10 @@ final class LocalizedPost: ObservableObject {
|
||||
@Published
|
||||
var images: [FileResource]
|
||||
|
||||
/// The labels to show beneath the title
|
||||
@Published
|
||||
var labels: [ContentLabel]
|
||||
|
||||
/// The text to show for the link to the `linkedPage`
|
||||
@Published
|
||||
var pageLinkText: String?
|
||||
@ -29,6 +33,7 @@ final class LocalizedPost: ObservableObject {
|
||||
text: String,
|
||||
lastModified: Date? = nil,
|
||||
images: [FileResource] = [],
|
||||
labels: [ContentLabel] = [],
|
||||
pageLinkText: String? = nil,
|
||||
linkPreview: LinkPreview = .init()) {
|
||||
self.content = content
|
||||
@ -36,6 +41,7 @@ final class LocalizedPost: ObservableObject {
|
||||
self.text = text
|
||||
self.lastModified = lastModified
|
||||
self.images = images
|
||||
self.labels = labels
|
||||
self.pageLinkText = pageLinkText
|
||||
self.linkPreview = linkPreview
|
||||
}
|
||||
@ -86,12 +92,14 @@ extension LocalizedPost {
|
||||
text: data.text,
|
||||
lastModified: data.lastModifiedDate,
|
||||
images: data.images.compactMap(context.postMedia),
|
||||
labels: data.labels?.compactMap { ContentLabel(context: context, data: $0) } ?? [],
|
||||
pageLinkText: data.pageLinkText,
|
||||
linkPreview: .init(context: context, data: data.linkPreview))
|
||||
}
|
||||
|
||||
var data: Data {
|
||||
.init(images: images.map { $0.id },
|
||||
labels: labels.map { $0.data }.nonEmpty,
|
||||
title: title,
|
||||
text: text,
|
||||
lastModifiedDate: lastModified,
|
||||
@ -102,6 +110,7 @@ extension LocalizedPost {
|
||||
/// The structure to store the metadata of a localized post
|
||||
struct Data: Codable {
|
||||
let images: [String]
|
||||
let labels: [ContentLabel.Data]?
|
||||
let title: String?
|
||||
let text: String
|
||||
let lastModifiedDate: Date?
|
||||
|
Reference in New Issue
Block a user