Last active
January 21, 2018 08:13
-
-
Save mczachurski/e04710945e8c0ff89375cc053bac0ddc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Foundation | |
import UIKit | |
class BaseTableViewController : UITableViewController { | |
var settingsHandler = SettingsHandler() | |
var settings:Settings! | |
// MARK: - View loading | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.clearsSelectionOnViewWillAppear = false | |
NotificationCenter.default.addObserver(self, selector: #selector(darkModeEnabled(_:)), name: .darkModeEnabled, object: nil) | |
NotificationCenter.default.addObserver(self, selector: #selector(darkModeDisabled(_:)), name: .darkModeDisabled, object: nil) | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
self.settings = self.settingsHandler.getDefaultSettings() | |
self.settings.isDarkMode ? enableDarkMode() : disableDarkMode() | |
} | |
deinit { | |
NotificationCenter.default.removeObserver(self, name: .darkModeEnabled, object: nil) | |
NotificationCenter.default.removeObserver(self, name: .darkModeDisabled, object: nil) | |
} | |
// MARK: - Theme | |
@objc func darkModeEnabled(_ notification: Notification) { | |
enableDarkMode() | |
self.tableView.reloadData() | |
} | |
@objc func darkModeDisabled(_ notification: Notification) { | |
disableDarkMode() | |
self.tableView.reloadData() | |
} | |
open func enableDarkMode() { | |
self.view.backgroundColor = UIColor.black | |
self.tableView.backgroundColor = UIColor.black | |
self.navigationController?.navigationBar.barStyle = .black | |
self.navigationController?.view.backgroundColor = UIColor.black | |
} | |
open func disableDarkMode() { | |
self.view.backgroundColor = UIColor.white | |
self.tableView.backgroundColor = UIColor.white | |
self.navigationController?.navigationBar.barStyle = .default | |
self.navigationController?.view.backgroundColor = UIColor.white | |
} | |
// MARK: - Table view data source | |
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { | |
if self.settings.isDarkMode { | |
cell.textLabel?.textColor = UIColor.white | |
cell.detailTextLabel?.textColor = UIColor.white | |
cell.backgroundColor = UIColor.black | |
cell.setSelectedColor(color: UIColor.darkBackground) | |
} | |
else { | |
cell.textLabel?.textColor = UIColor.black | |
cell.detailTextLabel?.textColor = UIColor.black | |
cell.backgroundColor = UIColor.white | |
cell.setSelectedColor(color: UIColor.lightBackground) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment