102 lines
2.4 KiB
Swift
102 lines
2.4 KiB
Swift
import Foundation
|
|
import SwiftUI
|
|
|
|
final class LocalizedPost: ObservableObject {
|
|
|
|
unowned let content: Content
|
|
|
|
@Published
|
|
var title: String?
|
|
|
|
@Published
|
|
var text: String
|
|
|
|
@Published
|
|
var lastModified: Date?
|
|
|
|
@Published
|
|
var images: [FileResource]
|
|
|
|
/// The text to show for the link to the `linkedPage`
|
|
@Published
|
|
var pageLinkText: String?
|
|
|
|
@Published
|
|
var linkPreview: LinkPreview
|
|
|
|
init(content: Content,
|
|
title: String? = nil,
|
|
text: String,
|
|
lastModified: Date? = nil,
|
|
images: [FileResource] = [],
|
|
pageLinkText: String? = nil,
|
|
linkPreview: LinkPreview = .init()) {
|
|
self.content = content
|
|
self.title = title
|
|
self.text = text
|
|
self.lastModified = lastModified
|
|
self.images = images
|
|
self.pageLinkText = pageLinkText
|
|
self.linkPreview = linkPreview
|
|
}
|
|
|
|
func contains(_ string: String) -> Bool {
|
|
if let title, title.contains(string) {
|
|
return true
|
|
}
|
|
return text.contains(string)
|
|
}
|
|
|
|
func remove(_ file: FileResource) {
|
|
if images.contains(file) {
|
|
images.remove(file)
|
|
}
|
|
linkPreview.remove(file)
|
|
}
|
|
|
|
// MARK: Images
|
|
|
|
var hasImages: Bool {
|
|
images.contains { $0.type.isImage }
|
|
}
|
|
|
|
var hasVideos: Bool {
|
|
images.contains { $0.type.isVideo }
|
|
}
|
|
}
|
|
|
|
// MARK: Storage
|
|
|
|
extension LocalizedPost {
|
|
|
|
convenience init(context: LoadingContext, data: Data) {
|
|
self.init(
|
|
content: context.content,
|
|
title: data.title,
|
|
text: data.text,
|
|
lastModified: data.lastModifiedDate,
|
|
images: data.images.compactMap(context.postMedia),
|
|
pageLinkText: data.pageLinkText,
|
|
linkPreview: .init(context: context, data: data.linkPreview))
|
|
}
|
|
|
|
var data: Data {
|
|
.init(images: images.map { $0.id },
|
|
title: title,
|
|
text: text,
|
|
lastModifiedDate: lastModified,
|
|
pageLinkText: pageLinkText,
|
|
linkPreview: linkPreview.data)
|
|
}
|
|
|
|
/// The structure to store the metadata of a localized post
|
|
struct Data: Codable {
|
|
let images: [String]
|
|
let title: String?
|
|
let text: String
|
|
let lastModifiedDate: Date?
|
|
let pageLinkText: String?
|
|
let linkPreview: LinkPreview.Data
|
|
}
|
|
}
|