2021-04-28 09:02:34 +02:00
|
|
|
//
|
|
|
|
// MediaPreviewImageViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-4-28.
|
|
|
|
//
|
|
|
|
|
2021-04-30 13:28:06 +02:00
|
|
|
import os.log
|
2021-04-28 09:02:34 +02:00
|
|
|
import UIKit
|
|
|
|
import Combine
|
2021-04-30 13:28:06 +02:00
|
|
|
import AlamofireImage
|
2021-04-28 09:02:34 +02:00
|
|
|
|
|
|
|
class MediaPreviewImageViewModel {
|
|
|
|
|
|
|
|
// input
|
|
|
|
let item: ImagePreviewItem
|
2021-04-30 13:28:06 +02:00
|
|
|
|
|
|
|
// output
|
|
|
|
let image: CurrentValueSubject<UIImage?, Never>
|
2021-05-12 12:26:53 +02:00
|
|
|
let altText: String?
|
2021-04-28 09:02:34 +02:00
|
|
|
|
2021-04-28 14:10:17 +02:00
|
|
|
init(meta: RemoteImagePreviewMeta) {
|
2021-04-28 09:02:34 +02:00
|
|
|
self.item = .status(meta)
|
2021-04-30 13:28:06 +02:00
|
|
|
self.image = CurrentValueSubject(meta.thumbnail)
|
2021-05-12 12:26:53 +02:00
|
|
|
self.altText = meta.altText
|
2021-04-30 13:28:06 +02:00
|
|
|
|
|
|
|
let url = meta.url
|
|
|
|
ImageDownloader.default.download(URLRequest(url: url), completion: { [weak self] response in
|
|
|
|
guard let self = self else { return }
|
|
|
|
switch response.result {
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: download image %s fail: %s", ((#file as NSString).lastPathComponent), #line, #function, url.debugDescription, error.localizedDescription)
|
|
|
|
case .success(let image):
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: download image %s success", ((#file as NSString).lastPathComponent), #line, #function, url.debugDescription)
|
|
|
|
self.image.value = image
|
|
|
|
}
|
|
|
|
})
|
2021-04-28 09:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
init(meta: LocalImagePreviewMeta) {
|
|
|
|
self.item = .local(meta)
|
2021-04-30 13:28:06 +02:00
|
|
|
self.image = CurrentValueSubject(meta.image)
|
2021-05-12 12:26:53 +02:00
|
|
|
self.altText = nil
|
2021-04-28 09:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension MediaPreviewImageViewModel {
|
|
|
|
enum ImagePreviewItem {
|
2021-04-28 14:10:17 +02:00
|
|
|
case status(RemoteImagePreviewMeta)
|
2021-04-28 09:02:34 +02:00
|
|
|
case local(LocalImagePreviewMeta)
|
2021-04-29 13:49:46 +02:00
|
|
|
|
|
|
|
var activityItems: [Any] {
|
|
|
|
var items: [Any] = []
|
|
|
|
|
|
|
|
switch self {
|
|
|
|
case .status(let meta):
|
|
|
|
items.append(meta.url)
|
|
|
|
case .local(let meta):
|
|
|
|
items.append(meta.image)
|
|
|
|
}
|
|
|
|
|
|
|
|
return items
|
|
|
|
}
|
2021-04-28 09:02:34 +02:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:10:17 +02:00
|
|
|
struct RemoteImagePreviewMeta {
|
2021-04-28 09:02:34 +02:00
|
|
|
let url: URL
|
|
|
|
let thumbnail: UIImage?
|
2021-05-12 12:26:53 +02:00
|
|
|
let altText: String?
|
2021-04-28 09:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct LocalImagePreviewMeta {
|
|
|
|
let image: UIImage
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|