Yes, I change the UI when colorScheme changes, otherwise what's the point of that environment variable?
For example:
RoundedRectangle(cornerRadius: 8)
.shadow(color: (colorScheme == .dark ? Color.systemGray.opacity(0.6) : Color.systemGray3.opacity(0.9)), radius: 4, x: 0, y: 0)
Is there some other way of doing this? If I create a new colour in Assets with a dark and light appearance to cover that use case, I would have to create tons of colours to cover every other use case, i.e.:
.background(colorScheme == .dark ? Color.green : Color.white)
.background(colorScheme == .dark ? Color.blue : Color.red)
What is the reason that SwiftUI will redraw a view - and bounce you back to the top of a List of views - because Dark Mode was turned on?
This is how modelData is created:
// A global:
let dataService: DataService = DataService.shared
// The singleton:
final class DataService {
static let shared = DataService()
private init() {}
let coreData: CoreData = CoreData()
let modelData: ModelData = ModelData()
}
The model initialiser does this:
availableItems = []
filteredItems = []
mainItem = itemDetails.blankItem
// Then the rest are settings that pull their values from defaults, such as:
lastUpdated = defaultsGetLastUpdatedDate()
It's a singleton, so when it's created at the app level, I call a method in the .onAppear {} of the app's WindowGroup:
func getInitialModelData() {
availableItems = dataService.coreData.getAllEvents()
filteredItems(contentsOf: availableItems)
mainItem = dataService.coreData.getMainItem()
}
That method is only ever called once.
At no point when I turn on Dark Mode does anything happen in ModelData.
These issues didn't happen before I switched to @StateObject.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: