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)
self.animationImage = source.image
self.player.play()
}
required init?(coder: NSCoder) {

View File

@ -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),

View File

@ -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)
}