diff --git a/Tusker/Preferences/Preferences.swift b/Tusker/Preferences/Preferences.swift index df3ad2e9..285b6970 100644 --- a/Tusker/Preferences/Preferences.swift +++ b/Tusker/Preferences/Preferences.swift @@ -48,6 +48,7 @@ class Preferences: Codable, ObservableObject { self.contentWarningCopyMode = try container.decode(ContentWarningCopyMode.self, forKey: .contentWarningCopyMode) self.mentionReblogger = try container.decode(Bool.self, forKey: .mentionReblogger) self.blurAllMedia = try container.decode(Bool.self, forKey: .blurAllMedia) + self.automaticallyPlayGifs = try container.decode(Bool.self, forKey: .automaticallyPlayGifs) 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) @@ -73,6 +74,7 @@ class Preferences: Codable, ObservableObject { try container.encode(contentWarningCopyMode, forKey: .contentWarningCopyMode) try container.encode(mentionReblogger, forKey: .mentionReblogger) try container.encode(blurAllMedia, forKey: .blurAllMedia) + try container.encode(automaticallyPlayGifs, forKey: .automaticallyPlayGifs) try container.encode(openLinksInApps, forKey: .openLinksInApps) try container.encode(useInAppSafari, forKey: .useInAppSafari) try container.encode(inAppSafariAutomaticReaderMode, forKey: .inAppSafariAutomaticReaderMode) @@ -97,6 +99,7 @@ class Preferences: Codable, ObservableObject { @Published var contentWarningCopyMode = ContentWarningCopyMode.asIs @Published var mentionReblogger = false @Published var blurAllMedia = false + @Published var automaticallyPlayGifs = true @Published var openLinksInApps = true @Published var useInAppSafari = true @Published var inAppSafariAutomaticReaderMode = false @@ -121,6 +124,7 @@ class Preferences: Codable, ObservableObject { case contentWarningCopyMode case mentionReblogger case blurAllMedia + case automaticallyPlayGifs case openLinksInApps case useInAppSafari case inAppSafariAutomaticReaderMode diff --git a/Tusker/Screens/Preferences/BehaviorPrefsView.swift b/Tusker/Screens/Preferences/BehaviorPrefsView.swift index ae221e0a..d0120a2d 100644 --- a/Tusker/Screens/Preferences/BehaviorPrefsView.swift +++ b/Tusker/Screens/Preferences/BehaviorPrefsView.swift @@ -60,6 +60,9 @@ struct BehaviorPrefsView: View { Toggle(isOn: $preferences.blurAllMedia) { Text("Blur All Media") } + Toggle(isOn: $preferences.automaticallyPlayGifs) { + Text("Automatically Play GIFs") + } } } diff --git a/Tusker/Views/Attachments/AttachmentView.swift b/Tusker/Views/Attachments/AttachmentView.swift index c634b417..7fcc790e 100644 --- a/Tusker/Views/Attachments/AttachmentView.swift +++ b/Tusker/Views/Attachments/AttachmentView.swift @@ -54,6 +54,17 @@ class AttachmentView: UIImageView, GIFAnimatable { isUserInteractionEnabled = true addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imagePressed))) + NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil) + } + + @objc func preferencesChanged() { + if let gifData = gifData { + if Preferences.shared.automaticallyPlayGifs && !isAnimatingGIF { + animate(withGIFData: gifData) + } else if !Preferences.shared.automaticallyPlayGifs && isAnimatingGIF { + stopAnimatingGIF() + } + } } func loadAttachment() { @@ -78,8 +89,12 @@ class AttachmentView: UIImageView, GIFAnimatable { self.attachmentRequest = nil DispatchQueue.main.async { if self.attachment.url.pathExtension == "gif" { - self.animate(withGIFData: data) self.gifData = data + if Preferences.shared.automaticallyPlayGifs { + self.animate(withGIFData: data) + } else { + self.image = UIImage(data: data) + } } else { self.image = UIImage(data: data) }