mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/MastodonCore/Persistence/Persistence+Card.swift

96 lines
2.8 KiB
Swift
Raw Normal View History

2022-11-24 05:03:45 +01:00
//
// Persistence+Card.swift
//
//
// Created by MainasuK on 2021-12-9.
//
import CoreData
import CoreDataStack
import Foundation
import MastodonSDK
import os.log
extension Persistence.Card {
public struct PersistContext {
public let domain: String
public let entity: Mastodon.Entity.Card
public let me: MastodonUser?
public let log = Logger(subsystem: "Card", category: "Persistence")
public init(
domain: String,
entity: Mastodon.Entity.Card,
me: MastodonUser?
) {
self.domain = domain
self.entity = entity
self.me = me
}
}
public struct PersistResult {
public let card: Card
public let isNewInsertion: Bool
public init(
card: Card,
isNewInsertion: Bool
) {
self.card = card
self.isNewInsertion = isNewInsertion
}
#if DEBUG
public let logger = Logger(subsystem: "Persistence.MastodonCard.PersistResult", category: "Persist")
public func log() {
let pollInsertionFlag = isNewInsertion ? "+" : "-"
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): [\(pollInsertionFlag)](\(card.title)):")
}
#endif
}
public static func create(
in managedObjectContext: NSManagedObjectContext,
context: PersistContext
) -> PersistResult {
var type: MastodonCardType {
switch context.entity.type {
case .link: return .link
case .photo: return .photo
case .video: return .video
case .rich: return ._other(context.entity.type.rawValue)
case ._other(let rawValue): return ._other(rawValue)
}
}
let property = Card.Property(
2022-11-24 06:51:39 +01:00
urlRaw: context.entity.url,
2022-11-24 05:03:45 +01:00
title: context.entity.title,
desc: context.entity.description,
type: type,
authorName: context.entity.authorName,
2022-11-24 06:51:39 +01:00
authorURLRaw: context.entity.authorURL,
2022-11-24 05:03:45 +01:00
providerName: context.entity.providerName,
2022-11-24 06:51:39 +01:00
providerURLRaw: context.entity.providerURL,
2022-11-24 05:03:45 +01:00
width: Int64(context.entity.width ?? 0),
height: Int64(context.entity.height ?? 0),
image: context.entity.image,
2022-11-24 06:51:39 +01:00
embedURLRaw: context.entity.embedURL,
blurhash: context.entity.blurhash,
html: context.entity.html.flatMap { $0.isEmpty ? nil : $0 }
2022-11-24 05:03:45 +01:00
)
let card = Card.insert(
into: managedObjectContext,
property: property
)
return PersistResult(
card: card,
isNewInsertion: true
)
}
}