Skip to content

Commit

Permalink
APPLE: fix system appearance not working on iOS (#1305)
Browse files Browse the repository at this point in the history
  • Loading branch information
rokas-ambrazevicius authored Oct 14, 2024
1 parent 090f851 commit a8796e7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public final class AppSettings: ObservableObject {
@AppStorage(AppSettingKey.currentAppearance.rawValue)
public var currentAppearance: AppSetting.Appearance = .automatic {
didSet {
// guard let keyWindow = AppSettings.keyWindow else { return }
// keyWindow.rootViewController?.overrideUserInterfaceStyle = currentAppearance.userInterfaceStyle
guard let keyWindow = AppSettings.keyWindow else { return }
keyWindow.rootViewController?.overrideUserInterfaceStyle = currentAppearance.userInterfaceStyle
}
}
#else
Expand Down Expand Up @@ -75,18 +75,20 @@ public final class AppSettings: ObservableObject {
}
}

#if os(iOS)
private extension AppSettings {
// static var keyWindow: UIWindow? {
// guard let window = UIApplication.shared.connectedScenes
// .compactMap({ $0 as? UIWindowScene })
// .flatMap({ $0.windows })
// .first(where: { $0.isKeyWindow })
// else {
// return nil
// }
// return window
// }
static var keyWindow: UIWindow? {
guard let window = UIApplication.shared.connectedScenes
.compactMap({ $0 as? UIWindowScene })
.flatMap({ $0.windows })
.first(where: { $0.isKeyWindow })
else {
return nil
}
return window
}
}
#endif

enum AppSettingKey: String {
case currentAppearance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ public struct AppSetting {
public var colorScheme: ColorScheme? {
switch self {
case .light:
print("light")
return .light
case .dark:
print("dark")
return .dark
#if os(iOS)
case .automatic:
print("system")
return nil
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ public struct AppearanceUpdate: ViewModifier {

public func body(content: Content) -> some View {
content
// .preferredColorScheme(appSettings.currentAppearance.colorScheme)
// .environment(\.colorScheme, appSettings.currentAppearance.colorScheme)
#if os(macOS)
.preferredColorScheme(appSettings.currentAppearance.colorScheme)
#endif
}
}

Expand Down

0 comments on commit a8796e7

Please sign in to comment.