Add preference to always blur media

This commit is contained in:
Shadowfacts 2019-11-17 12:52:42 -05:00
parent 6ce96764f3
commit 81a5fce602
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 54 additions and 28 deletions

View File

@ -8,7 +8,6 @@
import Foundation import Foundation
import Pachyderm import Pachyderm
import SwiftUI
import Combine import Combine
class Preferences: Codable, ObservableObject { class Preferences: Codable, ObservableObject {
@ -45,6 +44,7 @@ class Preferences: Codable, ObservableObject {
self.defaultPostVisibility = try container.decode(Status.Visibility.self, forKey: .defaultPostVisibility) self.defaultPostVisibility = try container.decode(Status.Visibility.self, forKey: .defaultPostVisibility)
self.automaticallySaveDrafts = try container.decode(Bool.self, forKey: .automaticallySaveDrafts) self.automaticallySaveDrafts = try container.decode(Bool.self, forKey: .automaticallySaveDrafts)
self.contentWarningCopyMode = try container.decode(ContentWarningCopyMode.self, forKey: .contentWarningCopyMode) 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.openLinksInApps = try container.decode(Bool.self, forKey: .openLinksInApps)
self.useInAppSafari = try container.decode(Bool.self, forKey: .useInAppSafari) self.useInAppSafari = try container.decode(Bool.self, forKey: .useInAppSafari)
self.inAppSafariAutomaticReaderMode = try container.decode(Bool.self, forKey: .inAppSafariAutomaticReaderMode) 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(defaultPostVisibility, forKey: .defaultPostVisibility)
try container.encode(automaticallySaveDrafts, forKey: .automaticallySaveDrafts) try container.encode(automaticallySaveDrafts, forKey: .automaticallySaveDrafts)
try container.encode(contentWarningCopyMode, forKey: .contentWarningCopyMode) try container.encode(contentWarningCopyMode, forKey: .contentWarningCopyMode)
try container.encode(blurAllMedia, forKey: .blurAllMedia)
try container.encode(openLinksInApps, forKey: .openLinksInApps) try container.encode(openLinksInApps, forKey: .openLinksInApps)
try container.encode(useInAppSafari, forKey: .useInAppSafari) try container.encode(useInAppSafari, forKey: .useInAppSafari)
try container.encode(inAppSafariAutomaticReaderMode, forKey: .inAppSafariAutomaticReaderMode) try container.encode(inAppSafariAutomaticReaderMode, forKey: .inAppSafariAutomaticReaderMode)
@ -86,6 +87,7 @@ class Preferences: Codable, ObservableObject {
@Published var defaultPostVisibility = Status.Visibility.public @Published var defaultPostVisibility = Status.Visibility.public
@Published var automaticallySaveDrafts = true @Published var automaticallySaveDrafts = true
@Published var contentWarningCopyMode = ContentWarningCopyMode.asIs @Published var contentWarningCopyMode = ContentWarningCopyMode.asIs
@Published var blurAllMedia = false
@Published var openLinksInApps = true @Published var openLinksInApps = true
@Published var useInAppSafari = true @Published var useInAppSafari = true
@Published var inAppSafariAutomaticReaderMode = false @Published var inAppSafariAutomaticReaderMode = false
@ -106,6 +108,7 @@ class Preferences: Codable, ObservableObject {
case defaultPostVisibility case defaultPostVisibility
case automaticallySaveDrafts case automaticallySaveDrafts
case contentWarningCopyMode case contentWarningCopyMode
case blurAllMedia
case openLinksInApps case openLinksInApps
case useInAppSafari case useInAppSafari
case inAppSafariAutomaticReaderMode case inAppSafariAutomaticReaderMode

View File

@ -15,6 +15,7 @@ struct BehaviorPrefsView: View {
List { List {
section1 section1
section2 section2
section3
}.listStyle(GroupedListStyle()) }.listStyle(GroupedListStyle())
.navigationBarTitle(Text("Behavior")) .navigationBarTitle(Text("Behavior"))
} }
@ -44,6 +45,14 @@ struct BehaviorPrefsView: View {
var section2: some View { var section2: some View {
Section(header: Text("READING")) { Section(header: Text("READING")) {
Toggle(isOn: $preferences.blurAllMedia) {
Text("Blur All Media")
}
}
}
var section3: some View {
Section(header: Text("LINKS")) {
Toggle(isOn: $preferences.openLinksInApps) { Toggle(isOn: $preferences.openLinksInApps) {
Text("Open Links in Apps") Text("Open Links in Apps")
} }

View File

@ -23,14 +23,10 @@ class AttachmentsContainerView: UIView {
var contentHidden: Bool! { var contentHidden: Bool! {
didSet { didSet {
guard let blurView = blurView, guard let blurView = blurView,
let hideButtonContainerView = hideButtonView else { return } let hideButtonView = hideButtonView else { return }
blurView.alpha = contentHidden ? 0 : 1 blurView.alpha = self.contentHidden ? 1 : 0
hideButtonContainerView.alpha = contentHidden ? 1 : 0 hideButtonView.alpha = self.contentHidden ? 0 : 1
UIView.animate(withDuration: 0.2) {
blurView.alpha = self.contentHidden ? 1 : 0
hideButtonContainerView.alpha = self.contentHidden ? 0 : 1
}
} }
} }
@ -38,6 +34,11 @@ class AttachmentsContainerView: UIView {
super.awakeFromNib() super.awakeFromNib()
self.isUserInteractionEnabled = true self.isUserInteractionEnabled = true
createBlurView()
createHideButton()
NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil)
} }
func getAttachmentView(for attachment: Attachment) -> AttachmentView? { func getAttachmentView(for attachment: Attachment) -> AttachmentView? {
@ -50,8 +51,8 @@ class AttachmentsContainerView: UIView {
self.statusID = status.id self.statusID = status.id
attachments = status.attachments.filter { $0.kind == .image || $0.kind == .video } attachments = status.attachments.filter { $0.kind == .image || $0.kind == .video }
attachmentViews.allObjects.forEach { $0.removeFromSuperview() }
attachmentViews.removeAllObjects() attachmentViews.removeAllObjects()
subviews.forEach { $0.removeFromSuperview() }
if attachments.count > 0 { if attachments.count > 0 {
self.isHidden = false self.isHidden = false
@ -62,14 +63,17 @@ class AttachmentsContainerView: UIView {
case 1: case 1:
let attachmentView = createAttachmentView(index: 0) let attachmentView = createAttachmentView(index: 0)
fillView(attachmentView) fillView(attachmentView)
sendSubviewToBack(attachmentView)
accessibilityElements.append(attachmentView) accessibilityElements.append(attachmentView)
case 2: case 2:
let left = createAttachmentView(index: 0) let left = createAttachmentView(index: 0)
let right = createAttachmentView(index: 1) let right = createAttachmentView(index: 1)
fillView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ let stack = createAttachmentsStack(axis: .horizontal, arrangedSubviews: [
left, left,
right right
])) ])
fillView(stack)
sendSubviewToBack(stack)
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
left.halfWidth() left.halfWidth()
]) ])
@ -79,13 +83,15 @@ class AttachmentsContainerView: UIView {
let left = createAttachmentView(index: 0) let left = createAttachmentView(index: 0)
let topRight = createAttachmentView(index: 1) let topRight = createAttachmentView(index: 1)
let bottomRight = createAttachmentView(index: 2) let bottomRight = createAttachmentView(index: 2)
fillView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ let outerStack = createAttachmentsStack(axis: .horizontal, arrangedSubviews: [
left, left,
createAttachmentsStack(axis: .vertical, arrangedSubviews: [ createAttachmentsStack(axis: .vertical, arrangedSubviews: [
topRight, topRight,
bottomRight bottomRight
]) ])
])) ])
fillView(outerStack)
sendSubviewToBack(outerStack)
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
left.halfWidth(), left.halfWidth(),
topRight.halfHeight(), topRight.halfHeight(),
@ -102,13 +108,15 @@ class AttachmentsContainerView: UIView {
]) ])
let topRight = createAttachmentView(index: 1) let topRight = createAttachmentView(index: 1)
let bottomRight = createAttachmentView(index: 3) let bottomRight = createAttachmentView(index: 3)
fillView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ let outerStack = createAttachmentsStack(axis: .horizontal, arrangedSubviews: [
left, left,
createAttachmentsStack(axis: .vertical, arrangedSubviews: [ createAttachmentsStack(axis: .vertical, arrangedSubviews: [
topRight, topRight,
bottomRight bottomRight
]) ])
])) ])
fillView(outerStack)
sendSubviewToBack(outerStack)
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
left.halfWidth(), left.halfWidth(),
topLeft.halfHeight(), topLeft.halfHeight(),
@ -139,13 +147,15 @@ class AttachmentsContainerView: UIView {
bottomLeft bottomLeft
]) ])
let topRight = createAttachmentView(index: 1) let topRight = createAttachmentView(index: 1)
fillView(createAttachmentsStack(axis: .horizontal, arrangedSubviews: [ let outerStack = createAttachmentsStack(axis: .horizontal, arrangedSubviews: [
left, left,
createAttachmentsStack(axis: .vertical, arrangedSubviews: [ createAttachmentsStack(axis: .vertical, arrangedSubviews: [
topRight, topRight,
moreView moreView
]) ])
])) ])
fillView(outerStack)
sendSubviewToBack(outerStack)
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
left.halfWidth(), left.halfWidth(),
topLeft.halfHeight(), topLeft.halfHeight(),
@ -166,11 +176,11 @@ class AttachmentsContainerView: UIView {
self.isHidden = true self.isHidden = true
} }
if status.sensitive { updateUIForPreferences()
contentHidden = true }
createBlurView()
createHideButton() @objc func updateUIForPreferences() {
} contentHidden = Preferences.shared.blurAllMedia || (MastodonCache.status(for: statusID)?.sensitive ?? false)
} }
private func createAttachmentView(index: Int) -> AttachmentView { private func createAttachmentView(index: Int) -> AttachmentView {
@ -195,7 +205,7 @@ class AttachmentsContainerView: UIView {
private func createBlurView() { private func createBlurView() {
let blur = UIBlurEffect(style: .dark) let blur = UIBlurEffect(style: .dark)
let blurView = UIVisualEffectView(effect: blur) let blurView = UIVisualEffectView(effect: blur)
blurView.effect = blur blurView.alpha = 0
blurView.translatesAutoresizingMaskIntoConstraints = false blurView.translatesAutoresizingMaskIntoConstraints = false
fillView(blurView) fillView(blurView)
let vibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: blur, style: .label)) let vibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: blur, style: .label))
@ -211,7 +221,7 @@ class AttachmentsContainerView: UIView {
let stack = UIStackView(arrangedSubviews: [ let stack = UIStackView(arrangedSubviews: [
imageView, imageView,
label label
]) ])
stack.axis = .vertical stack.axis = .vertical
stack.alignment = .center stack.alignment = .center
stack.translatesAutoresizingMaskIntoConstraints = false stack.translatesAutoresizingMaskIntoConstraints = false
@ -234,7 +244,7 @@ class AttachmentsContainerView: UIView {
let blurEffect = UIBlurEffect(style: .regular) let blurEffect = UIBlurEffect(style: .regular)
let hideButtonBlurView = UIVisualEffectView(effect: blurEffect) let hideButtonBlurView = UIVisualEffectView(effect: blurEffect)
hideButtonBlurView.translatesAutoresizingMaskIntoConstraints = false hideButtonBlurView.translatesAutoresizingMaskIntoConstraints = false
hideButtonBlurView.alpha = 0 hideButtonBlurView.alpha = 1
hideButtonBlurView.isUserInteractionEnabled = true hideButtonBlurView.isUserInteractionEnabled = true
hideButtonBlurView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideButtonTapped))) hideButtonBlurView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideButtonTapped)))
addSubview(hideButtonBlurView) addSubview(hideButtonBlurView)
@ -284,11 +294,15 @@ class AttachmentsContainerView: UIView {
// MARK: - Interaction // MARK: - Interaction
@objc func blurViewTapped() { @objc func blurViewTapped() {
contentHidden = false UIView.animate(withDuration: 0.2) {
self.contentHidden = false
}
} }
@objc func hideButtonTapped() { @objc func hideButtonTapped() {
contentHidden = true UIView.animate(withDuration: 0.2) {
self.contentHidden = true
}
} }
@objc func showSensitiveContent() { @objc func showSensitiveContent() {

View File

@ -230,7 +230,7 @@ class StatusTableViewCell: UITableViewCell {
} }
updateTimestampWorkItem?.cancel() updateTimestampWorkItem?.cancel()
updateTimestampWorkItem = nil updateTimestampWorkItem = nil
attachmentsView.subviews.forEach { $0.removeFromSuperview() } attachmentsView.attachmentViews.allObjects.forEach { $0.removeFromSuperview() }
showPinned = false showPinned = false
} }