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 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

View File

@ -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")
}

View File

@ -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
}
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))
@ -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() {

View File

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