// // MosaicImageViewContainer.swift // Mastodon // // Created by Cirno MainasuK on 2021-2-23. // import os.log import func AVFoundation.AVMakeRect import UIKit protocol MosaicImageViewContainerPresentable: class { var mosaicImageViewContainer: MosaicImageViewContainer { get } } protocol MosaicImageViewContainerDelegate: class { func mosaicImageViewContainer(_ mosaicImageViewContainer: MosaicImageViewContainer, didTapImageView imageView: UIImageView, atIndex index: Int) func mosaicImageViewContainer(_ mosaicImageViewContainer: MosaicImageViewContainer, contentWarningOverlayViewDidPressed contentWarningOverlayView: ContentWarningOverlayView) } final class MosaicImageViewContainer: UIView { weak var delegate: MosaicImageViewContainerDelegate? let container = UIStackView() var imageViews: [UIImageView] = [] { didSet { imageViews.forEach { imageView in imageView.isUserInteractionEnabled = true let tapGesture = UITapGestureRecognizer.singleTapGestureRecognizer tapGesture.addTarget(self, action: #selector(MosaicImageViewContainer.photoTapGestureRecognizerHandler(_:))) imageView.addGestureRecognizer(tapGesture) } } } let contentWarningOverlayView: ContentWarningOverlayView = { let contentWarningOverlayView = ContentWarningOverlayView() return contentWarningOverlayView }() private var containerHeightLayoutConstraint: NSLayoutConstraint! override init(frame: CGRect) { super.init(frame: frame) _init() } required init?(coder: NSCoder) { super.init(coder: coder) _init() } } extension MosaicImageViewContainer: ContentWarningOverlayViewDelegate { func contentWarningOverlayViewDidPressed(_ contentWarningOverlayView: ContentWarningOverlayView) { self.delegate?.mosaicImageViewContainer(self, contentWarningOverlayViewDidPressed: contentWarningOverlayView) } } extension MosaicImageViewContainer { private func _init() { container.translatesAutoresizingMaskIntoConstraints = false container.axis = .horizontal container.distribution = .fillEqually addSubview(container) containerHeightLayoutConstraint = container.heightAnchor.constraint(equalToConstant: 162).priority(.required - 1) NSLayoutConstraint.activate([ container.topAnchor.constraint(equalTo: topAnchor), container.leadingAnchor.constraint(equalTo: leadingAnchor), trailingAnchor.constraint(equalTo: container.trailingAnchor), bottomAnchor.constraint(equalTo: container.bottomAnchor), containerHeightLayoutConstraint ]) contentWarningOverlayView.delegate = self } } extension MosaicImageViewContainer { func reset() { container.arrangedSubviews.forEach { subview in container.removeArrangedSubview(subview) subview.removeFromSuperview() } container.subviews.forEach { subview in subview.removeFromSuperview() } contentWarningOverlayView.removeFromSuperview() contentWarningOverlayView.blurVisualEffectView.effect = ContentWarningOverlayView.blurVisualEffect contentWarningOverlayView.vibrancyVisualEffectView.alpha = 1.0 contentWarningOverlayView.isUserInteractionEnabled = true imageViews = [] container.spacing = 1 } func setupImageView(aspectRatio: CGSize, maxSize: CGSize) -> UIImageView { reset() let contentView = UIView() contentView.translatesAutoresizingMaskIntoConstraints = false container.addArrangedSubview(contentView) let rect = AVMakeRect( aspectRatio: aspectRatio, insideRect: CGRect(origin: .zero, size: maxSize) ) let imageView = UIImageView() imageViews.append(imageView) imageView.layer.masksToBounds = true imageView.layer.cornerRadius = ContentWarningOverlayView.cornerRadius imageView.layer.cornerCurve = .continuous imageView.contentMode = .scaleAspectFill imageView.translatesAutoresizingMaskIntoConstraints = false contentView.addSubview(imageView) NSLayoutConstraint.activate([ imageView.topAnchor.constraint(equalTo: contentView.topAnchor), imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), imageView.widthAnchor.constraint(equalToConstant: floor(rect.width)).priority(.required - 1), ]) containerHeightLayoutConstraint.constant = floor(rect.height) containerHeightLayoutConstraint.isActive = true addSubview(contentWarningOverlayView) NSLayoutConstraint.activate([ contentWarningOverlayView.topAnchor.constraint(equalTo: imageView.topAnchor), contentWarningOverlayView.leadingAnchor.constraint(equalTo: imageView.leadingAnchor), contentWarningOverlayView.trailingAnchor.constraint(equalTo: imageView.trailingAnchor), contentWarningOverlayView.bottomAnchor.constraint(equalTo: imageView.bottomAnchor), ]) return imageView } func setupImageViews(count: Int, maxHeight: CGFloat) -> [UIImageView] { reset() guard count > 1 else { return [] } containerHeightLayoutConstraint.constant = maxHeight containerHeightLayoutConstraint.isActive = true let contentLeftStackView = UIStackView() let contentRightStackView = UIStackView() [contentLeftStackView, contentRightStackView].forEach { stackView in stackView.axis = .vertical stackView.distribution = .fillEqually stackView.spacing = 1 } container.addArrangedSubview(contentLeftStackView) container.addArrangedSubview(contentRightStackView) var imageViews: [UIImageView] = [] for _ in 0..