Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/Scene/Share/ViewModel/MosaicImageViewModel.swift

56 lines
1.5 KiB
Swift
Raw Normal View History

//
// MosaicImageViewModel.swift
// Mastodon
//
// Created by Cirno MainasuK on 2021-2-23.
//
import UIKit
import Combine
import CoreDataStack
struct MosaicImageViewModel {
let metas: [MosaicMeta]
init(mediaAttachments: [Attachment]) {
var metas: [MosaicMeta] = []
for element in mediaAttachments where element.type == .image {
guard let meta = element.meta,
let width = meta.original?.width,
let height = meta.original?.height,
let url = URL(string: element.url) else {
continue
}
let mosaicMeta = MosaicMeta(
2021-07-02 13:48:56 +02:00
previewURL: element.previewURL.flatMap { URL(string: $0) },
url: url,
size: CGSize(width: width, height: height),
blurhash: element.blurhash,
altText: element.descriptionString
)
metas.append(mosaicMeta)
}
self.metas = metas
}
}
struct MosaicMeta {
static let edgeMaxLength: CGFloat = 20
2021-07-02 13:48:56 +02:00
let previewURL: URL?
let url: URL
let size: CGSize
let blurhash: String?
let altText: String?
func blurhashImagePublisher() -> AnyPublisher<UIImage?, Never> {
guard let blurhash = blurhash else {
return Just(nil).eraseToAnyPublisher()
}
return AppContext.shared.blurhashImageCacheService.image(blurhash: blurhash, size: size, url: url)
}
}