Fix gifv playback not continuing when returning from background

Closes #506
This commit is contained in:
Shadowfacts 2024-07-08 22:00:02 -07:00
parent a818457f8c
commit 8322d3a36c
1 changed files with 27 additions and 0 deletions

View File

@ -22,6 +22,7 @@ class GifvPlayerView: UIView {
let controller: GifvController let controller: GifvController
private var presentationSizeCancellable: AnyCancellable? private var presentationSizeCancellable: AnyCancellable?
private var wasPlayingWhenSceneBackgrounded = false
override var intrinsicContentSize: CGSize { override var intrinsicContentSize: CGSize {
controller.item.presentationSize controller.item.presentationSize
@ -45,4 +46,30 @@ class GifvPlayerView: UIView {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
override func willMove(toWindow newWindow: UIWindow?) {
super.willMove(toWindow: newWindow)
if let oldWindow = window,
let oldScene = oldWindow.windowScene {
NotificationCenter.default.removeObserver(self, name: UIScene.didEnterBackgroundNotification, object: oldScene)
NotificationCenter.default.removeObserver(self, name: UIScene.willEnterForegroundNotification, object: oldScene)
}
if let newWindow,
let newScene = newWindow.windowScene {
NotificationCenter.default.addObserver(self, selector: #selector(sceneDidEnterBackground), name: UIScene.didEnterBackgroundNotification, object: newScene)
NotificationCenter.default.addObserver(self, selector: #selector(sceneWillEnterForeground), name: UIScene.willEnterForegroundNotification, object: newScene)
}
}
@objc private func sceneDidEnterBackground() {
wasPlayingWhenSceneBackgrounded = controller.player.rate > 0
}
@objc private func sceneWillEnterForeground() {
if wasPlayingWhenSceneBackgrounded {
controller.play()
}
}
} }