Try conditional modifier for preferredColorScheme

This commit is contained in:
Rizwan Mohamed Ibrahim 2020-07-03 12:35:12 +05:30
parent 959eef6a1a
commit fc548ac913
No known key found for this signature in database
GPG Key ID: D5BEE468D448BCC5
4 changed files with 32 additions and 12 deletions

View File

@ -146,7 +146,9 @@ struct MainApp: App {
SceneNavigationView()
.environmentObject(sceneModel)
.environmentObject(defaults)
}.commands {
.modifier(PreferredColorSchemeModifier(preferredColorScheme: defaults.userInterfaceColorPalette))
}
.commands {
CommandGroup(after: .newItem, addition: {
Button("New Feed", action: {})
.keyboardShortcut("N")
@ -193,3 +195,20 @@ struct MainApp: App {
#endif
}
}
struct PreferredColorSchemeModifier: ViewModifier {
var preferredColorScheme: UserInterfaceColorPalette
@ViewBuilder
func body(content: Content) -> some View {
switch preferredColorScheme {
case .automatic:
content
case .dark:
content.preferredColorScheme(.dark)
case .light:
content.preferredColorScheme(.light)
}
}
}

View File

@ -9,7 +9,8 @@
import SwiftUI
struct SidebarToolbar: View {
@EnvironmentObject private var appSettings: AppDefaults
@State private var showSettings: Bool = false
var body: some View {
@ -40,7 +41,7 @@ struct SidebarToolbar: View {
}
.background(VisualEffectBlur(blurStyle: .systemChromeMaterial).edgesIgnoringSafeArea(.bottom))
.sheet(isPresented: $showSettings, onDismiss: { showSettings = false }) {
SettingsView()
SettingsView().modifier(PreferredColorSchemeModifier(preferredColorScheme: appSettings.userInterfaceColorPalette))
}
}

View File

@ -407,13 +407,13 @@ private extension AppDelegate {
}
func updateUserInterfaceStyle() {
switch AppDefaults.shared.userInterfaceColorPalette {
case .automatic:
window?.overrideUserInterfaceStyle = .unspecified
case .light:
window?.overrideUserInterfaceStyle = .light
case .dark:
window?.overrideUserInterfaceStyle = .dark
}
// switch AppDefaults.shared.userInterfaceColorPalette {
// case .automatic:
// window?.overrideUserInterfaceStyle = .unspecified
// case .light:
// window?.overrideUserInterfaceStyle = .light
// case .dark:
// window?.overrideUserInterfaceStyle = .dark
// }
}
}

View File

@ -31,7 +31,7 @@ struct ColorPaletteContainerView: View {
if let colorPalette = UserInterfaceColorPalette(rawValue: index) {
appSettings.userInterfaceColorPalette = colorPalette
}
presentationMode.wrappedValue.dismiss()
self.presentationMode.wrappedValue.dismiss()
}
}