diff --git a/Tusker/Screens/Large Image/LargeImageContentView.swift b/Tusker/Screens/Large Image/LargeImageContentView.swift index 3a7fbe0d45..35ab86ab76 100644 --- a/Tusker/Screens/Large Image/LargeImageContentView.swift +++ b/Tusker/Screens/Large Image/LargeImageContentView.swift @@ -78,6 +78,8 @@ class LargeImageGifvContentView: GifvAttachmentView, LargeImageContentView { super.init(asset: asset, gravity: .resizeAspect) self.animationImage = source.image + + self.player.play() } required init?(coder: NSCoder) { diff --git a/Tusker/Views/Attachments/AttachmentView.swift b/Tusker/Views/Attachments/AttachmentView.swift index 1e9f9df563..cc0cdbdec7 100644 --- a/Tusker/Views/Attachments/AttachmentView.swift +++ b/Tusker/Views/Attachments/AttachmentView.swift @@ -21,6 +21,7 @@ class AttachmentView: UIImageView, GIFAnimatable { weak var delegate: AttachmentViewDelegate? var playImageView: UIImageView? + var gifvView: GifvAttachmentView? var attachment: Attachment! var index: Int! @@ -28,6 +29,9 @@ class AttachmentView: UIImageView, GIFAnimatable { private var attachmentRequest: ImageCache.Request? var gifData: Data? + private var autoplayGifs: Bool { + Preferences.shared.automaticallyPlayGifs && !ProcessInfo.processInfo.isLowPowerModeEnabled + } public lazy var animator: Animator? = Animator(withDelegate: self) @@ -55,17 +59,29 @@ class AttachmentView: UIImageView, GIFAnimatable { isUserInteractionEnabled = true addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imagePressed))) - NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(gifPlaybackModeChanged), name: .preferencesChanged, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(gifPlaybackModeChanged), name: .NSProcessInfoPowerStateDidChange, object: nil) addInteraction(UIContextMenuInteraction(delegate: self)) } - @objc func preferencesChanged() { - if let gifData = gifData { - if Preferences.shared.automaticallyPlayGifs && !isAnimatingGIF { - animate(withGIFData: gifData) - } else if !Preferences.shared.automaticallyPlayGifs && isAnimatingGIF { - stopAnimatingGIF() + @objc func gifPlaybackModeChanged() { + // NSProcessInfoPowerStateDidChange is sometimes fired on a background thread + DispatchQueue.main.async { + if self.attachment.kind == .image, + let gifData = self.gifData { + if self.autoplayGifs && !self.isAnimatingGIF { + self.animate(withGIFData: gifData) + } else if !self.autoplayGifs && self.isAnimatingGIF { + self.stopAnimatingGIF() + } + } else if self.attachment.kind == .gifv, + let gifvView = self.gifvView { + if self.autoplayGifs { + gifvView.player.play() + } else { + gifvView.player.pause() + } } } } @@ -95,7 +111,7 @@ class AttachmentView: UIImageView, GIFAnimatable { DispatchQueue.main.async { if self.attachment.url.pathExtension == "gif" { self.gifData = data - if Preferences.shared.automaticallyPlayGifs { + if self.autoplayGifs { self.animate(withGIFData: data) } else { self.image = UIImage(data: data) @@ -166,7 +182,11 @@ class AttachmentView: UIImageView, GIFAnimatable { } let gifvView = GifvAttachmentView(asset: asset, gravity: .resizeAspectFill) + self.gifvView = gifvView gifvView.translatesAutoresizingMaskIntoConstraints = false + if autoplayGifs { + gifvView.player.play() + } addSubview(gifvView) NSLayoutConstraint.activate([ gifvView.leadingAnchor.constraint(equalTo: leadingAnchor), diff --git a/Tusker/Views/Attachments/GifvAttachmentView.swift b/Tusker/Views/Attachments/GifvAttachmentView.swift index b737b2be03..6ab2eec8a8 100644 --- a/Tusker/Views/Attachments/GifvAttachmentView.swift +++ b/Tusker/Views/Attachments/GifvAttachmentView.swift @@ -31,7 +31,6 @@ class GifvAttachmentView: UIView { playerLayer.player = player playerLayer.videoGravity = gravity player.isMuted = true - player.play() NotificationCenter.default.addObserver(self, selector: #selector(restartItem), name: .AVPlayerItemDidPlayToEndTime, object: item) }