Disable automatic GIF playback in low-power mode

This commit is contained in:
Shadowfacts 2020-08-16 19:14:32 -04:00
parent 0986fa285e
commit 95ebca04d2
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 30 additions and 9 deletions

View File

@ -78,6 +78,8 @@ class LargeImageGifvContentView: GifvAttachmentView, LargeImageContentView {
super.init(asset: asset, gravity: .resizeAspect) super.init(asset: asset, gravity: .resizeAspect)
self.animationImage = source.image self.animationImage = source.image
self.player.play()
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {

View File

@ -21,6 +21,7 @@ class AttachmentView: UIImageView, GIFAnimatable {
weak var delegate: AttachmentViewDelegate? weak var delegate: AttachmentViewDelegate?
var playImageView: UIImageView? var playImageView: UIImageView?
var gifvView: GifvAttachmentView?
var attachment: Attachment! var attachment: Attachment!
var index: Int! var index: Int!
@ -28,6 +29,9 @@ class AttachmentView: UIImageView, GIFAnimatable {
private var attachmentRequest: ImageCache.Request? private var attachmentRequest: ImageCache.Request?
var gifData: Data? var gifData: Data?
private var autoplayGifs: Bool {
Preferences.shared.automaticallyPlayGifs && !ProcessInfo.processInfo.isLowPowerModeEnabled
}
public lazy var animator: Animator? = Animator(withDelegate: self) public lazy var animator: Animator? = Animator(withDelegate: self)
@ -55,17 +59,29 @@ class AttachmentView: UIImageView, GIFAnimatable {
isUserInteractionEnabled = true isUserInteractionEnabled = true
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imagePressed))) 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)) addInteraction(UIContextMenuInteraction(delegate: self))
} }
@objc func preferencesChanged() { @objc func gifPlaybackModeChanged() {
if let gifData = gifData { // NSProcessInfoPowerStateDidChange is sometimes fired on a background thread
if Preferences.shared.automaticallyPlayGifs && !isAnimatingGIF { DispatchQueue.main.async {
animate(withGIFData: gifData) if self.attachment.kind == .image,
} else if !Preferences.shared.automaticallyPlayGifs && isAnimatingGIF { let gifData = self.gifData {
stopAnimatingGIF() 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 { DispatchQueue.main.async {
if self.attachment.url.pathExtension == "gif" { if self.attachment.url.pathExtension == "gif" {
self.gifData = data self.gifData = data
if Preferences.shared.automaticallyPlayGifs { if self.autoplayGifs {
self.animate(withGIFData: data) self.animate(withGIFData: data)
} else { } else {
self.image = UIImage(data: data) self.image = UIImage(data: data)
@ -166,7 +182,11 @@ class AttachmentView: UIImageView, GIFAnimatable {
} }
let gifvView = GifvAttachmentView(asset: asset, gravity: .resizeAspectFill) let gifvView = GifvAttachmentView(asset: asset, gravity: .resizeAspectFill)
self.gifvView = gifvView
gifvView.translatesAutoresizingMaskIntoConstraints = false gifvView.translatesAutoresizingMaskIntoConstraints = false
if autoplayGifs {
gifvView.player.play()
}
addSubview(gifvView) addSubview(gifvView)
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
gifvView.leadingAnchor.constraint(equalTo: leadingAnchor), gifvView.leadingAnchor.constraint(equalTo: leadingAnchor),

View File

@ -31,7 +31,6 @@ class GifvAttachmentView: UIView {
playerLayer.player = player playerLayer.player = player
playerLayer.videoGravity = gravity playerLayer.videoGravity = gravity
player.isMuted = true player.isMuted = true
player.play()
NotificationCenter.default.addObserver(self, selector: #selector(restartItem), name: .AVPlayerItemDidPlayToEndTime, object: item) NotificationCenter.default.addObserver(self, selector: #selector(restartItem), name: .AVPlayerItemDidPlayToEndTime, object: item)
} }