2021-02-23 12:18:34 +01:00
|
|
|
//
|
|
|
|
// MosaicImageViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by Cirno MainasuK on 2021-2-23.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2021-04-16 14:06:36 +02:00
|
|
|
import Combine
|
2021-02-23 12:18:34 +01:00
|
|
|
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,
|
2021-06-20 19:38:13 +02:00
|
|
|
let url = URL(string: element.url) else {
|
2021-02-23 12:18:34 +01:00
|
|
|
continue
|
|
|
|
}
|
2021-04-16 14:06:36 +02:00
|
|
|
let mosaicMeta = MosaicMeta(
|
2021-06-20 19:38:13 +02:00
|
|
|
priviewURL: element.previewURL.flatMap { URL(string: $0) },
|
2021-04-16 14:06:36 +02:00
|
|
|
url: url,
|
|
|
|
size: CGSize(width: width, height: height),
|
2021-05-12 12:26:53 +02:00
|
|
|
blurhash: element.blurhash,
|
|
|
|
altText: element.descriptionString
|
2021-04-16 14:06:36 +02:00
|
|
|
)
|
|
|
|
metas.append(mosaicMeta)
|
2021-02-23 12:18:34 +01:00
|
|
|
}
|
|
|
|
self.metas = metas
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MosaicMeta {
|
2021-04-16 14:06:36 +02:00
|
|
|
static let edgeMaxLength: CGFloat = 20
|
2021-06-20 19:38:13 +02:00
|
|
|
|
|
|
|
let priviewURL: URL?
|
2021-02-23 12:18:34 +01:00
|
|
|
let url: URL
|
|
|
|
let size: CGSize
|
2021-04-16 14:06:36 +02:00
|
|
|
let blurhash: String?
|
2021-05-12 12:26:53 +02:00
|
|
|
let altText: String?
|
2021-04-16 14:06:36 +02:00
|
|
|
|
|
|
|
func blurhashImagePublisher() -> AnyPublisher<UIImage?, Never> {
|
2021-04-28 13:06:45 +02:00
|
|
|
guard let blurhash = blurhash else {
|
2021-06-16 12:32:48 +02:00
|
|
|
return Just(nil).eraseToAnyPublisher()
|
2021-04-28 13:06:45 +02:00
|
|
|
}
|
2021-06-16 12:32:48 +02:00
|
|
|
return AppContext.shared.blurhashImageCacheService.image(blurhash: blurhash, size: size, url: url)
|
2021-04-28 13:06:45 +02:00
|
|
|
}
|
|
|
|
|
2021-02-23 12:18:34 +01:00
|
|
|
}
|