Skip to content
Permalink
Browse files

Add support for ios 13 dark mode

  • Loading branch information
GianniCarlo committed Jan 7, 2020
1 parent 80f1f7d commit 63aefc11e78349b759a8b3a6c7f7aaf82432c860

Large diffs are not rendered by default.

@@ -23,6 +23,7 @@ enum Constants {
case themeBrightnessEnabled = "userSettingsBrightnessEnabled"
case themeBrightnessThreshold = "userSettingsBrightnessThreshold"
case themeDarkVariantEnabled = "userSettingsThemeDarkVariant"
case systemThemeVariantEnabled = "userSettingsSystemThemeVariant"
case chapterContextEnabled = "userSettingsChapterContext"
case remainingTimeEnabled = "userSettingsRemainingTime"
case smartRewindEnabled = "userSettingsSmartRewind"
@@ -239,8 +239,6 @@
<array>
<string>armv7</string>
</array>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDefault</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
@@ -213,6 +213,15 @@ class LibraryViewController: ItemListViewController, UIGestureRecognizerDelegate
present(alert, animated: true, completion: nil)
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)

guard #available(iOS 12.0, *),
self.traitCollection.userInterfaceStyle != .unspecified else { return }

ThemeManager.shared.checkSystemMode()
}

// MARK: - Callback events
@objc func onNewFileUrl() {
@@ -32,8 +32,20 @@ final class ThemeManager: ThemeProvider {
}
}

public func checkSystemMode() {
guard #available(iOS 12.0, *),
UserDefaults.standard.bool(forKey: Constants.UserDefaults.systemThemeVariantEnabled.rawValue) else { return }

self.useDarkVariant = UIScreen.main.traitCollection.userInterfaceStyle == .dark
}

private init() {
self.useDarkVariant = UserDefaults.standard.bool(forKey: Constants.UserDefaults.themeDarkVariantEnabled.rawValue)
if #available(iOS 12.0, *),
UserDefaults.standard.bool(forKey: Constants.UserDefaults.systemThemeVariantEnabled.rawValue) {
self.useDarkVariant = UIScreen.main.traitCollection.userInterfaceStyle == .dark
} else {
self.useDarkVariant = UserDefaults.standard.bool(forKey: Constants.UserDefaults.themeDarkVariantEnabled.rawValue)
}

if UserDefaults.standard.bool(forKey: Constants.UserDefaults.themeBrightnessEnabled.rawValue) {
let threshold = UserDefaults.standard.float(forKey: Constants.UserDefaults.themeBrightnessThreshold.rawValue)
@@ -20,6 +20,7 @@ class ThemesViewController: UIViewController {
@IBOutlet weak var sunImageView: UIImageView!
@IBOutlet weak var sunLeadingConstraint: NSLayoutConstraint!

@IBOutlet weak var systemModeSwitch: UISwitch!
@IBOutlet weak var darkModeSwitch: UISwitch!

@IBOutlet weak var localThemesTableView: UITableView!
@@ -69,13 +70,19 @@ class ThemesViewController: UIViewController {
self.extractedThemesTableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: self.extractedThemesTableView.frame.size.width, height: 1))

self.darkModeSwitch.isOn = UserDefaults.standard.bool(forKey: Constants.UserDefaults.themeDarkVariantEnabled.rawValue)
self.systemModeSwitch.isOn = UserDefaults.standard.bool(forKey: Constants.UserDefaults.systemThemeVariantEnabled.rawValue)
self.brightnessSwitch.isOn = UserDefaults.standard.bool(forKey: Constants.UserDefaults.themeBrightnessEnabled.rawValue)
self.brightnessSlider.value = UserDefaults.standard.float(forKey: Constants.UserDefaults.themeBrightnessThreshold.rawValue)

if self.brightnessSwitch.isOn {
self.toggleAutomaticBrightness(animated: false)
}

if self.systemModeSwitch.isOn {
self.brightnessSwitch.isEnabled = false
self.darkModeSwitch.isEnabled = false
}

self.brightnessChanged()

NotificationCenter.default.addObserver(self, selector: #selector(self.brightnessChanged), name: UIScreen.brightnessDidChangeNotification, object: nil)
@@ -154,6 +161,32 @@ class ThemesViewController: UIViewController {
self.sunLeadingConstraint.constant = (brightness * self.brightnessSlider.bounds.width) - (self.sunImageView.bounds.width / 2)
}

@IBAction func toggleSystemMode(_ sender: UISwitch) {
guard #available(iOS 13.0, *) else {
self.showAlert("Alert", message: "This feature is available on devices running iOS 13 or later ")
return
}

UserDefaults.standard.set(sender.isOn, forKey: Constants.UserDefaults.systemThemeVariantEnabled.rawValue)

self.brightnessSwitch.isEnabled = !sender.isOn
self.darkModeSwitch.isEnabled = !sender.isOn

guard !sender.isOn else {
ThemeManager.shared.checkSystemMode()
return
}

//handle switching variant if the other toggle is enabled
let darkVariantEnabled = UserDefaults.standard.bool(forKey: Constants.UserDefaults.themeDarkVariantEnabled.rawValue)

if UserDefaults.standard.bool(forKey: Constants.UserDefaults.themeBrightnessEnabled.rawValue) {
self.sliderUp(self.brightnessSlider)
} else if ThemeManager.shared.useDarkVariant != darkVariantEnabled {
ThemeManager.shared.useDarkVariant = darkVariantEnabled
}
}

@IBAction func toggleDarkMode(_ sender: UISwitch) {
UserDefaults.standard.set(sender.isOn, forKey: Constants.UserDefaults.themeDarkVariantEnabled.rawValue)
ThemeManager.shared.useDarkVariant = sender.isOn

0 comments on commit 63aefc1

Please sign in to comment.
You can’t perform that action at this time.