Fix iOS 15 Rotation (#1174)

* fix and clean

* fix
This commit is contained in:
Ethan Pippin 2024-08-08 22:25:32 -06:00 committed by GitHub
parent f2f8f8be14
commit 30e944ffc4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 126 additions and 36 deletions

View File

@ -38,6 +38,8 @@ struct SupportedOrientationsPreferenceKey: PreferenceKey {
static var defaultValue: UIInterfaceOrientationMask = .allButUpsideDown
static func reduce(value: inout UIInterfaceOrientationMask, nextValue: () -> UIInterfaceOrientationMask) {}
static func reduce(value: inout UIInterfaceOrientationMask, nextValue: () -> UIInterfaceOrientationMask) {
value = nextValue()
}
}
#endif

View File

@ -81,6 +81,8 @@ public class UIPreferencesHostingController: UIHostingController<AnyView> {
didSet {
if #available(iOS 16, *) {
setNeedsUpdateOfSupportedInterfaceOrientations()
} else {
AppRotationUtility.lockOrientation(_orientations)
}
}
}
@ -111,3 +113,27 @@ public class UIPreferencesHostingController: UIHostingController<AnyView> {
#endif
}
// TODO: remove after iOS 15 support removed
#if os(iOS)
enum AppRotationUtility {
static func lockOrientation(_ orientationLock: UIInterfaceOrientationMask) {
guard UIDevice.current.userInterfaceIdiom == .phone else { return }
let rotateOrientation: UIInterfaceOrientation
switch orientationLock {
case .landscape:
rotateOrientation = .landscapeRight
default:
rotateOrientation = .portrait
}
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}
}
#endif

View File

@ -26,11 +26,22 @@ final class LiveVideoPlayerCoordinator: NavigationCoordinatable {
self.videoPlayerManager = manager
}
#if os(iOS)
@ViewBuilder
func makeStart() -> some View {
#if os(iOS)
PreferencesView {
private var versionedView: some View {
if #available(iOS 16, *) {
PreferencesView {
Group {
if Defaults[.VideoPlayer.videoPlayerType] == .swiftfin {
LiveVideoPlayer(manager: self.videoPlayerManager)
} else {
LiveNativeVideoPlayer(manager: self.videoPlayerManager)
}
}
.preferredColorScheme(.dark)
.supportedOrientations(UIDevice.isPhone ? .landscape : .allButUpsideDown)
}
} else {
Group {
if Defaults[.VideoPlayer.videoPlayerType] == .swiftfin {
LiveVideoPlayer(manager: self.videoPlayerManager)
@ -40,10 +51,20 @@ final class LiveVideoPlayerCoordinator: NavigationCoordinatable {
}
.preferredColorScheme(.dark)
.supportedOrientations(UIDevice.isPhone ? .landscape : .allButUpsideDown)
.preferredColorScheme(.dark)
.supportedOrientations(UIDevice.isPhone ? .landscape : .allButUpsideDown)
}
.ignoresSafeArea()
.backport
.persistentSystemOverlays(.hidden)
}
#endif
@ViewBuilder
func makeStart() -> some View {
#if os(iOS)
versionedView
.ignoresSafeArea()
.backport
.persistentSystemOverlays(.hidden)
#else

View File

@ -26,16 +26,24 @@ final class VideoPlayerCoordinator: NavigationCoordinatable {
self.videoPlayerManager = manager
}
@ViewBuilder
func makeStart() -> some View {
#if os(iOS)
// TODO: removed after iOS 15 support removed
// Some settings have to apply to the root PreferencesView and this
// one - separately.
// It is assumed that because Stinsen adds a lot of views that the
// PreferencesView isn't in the right place in the VC chain so that
// it can apply the settings, even SwiftUI settings.
PreferencesView {
#if os(iOS)
@ViewBuilder
private var versionedView: some View {
if #available(iOS 16, *) {
PreferencesView {
Group {
if Defaults[.VideoPlayer.videoPlayerType] == .swiftfin {
VideoPlayer(manager: self.videoPlayerManager)
} else {
NativeVideoPlayer(manager: self.videoPlayerManager)
}
}
.preferredColorScheme(.dark)
.supportedOrientations(UIDevice.isPhone ? .landscape : .allButUpsideDown)
}
} else {
Group {
if Defaults[.VideoPlayer.videoPlayerType] == .swiftfin {
VideoPlayer(manager: self.videoPlayerManager)
@ -46,9 +54,22 @@ final class VideoPlayerCoordinator: NavigationCoordinatable {
.preferredColorScheme(.dark)
.supportedOrientations(UIDevice.isPhone ? .landscape : .allButUpsideDown)
}
.ignoresSafeArea()
.backport
.persistentSystemOverlays(.hidden)
}
#endif
@ViewBuilder
func makeStart() -> some View {
#if os(iOS)
// Some settings have to apply to the root PreferencesView and this
// one - separately.
// It is assumed that because Stinsen adds a lot of views that the
// PreferencesView isn't in the right place in the VC chain so that
// it can apply the settings, even SwiftUI settings.
versionedView
.ignoresSafeArea()
.backport
.persistentSystemOverlays(.hidden)
#else
if Defaults[.VideoPlayer.videoPlayerType] == .swiftfin {

View File

@ -86,4 +86,6 @@ extension Notifications.Key {
static let didDeleteServer = NotificationKey("didDeleteServer")
static let didChangeUserProfileImage = NotificationKey("didChangeUserProfileImage")
static let didStartPlayback = NotificationKey("didStartPlayback")
}

View File

@ -71,31 +71,49 @@ struct SwiftfinApp: App {
}
}
var body: some Scene {
WindowGroup {
// TODO: removed after iOS 15 support removed
@ViewBuilder
private var versionedView: some View {
if #available(iOS 16, *) {
PreferencesView {
MainCoordinator()
.view()
.supportedOrientations(UIDevice.isPad ? .allButUpsideDown : .portrait)
}
.ignoresSafeArea()
.onNotification(UIApplication.didEnterBackgroundNotification) { _ in
Defaults[.backgroundTimeStamp] = Date.now
}
.onNotification(UIApplication.willEnterForegroundNotification) { _ in
// TODO: needs to check if any background playback is happening
// - atow, background video playback isn't officially supported
let backgroundedInterval = Date.now.timeIntervalSince(Defaults[.backgroundTimeStamp])
if backgroundedInterval > Defaults[.backgroundSignOutInterval] {
Defaults[.lastSignedInUserID] = nil
Container.shared.currentUserSession.reset()
Notifications[.didSignOut].post()
} else {
PreferencesView {
PreferencesView {
MainCoordinator()
.view()
.supportedOrientations(UIDevice.isPad ? .allButUpsideDown : .portrait)
}
.ignoresSafeArea()
}
}
}
var body: some Scene {
WindowGroup {
versionedView
.ignoresSafeArea()
.onNotification(UIApplication.didEnterBackgroundNotification) { _ in
Defaults[.backgroundTimeStamp] = Date.now
}
.onNotification(UIApplication.willEnterForegroundNotification) { _ in
// TODO: needs to check if any background playback is happening
// - atow, background video playback isn't officially supported
let backgroundedInterval = Date.now.timeIntervalSince(Defaults[.backgroundTimeStamp])
if backgroundedInterval > Defaults[.backgroundSignOutInterval] {
Defaults[.lastSignedInUserID] = nil
Container.shared.currentUserSession.reset()
Notifications[.didSignOut].post()
}
}
}
}
}
extension UINavigationController {