yattee/Shared/Player/PlayerOrientation.swift

78 lines
2.8 KiB
Swift
Raw Normal View History

import Defaults
import Foundation
import SwiftUI
extension VideoPlayerView {
func configureOrientationUpdatesBasedOnAccelerometer() {
let currentOrientation = OrientationTracker.shared.currentInterfaceOrientation
if currentOrientation.isLandscape,
Defaults[.enterFullscreenInLandscape],
2022-11-13 12:15:44 +00:00
!Defaults[.honorSystemOrientationLock],
!player.playingFullScreen,
2022-11-13 12:15:44 +00:00
!player.currentItem.isNil,
player.lockedOrientation.isNil || player.lockedOrientation!.contains(.landscape),
!player.playingInPictureInPicture,
player.presentingPlayer
{
DispatchQueue.main.async {
2022-09-01 18:01:01 +00:00
player.controls.presentingControls = false
player.enterFullScreen(showControls: false)
}
player.onPresentPlayer.append {
Orientation.lockOrientation(.allButUpsideDown, andRotateTo: currentOrientation)
}
}
orientationObserver = NotificationCenter.default.addObserver(
forName: OrientationTracker.deviceOrientationChangedNotification,
object: nil,
queue: .main
) { _ in
guard !Defaults[.honorSystemOrientationLock],
player.presentingPlayer,
!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-31 19:24:46 +00:00
orientationDebouncer.call()
}
}
}
func stopOrientationUpdates() {
guard let observer = orientationObserver else { return }
NotificationCenter.default.removeObserver(observer)
}
}