metatext-app-ios-iphone-ipad/Views/Status/StatusAttachmentView.swift

158 lines
6.2 KiB
Swift
Raw Normal View History

2020-08-28 21:56:28 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
2020-10-15 09:44:01 +02:00
import AVKit
2020-08-28 21:56:28 +02:00
import Kingfisher
2020-09-05 04:31:43 +02:00
import UIKit
2020-09-01 09:33:49 +02:00
import ViewModels
2020-08-28 21:56:28 +02:00
2020-10-11 23:03:25 +02:00
final class StatusAttachmentView: UIView {
2020-10-15 09:44:01 +02:00
let playerView = PlayerView()
2020-08-28 21:56:28 +02:00
let imageView = AnimatedImageView()
let button = UIButton()
2020-10-15 09:44:01 +02:00
var playing: Bool = false {
didSet {
if playing {
play()
2020-10-22 07:05:50 +02:00
imageView.tag = 0
playerView.tag = viewModel.tag
2020-10-15 09:44:01 +02:00
} else {
stop()
2020-10-22 07:05:50 +02:00
imageView.tag = viewModel.tag
playerView.tag = 0
2020-10-15 09:44:01 +02:00
}
}
}
private let viewModel: AttachmentViewModel
private var playerLooper: AVPlayerLooper?
2020-08-28 21:56:28 +02:00
init(viewModel: AttachmentViewModel) {
self.viewModel = viewModel
super.init(frame: .zero)
2020-10-15 09:44:01 +02:00
initialSetup()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension StatusAttachmentView {
func play() {
let player = PlayerCache.shared.player(url: viewModel.attachment.url)
if let cachedPlayerLooper = Self.playerLooperCache[player] {
playerLooper = cachedPlayerLooper
} else if let item = player.currentItem {
playerLooper = AVPlayerLooper(player: player, templateItem: item)
Self.playerLooperCache[player] = playerLooper
}
player.isMuted = true
player.play()
playerView.player = player
playerView.isHidden = false
}
func stop() {
if let item = playerView.player?.currentItem {
let imageGenerator = AVAssetImageGenerator(asset: item.asset)
imageGenerator.requestedTimeToleranceAfter = .zero
imageGenerator.requestedTimeToleranceBefore = .zero
if let image = try? imageGenerator.copyCGImage(at: item.currentTime(), actualTime: nil) {
imageView.image = .init(cgImage: image)
}
}
playerView.player = nil
playerView.isHidden = true
}
}
private extension StatusAttachmentView {
static var playerLooperCache = [AVQueuePlayer: AVPlayerLooper]()
// swiftlint:disable:next function_body_length
func initialSetup() {
2020-08-28 21:56:28 +02:00
addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
2020-10-22 07:05:50 +02:00
imageView.tag = viewModel.tag
2020-08-28 21:56:28 +02:00
2020-10-15 09:44:01 +02:00
let blurEffect = UIBlurEffect(style: .systemUltraThinMaterial)
let playView = UIVisualEffectView(effect: blurEffect)
let playVibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: blurEffect))
let playImageView = UIImageView(
image: UIImage(systemName: "play.circle",
withConfiguration: UIImage.SymbolConfiguration(textStyle: .largeTitle)))
playImageView.translatesAutoresizingMaskIntoConstraints = false
playVibrancyView.translatesAutoresizingMaskIntoConstraints = false
playVibrancyView.contentView.addSubview(playImageView)
playView.contentView.addSubview(playVibrancyView)
addSubview(playView)
playView.translatesAutoresizingMaskIntoConstraints = false
playView.clipsToBounds = true
playView.layer.cornerRadius = .defaultCornerRadius
playView.isHidden = viewModel.attachment.type == .image
addSubview(playerView)
playerView.translatesAutoresizingMaskIntoConstraints = false
2020-10-21 10:07:13 +02:00
playerView.videoGravity = .resizeAspectFill
2020-10-15 09:44:01 +02:00
playerView.isHidden = true
2020-08-28 21:56:28 +02:00
addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
2020-09-29 03:32:28 +02:00
button.setBackgroundImage(.highlightedButtonBackground, for: .highlighted)
2020-08-28 21:56:28 +02:00
switch viewModel.attachment.type {
2020-10-15 09:44:01 +02:00
case .image, .video, .gifv:
2020-08-28 21:56:28 +02:00
imageView.kf.setImage(with: viewModel.attachment.previewUrl)
2020-10-20 08:41:10 +02:00
case .audio:
playImageView.image = UIImage(systemName: "waveform.circle",
withConfiguration: UIImage.SymbolConfiguration(textStyle: .largeTitle))
backgroundColor = .secondarySystemBackground
2020-08-28 21:56:28 +02:00
default:
break
}
2020-09-29 03:32:28 +02:00
NSLayoutConstraint.activate([
2020-10-15 09:44:01 +02:00
imageView.leadingAnchor.constraint(equalTo: leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: trailingAnchor),
imageView.topAnchor.constraint(equalTo: topAnchor),
imageView.bottomAnchor.constraint(equalTo: bottomAnchor),
playerView.leadingAnchor.constraint(equalTo: leadingAnchor),
playerView.trailingAnchor.constraint(equalTo: trailingAnchor),
playerView.topAnchor.constraint(equalTo: topAnchor),
playerView.bottomAnchor.constraint(equalTo: bottomAnchor),
playImageView.centerXAnchor.constraint(equalTo: playView.contentView.centerXAnchor),
playImageView.centerYAnchor.constraint(equalTo: playView.contentView.centerYAnchor),
playVibrancyView.leadingAnchor.constraint(equalTo: playView.leadingAnchor),
playVibrancyView.topAnchor.constraint(equalTo: playView.topAnchor),
playVibrancyView.trailingAnchor.constraint(equalTo: playView.trailingAnchor),
playVibrancyView.bottomAnchor.constraint(equalTo: playView.bottomAnchor),
playView.centerXAnchor.constraint(equalTo: centerXAnchor),
playView.centerYAnchor.constraint(equalTo: centerYAnchor),
playView.trailingAnchor.constraint(
equalTo: playImageView.trailingAnchor, constant: .compactSpacing),
playView.bottomAnchor.constraint(
equalTo: playImageView.bottomAnchor, constant: .compactSpacing),
playImageView.topAnchor.constraint(
equalTo: playView.topAnchor, constant: .compactSpacing),
playImageView.leadingAnchor.constraint(
equalTo: playView.leadingAnchor, constant: .compactSpacing),
button.leadingAnchor.constraint(equalTo: leadingAnchor),
button.trailingAnchor.constraint(equalTo: trailingAnchor),
button.topAnchor.constraint(equalTo: topAnchor),
button.bottomAnchor.constraint(equalTo: bottomAnchor)
2020-09-29 03:32:28 +02:00
])
2020-08-28 21:56:28 +02:00
}
}