forked from shadowfacts/Tusker
Disable automatic GIF playback in low-power mode
This commit is contained in:
parent
0986fa285e
commit
95ebca04d2
|
@ -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) {
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue