Repository: CodeEditApp/CodeEdit
File: CodeEditModules/Modules/AppPreferences/src/Sections/ThemePreferences/Model/ThemeModel.swift (Swift)

Code before:
                        if let attributes = overrides[key] {
                            theme.editor[key] = attributes
                        }
                    }
                }

                // add the theme to themes array
                self.themes.append(theme)

                // if there already is a selected theme in `preferences.json` select this theme
                // otherwise take the first in the list
                self.selectedDarkTheme = self.darkThemes.first { $0.name == prefs.theme.selectedDarkTheme } ?? self.darkThemes.first
                self.selectedLightTheme = self.lightThemes.first { $0.name == prefs.theme.selectedLightTheme } ?? self.lightThemes.first
                
                // For selecting the default theme, doing it correctly on startup requires some more logic
                let userSelectedTheme = self.themes.first { $0.name == prefs.theme.selectedTheme }
                if userSelectedTheme != nil {
                    self.selectedTheme = userSelectedTheme
                } else {
                    print(colorScheme == .dark)
                    if colorScheme == .dark {
                        self.selectedTheme = self.selectedDarkTheme
                    } else {
                        self.selectedTheme = self.selectedLightTheme
                    }
                }
            }
        }
    }
    
    /// This function stores  'dark' and 'light' themes into `ThemePreferences` if user happens to select a theme
    func setDarkLightThemes() {
        if self.selectedTheme?.appearance == .dark {
            self.selectedDarkTheme = self.selectedTheme
        } else if self.selectedTheme?.appearance == .light {
            self.selectedLightTheme = self.selectedTheme
        }
    }

    private func loadBundledThemes() throws {
        let bundledThemeNames: [String] = [
            "codeedit-xcode-dark",
            "codeedit-xcode-light",
            "codeedit-github-dark",
            "codeedit-github-light",
            "codeedit-midnight"
        ]
        for themeName in bundledThemeNames {
            guard let defaultUrl = Bundle.main.url(forResource: themeName, withExtension: "json") else {
                return
            }

Review comment (suggestion):
So, this part is not working the way I want. It seems that ColorScheme is not yet initialized by the application at this point. The `colorScheme` variable is always `.light` even if the system appearance is dark. What happens, is that the app will always open with a white theme at the first start, instead of using the appearance of the system.

The question is where should this code be executed, so that it works haha

Code after:
                }

                // add the theme to themes array
                self.themes.append(theme)

                // if there already is a selected theme in `preferences.json` select this theme
                // otherwise take the first in the list
                self.selectedDarkTheme = self.darkThemes.first {
                    $0.name == prefs.theme.selectedDarkTheme
                } ?? self.darkThemes.first

                self.selectedLightTheme = self.lightThemes.first {
                    $0.name == prefs.theme.selectedLightTheme
                } ?? self.lightThemes.first

                // For selecting the default theme, doing it correctly on startup requires some more logic
                let userSelectedTheme = self.themes.first { $0.name == prefs.theme.selectedTheme }
                let systemAppearance = NSAppearance.currentDrawing().name
                if userSelectedTheme != nil {
                    self.selectedTheme = userSelectedTheme
                } else {
                    if systemAppearance == .darkAqua {
                        self.selectedTheme = self.selectedDarkTheme
                    } else {
                        self.selectedTheme = self.selectedLightTheme
                    }
                }
            }
        }
    }

    /// This function stores  'dark' and 'light' themes into `ThemePreferences` if user happens to select a theme
    private func updateAppearanceTheme() {
        if self.selectedTheme?.appearance == .dark {
            self.selectedDarkTheme = self.selectedTheme
        } else if self.selectedTheme?.appearance == .light {
            self.selectedLightTheme = self.selectedTheme
        }
    }

    private func loadBundledThemes() throws {
        let bundledThemeNames: [String] = [
            "codeedit-xcode-dark",
            "codeedit-xcode-light",
            "codeedit-github-dark",
            "codeedit-github-light",
            "codeedit-midnight"
        ]
        for themeName in bundledThemeNames {
            guard let defaultUrl = Bundle.main.url(forResource: themeName, withExtension: "json") else {
                return