From 1f56823a17f30fd9fa772ab5b9ac16037ff79d30 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 22 Feb 2020 13:12:28 -0500 Subject: [PATCH] Add preference to disable gif animation in timelines --- Tusker/Preferences/Preferences.swift | 4 ++++ .../Screens/Preferences/BehaviorPrefsView.swift | 3 +++ Tusker/Views/Attachments/AttachmentView.swift | 17 ++++++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Tusker/Preferences/Preferences.swift b/Tusker/Preferences/Preferences.swift index df3ad2e96e..285b697025 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 ae221e0a2a..d0120a2d4e 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 c634b417ce..7fcc790eb1 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) }