2022-08-28 17:18:49 +00:00
|
|
|
import Defaults
|
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
extension VideoPlayerView {
|
|
|
|
func configureOrientationUpdatesBasedOnAccelerometer() {
|
|
|
|
let currentOrientation = OrientationTracker.shared.currentInterfaceOrientation
|
|
|
|
if currentOrientation.isLandscape,
|
|
|
|
Defaults[.enterFullscreenInLandscape],
|
|
|
|
!player.playingFullScreen,
|
2022-09-24 12:47:24 +00:00
|
|
|
!player.playingInPictureInPicture,
|
|
|
|
!player.currentItem.isNil,
|
|
|
|
player.lockedOrientation.isNil || player.lockedOrientation!.contains(.landscape)
|
2022-08-28 17:18:49 +00:00
|
|
|
{
|
|
|
|
guard player.presentingPlayer else { return }
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
2022-09-01 18:01:01 +00:00
|
|
|
player.controls.presentingControls = false
|
2022-08-28 17:18:49 +00:00
|
|
|
player.enterFullScreen(showControls: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
player.onPresentPlayer.append {
|
|
|
|
Orientation.lockOrientation(.allButUpsideDown, andRotateTo: currentOrientation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
orientationObserver = NotificationCenter.default.addObserver(
|
|
|
|
forName: OrientationTracker.deviceOrientationChangedNotification,
|
|
|
|
object: nil,
|
|
|
|
queue: .main
|
|
|
|
) { _ in
|
2022-09-24 12:47:24 +00:00
|
|
|
guard player.presentingPlayer,
|
2022-08-28 17:18:49 +00:00
|
|
|
!player.playingInPictureInPicture,
|
|
|
|
player.lockedOrientation.isNil
|
|
|
|
else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let orientation = OrientationTracker.shared.currentInterfaceOrientation
|
|
|
|
|
|
|
|
guard lastOrientation != orientation else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lastOrientation = orientation
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
guard Defaults[.enterFullscreenInLandscape],
|
|
|
|
player.presentingPlayer
|
|
|
|
else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-31 19:24:46 +00:00
|
|
|
orientationDebouncer.callback = {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
if orientation.isLandscape {
|
2022-09-01 18:01:01 +00:00
|
|
|
player.controls.presentingControls = false
|
2022-08-31 19:24:46 +00:00
|
|
|
player.enterFullScreen(showControls: false)
|
|
|
|
Orientation.lockOrientation(OrientationTracker.shared.currentInterfaceOrientationMask, andRotateTo: orientation)
|
|
|
|
} else {
|
|
|
|
player.exitFullScreen(showControls: false)
|
|
|
|
Orientation.lockOrientation(.allButUpsideDown, andRotateTo: .portrait)
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 17:18:49 +00:00
|
|
|
}
|
2022-08-31 19:24:46 +00:00
|
|
|
|
|
|
|
orientationDebouncer.call()
|
2022-08-28 17:18:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopOrientationUpdates() {
|
|
|
|
guard let observer = orientationObserver else { return }
|
|
|
|
NotificationCenter.default.removeObserver(observer)
|
|
|
|
}
|
|
|
|
}
|