I have an enum that handles the theme for my app. I would like to have a .system
option where it will return a different value depending on the system’s theme (light or dark). Below is how I have it implemented, irrelevant code removed:
enum Theme: String, CaseIterable, Identifiable, Codable {
case system
case dark
case light
/* more cases */
var mainColor: Color {
@Environment(.colorScheme) var colorScheme
switch self {
case .system: return colorScheme == .dark ? .dark : .light
default: return Color(rawValue)
}
}
}
When I run it, though, I get the error Accessing Environment<ColorScheme>'s value outside of being installed on a View. This will always read the default value and will not update
. This makes sense, as obviously the enum is not a view. With that said, how can I access the colorScheme
environment variable from my enum? Or is there a different way to do this?