Start adding sensitive media hiding

This commit is contained in:
Shadowfacts 2019-07-01 21:11:30 -04:00
parent 5f5ff68b80
commit c29ebbb6a0
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 127 additions and 10 deletions

View File

@ -13,8 +13,26 @@ class AttachmentsContainerView: UIView {
var delegate: AttachmentViewDelegate? var delegate: AttachmentViewDelegate?
var statusID: String!
let attachmentViews: NSHashTable<AttachmentView> = .weakObjects() let attachmentViews: NSHashTable<AttachmentView> = .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() { override func awakeFromNib() {
super.awakeFromNib() super.awakeFromNib()
@ -28,6 +46,7 @@ class AttachmentsContainerView: UIView {
// MARK: - User Interaface // MARK: - User Interaface
func updateUI(status: Status) { func updateUI(status: Status) {
self.statusID = status.id
let attachments = status.attachments.filter { $0.kind == .image } let attachments = status.attachments.filter { $0.kind == .image }
attachmentViews.removeAllObjects() attachmentViews.removeAllObjects()
@ -38,10 +57,10 @@ class AttachmentsContainerView: UIView {
switch attachments.count { switch attachments.count {
case 1: case 1:
makeMainView(createAttachmentView(attachments[0])) fillView(createAttachmentView(attachments[0]))
case 2: case 2:
let left = createAttachmentView(attachments[0]) let left = createAttachmentView(attachments[0])
makeMainView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ fillView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [
left, left,
createAttachmentView(attachments[1]) createAttachmentView(attachments[1])
])) ]))
@ -51,7 +70,7 @@ class AttachmentsContainerView: UIView {
case 3: case 3:
let left = createAttachmentView(attachments[0]) let left = createAttachmentView(attachments[0])
let topRight = createAttachmentView(attachments[1]) let topRight = createAttachmentView(attachments[1])
makeMainView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ fillView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [
left, left,
createAttachmentsStack(axis: .vertical, arrangedSubviews: [ createAttachmentsStack(axis: .vertical, arrangedSubviews: [
topRight, topRight,
@ -69,7 +88,7 @@ class AttachmentsContainerView: UIView {
createAttachmentView(attachments[2]) createAttachmentView(attachments[2])
]) ])
let topRight = createAttachmentView(attachments[1]) let topRight = createAttachmentView(attachments[1])
makeMainView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ fillView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [
left, left,
createAttachmentsStack(axis: .vertical, arrangedSubviews: [ createAttachmentsStack(axis: .vertical, arrangedSubviews: [
topRight, topRight,
@ -87,6 +106,12 @@ class AttachmentsContainerView: UIView {
} else { } else {
self.isHidden = true self.isHidden = true
} }
if status.sensitive {
contentHidden = true
createBlurView()
createHideButton()
}
} }
private func createAttachmentView(_ attachment: Attachment) -> AttachmentView { private func createAttachmentView(_ attachment: Attachment) -> AttachmentView {
@ -105,14 +130,106 @@ class AttachmentsContainerView: UIView {
return stack return stack
} }
private func makeMainView(_ view: UIView) { private func createBlurView() {
addSubview(view) 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([ NSLayoutConstraint.activate([
view.leadingAnchor.constraint(equalTo: leadingAnchor), imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor, multiplier: image.size.width / image.size.height),
view.trailingAnchor.constraint(equalTo: trailingAnchor), imageView.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.2),
view.topAnchor.constraint(equalTo: topAnchor), stack.centerXAnchor.constraint(equalTo: centerXAnchor),
view.bottomAnchor.constraint(equalTo: bottomAnchor) 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
}
} }
} }