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-07-21 13:45:24 +02:00
|
|
|
import Alamofire
|
|
|
|
import AlamofireImage
|
|
|
|
import FLAnimatedImage
|
2021-04-28 09:02:34 +02:00
|
|
|
|
|
|
|
class MediaPreviewImageViewModel {
|
2021-07-02 13:48:56 +02:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2021-04-28 09:02:34 +02:00
|
|
|
|
|
|
|
// input
|
|
|
|
let item: ImagePreviewItem
|
2021-04-30 13:28:06 +02:00
|
|
|
|
|
|
|
// output
|
2021-07-21 13:45:24 +02:00
|
|
|
let image: CurrentValueSubject<(UIImage?, FLAnimatedImage?), 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-07-21 13:45:24 +02:00
|
|
|
self.image = CurrentValueSubject((meta.thumbnail, nil))
|
2021-05-12 12:26:53 +02:00
|
|
|
self.altText = meta.altText
|
2021-04-30 13:28:06 +02:00
|
|
|
|
|
|
|
let url = meta.url
|
2021-07-21 13:45:24 +02:00
|
|
|
AF.request(url).publishData()
|
|
|
|
.map { response in
|
|
|
|
switch response.result {
|
|
|
|
case .success(let data):
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: download image %s success", ((#file as NSString).lastPathComponent), #line, #function, url.debugDescription)
|
|
|
|
let image = UIImage(data: data, scale: UIScreen.main.scale)
|
|
|
|
let animatedImage = FLAnimatedImage(animatedGIFData: data)
|
|
|
|
return (image, animatedImage)
|
2021-07-02 13:48:56 +02:00
|
|
|
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)
|
2021-07-21 13:45:24 +02:00
|
|
|
return (nil, nil)
|
2021-07-02 13:48:56 +02:00
|
|
|
}
|
2021-04-30 13:28:06 +02:00
|
|
|
}
|
2021-07-21 13:45:24 +02:00
|
|
|
.assign(to: \.value, on: image)
|
2021-07-02 13:48:56 +02:00
|
|
|
.store(in: &disposeBag)
|
2021-04-28 09:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
init(meta: LocalImagePreviewMeta) {
|
|
|
|
self.item = .local(meta)
|
2021-07-21 13:45:24 +02:00
|
|
|
self.image = CurrentValueSubject((meta.image, nil))
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|