Set appearance on app launch

This commit is contained in:
Ethan Pippin 2021-07-26 14:20:03 -06:00
parent 4bc06ef23c
commit 477d011201
2 changed files with 11 additions and 2 deletions

View File

@ -7,6 +7,7 @@
import SwiftUI
import MessageUI
import Defaults
// The notification we'll send when a shake gesture happens.
extension UIDevice {
@ -210,6 +211,7 @@ class EmailHelper: NSObject, MFMailComposeViewControllerDelegate {
@main
struct JellyfinPlayerApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@Default(.appAppearance) var appAppearance
let persistenceController = PersistenceController.shared
@ -217,6 +219,9 @@ struct JellyfinPlayerApp: App {
WindowGroup {
SplashView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.onAppear(perform: {
setupAppearance()
})
.withHostingWindow { window in
window?.rootViewController = PreferenceUIHostingController(wrappedView: SplashView().environment(\.managedObjectContext, persistenceController.container.viewContext))
}
@ -225,6 +230,11 @@ struct JellyfinPlayerApp: App {
}
}
}
private func setupAppearance() {
guard let storedAppearance = AppAppearance(rawValue: appAppearance) else { return }
UIApplication.shared.windows.first?.overrideUserInterfaceStyle = storedAppearance.style
}
}
class AppDelegate: NSObject, UIApplicationDelegate {

View File

@ -67,9 +67,8 @@ struct SettingsView: View {
Text(appearance.localizedName).tag(appearance.rawValue)
}
}.onChange(of: appAppearance, perform: { value in
guard let window = UIApplication.shared.windows.first else { return }
guard let appearance = AppAppearance(rawValue: value) else { return }
window.overrideUserInterfaceStyle = appearance.style
UIApplication.shared.windows.first?.overrideUserInterfaceStyle = appearance.style
})
}