From a666776c88838f3cd41cec8b4f7c1f9cc2d30397 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Mon, 17 Aug 2020 10:08:30 +1000 Subject: [PATCH] Implement preliminary app mode switch --- .../Loki/View Controllers/SettingsVC.swift | 24 +++++++++++++++++-- .../Loki/Redesign/Style Guide/AppMode.swift | 3 +++ .../src/Loki/Utilities/LKUserDefaults.swift | 1 + .../Loki/Utilities/Notification+Loki.swift | 2 ++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Signal/src/Loki/View Controllers/SettingsVC.swift b/Signal/src/Loki/View Controllers/SettingsVC.swift index 5c354a70c..9f93aed86 100644 --- a/Signal/src/Loki/View Controllers/SettingsVC.swift +++ b/Signal/src/Loki/View Controllers/SettingsVC.swift @@ -128,6 +128,12 @@ final class SettingsVC : BaseVC, AvatarViewHelperDelegate { stackView.pin(to: scrollView) view.addSubview(scrollView) scrollView.pin(to: view) + // Register for notifications + NotificationCenter.default.addObserver(self, selector: #selector(handleAppModeSwitchedNotification(_:)), name: .appModeSwitched, object: nil) + } + + deinit { + NotificationCenter.default.removeObserver(self) } private func getSettingButtons() -> [UIView] { @@ -203,6 +209,11 @@ final class SettingsVC : BaseVC, AvatarViewHelperDelegate { } // MARK: Updating + @objc private func handleAppModeSwitchedNotification(_ notification: Notification) { + updateNavigationBarButtons() + // TODO: Redraw UI + } + private func handleIsEditingDisplayNameChanged() { updateNavigationBarButtons() UIView.animate(withDuration: 0.25) { @@ -228,9 +239,12 @@ final class SettingsVC : BaseVC, AvatarViewHelperDelegate { let closeButton = UIBarButtonItem(image: #imageLiteral(resourceName: "X"), style: .plain, target: self, action: #selector(close)) closeButton.tintColor = Colors.text navigationItem.leftBarButtonItem = closeButton + let appModeIcon = UserDefaults.standard[.isUsingDarkMode] ? #imageLiteral(resourceName: "ic_dark_theme_on") : #imageLiteral(resourceName: "ic_dark_theme_off") + let appModeButton = UIBarButtonItem(image: appModeIcon, style: .plain, target: self, action: #selector(switchAppMode)) + appModeButton.tintColor = Colors.text let qrCodeButton = UIBarButtonItem(image: #imageLiteral(resourceName: "QRCode"), style: .plain, target: self, action: #selector(showQRCode)) qrCodeButton.tintColor = Colors.text - navigationItem.rightBarButtonItem = qrCodeButton + navigationItem.rightBarButtonItems = [ qrCodeButton/*, appModeButton*/ ] } } @@ -281,7 +295,13 @@ final class SettingsVC : BaseVC, AvatarViewHelperDelegate { @objc private func close() { dismiss(animated: true, completion: nil) } - + + @objc private func switchAppMode() { + let isUsingDarkMode = UserDefaults.standard[.isUsingDarkMode] + UserDefaults.standard[.isUsingDarkMode] = !isUsingDarkMode + NotificationCenter.default.post(name: .appModeSwitched, object: nil) + } + @objc private func showQRCode() { let qrCodeVC = QRCodeVC() navigationController!.pushViewController(qrCodeVC, animated: true) diff --git a/SignalMessaging/Loki/Redesign/Style Guide/AppMode.swift b/SignalMessaging/Loki/Redesign/Style Guide/AppMode.swift index ee386e790..26fadb3d5 100644 --- a/SignalMessaging/Loki/Redesign/Style Guide/AppMode.swift +++ b/SignalMessaging/Loki/Redesign/Style Guide/AppMode.swift @@ -3,6 +3,9 @@ public enum AppMode { case light, dark public static var current: AppMode = .dark +// public static var current: AppMode { +// return UserDefaults.standard[.isUsingDarkMode] ? .dark : .light +// } } public var isLightMode: Bool { diff --git a/SignalServiceKit/src/Loki/Utilities/LKUserDefaults.swift b/SignalServiceKit/src/Loki/Utilities/LKUserDefaults.swift index 21ab9158d..7023b3a49 100644 --- a/SignalServiceKit/src/Loki/Utilities/LKUserDefaults.swift +++ b/SignalServiceKit/src/Loki/Utilities/LKUserDefaults.swift @@ -10,6 +10,7 @@ public enum LKUserDefaults { case hasViewedSeed /// Whether the device was unlinked as a slave device (used to notify the user on the landing screen). case wasUnlinked + case isUsingDarkMode case isUsingFullAPNs } diff --git a/SignalServiceKit/src/Loki/Utilities/Notification+Loki.swift b/SignalServiceKit/src/Loki/Utilities/Notification+Loki.swift index da6894d6d..1385a044a 100644 --- a/SignalServiceKit/src/Loki/Utilities/Notification+Loki.swift +++ b/SignalServiceKit/src/Loki/Utilities/Notification+Loki.swift @@ -2,6 +2,7 @@ public extension Notification.Name { // State changes + public static let appModeSwitched = Notification.Name("appModeSwitched") public static let blockedContactsUpdated = Notification.Name("blockedContactsUpdated") public static let contactOnlineStatusChanged = Notification.Name("contactOnlineStatusChanged") public static let groupThreadUpdated = Notification.Name("groupThreadUpdated") @@ -28,6 +29,7 @@ public extension Notification.Name { @objc public extension NSNotification { // State changes + @objc public static let appModeSwitched = Notification.Name.appModeSwitched.rawValue as NSString @objc public static let blockedContactsUpdated = Notification.Name.blockedContactsUpdated.rawValue as NSString @objc public static let contactOnlineStatusChanged = Notification.Name.contactOnlineStatusChanged.rawValue as NSString @objc public static let groupThreadUpdated = Notification.Name.groupThreadUpdated.rawValue as NSString