From 81a5fce602a42d463b0bf26e11e2822f90fb4d95 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 17 Nov 2019 12:52:42 -0500 Subject: [PATCH] Add preference to always blur media --- Tusker/Preferences/Preferences.swift | 5 +- .../Preferences/BehaviorPrefsView.swift | 9 +++ .../AttachmentsContainerView.swift | 66 +++++++++++-------- Tusker/Views/Status/StatusTableViewCell.swift | 2 +- 4 files changed, 54 insertions(+), 28 deletions(-) diff --git a/Tusker/Preferences/Preferences.swift b/Tusker/Preferences/Preferences.swift index 5135ff4653..cdb1a8e931 100644 --- a/Tusker/Preferences/Preferences.swift +++ b/Tusker/Preferences/Preferences.swift @@ -8,7 +8,6 @@ import Foundation import Pachyderm -import SwiftUI import Combine class Preferences: Codable, ObservableObject { @@ -45,6 +44,7 @@ class Preferences: Codable, ObservableObject { self.defaultPostVisibility = try container.decode(Status.Visibility.self, forKey: .defaultPostVisibility) self.automaticallySaveDrafts = try container.decode(Bool.self, forKey: .automaticallySaveDrafts) self.contentWarningCopyMode = try container.decode(ContentWarningCopyMode.self, forKey: .contentWarningCopyMode) + self.blurAllMedia = try container.decode(Bool.self, forKey: .blurAllMedia) self.openLinksInApps = try container.decode(Bool.self, forKey: .openLinksInApps) self.useInAppSafari = try container.decode(Bool.self, forKey: .useInAppSafari) self.inAppSafariAutomaticReaderMode = try container.decode(Bool.self, forKey: .inAppSafariAutomaticReaderMode) @@ -66,6 +66,7 @@ class Preferences: Codable, ObservableObject { try container.encode(defaultPostVisibility, forKey: .defaultPostVisibility) try container.encode(automaticallySaveDrafts, forKey: .automaticallySaveDrafts) try container.encode(contentWarningCopyMode, forKey: .contentWarningCopyMode) + try container.encode(blurAllMedia, forKey: .blurAllMedia) try container.encode(openLinksInApps, forKey: .openLinksInApps) try container.encode(useInAppSafari, forKey: .useInAppSafari) try container.encode(inAppSafariAutomaticReaderMode, forKey: .inAppSafariAutomaticReaderMode) @@ -86,6 +87,7 @@ class Preferences: Codable, ObservableObject { @Published var defaultPostVisibility = Status.Visibility.public @Published var automaticallySaveDrafts = true @Published var contentWarningCopyMode = ContentWarningCopyMode.asIs + @Published var blurAllMedia = false @Published var openLinksInApps = true @Published var useInAppSafari = true @Published var inAppSafariAutomaticReaderMode = false @@ -106,6 +108,7 @@ class Preferences: Codable, ObservableObject { case defaultPostVisibility case automaticallySaveDrafts case contentWarningCopyMode + case blurAllMedia case openLinksInApps case useInAppSafari case inAppSafariAutomaticReaderMode diff --git a/Tusker/Screens/Preferences/BehaviorPrefsView.swift b/Tusker/Screens/Preferences/BehaviorPrefsView.swift index c062583db9..f7074cd8df 100644 --- a/Tusker/Screens/Preferences/BehaviorPrefsView.swift +++ b/Tusker/Screens/Preferences/BehaviorPrefsView.swift @@ -15,6 +15,7 @@ struct BehaviorPrefsView: View { List { section1 section2 + section3 }.listStyle(GroupedListStyle()) .navigationBarTitle(Text("Behavior")) } @@ -44,6 +45,14 @@ struct BehaviorPrefsView: View { var section2: some View { Section(header: Text("READING")) { + Toggle(isOn: $preferences.blurAllMedia) { + Text("Blur All Media") + } + } + } + + var section3: some View { + Section(header: Text("LINKS")) { Toggle(isOn: $preferences.openLinksInApps) { Text("Open Links in Apps") } diff --git a/Tusker/Views/Attachments/AttachmentsContainerView.swift b/Tusker/Views/Attachments/AttachmentsContainerView.swift index f1d8738797..015f069ea7 100644 --- a/Tusker/Views/Attachments/AttachmentsContainerView.swift +++ b/Tusker/Views/Attachments/AttachmentsContainerView.swift @@ -23,14 +23,10 @@ class AttachmentsContainerView: UIView { var contentHidden: Bool! { didSet { guard let blurView = blurView, - let hideButtonContainerView = hideButtonView else { return } + let hideButtonView = hideButtonView else { return } - blurView.alpha = contentHidden ? 0 : 1 - hideButtonContainerView.alpha = contentHidden ? 1 : 0 - UIView.animate(withDuration: 0.2) { - blurView.alpha = self.contentHidden ? 1 : 0 - hideButtonContainerView.alpha = self.contentHidden ? 0 : 1 - } + blurView.alpha = self.contentHidden ? 1 : 0 + hideButtonView.alpha = self.contentHidden ? 0 : 1 } } @@ -38,6 +34,11 @@ class AttachmentsContainerView: UIView { super.awakeFromNib() self.isUserInteractionEnabled = true + + createBlurView() + createHideButton() + + NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) } func getAttachmentView(for attachment: Attachment) -> AttachmentView? { @@ -50,8 +51,8 @@ class AttachmentsContainerView: UIView { self.statusID = status.id attachments = status.attachments.filter { $0.kind == .image || $0.kind == .video } + attachmentViews.allObjects.forEach { $0.removeFromSuperview() } attachmentViews.removeAllObjects() - subviews.forEach { $0.removeFromSuperview() } if attachments.count > 0 { self.isHidden = false @@ -62,14 +63,17 @@ class AttachmentsContainerView: UIView { case 1: let attachmentView = createAttachmentView(index: 0) fillView(attachmentView) + sendSubviewToBack(attachmentView) accessibilityElements.append(attachmentView) case 2: let left = createAttachmentView(index: 0) let right = createAttachmentView(index: 1) - fillView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ + let stack = createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ left, right - ])) + ]) + fillView(stack) + sendSubviewToBack(stack) NSLayoutConstraint.activate([ left.halfWidth() ]) @@ -79,13 +83,15 @@ class AttachmentsContainerView: UIView { let left = createAttachmentView(index: 0) let topRight = createAttachmentView(index: 1) let bottomRight = createAttachmentView(index: 2) - fillView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ + let outerStack = createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ left, createAttachmentsStack(axis: .vertical, arrangedSubviews: [ topRight, bottomRight ]) - ])) + ]) + fillView(outerStack) + sendSubviewToBack(outerStack) NSLayoutConstraint.activate([ left.halfWidth(), topRight.halfHeight(), @@ -102,13 +108,15 @@ class AttachmentsContainerView: UIView { ]) let topRight = createAttachmentView(index: 1) let bottomRight = createAttachmentView(index: 3) - fillView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ + let outerStack = createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ left, createAttachmentsStack(axis: .vertical, arrangedSubviews: [ topRight, bottomRight ]) - ])) + ]) + fillView(outerStack) + sendSubviewToBack(outerStack) NSLayoutConstraint.activate([ left.halfWidth(), topLeft.halfHeight(), @@ -139,13 +147,15 @@ class AttachmentsContainerView: UIView { bottomLeft ]) let topRight = createAttachmentView(index: 1) - fillView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ + let outerStack = createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ left, createAttachmentsStack(axis: .vertical, arrangedSubviews: [ topRight, moreView ]) - ])) + ]) + fillView(outerStack) + sendSubviewToBack(outerStack) NSLayoutConstraint.activate([ left.halfWidth(), topLeft.halfHeight(), @@ -166,11 +176,11 @@ class AttachmentsContainerView: UIView { self.isHidden = true } - if status.sensitive { - contentHidden = true - createBlurView() - createHideButton() - } + updateUIForPreferences() + } + + @objc func updateUIForPreferences() { + contentHidden = Preferences.shared.blurAllMedia || (MastodonCache.status(for: statusID)?.sensitive ?? false) } private func createAttachmentView(index: Int) -> AttachmentView { @@ -195,7 +205,7 @@ class AttachmentsContainerView: UIView { private func createBlurView() { let blur = UIBlurEffect(style: .dark) let blurView = UIVisualEffectView(effect: blur) - blurView.effect = blur + blurView.alpha = 0 blurView.translatesAutoresizingMaskIntoConstraints = false fillView(blurView) let vibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: blur, style: .label)) @@ -211,7 +221,7 @@ class AttachmentsContainerView: UIView { let stack = UIStackView(arrangedSubviews: [ imageView, label - ]) + ]) stack.axis = .vertical stack.alignment = .center stack.translatesAutoresizingMaskIntoConstraints = false @@ -234,7 +244,7 @@ class AttachmentsContainerView: UIView { let blurEffect = UIBlurEffect(style: .regular) let hideButtonBlurView = UIVisualEffectView(effect: blurEffect) hideButtonBlurView.translatesAutoresizingMaskIntoConstraints = false - hideButtonBlurView.alpha = 0 + hideButtonBlurView.alpha = 1 hideButtonBlurView.isUserInteractionEnabled = true hideButtonBlurView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideButtonTapped))) addSubview(hideButtonBlurView) @@ -284,11 +294,15 @@ class AttachmentsContainerView: UIView { // MARK: - Interaction @objc func blurViewTapped() { - contentHidden = false + UIView.animate(withDuration: 0.2) { + self.contentHidden = false + } } @objc func hideButtonTapped() { - contentHidden = true + UIView.animate(withDuration: 0.2) { + self.contentHidden = true + } } @objc func showSensitiveContent() { diff --git a/Tusker/Views/Status/StatusTableViewCell.swift b/Tusker/Views/Status/StatusTableViewCell.swift index 45395059ab..260bdb4421 100644 --- a/Tusker/Views/Status/StatusTableViewCell.swift +++ b/Tusker/Views/Status/StatusTableViewCell.swift @@ -230,7 +230,7 @@ class StatusTableViewCell: UITableViewCell { } updateTimestampWorkItem?.cancel() updateTimestampWorkItem = nil - attachmentsView.subviews.forEach { $0.removeFromSuperview() } + attachmentsView.attachmentViews.allObjects.forEach { $0.removeFromSuperview() } showPinned = false }