diff --git a/Tusker/Views/Attachments/GifvPlayerView.swift b/Tusker/Views/Attachments/GifvPlayerView.swift index cf9777779c..bf60534577 100644 --- a/Tusker/Views/Attachments/GifvPlayerView.swift +++ b/Tusker/Views/Attachments/GifvPlayerView.swift @@ -22,6 +22,7 @@ class GifvPlayerView: UIView { let controller: GifvController private var presentationSizeCancellable: AnyCancellable? + private var wasPlayingWhenSceneBackgrounded = false override var intrinsicContentSize: CGSize { controller.item.presentationSize @@ -45,4 +46,30 @@ class GifvPlayerView: UIView { 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() + } + } + }