2021-03-11 08:10:41 +01:00
|
|
|
//
|
2021-03-11 12:06:15 +01:00
|
|
|
// ContentWarningOverlayView.swift
|
2021-03-11 08:10:41 +01:00
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/3/11.
|
|
|
|
//
|
|
|
|
|
2021-03-11 12:06:15 +01:00
|
|
|
import os.log
|
2021-03-11 08:10:41 +01:00
|
|
|
import Foundation
|
|
|
|
import UIKit
|
|
|
|
|
2021-05-08 05:03:34 +02:00
|
|
|
protocol ContentWarningOverlayViewDelegate: AnyObject {
|
2021-03-11 12:06:15 +01:00
|
|
|
func contentWarningOverlayViewDidPressed(_ contentWarningOverlayView: ContentWarningOverlayView)
|
|
|
|
}
|
|
|
|
|
|
|
|
class ContentWarningOverlayView: UIView {
|
|
|
|
|
2021-03-11 08:10:41 +01:00
|
|
|
static let cornerRadius: CGFloat = 4
|
|
|
|
static let blurVisualEffect = UIBlurEffect(style: .systemUltraThinMaterial)
|
2021-04-16 14:06:36 +02:00
|
|
|
|
2021-03-11 12:06:15 +01:00
|
|
|
let blurVisualEffectView = UIVisualEffectView(effect: ContentWarningOverlayView.blurVisualEffect)
|
|
|
|
let vibrancyVisualEffectView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: ContentWarningOverlayView.blurVisualEffect))
|
2021-04-16 14:06:36 +02:00
|
|
|
let vibrancyContentWarningLabel: UILabel = {
|
|
|
|
let label = UILabel()
|
|
|
|
label.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 15))
|
|
|
|
label.text = L10n.Common.Controls.Status.mediaContentWarning
|
|
|
|
label.textAlignment = .center
|
|
|
|
label.numberOfLines = 0
|
2021-05-12 12:26:53 +02:00
|
|
|
label.isAccessibilityElement = false
|
2021-04-16 14:06:36 +02:00
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
|
|
|
let blurContentImageView: UIImageView = {
|
|
|
|
let imageView = UIImageView()
|
|
|
|
imageView.layer.masksToBounds = false
|
|
|
|
return imageView
|
|
|
|
}()
|
|
|
|
let blurContentWarningTitleLabel: UILabel = {
|
|
|
|
let label = UILabel()
|
2021-05-10 12:48:04 +02:00
|
|
|
label.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 17), maximumPointSize: 23)
|
2021-04-16 14:06:36 +02:00
|
|
|
label.text = L10n.Common.Controls.Status.mediaContentWarning
|
|
|
|
label.textColor = Asset.Colors.Label.primary.color
|
|
|
|
label.textAlignment = .center
|
2021-05-12 12:26:53 +02:00
|
|
|
label.isAccessibilityElement = false
|
2021-04-16 14:06:36 +02:00
|
|
|
return label
|
|
|
|
}()
|
|
|
|
let blurContentWarningLabel: UILabel = {
|
2021-03-11 08:10:41 +01:00
|
|
|
let label = UILabel()
|
2021-05-10 12:48:04 +02:00
|
|
|
label.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 15), maximumPointSize: 20)
|
2021-03-11 08:10:41 +01:00
|
|
|
label.text = L10n.Common.Controls.Status.mediaContentWarning
|
2021-04-16 14:06:36 +02:00
|
|
|
label.textColor = Asset.Colors.Label.secondary.color
|
2021-03-11 08:10:41 +01:00
|
|
|
label.textAlignment = .center
|
2021-04-16 14:06:36 +02:00
|
|
|
label.layer.setupShadow()
|
2021-05-12 12:26:53 +02:00
|
|
|
label.isAccessibilityElement = false
|
2021-03-11 08:10:41 +01:00
|
|
|
return label
|
|
|
|
}()
|
2021-03-11 12:06:15 +01:00
|
|
|
|
|
|
|
let tapGestureRecognizer = UITapGestureRecognizer.singleTapGestureRecognizer
|
|
|
|
|
|
|
|
weak var delegate: ContentWarningOverlayViewDelegate?
|
2021-03-11 08:10:41 +01:00
|
|
|
|
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 12:06:15 +01:00
|
|
|
extension ContentWarningOverlayView {
|
2021-03-11 08:10:41 +01:00
|
|
|
private func _init() {
|
2021-03-11 08:34:30 +01:00
|
|
|
backgroundColor = .clear
|
2021-04-19 12:33:11 +02:00
|
|
|
isUserInteractionEnabled = true
|
2021-04-16 14:06:36 +02:00
|
|
|
|
|
|
|
// visual effect style
|
2021-03-11 08:10:41 +01:00
|
|
|
// add blur visual effect view in the setup method
|
|
|
|
blurVisualEffectView.layer.masksToBounds = true
|
2021-03-11 12:06:15 +01:00
|
|
|
blurVisualEffectView.layer.cornerRadius = ContentWarningOverlayView.cornerRadius
|
2021-03-11 08:10:41 +01:00
|
|
|
blurVisualEffectView.layer.cornerCurve = .continuous
|
|
|
|
|
|
|
|
vibrancyVisualEffectView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
blurVisualEffectView.contentView.addSubview(vibrancyVisualEffectView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
vibrancyVisualEffectView.topAnchor.constraint(equalTo: blurVisualEffectView.topAnchor),
|
|
|
|
vibrancyVisualEffectView.leadingAnchor.constraint(equalTo: blurVisualEffectView.leadingAnchor),
|
|
|
|
vibrancyVisualEffectView.trailingAnchor.constraint(equalTo: blurVisualEffectView.trailingAnchor),
|
|
|
|
vibrancyVisualEffectView.bottomAnchor.constraint(equalTo: blurVisualEffectView.bottomAnchor),
|
|
|
|
])
|
|
|
|
|
2021-04-16 14:06:36 +02:00
|
|
|
vibrancyContentWarningLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
vibrancyVisualEffectView.contentView.addSubview(vibrancyContentWarningLabel)
|
2021-03-11 08:10:41 +01:00
|
|
|
NSLayoutConstraint.activate([
|
2021-04-16 14:06:36 +02:00
|
|
|
vibrancyContentWarningLabel.leadingAnchor.constraint(equalTo: vibrancyVisualEffectView.contentView.layoutMarginsGuide.leadingAnchor),
|
|
|
|
vibrancyContentWarningLabel.trailingAnchor.constraint(equalTo: vibrancyVisualEffectView.contentView.layoutMarginsGuide.trailingAnchor),
|
|
|
|
vibrancyContentWarningLabel.centerYAnchor.constraint(equalTo: vibrancyVisualEffectView.contentView.centerYAnchor),
|
2021-03-11 08:10:41 +01:00
|
|
|
])
|
2021-03-11 10:24:00 +01:00
|
|
|
|
2021-03-11 08:34:30 +01:00
|
|
|
blurVisualEffectView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
addSubview(blurVisualEffectView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
blurVisualEffectView.topAnchor.constraint(equalTo: topAnchor),
|
|
|
|
blurVisualEffectView.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
|
|
blurVisualEffectView.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
|
|
blurVisualEffectView.bottomAnchor.constraint(equalTo: bottomAnchor),
|
|
|
|
])
|
2021-04-16 14:06:36 +02:00
|
|
|
|
|
|
|
// blur image style
|
|
|
|
blurContentImageView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
addSubview(blurContentImageView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
blurContentImageView.topAnchor.constraint(equalTo: topAnchor),
|
|
|
|
blurContentImageView.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
|
|
blurContentImageView.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
|
|
blurContentImageView.bottomAnchor.constraint(equalTo: bottomAnchor),
|
|
|
|
])
|
|
|
|
|
|
|
|
let blurContentWarningLabelContainer = UIStackView()
|
|
|
|
blurContentWarningLabelContainer.axis = .vertical
|
|
|
|
blurContentWarningLabelContainer.spacing = 4
|
|
|
|
blurContentWarningLabelContainer.alignment = .center
|
|
|
|
|
|
|
|
blurContentWarningLabelContainer.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
blurContentImageView.addSubview(blurContentWarningLabelContainer)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
blurContentWarningLabelContainer.topAnchor.constraint(equalTo: topAnchor),
|
|
|
|
blurContentWarningLabelContainer.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
|
|
blurContentWarningLabelContainer.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
|
|
blurContentWarningLabelContainer.bottomAnchor.constraint(equalTo: bottomAnchor),
|
|
|
|
])
|
|
|
|
|
|
|
|
let topPaddingView = UIView()
|
|
|
|
let bottomPaddingView = UIView()
|
|
|
|
topPaddingView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
blurContentWarningLabelContainer.addArrangedSubview(topPaddingView)
|
|
|
|
blurContentWarningLabelContainer.addArrangedSubview(blurContentWarningTitleLabel)
|
|
|
|
blurContentWarningLabelContainer.addArrangedSubview(blurContentWarningLabel)
|
|
|
|
bottomPaddingView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
blurContentWarningLabelContainer.addArrangedSubview(bottomPaddingView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
topPaddingView.heightAnchor.constraint(equalTo: bottomPaddingView.heightAnchor, multiplier: 1.0).priority(.defaultHigh),
|
|
|
|
])
|
|
|
|
blurContentWarningTitleLabel.setContentHuggingPriority(.defaultHigh + 2, for: .vertical)
|
|
|
|
blurContentWarningLabel.setContentHuggingPriority(.defaultHigh + 1, for: .vertical)
|
2021-03-11 12:06:15 +01:00
|
|
|
|
|
|
|
tapGestureRecognizer.addTarget(self, action: #selector(ContentWarningOverlayView.tapGestureRecognizerHandler(_:)))
|
|
|
|
addGestureRecognizer(tapGestureRecognizer)
|
2021-04-16 14:06:36 +02:00
|
|
|
|
|
|
|
configure(style: .visualEffectView)
|
2021-03-11 12:06:15 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 10:24:00 +01:00
|
|
|
|
2021-04-16 14:06:36 +02:00
|
|
|
extension ContentWarningOverlayView {
|
|
|
|
|
|
|
|
enum Style {
|
|
|
|
case visualEffectView
|
|
|
|
case blurContentImageView
|
|
|
|
}
|
|
|
|
|
|
|
|
func configure(style: Style) {
|
|
|
|
switch style {
|
|
|
|
case .visualEffectView:
|
|
|
|
blurVisualEffectView.isHidden = false
|
|
|
|
vibrancyVisualEffectView.isHidden = false
|
|
|
|
blurContentImageView.isHidden = true
|
|
|
|
case .blurContentImageView:
|
|
|
|
blurVisualEffectView.isHidden = true
|
|
|
|
vibrancyVisualEffectView.isHidden = true
|
|
|
|
blurContentImageView.isHidden = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 12:33:11 +02:00
|
|
|
func update(isRevealing: Bool, style: Style) {
|
|
|
|
switch style {
|
|
|
|
case .visualEffectView:
|
|
|
|
blurVisualEffectView.effect = isRevealing ? nil : ContentWarningOverlayView.blurVisualEffect
|
|
|
|
vibrancyVisualEffectView.alpha = isRevealing ? 0 : 1
|
|
|
|
isUserInteractionEnabled = !isRevealing
|
|
|
|
case .blurContentImageView:
|
|
|
|
assertionFailure("not handle here")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 07:40:14 +02:00
|
|
|
func update(cornerRadius: CGFloat) {
|
|
|
|
blurVisualEffectView.layer.cornerRadius = cornerRadius
|
|
|
|
}
|
|
|
|
|
2021-04-16 14:06:36 +02:00
|
|
|
}
|
|
|
|
|
2021-03-11 12:06:15 +01:00
|
|
|
extension ContentWarningOverlayView {
|
|
|
|
@objc private func tapGestureRecognizerHandler(_ sender: UITapGestureRecognizer) {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
delegate?.contentWarningOverlayViewDidPressed(self)
|
2021-03-11 08:10:41 +01:00
|
|
|
}
|
|
|
|
}
|