From 99721586cdea13dcf25924def6898d3720765e1c Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Mon, 24 Aug 2020 11:28:56 +1000 Subject: [PATCH] Make remaining UI responsive to app mode changes --- Signal/src/Loki/View Controllers/BaseVC.swift | 13 ++++++++-- Signal/src/Loki/View Controllers/HomeVC.swift | 7 ++++++ .../Loki/View Controllers/SettingsVC.swift | 25 ++++++++++++++++--- .../Loki/Redesign/Style Guide/Gradients.swift | 6 ++++- .../Loki/Utilities/Notification+Loki.swift | 2 ++ 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/Signal/src/Loki/View Controllers/BaseVC.swift b/Signal/src/Loki/View Controllers/BaseVC.swift index a29414787..764a02694 100644 --- a/Signal/src/Loki/View Controllers/BaseVC.swift +++ b/Signal/src/Loki/View Controllers/BaseVC.swift @@ -1,5 +1,6 @@ class BaseVC : UIViewController { + private var hasGradient = false override var preferredStatusBarStyle: UIStatusBarStyle { return isLightMode ? .default : .lightContent } @@ -23,10 +24,12 @@ class BaseVC : UIViewController { override func viewDidLoad() { setNeedsStatusBarAppearanceUpdate() - NotificationCenter.default.addObserver(self, selector: #selector(handleUnexpectedDeviceLinkRequestReceivedNotification), name: .unexpectedDeviceLinkRequestReceived, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(handleUnexpectedDeviceLinkRequestReceivedNotification(_:)), name: .unexpectedDeviceLinkRequestReceived, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(handleAppModeChangedNotification(_:)), name: .appModeChanged, object: nil) } internal func setUpGradientBackground() { + hasGradient = true view.backgroundColor = .clear let gradient = Gradients.defaultLokiBackground view.setGradient(gradient) @@ -68,7 +71,7 @@ class BaseVC : UIViewController { NotificationCenter.default.removeObserver(self) } - @objc private func handleUnexpectedDeviceLinkRequestReceivedNotification() { + @objc private func handleUnexpectedDeviceLinkRequestReceivedNotification(_ notification: Notification) { guard DeviceLinkingUtilities.shouldShowUnexpectedDeviceLinkRequestReceivedAlert else { return } DispatchQueue.main.async { let alert = UIAlertController(title: "Device Link Request Received", message: "Open the device link screen by going to \"Settings\" > \"Devices\" > \"Link a Device\" to link your devices.", preferredStyle: .alert) @@ -76,4 +79,10 @@ class BaseVC : UIViewController { self.present(alert, animated: true, completion: nil) } } + + @objc internal func handleAppModeChangedNotification(_ notification: Notification) { + if hasGradient { + setUpGradientBackground() // Re-do the gradient + } + } } diff --git a/Signal/src/Loki/View Controllers/HomeVC.swift b/Signal/src/Loki/View Controllers/HomeVC.swift index 585042111..728935795 100644 --- a/Signal/src/Loki/View Controllers/HomeVC.swift +++ b/Signal/src/Loki/View Controllers/HomeVC.swift @@ -348,6 +348,13 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIScrol pathStatusViewContainer.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showPath))) navigationItem.rightBarButtonItem = UIBarButtonItem(customView: pathStatusViewContainer) } + + @objc override internal func handleAppModeChangedNotification(_ notification: Notification) { + super.handleAppModeChangedNotification(notification) + let gradient = Gradients.homeVCFade + fadeView.setGradient(gradient) // Re-do gradient + tableView.reloadData() + } // MARK: Interaction func handleContinueButtonTapped(from seedReminderView: SeedReminderView) { diff --git a/Signal/src/Loki/View Controllers/SettingsVC.swift b/Signal/src/Loki/View Controllers/SettingsVC.swift index 231de81d5..a7c263fe9 100644 --- a/Signal/src/Loki/View Controllers/SettingsVC.swift +++ b/Signal/src/Loki/View Controllers/SettingsVC.swift @@ -49,6 +49,13 @@ final class SettingsVC : BaseVC, AvatarViewHelperDelegate { result.addTarget(self, action: #selector(copyPublicKey), for: UIControl.Event.touchUpInside) return result }() + + private lazy var settingButtonsStackView: UIStackView = { + let result = UIStackView() + result.axis = .vertical + result.alignment = .fill + return result + }() // MARK: Lifecycle override func viewDidLoad() { @@ -110,9 +117,9 @@ final class SettingsVC : BaseVC, AvatarViewHelperDelegate { topStackView.layoutMargins = UIEdgeInsets(top: 0, left: Values.largeSpacing, bottom: 0, right: Values.largeSpacing) topStackView.isLayoutMarginsRelativeArrangement = true // Set up setting buttons stack view - let settingButtonsStackView = UIStackView(arrangedSubviews: getSettingButtons() ) - settingButtonsStackView.axis = .vertical - settingButtonsStackView.alignment = .fill + getSettingButtons().forEach { settingButton in + settingButtonsStackView.addArrangedSubview(settingButton) + } // Set up stack view let stackView = UIStackView(arrangedSubviews: [ topStackView, settingButtonsStackView ]) stackView.axis = .vertical @@ -288,6 +295,17 @@ final class SettingsVC : BaseVC, AvatarViewHelperDelegate { }, requiresSync: true) } } + + @objc override internal func handleAppModeChangedNotification(_ notification: Notification) { + super.handleAppModeChangedNotification(notification) + settingButtonsStackView.arrangedSubviews.forEach { settingButton in + settingButtonsStackView.removeArrangedSubview(settingButton) + settingButton.removeFromSuperview() + } + getSettingButtons().forEach { settingButton in + settingButtonsStackView.addArrangedSubview(settingButton) // Re-do setting buttons + } + } // MARK: Interaction @objc private func close() { @@ -297,6 +315,7 @@ final class SettingsVC : BaseVC, AvatarViewHelperDelegate { @objc private func switchAppMode() { let newAppMode: AppMode = isLightMode ? .dark : .light AppModeManager.shared.setCurrentAppMode(to: newAppMode) + NotificationCenter.default.post(name: .appModeChanged, object: nil) } @objc private func showQRCode() { diff --git a/SignalMessaging/Loki/Redesign/Style Guide/Gradients.swift b/SignalMessaging/Loki/Redesign/Style Guide/Gradients.swift index e9e2f4821..bbb3b884e 100644 --- a/SignalMessaging/Loki/Redesign/Style Guide/Gradients.swift +++ b/SignalMessaging/Loki/Redesign/Style Guide/Gradients.swift @@ -19,7 +19,11 @@ public final class Gradient : NSObject { let layer = CAGradientLayer() layer.frame = UIScreen.main.bounds layer.colors = [ gradient.start.cgColor, gradient.end.cgColor ] - self.layer.insertSublayer(layer, at: 0) + if let existingSublayer = self.layer.sublayers?[0] { + self.layer.replaceSublayer(existingSublayer, with: layer) + } else { + self.layer.insertSublayer(layer, at: 0) + } } } diff --git a/SignalServiceKit/src/Loki/Utilities/Notification+Loki.swift b/SignalServiceKit/src/Loki/Utilities/Notification+Loki.swift index da6894d6d..2830f82bc 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 appModeChanged = Notification.Name("appModeChanged") 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 appModeChanged = Notification.Name.appModeChanged.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