2022-01-27 14:23:39 +01:00
|
|
|
//
|
|
|
|
// MediaView+Configuration.swift
|
|
|
|
// TwidereX
|
|
|
|
//
|
|
|
|
// Created by Cirno MainasuK on 2021-10-14.
|
|
|
|
// Copyright © 2021 Twidere. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import Photos
|
2022-05-11 12:39:34 +02:00
|
|
|
import AlamofireImage
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
extension MediaView {
|
2022-01-29 12:51:40 +01:00
|
|
|
public class Configuration: Hashable {
|
|
|
|
|
2022-02-09 13:35:19 +01:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
2022-01-29 12:51:40 +01:00
|
|
|
public let info: Info
|
|
|
|
public let blurhash: String?
|
|
|
|
|
|
|
|
@Published public var isReveal = true
|
2022-02-09 13:35:19 +01:00
|
|
|
@Published public var previewImage: UIImage?
|
2022-01-29 12:51:40 +01:00
|
|
|
@Published public var blurhashImage: UIImage?
|
|
|
|
public var blurhashImageDisposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
public init(
|
|
|
|
info: MediaView.Configuration.Info,
|
|
|
|
blurhash: String?
|
|
|
|
) {
|
|
|
|
self.info = info
|
|
|
|
self.blurhash = blurhash
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
public var aspectRadio: CGSize {
|
2022-01-29 12:51:40 +01:00
|
|
|
switch info {
|
2022-01-27 14:23:39 +01:00
|
|
|
case .image(let info): return info.aspectRadio
|
|
|
|
case .gif(let info): return info.aspectRadio
|
|
|
|
case .video(let info): return info.aspectRadio
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 13:35:19 +01:00
|
|
|
public var previewURL: String? {
|
|
|
|
switch info {
|
|
|
|
case .image(let info):
|
|
|
|
return info.assetURL
|
|
|
|
case .gif(let info):
|
|
|
|
return info.previewURL
|
|
|
|
case .video(let info):
|
|
|
|
return info.previewURL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
public var assetURL: String? {
|
2022-01-29 12:51:40 +01:00
|
|
|
switch info {
|
2022-01-27 14:23:39 +01:00
|
|
|
case .image(let info):
|
|
|
|
return info.assetURL
|
|
|
|
case .gif(let info):
|
|
|
|
return info.assetURL
|
|
|
|
case .video(let info):
|
|
|
|
return info.assetURL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public var resourceType: PHAssetResourceType {
|
2022-01-29 12:51:40 +01:00
|
|
|
switch info {
|
2022-01-27 14:23:39 +01:00
|
|
|
case .image:
|
|
|
|
return .photo
|
|
|
|
case .gif:
|
|
|
|
return .video
|
|
|
|
case .video:
|
|
|
|
return .video
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 12:51:40 +01:00
|
|
|
public static func == (lhs: MediaView.Configuration, rhs: MediaView.Configuration) -> Bool {
|
|
|
|
return lhs.info == rhs.info
|
|
|
|
&& lhs.blurhash == rhs.blurhash
|
|
|
|
&& lhs.isReveal == rhs.isReveal
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
|
|
|
|
2022-01-29 12:51:40 +01:00
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(info)
|
|
|
|
hasher.combine(blurhash)
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
2022-01-29 12:51:40 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 12:51:40 +01:00
|
|
|
extension MediaView.Configuration {
|
|
|
|
|
|
|
|
public enum Info: Hashable {
|
|
|
|
case image(info: ImageInfo)
|
|
|
|
case gif(info: VideoInfo)
|
|
|
|
case video(info: VideoInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
public struct ImageInfo: Hashable {
|
|
|
|
public let aspectRadio: CGSize
|
|
|
|
public let assetURL: String?
|
|
|
|
|
|
|
|
public init(
|
|
|
|
aspectRadio: CGSize,
|
|
|
|
assetURL: String?
|
|
|
|
) {
|
|
|
|
self.aspectRadio = aspectRadio
|
|
|
|
self.assetURL = assetURL
|
|
|
|
}
|
|
|
|
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(aspectRadio.width)
|
|
|
|
hasher.combine(aspectRadio.height)
|
|
|
|
assetURL.flatMap { hasher.combine($0) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public struct VideoInfo: Hashable {
|
|
|
|
public let aspectRadio: CGSize
|
|
|
|
public let assetURL: String?
|
|
|
|
public let previewURL: String?
|
|
|
|
public let durationMS: Int?
|
|
|
|
|
|
|
|
public init(
|
|
|
|
aspectRadio: CGSize,
|
|
|
|
assetURL: String?,
|
|
|
|
previewURL: String?,
|
|
|
|
durationMS: Int?
|
|
|
|
) {
|
|
|
|
self.aspectRadio = aspectRadio
|
|
|
|
self.assetURL = assetURL
|
|
|
|
self.previewURL = previewURL
|
|
|
|
self.durationMS = durationMS
|
|
|
|
}
|
|
|
|
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(aspectRadio.width)
|
|
|
|
hasher.combine(aspectRadio.height)
|
|
|
|
assetURL.flatMap { hasher.combine($0) }
|
|
|
|
previewURL.flatMap { hasher.combine($0) }
|
|
|
|
durationMS.flatMap { hasher.combine($0) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-05-11 12:39:34 +02:00
|
|
|
|
|
|
|
extension MediaView.Configuration {
|
|
|
|
|
|
|
|
public func load() {
|
|
|
|
if let previewURL = previewURL,
|
|
|
|
let url = URL(string: previewURL)
|
|
|
|
{
|
|
|
|
let placeholder = UIImage.placeholder(color: .systemGray6)
|
|
|
|
let request = URLRequest(url: url)
|
|
|
|
ImageDownloader.default.download(request, completion: { [weak self] response in
|
|
|
|
guard let self = self else { return }
|
|
|
|
switch response.result {
|
|
|
|
case .success(let image):
|
|
|
|
self.previewImage = image
|
|
|
|
case .failure:
|
|
|
|
self.previewImage = placeholder
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if let assetURL = assetURL,
|
|
|
|
let blurhash = blurhash
|
|
|
|
{
|
|
|
|
BlurhashImageCacheService.shared.image(
|
|
|
|
blurhash: blurhash,
|
|
|
|
size: aspectRadio,
|
|
|
|
url: assetURL
|
|
|
|
)
|
|
|
|
.assign(to: \.blurhashImage, on: self)
|
|
|
|
.store(in: &blurhashImageDisposeBag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|