From dc223171b3371d55673091df65c549477ba18f4e Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Mon, 9 Jan 2023 20:39:42 +0100 Subject: [PATCH] Support hide sensitive medias / hide all medias --- .../Env/Sources/Env/UserPreferences.swift | 2 +- .../Sources/Models/ServerPreferences.swift | 10 ++--- Packages/Models/Sources/Models/Status.swift | 6 ++- .../Status/Row/StatusMediaPreviewView.swift | 38 +++++++++++++++++++ .../Sources/Status/Row/StatusRowView.swift | 4 +- 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/Packages/Env/Sources/Env/UserPreferences.swift b/Packages/Env/Sources/Env/UserPreferences.swift index 0cc12318..d6638385 100644 --- a/Packages/Env/Sources/Env/UserPreferences.swift +++ b/Packages/Env/Sources/Env/UserPreferences.swift @@ -21,7 +21,7 @@ public class UserPreferences: ObservableObject { } } - @Published private var serverPreferences: ServerPreferences? + @Published public var serverPreferences: ServerPreferences? public init() { } diff --git a/Packages/Models/Sources/Models/ServerPreferences.swift b/Packages/Models/Sources/Models/ServerPreferences.swift index 02f6a76e..47915a47 100644 --- a/Packages/Models/Sources/Models/ServerPreferences.swift +++ b/Packages/Models/Sources/Models/ServerPreferences.swift @@ -1,11 +1,11 @@ import Foundation public struct ServerPreferences: Decodable { - public let postVisibility: Visibility - public let postIsSensitive: Bool - public let postLanguage: String - public let autoExpandmedia: AutoExpandMedia - public let autoExpandSpoilers: Bool + public let postVisibility: Visibility? + public let postIsSensitive: Bool? + public let postLanguage: String? + public let autoExpandmedia: AutoExpandMedia? + public let autoExpandSpoilers: Bool? public enum AutoExpandMedia: String, Decodable { case showAll = "show_all" diff --git a/Packages/Models/Sources/Models/Status.swift b/Packages/Models/Sources/Models/Status.swift index 8a4fd3ff..e772d198 100644 --- a/Packages/Models/Sources/Models/Status.swift +++ b/Packages/Models/Sources/Models/Status.swift @@ -49,6 +49,7 @@ public protocol AnyStatus { var poll: Poll? { get } var spoilerText: String { get } var filtered: [Filtered]? { get } + var sensitive: Bool { get } } @@ -81,6 +82,7 @@ public struct Status: AnyStatus, Codable, Identifiable { public let poll: Poll? public let spoilerText: String public let filtered: [Filtered]? + public let sensitive: Bool public static func placeholder() -> Status { .init(id: UUID().uuidString, @@ -106,7 +108,8 @@ public struct Status: AnyStatus, Codable, Identifiable { visibility: .pub, poll: nil, spoilerText: "", - filtered: []) + filtered: [], + sensitive: false) } public static func placeholders() -> [Status] { @@ -142,4 +145,5 @@ public struct ReblogStatus: AnyStatus, Codable, Identifiable { public let poll: Poll? public let spoilerText: String public let filtered: [Filtered]? + public let sensitive: Bool } diff --git a/Packages/Status/Sources/Status/Row/StatusMediaPreviewView.swift b/Packages/Status/Sources/Status/Row/StatusMediaPreviewView.swift index 83a417a4..05d4342d 100644 --- a/Packages/Status/Sources/Status/Row/StatusMediaPreviewView.swift +++ b/Packages/Status/Sources/Status/Row/StatusMediaPreviewView.swift @@ -6,16 +6,19 @@ import NukeUI import DesignSystem public struct StatusMediaPreviewView: View { + @EnvironmentObject private var preferences: UserPreferences @EnvironmentObject private var quickLook: QuickLook @EnvironmentObject private var theme: Theme public let attachements: [MediaAttachement] + public let sensitive: Bool public let isNotifications: Bool @State private var isQuickLookLoading: Bool = false @State private var width: CGFloat = 0 @State private var altTextDisplayed: String? @State private var isAltAlertDisplayed: Bool = false + @State private var isHidingMedia: Bool = false private var imageMaxHeight: CGFloat { if isNotifications { @@ -89,6 +92,11 @@ public struct StatusMediaPreviewView: View { quickLookLoadingView .transition(.opacity) } + + if isHidingMedia { + sensitiveMediaOverlay + .transition(.opacity) + } } .alert("Image description", isPresented: $isAltAlertDisplayed) { @@ -96,6 +104,15 @@ public struct StatusMediaPreviewView: View { } message: { Text(altTextDisplayed ?? "") } + .onAppear { + if sensitive && preferences.serverPreferences?.autoExpandmedia == .hideSensitive { + isHidingMedia = true + } else if preferences.serverPreferences?.autoExpandmedia == .hideAll { + isHidingMedia = true + } else { + isHidingMedia = false + } + } } @@ -242,4 +259,25 @@ public struct StatusMediaPreviewView: View { } .background(.ultraThinMaterial) } + + private var sensitiveMediaOverlay: some View { + Rectangle() + .background(.ultraThinMaterial) + .overlay { + if !isNotifications { + Button { + withAnimation { + isHidingMedia = false + } + } label: { + if sensitive { + Label("Show sensitive content", systemImage: "eye") + } else { + Label("Show content", systemImage: "eye") + } + } + .buttonStyle(.borderedProminent) + } + } + } } diff --git a/Packages/Status/Sources/Status/Row/StatusRowView.swift b/Packages/Status/Sources/Status/Row/StatusRowView.swift index 03a36ce9..499100fb 100644 --- a/Packages/Status/Sources/Status/Row/StatusRowView.swift +++ b/Packages/Status/Sources/Status/Row/StatusRowView.swift @@ -197,7 +197,9 @@ public struct StatusRowView: View { if !status.mediaAttachments.isEmpty { HStack { - StatusMediaPreviewView(attachements: status.mediaAttachments, isNotifications: viewModel.isCompact) + StatusMediaPreviewView(attachements: status.mediaAttachments, + sensitive: status.sensitive, + isNotifications: viewModel.isCompact) .padding(.vertical, 4) Spacer() }