Last active
December 9, 2022 09:14
-
-
Save perseusrealdeal/11b1bab47f13134832b859f49d9af706 to your computer and use it in GitHub Desktop.
How to switch Perseus Dark Mode manually.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DarkModeSwitching.swift | |
// DarkModeDiscovery | |
// | |
// Created by Mikhail Zhigulin in 7530. | |
// | |
// Copyright © 7530 - 7531 Mikhail Zhigulin of Novosibirsk. | |
// All rights reserved. | |
// | |
// | |
// MIT License | |
// | |
// Copyright © 7530 - 7531 Mikhail Zhigulin of Novosibirsk | |
// | |
// The year starts from the creation of the world according to a Slavic calendar. | |
// September, the 1st of Slavic year. | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
#if canImport(UIKit) | |
import UIKit | |
#elseif canImport(Cocoa) | |
import Cocoa | |
#endif | |
// Uncomment the instruction if added Perseus Dark Mode via package manager. | |
// import PerseusDarkMode | |
// Dark Mode option key used in Settings bundle. | |
let DARK_MODE_SETTINGS_KEY = "dark_mode_preference" | |
func changeDarkModeManually(_ userChoice: DarkModeOption) { | |
// Change Dark Mode value in settings bundle | |
UserDefaults.standard.setValue(userChoice.rawValue, forKey: DARK_MODE_SETTINGS_KEY) | |
// Change Dark Mode value in Perseus Dark Mode library | |
AppearanceService.DarkModeUserChoice = userChoice | |
// Update appearance in accoring with changed Dark Mode Style | |
AppearanceService.makeUp() | |
} | |
func isDarkModeSettingsChanged() -> DarkModeOption? { | |
// Load enum int value from settings | |
let option = UserDefaults.standard.valueExists(forKey: DARK_MODE_SETTINGS_KEY) ? | |
UserDefaults.standard.integer(forKey: DARK_MODE_SETTINGS_KEY) : -1 | |
// Try to cast int value to enum | |
guard option != -1, let settingsDarkMode = DarkModeOption.init(rawValue: option) | |
else { return nil } // Should throw exception if init gives nil | |
// Report change | |
return settingsDarkMode != AppearanceService.DarkModeUserChoice ? settingsDarkMode : nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment