diff --git a/Tusker/Views/Attachments/AttachmentsContainerView.swift b/Tusker/Views/Attachments/AttachmentsContainerView.swift index 72a595795b..be5d580c64 100644 --- a/Tusker/Views/Attachments/AttachmentsContainerView.swift +++ b/Tusker/Views/Attachments/AttachmentsContainerView.swift @@ -13,8 +13,26 @@ class AttachmentsContainerView: UIView { var delegate: AttachmentViewDelegate? + var statusID: String! + let attachmentViews: NSHashTable = .weakObjects() + var blurView: UIVisualEffectView? + var hideButton: UIButton? + var contentHidden: Bool! { + didSet { + guard let blurView = blurView, + let hideButton = hideButton else { return } + + blurView.alpha = contentHidden ? 0 : 1 + hideButton.alpha = contentHidden ? 1 : 0 + UIView.animate(withDuration: 0.2) { + blurView.alpha = self.contentHidden ? 1 : 0 + hideButton.alpha = self.contentHidden ? 0 : 1 + } + } + } + override func awakeFromNib() { super.awakeFromNib() @@ -28,6 +46,7 @@ class AttachmentsContainerView: UIView { // MARK: - User Interaface func updateUI(status: Status) { + self.statusID = status.id let attachments = status.attachments.filter { $0.kind == .image } attachmentViews.removeAllObjects() @@ -38,10 +57,10 @@ class AttachmentsContainerView: UIView { switch attachments.count { case 1: - makeMainView(createAttachmentView(attachments[0])) + fillView(createAttachmentView(attachments[0])) case 2: let left = createAttachmentView(attachments[0]) - makeMainView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ + fillView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ left, createAttachmentView(attachments[1]) ])) @@ -51,7 +70,7 @@ class AttachmentsContainerView: UIView { case 3: let left = createAttachmentView(attachments[0]) let topRight = createAttachmentView(attachments[1]) - makeMainView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ + fillView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ left, createAttachmentsStack(axis: .vertical, arrangedSubviews: [ topRight, @@ -69,7 +88,7 @@ class AttachmentsContainerView: UIView { createAttachmentView(attachments[2]) ]) let topRight = createAttachmentView(attachments[1]) - makeMainView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ + fillView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ left, createAttachmentsStack(axis: .vertical, arrangedSubviews: [ topRight, @@ -87,6 +106,12 @@ class AttachmentsContainerView: UIView { } else { self.isHidden = true } + + if status.sensitive { + contentHidden = true + createBlurView() + createHideButton() + } } private func createAttachmentView(_ attachment: Attachment) -> AttachmentView { @@ -105,16 +130,108 @@ class AttachmentsContainerView: UIView { return stack } - private func makeMainView(_ view: UIView) { - addSubview(view) + private func createBlurView() { + let blur = UIBlurEffect(style: .dark) + let blurView = UIVisualEffectView(effect: blur) + // let blurView = UIVisualEffectView(frame: bounds) + blurView.effect = blur + blurView.translatesAutoresizingMaskIntoConstraints = false + fillView(blurView) + // addSubview(blurView) + let vibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: blur, style: .label)) + // let vibrancyView = UIVisualEffectView(frame: blurView.bounds) + // vibrancyView.effect = UIVibrancyEffect(blurEffect: blur, style: .label) + vibrancyView.translatesAutoresizingMaskIntoConstraints = false + fillView(vibrancyView, in: blurView.contentView) + // addSubview(vibrancyView) + blurView.contentView.addSubview(vibrancyView) + + let image = UIImage(systemName: "eye")! + let imageView = UIImageView(image: image) + imageView.translatesAutoresizingMaskIntoConstraints = false + let label = UILabel() + label.text = "Sensitive Content" + let stack = UIStackView(arrangedSubviews: [ + imageView, + label + ]) + stack.axis = .vertical + stack.alignment = .center + stack.translatesAutoresizingMaskIntoConstraints = false + vibrancyView.contentView.addSubview(stack) NSLayoutConstraint.activate([ - view.leadingAnchor.constraint(equalTo: leadingAnchor), - view.trailingAnchor.constraint(equalTo: trailingAnchor), - view.topAnchor.constraint(equalTo: topAnchor), - view.bottomAnchor.constraint(equalTo: bottomAnchor) + imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor, multiplier: image.size.width / image.size.height), + imageView.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.2), + stack.centerXAnchor.constraint(equalTo: centerXAnchor), + stack.centerYAnchor.constraint(equalTo: centerYAnchor), + stack.widthAnchor.constraint(equalTo: widthAnchor) + ]) + + self.blurView = blurView + + blurView.isUserInteractionEnabled = true + blurView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(blurViewTapped))) + } + + private func createHideButton() { + let hideButton = UIButton() + hideButton.translatesAutoresizingMaskIntoConstraints = false + hideButton.alpha = 0 + hideButton.layer.cornerRadius = 2 + hideButton.layer.masksToBounds = true +// hideButton.backgroundColor = tintColor +// hideButton.tintColor = + hideButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal) + hideButton.addTarget(self, action: #selector(hideButtonTapped), for: .touchUpInside) + + addSubview(hideButton) + NSLayoutConstraint.activate([ + hideButton.topAnchor.constraint(equalTo: topAnchor, constant: 8), + hideButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8) + ]) + + self.hideButton = hideButton + } + + private func fillView(_ view: UIView, in parentView: UIView? = nil) { + let parentView = parentView ?? self + parentView.addSubview(view) + NSLayoutConstraint.activate([ + view.leadingAnchor.constraint(equalTo: parentView.leadingAnchor), + view.trailingAnchor.constraint(equalTo: parentView.trailingAnchor), + view.topAnchor.constraint(equalTo: parentView.topAnchor), + view.bottomAnchor.constraint(equalTo: parentView.bottomAnchor) ]) } + // MARK: - Interaction + + @objc func blurViewTapped() { + contentHidden = false + } + + @objc func hideButtonTapped() { + contentHidden = true + } + + @objc func showSensitiveContent() { + guard let blurView = blurView else { return } + + blurView.alpha = 1 + UIView.animate(withDuration: 0.2) { + blurView.alpha = 0 + } + } + + @objc func hideSensitiveContent() { + guard let blurView = self.blurView else { return } + + blurView.alpha = 0 + UIView.animate(withDuration: 0.2) { + blurView.alpha = 1 + } + } + } fileprivate extension UIView {