Add preference for inverted alt text badge

Closes #423
This commit is contained in:
Shadowfacts 2024-04-01 18:47:19 -04:00
parent cf317e15e9
commit 2467297f04
3 changed files with 27 additions and 4 deletions

View File

@ -87,6 +87,7 @@ public final class Preferences: Codable, ObservableObject {
self.automaticallyPlayGifs = try container.decode(Bool.self, forKey: .automaticallyPlayGifs)
self.showUncroppedMediaInline = try container.decodeIfPresent(Bool.self, forKey: .showUncroppedMediaInline) ?? true
self.showAttachmentBadges = try container.decodeIfPresent(Bool.self, forKey: .showAttachmentBadges) ?? true
self.attachmentAltBadgeInverted = try container.decodeIfPresent(Bool.self, forKey: .attachmentAltBadgeInverted) ?? false
self.openLinksInApps = try container.decode(Bool.self, forKey: .openLinksInApps)
self.useInAppSafari = try container.decode(Bool.self, forKey: .useInAppSafari)
@ -145,6 +146,7 @@ public final class Preferences: Codable, ObservableObject {
try container.encode(automaticallyPlayGifs, forKey: .automaticallyPlayGifs)
try container.encode(showUncroppedMediaInline, forKey: .showUncroppedMediaInline)
try container.encode(showAttachmentBadges, forKey: .showAttachmentBadges)
try container.encode(attachmentAltBadgeInverted, forKey: .attachmentAltBadgeInverted)
try container.encode(openLinksInApps, forKey: .openLinksInApps)
try container.encode(useInAppSafari, forKey: .useInAppSafari)
@ -211,6 +213,7 @@ public final class Preferences: Codable, ObservableObject {
@Published public var automaticallyPlayGifs = true
@Published public var showUncroppedMediaInline = true
@Published public var showAttachmentBadges = true
@Published public var attachmentAltBadgeInverted = false
// MARK: Behavior
@Published public var openLinksInApps = true
@ -274,6 +277,7 @@ public final class Preferences: Codable, ObservableObject {
case automaticallyPlayGifs
case showUncroppedMediaInline
case showAttachmentBadges
case attachmentAltBadgeInverted
case openLinksInApps
case useInAppSafari

View File

@ -44,8 +44,13 @@ struct MediaPrefsView: View {
}
Toggle(isOn: $preferences.showAttachmentBadges) {
Text("Show GIF/ALT Badges")
Text("Show GIF/\(Text("Alt").font(.body.lowercaseSmallCaps())) Badges")
}
Toggle(isOn: $preferences.attachmentAltBadgeInverted) {
Text("Show Badge when Missing \(Text("Alt").font(.body.lowercaseSmallCaps()))")
}
.disabled(!preferences.showAttachmentBadges)
}
.appGroupedListRowBackground()
}

View File

@ -30,6 +30,8 @@ class AttachmentView: GIFImageView {
var attachment: Attachment!
var index: Int!
private var currentBadges: Badges = []
private var loadAttachmentTask: Task<Void, Never>?
private var source: Source?
var attachmentImage: UIImage? {
@ -103,8 +105,9 @@ class AttachmentView: GIFImageView {
}
}
if getBadges().isEmpty != Preferences.shared.showAttachmentBadges {
createBadgesView(getBadges())
let newBadges = getBadges()
if currentBadges != newBadges {
createBadgesView(newBadges)
}
}
@ -175,8 +178,13 @@ class AttachmentView: GIFImageView {
return []
}
var badges: Badges = []
if attachment.description?.isEmpty == false {
let hasDescription = attachment.description?.isEmpty == false
if hasDescription,
!Preferences.shared.attachmentAltBadgeInverted {
badges.formUnion(.alt)
} else if !hasDescription,
Preferences.shared.attachmentAltBadgeInverted {
badges.formUnion(.noAlt)
}
if attachment.kind == .gifv || attachment.url.pathExtension == "gif" {
badges.formUnion(.gif)
@ -354,6 +362,8 @@ class AttachmentView: GIFImageView {
}
private func createBadgesView(_ badges: Badges) {
currentBadges = badges
guard !badges.isEmpty else {
badgeContainer?.removeFromSuperview()
badgeContainer = nil
@ -393,6 +403,9 @@ class AttachmentView: GIFImageView {
if badges.contains(.alt) {
makeBadgeView(text: "ALT")
}
if badges.contains(.noAlt) {
makeBadgeView(text: "No ALT")
}
let first = stack.arrangedSubviews.first!
first.layer.masksToBounds = true
@ -445,6 +458,7 @@ fileprivate extension AttachmentView {
struct Badges: OptionSet {
static let gif = Badges(rawValue: 1 << 0)
static let alt = Badges(rawValue: 1 << 1)
static let noAlt = Badges(rawValue: 1 << 2)
let rawValue: Int
}