From 991c6eb477c5f9ca2dbf19f98afd8bc453a1a3ea Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Sat, 30 May 2020 08:20:30 +1000 Subject: [PATCH] Reduce code duplication, clean & fix path re-building bug --- .../Loki/Components/ConversationCell.swift | 4 +-- .../src/Loki/Utilities/MentionUtilities.swift | 2 +- Signal/src/Loki/View Controllers/BaseVC.swift | 31 +++++++++++++++++++ .../Loki/View Controllers/DeviceLinksVC.swift | 19 ++---------- .../Loki/View Controllers/DisplayNameVC.swift | 20 ++---------- .../View Controllers/GroupMembersVC.swift | 19 ++---------- Signal/src/Loki/View Controllers/HomeVC.swift | 21 +++---------- .../View Controllers/JoinPublicChatVC.swift | 18 ++--------- .../src/Loki/View Controllers/LandingVC.swift | 20 ++---------- .../Loki/View Controllers/LinkDeviceVC.swift | 18 ++--------- .../View Controllers/NewClosedGroupVC.swift | 20 ++---------- .../View Controllers/NewPrivateChatVC.swift | 18 ++--------- .../src/Loki/View Controllers/PNModeVC.swift | 20 ++---------- Signal/src/Loki/View Controllers/PathVC.swift | 29 ++++++----------- .../src/Loki/View Controllers/QRCodeVC.swift | 18 ++--------- .../Loki/View Controllers/RegisterVC.swift | 20 ++---------- .../src/Loki/View Controllers/RestoreVC.swift | 20 ++---------- .../ScanQRCodeWrapperVC.swift | 5 +-- Signal/src/Loki/View Controllers/SeedVC.swift | 20 ++---------- .../Loki/View Controllers/SettingsVC.swift | 19 ++---------- .../ConversationViewController.m | 2 +- .../ViewControllers/HomeView/HomeViewCell.m | 2 +- SignalMessaging/Views/ContactCellView.m | 2 +- .../src/Loki/API/LokiAPI+SwarmAPI.swift | 6 ++-- .../src/Loki/API/LokiPoller.swift | 2 +- .../API/Onion Requests/OnionRequestAPI.swift | 4 +-- .../Loki/Database/LokiDatabaseUtilities.swift | 16 ++++++---- .../Protocol/Mentions/MentionsManager.swift | 16 +++++----- .../src/Messages/OWSMessageManager.m | 2 +- 29 files changed, 119 insertions(+), 294 deletions(-) diff --git a/Signal/src/Loki/Components/ConversationCell.swift b/Signal/src/Loki/Components/ConversationCell.swift index 585370231..d6fdde318 100644 --- a/Signal/src/Loki/Components/ConversationCell.swift +++ b/Signal/src/Loki/Components/ConversationCell.swift @@ -134,14 +134,14 @@ final class ConversationCell : UITableViewCell { // MARK: Updating private func update() { - MentionsManager.populateUserHexEncodedPublicKeyCacheIfNeeded(for: threadViewModel.threadRecord.uniqueId!) // FIXME: This is a terrible place to do this + MentionsManager.populateUserPublicKeyCacheIfNeeded(for: threadViewModel.threadRecord.uniqueId!) // FIXME: This is a terrible place to do this unreadMessagesIndicatorView.alpha = threadViewModel.hasUnreadMessages ? 1 : 0.0001 // Setting the alpha to exactly 0 causes an issue on iOS 12 if threadViewModel.isGroupThread { if threadViewModel.name == "Session Public Chat" { profilePictureView.hexEncodedPublicKey = "" profilePictureView.isRSSFeed = true } else { - var users = MentionsManager.userHexEncodedPublicKeyCache[threadViewModel.threadRecord.uniqueId!] ?? [] + var users = MentionsManager.userPublicKeyCache[threadViewModel.threadRecord.uniqueId!] ?? [] users.remove(getUserHexEncodedPublicKey()) let randomUsers = users.sorted().prefix(2) // Sort to provide a level of stability if !randomUsers.isEmpty { diff --git a/Signal/src/Loki/Utilities/MentionUtilities.swift b/Signal/src/Loki/Utilities/MentionUtilities.swift index ce232836a..b71aac2b5 100644 --- a/Signal/src/Loki/Utilities/MentionUtilities.swift +++ b/Signal/src/Loki/Utilities/MentionUtilities.swift @@ -18,7 +18,7 @@ public final class MentionUtilities : NSObject { } var string = string let regex = try! NSRegularExpression(pattern: "@[0-9a-fA-F]*", options: []) - let knownHexEncodedPublicKeys = MentionsManager.userHexEncodedPublicKeyCache[threadID] ?? [] // Should always be populated at this point + let knownHexEncodedPublicKeys = MentionsManager.userPublicKeyCache[threadID] ?? [] // Should always be populated at this point var mentions: [(range: NSRange, hexEncodedPublicKey: String)] = [] var outerMatch = regex.firstMatch(in: string, options: .withoutAnchoringBounds, range: NSRange(location: 0, length: string.count)) while let match = outerMatch { diff --git a/Signal/src/Loki/View Controllers/BaseVC.swift b/Signal/src/Loki/View Controllers/BaseVC.swift index 8bdf37ab0..6772444b9 100644 --- a/Signal/src/Loki/View Controllers/BaseVC.swift +++ b/Signal/src/Loki/View Controllers/BaseVC.swift @@ -8,6 +8,37 @@ class BaseVC : UIViewController { NotificationCenter.default.addObserver(self, selector: #selector(handleUnexpectedDeviceLinkRequestReceivedNotification), name: .unexpectedDeviceLinkRequestReceived, object: nil) } + internal func setUpGradientBackground() { + view.backgroundColor = .clear + let gradient = Gradients.defaultLokiBackground + view.setGradient(gradient) + } + + internal func setUpNavBarStyle() { + let navigationBar = navigationController!.navigationBar + navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) + navigationBar.shadowImage = UIImage() + navigationBar.isTranslucent = false + navigationBar.barTintColor = Colors.navigationBarBackground + } + + internal func setNavBarTitle(_ title: String, customFontSize: CGFloat? = nil) { + let titleLabel = UILabel() + titleLabel.text = title + titleLabel.textColor = Colors.text + titleLabel.font = .boldSystemFont(ofSize: customFontSize ?? Values.veryLargeFontSize) + navigationItem.titleView = titleLabel + } + + internal func setUpNavBarSessionIcon() { + let logoImageView = UIImageView() + logoImageView.image = #imageLiteral(resourceName: "SessionGreen32") + logoImageView.contentMode = .scaleAspectFit + logoImageView.set(.width, to: 32) + logoImageView.set(.height, to: 32) + navigationItem.titleView = logoImageView + } + deinit { NotificationCenter.default.removeObserver(self) } diff --git a/Signal/src/Loki/View Controllers/DeviceLinksVC.swift b/Signal/src/Loki/View Controllers/DeviceLinksVC.swift index 624bb7a53..167a72303 100644 --- a/Signal/src/Loki/View Controllers/DeviceLinksVC.swift +++ b/Signal/src/Loki/View Controllers/DeviceLinksVC.swift @@ -38,22 +38,9 @@ final class DeviceLinksVC : BaseVC, UITableViewDataSource, UITableViewDelegate, // MARK: Lifecycle override func viewDidLoad() { super.viewDidLoad() - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - // Set navigation bar background color - let navigationBar = navigationController!.navigationBar - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground - // Customize title - let titleLabel = UILabel() - titleLabel.text = NSLocalizedString("Devices", comment: "") - titleLabel.textColor = Colors.text - titleLabel.font = .boldSystemFont(ofSize: Values.veryLargeFontSize) - navigationItem.titleView = titleLabel + setUpGradientBackground() + setUpNavBarStyle() + setNavBarTitle(NSLocalizedString("Devices", comment: "")) // Set up link new device button let linkNewDeviceButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(linkNewDevice)) linkNewDeviceButton.tintColor = Colors.text diff --git a/Signal/src/Loki/View Controllers/DisplayNameVC.swift b/Signal/src/Loki/View Controllers/DisplayNameVC.swift index 80411676c..5c256880e 100644 --- a/Signal/src/Loki/View Controllers/DisplayNameVC.swift +++ b/Signal/src/Loki/View Controllers/DisplayNameVC.swift @@ -15,23 +15,9 @@ final class DisplayNameVC : BaseVC { // MARK: Lifecycle override func viewDidLoad() { super.viewDidLoad() - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - // Set up navigation bar - let navigationBar = navigationController!.navigationBar - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground - // Set up logo image view - let logoImageView = UIImageView() - logoImageView.image = #imageLiteral(resourceName: "SessionGreen32") - logoImageView.contentMode = .scaleAspectFit - logoImageView.set(.width, to: 32) - logoImageView.set(.height, to: 32) - navigationItem.titleView = logoImageView + setUpGradientBackground() + setUpNavBarStyle() + setUpNavBarSessionIcon() // Set up title label let titleLabel = UILabel() titleLabel.textColor = Colors.text diff --git a/Signal/src/Loki/View Controllers/GroupMembersVC.swift b/Signal/src/Loki/View Controllers/GroupMembersVC.swift index 68bd4ed39..85ea67fd3 100644 --- a/Signal/src/Loki/View Controllers/GroupMembersVC.swift +++ b/Signal/src/Loki/View Controllers/GroupMembersVC.swift @@ -33,22 +33,9 @@ final class GroupMembersVC : BaseVC, UITableViewDataSource { override func viewDidLoad() { super.viewDidLoad() - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - // Set navigation bar background color - let navigationBar = navigationController!.navigationBar - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground - // Customize title - let titleLabel = UILabel() - titleLabel.text = NSLocalizedString("Group Members", comment: "") - titleLabel.textColor = Colors.text - titleLabel.font = .boldSystemFont(ofSize: Values.veryLargeFontSize) - navigationItem.titleView = titleLabel + setUpGradientBackground() + setUpNavBarStyle() + setNavBarTitle(NSLocalizedString("Group Members", comment: "")) // Set up explanation label let explanationLabel = UILabel() explanationLabel.textColor = Colors.text.withAlphaComponent(Values.unimportantElementOpacity) diff --git a/Signal/src/Loki/View Controllers/HomeVC.swift b/Signal/src/Loki/View Controllers/HomeVC.swift index 3998bde18..a9dc0ee2d 100644 --- a/Signal/src/Loki/View Controllers/HomeVC.swift +++ b/Signal/src/Loki/View Controllers/HomeVC.swift @@ -85,25 +85,12 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIScrol override func viewDidLoad() { super.viewDidLoad() SignalApp.shared().homeViewController = self - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - // Set navigation bar background color - if let navigationBar = navigationController?.navigationBar { - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground + setUpGradientBackground() + if navigationController?.navigationBar != nil { + setUpNavBarStyle() } - // Set up navigation bar buttons updateNavigationBarButtons() - // Customize title - let titleLabel = UILabel() - titleLabel.text = NSLocalizedString("Messages", comment: "") - titleLabel.textColor = Colors.text - titleLabel.font = .boldSystemFont(ofSize: Values.veryLargeFontSize) - navigationItem.titleView = titleLabel + setNavBarTitle(NSLocalizedString("Messages", comment: "")) // Set up seed reminder view if needed let userDefaults = UserDefaults.standard let hasViewedSeed = userDefaults[.hasViewedSeed] diff --git a/Signal/src/Loki/View Controllers/JoinPublicChatVC.swift b/Signal/src/Loki/View Controllers/JoinPublicChatVC.swift index e65772e6b..54994be0d 100644 --- a/Signal/src/Loki/View Controllers/JoinPublicChatVC.swift +++ b/Signal/src/Loki/View Controllers/JoinPublicChatVC.swift @@ -42,26 +42,14 @@ final class JoinPublicChatVC : BaseVC, UIPageViewControllerDataSource, UIPageVie // MARK: Lifecycle override func viewDidLoad() { super.viewDidLoad() - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - // Set navigation bar background color + setUpGradientBackground() + setUpNavBarStyle() + setNavBarTitle(NSLocalizedString("Join Open Group", comment: "")) let navigationBar = navigationController!.navigationBar - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground // Set up navigation bar buttons let closeButton = UIBarButtonItem(image: #imageLiteral(resourceName: "X"), style: .plain, target: self, action: #selector(close)) closeButton.tintColor = Colors.text navigationItem.leftBarButtonItem = closeButton - // Customize title - let titleLabel = UILabel() - titleLabel.text = NSLocalizedString("Join Open Group", comment: "") - titleLabel.textColor = Colors.text - titleLabel.font = .boldSystemFont(ofSize: Values.veryLargeFontSize) - navigationItem.titleView = titleLabel // Set up page VC let hasCameraAccess = (AVCaptureDevice.authorizationStatus(for: .video) == .authorized) pages = [ enterChatURLVC, (hasCameraAccess ? scanQRCodeWrapperVC : scanQRCodePlaceholderVC) ] diff --git a/Signal/src/Loki/View Controllers/LandingVC.swift b/Signal/src/Loki/View Controllers/LandingVC.swift index 5005becca..f3b6f9b9b 100644 --- a/Signal/src/Loki/View Controllers/LandingVC.swift +++ b/Signal/src/Loki/View Controllers/LandingVC.swift @@ -36,23 +36,9 @@ final class LandingVC : BaseVC, LinkDeviceVCDelegate, DeviceLinkingModalDelegate // MARK: Lifecycle override func viewDidLoad() { super.viewDidLoad() - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - // Set up navigation bar - let navigationBar = navigationController!.navigationBar - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground - // Set up logo image view - let logoImageView = UIImageView() - logoImageView.image = #imageLiteral(resourceName: "SessionGreen32") - logoImageView.contentMode = .scaleAspectFit - logoImageView.set(.width, to: 32) - logoImageView.set(.height, to: 32) - navigationItem.titleView = logoImageView + setUpGradientBackground() + setUpNavBarStyle() + setUpNavBarSessionIcon() // Set up title label let titleLabel = UILabel() titleLabel.textColor = Colors.text diff --git a/Signal/src/Loki/View Controllers/LinkDeviceVC.swift b/Signal/src/Loki/View Controllers/LinkDeviceVC.swift index e71d6a71b..30b81dd0d 100644 --- a/Signal/src/Loki/View Controllers/LinkDeviceVC.swift +++ b/Signal/src/Loki/View Controllers/LinkDeviceVC.swift @@ -42,26 +42,14 @@ final class LinkDeviceVC : BaseVC, UIPageViewControllerDataSource, UIPageViewCon // MARK: Lifecycle override func viewDidLoad() { super.viewDidLoad() - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - // Set navigation bar background color + setUpGradientBackground() + setUpNavBarStyle() + setNavBarTitle(NSLocalizedString("Link Device", comment: "")) let navigationBar = navigationController!.navigationBar - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground // Set up navigation bar buttons let closeButton = UIBarButtonItem(image: #imageLiteral(resourceName: "X"), style: .plain, target: self, action: #selector(close)) closeButton.tintColor = Colors.text navigationItem.leftBarButtonItem = closeButton - // Customize title - let titleLabel = UILabel() - titleLabel.text = NSLocalizedString("Link Device", comment: "") - titleLabel.textColor = Colors.text - titleLabel.font = .boldSystemFont(ofSize: Values.veryLargeFontSize) - navigationItem.titleView = titleLabel // Set up page VC let hasCameraAccess = (AVCaptureDevice.authorizationStatus(for: .video) == .authorized) pages = [ enterPublicKeyVC, (hasCameraAccess ? scanQRCodeWrapperVC : scanQRCodePlaceholderVC) ] diff --git a/Signal/src/Loki/View Controllers/NewClosedGroupVC.swift b/Signal/src/Loki/View Controllers/NewClosedGroupVC.swift index dae73a965..ad4f87698 100644 --- a/Signal/src/Loki/View Controllers/NewClosedGroupVC.swift +++ b/Signal/src/Loki/View Controllers/NewClosedGroupVC.swift @@ -45,16 +45,9 @@ final class NewClosedGroupVC : BaseVC, UITableViewDataSource, UITableViewDelegat // MARK: Lifecycle override func viewDidLoad() { super.viewDidLoad() - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - // Set navigation bar background color - let navigationBar = navigationController!.navigationBar - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground + setUpGradientBackground() + setUpNavBarStyle() + setNavBarTitle(NSLocalizedString("New Closed Group", comment: "")) // Set up navigation bar buttons let closeButton = UIBarButtonItem(image: #imageLiteral(resourceName: "X"), style: .plain, target: self, action: #selector(close)) closeButton.tintColor = Colors.text @@ -62,13 +55,6 @@ final class NewClosedGroupVC : BaseVC, UITableViewDataSource, UITableViewDelegat let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(createClosedGroup)) doneButton.tintColor = Colors.text navigationItem.rightBarButtonItem = doneButton - // Customize title - let titleLabel = UILabel() - titleLabel.text = NSLocalizedString("New Closed Group", comment: "") - titleLabel.textColor = Colors.text - let titleLabelFontSize = isSmallScreen ? Values.mediumFontSize : Values.largeFontSize - titleLabel.font = .boldSystemFont(ofSize: titleLabelFontSize) - navigationItem.titleView = titleLabel // Set up content if !contacts.isEmpty { view.addSubview(nameTextField) diff --git a/Signal/src/Loki/View Controllers/NewPrivateChatVC.swift b/Signal/src/Loki/View Controllers/NewPrivateChatVC.swift index 3c4cbf1e8..303ceea65 100644 --- a/Signal/src/Loki/View Controllers/NewPrivateChatVC.swift +++ b/Signal/src/Loki/View Controllers/NewPrivateChatVC.swift @@ -41,26 +41,14 @@ final class NewPrivateChatVC : BaseVC, UIPageViewControllerDataSource, UIPageVie // MARK: Lifecycle override func viewDidLoad() { super.viewDidLoad() - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - // Set navigation bar background color + setUpGradientBackground() + setUpNavBarStyle() + setNavBarTitle(NSLocalizedString("New Session", comment: "")) let navigationBar = navigationController!.navigationBar - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground // Set up navigation bar buttons let closeButton = UIBarButtonItem(image: #imageLiteral(resourceName: "X"), style: .plain, target: self, action: #selector(close)) closeButton.tintColor = Colors.text navigationItem.leftBarButtonItem = closeButton - // Customize title - let titleLabel = UILabel() - titleLabel.text = NSLocalizedString("New Session", comment: "") - titleLabel.textColor = Colors.text - titleLabel.font = .boldSystemFont(ofSize: Values.veryLargeFontSize) - navigationItem.titleView = titleLabel // Set up page VC let hasCameraAccess = (AVCaptureDevice.authorizationStatus(for: .video) == .authorized) pages = [ enterPublicKeyVC, (hasCameraAccess ? scanQRCodeWrapperVC : scanQRCodePlaceholderVC) ] diff --git a/Signal/src/Loki/View Controllers/PNModeVC.swift b/Signal/src/Loki/View Controllers/PNModeVC.swift index 30024f1e3..4572d2e16 100644 --- a/Signal/src/Loki/View Controllers/PNModeVC.swift +++ b/Signal/src/Loki/View Controllers/PNModeVC.swift @@ -17,23 +17,9 @@ final class PNModeVC : BaseVC, OptionViewDelegate { // MARK: Lifecycle override func viewDidLoad() { super.viewDidLoad() - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - // Set up navigation bar - let navigationBar = navigationController!.navigationBar - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground - // Set up logo image view - let logoImageView = UIImageView() - logoImageView.image = #imageLiteral(resourceName: "SessionGreen32") - logoImageView.contentMode = .scaleAspectFit - logoImageView.set(.width, to: 32) - logoImageView.set(.height, to: 32) - navigationItem.titleView = logoImageView + setUpGradientBackground() + setUpNavBarStyle() + setUpNavBarSessionIcon() // Set up title label let titleLabel = UILabel() titleLabel.textColor = Colors.text diff --git a/Signal/src/Loki/View Controllers/PathVC.swift b/Signal/src/Loki/View Controllers/PathVC.swift index 78a0fc1d9..ac03d20b8 100644 --- a/Signal/src/Loki/View Controllers/PathVC.swift +++ b/Signal/src/Loki/View Controllers/PathVC.swift @@ -26,25 +26,15 @@ final class PathVC : BaseVC { // MARK: Lifecycle override func viewDidLoad() { super.viewDidLoad() - setUpBackground() + setUpGradientBackground() setUpNavBar() setUpViewHierarchy() registerObservers() } - private func setUpBackground() { - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - } - private func setUpNavBar() { - // Set up navigation bar style - let navigationBar = navigationController!.navigationBar - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground + setUpNavBarStyle() + setNavBarTitle(NSLocalizedString("Path", comment: "")) // Set up close button let closeButton = UIBarButtonItem(image: #imageLiteral(resourceName: "X"), style: .plain, target: self, action: #selector(close)) closeButton.tintColor = Colors.text @@ -52,12 +42,6 @@ final class PathVC : BaseVC { let learnMoreButton = UIBarButtonItem(image: #imageLiteral(resourceName: "QuestionMark").scaled(to: CGSize(width: 24, height: 24)), style: .plain, target: self, action: #selector(learnMore)) learnMoreButton.tintColor = Colors.text navigationItem.rightBarButtonItem = learnMoreButton - // Customize title - let titleLabel = UILabel() - titleLabel.text = NSLocalizedString("Path", comment: "") - titleLabel.textColor = Colors.text - titleLabel.font = .boldSystemFont(ofSize: Values.veryLargeFontSize) - navigationItem.titleView = titleLabel } private func setUpViewHierarchy() { @@ -200,6 +184,13 @@ final class PathVC : BaseVC { } @objc private func rebuildPath() { + // Dispatch async on the main queue to avoid nested write transactions + DispatchQueue.main.async { + let storage = OWSPrimaryStorage.shared() + storage.dbReadWriteConnection.readWrite { transaction in + storage.clearOnionRequestPaths(in: transaction) + } + } OnionRequestAPI.guardSnodes = [] OnionRequestAPI.paths = [] let _ = OnionRequestAPI.buildPaths() diff --git a/Signal/src/Loki/View Controllers/QRCodeVC.swift b/Signal/src/Loki/View Controllers/QRCodeVC.swift index 3beef2d10..1a3c494c5 100644 --- a/Signal/src/Loki/View Controllers/QRCodeVC.swift +++ b/Signal/src/Loki/View Controllers/QRCodeVC.swift @@ -42,22 +42,10 @@ final class QRCodeVC : BaseVC, UIPageViewControllerDataSource, UIPageViewControl // MARK: Lifecycle override func viewDidLoad() { super.viewDidLoad() - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - // Set navigation bar background color + setUpGradientBackground() + setUpNavBarStyle() + setNavBarTitle(NSLocalizedString("QR Code", comment: "")) let navigationBar = navigationController!.navigationBar - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground - // Customize title - let titleLabel = UILabel() - titleLabel.text = NSLocalizedString("QR Code", comment: "") - titleLabel.textColor = Colors.text - titleLabel.font = .boldSystemFont(ofSize: Values.veryLargeFontSize) - navigationItem.titleView = titleLabel // Set up page VC let hasCameraAccess = (AVCaptureDevice.authorizationStatus(for: .video) == .authorized) pages = [ viewMyQRCodeVC, (hasCameraAccess ? scanQRCodeWrapperVC : scanQRCodePlaceholderVC) ] diff --git a/Signal/src/Loki/View Controllers/RegisterVC.swift b/Signal/src/Loki/View Controllers/RegisterVC.swift index 8d54b9818..b612de685 100644 --- a/Signal/src/Loki/View Controllers/RegisterVC.swift +++ b/Signal/src/Loki/View Controllers/RegisterVC.swift @@ -40,23 +40,9 @@ final class RegisterVC : BaseVC { // MARK: Lifecycle override func viewDidLoad() { super.viewDidLoad() - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - // Set up navigation bar - let navigationBar = navigationController!.navigationBar - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground - // Set up logo image view - let logoImageView = UIImageView() - logoImageView.image = #imageLiteral(resourceName: "SessionGreen32") - logoImageView.contentMode = .scaleAspectFit - logoImageView.set(.width, to: 32) - logoImageView.set(.height, to: 32) - navigationItem.titleView = logoImageView + setUpGradientBackground() + setUpNavBarStyle() + setUpNavBarSessionIcon() // Set up title label let titleLabel = UILabel() titleLabel.textColor = Colors.text diff --git a/Signal/src/Loki/View Controllers/RestoreVC.swift b/Signal/src/Loki/View Controllers/RestoreVC.swift index 58302ef1e..15d888830 100644 --- a/Signal/src/Loki/View Controllers/RestoreVC.swift +++ b/Signal/src/Loki/View Controllers/RestoreVC.swift @@ -32,23 +32,9 @@ final class RestoreVC : BaseVC { // MARK: Lifecycle override func viewDidLoad() { super.viewDidLoad() - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - // Set up navigation bar - let navigationBar = navigationController!.navigationBar - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground - // Set up logo image view - let logoImageView = UIImageView() - logoImageView.image = #imageLiteral(resourceName: "SessionGreen32") - logoImageView.contentMode = .scaleAspectFit - logoImageView.set(.width, to: 32) - logoImageView.set(.height, to: 32) - navigationItem.titleView = logoImageView + setUpGradientBackground() + setUpNavBarStyle() + setUpNavBarSessionIcon() // Set up title label let titleLabel = UILabel() titleLabel.textColor = Colors.text diff --git a/Signal/src/Loki/View Controllers/ScanQRCodeWrapperVC.swift b/Signal/src/Loki/View Controllers/ScanQRCodeWrapperVC.swift index d4626e1db..5a427f63b 100644 --- a/Signal/src/Loki/View Controllers/ScanQRCodeWrapperVC.swift +++ b/Signal/src/Loki/View Controllers/ScanQRCodeWrapperVC.swift @@ -28,10 +28,7 @@ final class ScanQRCodeWrapperVC : BaseVC { if isPresentedModally { navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(close)) } - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) + setUpGradientBackground() // Set up scan QR code VC scanQRCodeVC.scanDelegate = delegate let scanQRCodeVCView = scanQRCodeVC.view! diff --git a/Signal/src/Loki/View Controllers/SeedVC.swift b/Signal/src/Loki/View Controllers/SeedVC.swift index c61b29ca1..cdcacfd5a 100644 --- a/Signal/src/Loki/View Controllers/SeedVC.swift +++ b/Signal/src/Loki/View Controllers/SeedVC.swift @@ -55,23 +55,9 @@ final class SeedVC : BaseVC { // MARK: Lifecycle override func viewDidLoad() { super.viewDidLoad() - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - // Set up navigation bar - let navigationBar = navigationController!.navigationBar - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground - // Customize title - let navigationBarTitleLabel = UILabel() - navigationBarTitleLabel.text = NSLocalizedString("Your Recovery Phrase", comment: "") - navigationBarTitleLabel.textColor = Colors.text - let titleLabelFontSize = isSmallScreen ? Values.largeFontSize : Values.veryLargeFontSize - navigationBarTitleLabel.font = .boldSystemFont(ofSize: titleLabelFontSize) - navigationItem.titleView = navigationBarTitleLabel + setUpGradientBackground() + setUpNavBarStyle() + setNavBarTitle(NSLocalizedString("Your Recovery Phrase", comment: "")) // Set up navigation bar buttons let closeButton = UIBarButtonItem(image: #imageLiteral(resourceName: "X"), style: .plain, target: self, action: #selector(close)) closeButton.tintColor = Colors.text diff --git a/Signal/src/Loki/View Controllers/SettingsVC.swift b/Signal/src/Loki/View Controllers/SettingsVC.swift index d60f29a3b..bfd72e9ed 100644 --- a/Signal/src/Loki/View Controllers/SettingsVC.swift +++ b/Signal/src/Loki/View Controllers/SettingsVC.swift @@ -53,27 +53,14 @@ final class SettingsVC : BaseVC, AvatarViewHelperDelegate { // MARK: Lifecycle override func viewDidLoad() { super.viewDidLoad() - // Set gradient background - view.backgroundColor = .clear - let gradient = Gradients.defaultLokiBackground - view.setGradient(gradient) - // Set navigation bar background color - let navigationBar = navigationController!.navigationBar - navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) - navigationBar.shadowImage = UIImage() - navigationBar.isTranslucent = false - navigationBar.barTintColor = Colors.navigationBarBackground + setUpGradientBackground() + setUpNavBarStyle() + setNavBarTitle(NSLocalizedString("Settings", comment: "")) // Set up navigation bar buttons let backButton = UIBarButtonItem(title: NSLocalizedString("Back", comment: ""), style: .plain, target: nil, action: nil) backButton.tintColor = Colors.text navigationItem.backBarButtonItem = backButton updateNavigationBarButtons() - // Customize title - let titleLabel = UILabel() - titleLabel.text = NSLocalizedString("Settings", comment: "") - titleLabel.textColor = Colors.text - titleLabel.font = .boldSystemFont(ofSize: Values.veryLargeFontSize) - navigationItem.titleView = titleLabel // Set up profile picture view let profilePictureTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showEditProfilePictureUI)) profilePictureView.addGestureRecognizer(profilePictureTapGestureRecognizer) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 726a3ba83..e57323112 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -605,7 +605,7 @@ typedef enum : NSUInteger { userInfo:nil repeats:YES]; - [LKMentionsManager populateUserHexEncodedPublicKeyCacheIfNeededFor:thread.uniqueId in:nil]; + [LKMentionsManager populateUserPublicKeyCacheIfNeededFor:thread.uniqueId in:nil]; } - (void)dealloc diff --git a/Signal/src/ViewControllers/HomeView/HomeViewCell.m b/Signal/src/ViewControllers/HomeView/HomeViewCell.m index b5a1232ea..93929942f 100644 --- a/Signal/src/ViewControllers/HomeView/HomeViewCell.m +++ b/Signal/src/ViewControllers/HomeView/HomeViewCell.m @@ -395,7 +395,7 @@ NS_ASSUME_NONNULL_BEGIN } NSString *displayableText = thread.lastMessageText; if (displayableText) { - [LKMentionsManager populateUserHexEncodedPublicKeyCacheIfNeededFor:thread.threadRecord.uniqueId in:nil]; // TODO: Terrible place to do this, but okay for now + [LKMentionsManager populateUserPublicKeyCacheIfNeededFor:thread.threadRecord.uniqueId in:nil]; // TODO: Terrible place to do this, but okay for now displayableText = [LKMentionUtilities highlightMentionsIn:displayableText threadID:thread.threadRecord.uniqueId]; [snippetText appendAttributedString:[[NSAttributedString alloc] initWithString:displayableText diff --git a/SignalMessaging/Views/ContactCellView.m b/SignalMessaging/Views/ContactCellView.m index f24186ddf..61bd10717 100644 --- a/SignalMessaging/Views/ContactCellView.m +++ b/SignalMessaging/Views/ContactCellView.m @@ -213,7 +213,7 @@ const CGFloat kContactCellAvatarTextMargin = 12; { if (self.thread.isGroupThread) { NSMutableArray *sortedUsers = @[].mutableCopy; - NSSet *users = LKMentionsManager.userHexEncodedPublicKeyCache[self.thread.uniqueId]; + NSSet *users = LKMentionsManager.userPublicKeyCache[self.thread.uniqueId]; if (users != nil) { for (NSString *user in users) { [sortedUsers addObject:user]; diff --git a/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift index 87de8a9e4..95c39304a 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift @@ -15,7 +15,7 @@ public extension LokiAPI { // MARK: Caching internal static var swarmCache: [String:[LokiAPITarget]] = [:] - internal static func dropIfNeeded(_ target: LokiAPITarget, hexEncodedPublicKey: String) { + internal static func dropSnodeIfNeeded(_ target: LokiAPITarget, hexEncodedPublicKey: String) { let swarm = LokiAPI.swarmCache[hexEncodedPublicKey] if var swarm = swarm, let index = swarm.firstIndex(of: target) { swarm.remove(at: index) @@ -175,7 +175,7 @@ internal extension Promise { print("[Loki] Couldn't reach snode at: \(target); setting failure count to \(newFailureCount).") if newFailureCount >= LokiAPI.failureThreshold { print("[Loki] Failure threshold reached for: \(target); dropping it.") - LokiAPI.dropIfNeeded(target, hexEncodedPublicKey: hexEncodedPublicKey) // Remove it from the swarm cache associated with the given public key + LokiAPI.dropSnodeIfNeeded(target, hexEncodedPublicKey: hexEncodedPublicKey) // Remove it from the swarm cache associated with the given public key LokiAPI.snodePool.remove(target) // Remove it from the snode pool // Dispatch async on the main queue to avoid nested write transactions DispatchQueue.main.async { @@ -192,7 +192,7 @@ internal extension Promise { case 421: // The snode isn't associated with the given public key anymore print("[Loki] Invalidating swarm for: \(hexEncodedPublicKey).") - LokiAPI.dropIfNeeded(target, hexEncodedPublicKey: hexEncodedPublicKey) + LokiAPI.dropSnodeIfNeeded(target, hexEncodedPublicKey: hexEncodedPublicKey) case 432: // The PoW difficulty is too low if case LokiHTTPClient.HTTPError.networkError(_, let result, _) = error, let json = result as? JSON, let powDifficulty = json["difficulty"] as? Int { diff --git a/SignalServiceKit/src/Loki/API/LokiPoller.swift b/SignalServiceKit/src/Loki/API/LokiPoller.swift index 694a01952..e455fe794 100644 --- a/SignalServiceKit/src/Loki/API/LokiPoller.swift +++ b/SignalServiceKit/src/Loki/API/LokiPoller.swift @@ -84,7 +84,7 @@ public final class LokiPoller : NSObject { self?.pollCount = 0 } else { print("[Loki] Polling \(nextSnode) failed; dropping it and switching to next snode.") - LokiAPI.dropIfNeeded(nextSnode, hexEncodedPublicKey: userHexEncodedPublicKey) + LokiAPI.dropSnodeIfNeeded(nextSnode, hexEncodedPublicKey: userHexEncodedPublicKey) } self?.pollNextSnode(seal: seal) } diff --git a/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift b/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift index ecc02009f..6ce07203d 100644 --- a/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift +++ b/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift @@ -275,7 +275,7 @@ private extension Promise where T == JSON { print("[Loki] Couldn't reach snode at: \(snode); setting failure count to \(newFailureCount).") if newFailureCount >= LokiAPI.failureThreshold { print("[Loki] Failure threshold reached for: \(snode); dropping it.") - LokiAPI.dropIfNeeded(snode, hexEncodedPublicKey: hexEncodedPublicKey) // Remove it from the swarm cache associated with the given public key + LokiAPI.dropSnodeIfNeeded(snode, hexEncodedPublicKey: hexEncodedPublicKey) // Remove it from the swarm cache associated with the given public key LokiAPI.snodePool.remove(snode) // Remove it from the snode pool // Dispatch async on the main queue to avoid nested write transactions DispatchQueue.main.async { @@ -292,7 +292,7 @@ private extension Promise where T == JSON { case 421: // The snode isn't associated with the given public key anymore print("[Loki] Invalidating swarm for: \(hexEncodedPublicKey).") - LokiAPI.dropIfNeeded(snode, hexEncodedPublicKey: hexEncodedPublicKey) + LokiAPI.dropSnodeIfNeeded(snode, hexEncodedPublicKey: hexEncodedPublicKey) case 432: // The proof of work difficulty is too low if let powDifficulty = json["difficulty"] as? Int { diff --git a/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift b/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift index a8eec9a78..dad6831ac 100644 --- a/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift +++ b/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift @@ -3,8 +3,8 @@ public final class LokiDatabaseUtilities : NSObject { private override init() { } - - // MARK: Quotes + + // MARK: - Quotes @objc(getServerIDForQuoteWithID:quoteeHexEncodedPublicKey:threadID:transaction:) public static func getServerID(quoteID: UInt64, quoteeHexEncodedPublicKey: String, threadID: String, transaction: YapDatabaseReadTransaction) -> UInt64 { guard let message = TSInteraction.interactions(withTimestamp: quoteID, filter: { interaction in @@ -20,8 +20,10 @@ public final class LokiDatabaseUtilities : NSObject { }, with: transaction).first as! TSMessage? else { return 0 } return message.openGroupServerMessageID } - - // MARK: Device Links + + + + // MARK: - Device Links @objc(getLinkedDeviceHexEncodedPublicKeysFor:in:) public static func getLinkedDeviceHexEncodedPublicKeys(for hexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> Set { let storage = OWSPrimaryStorage.shared() @@ -54,8 +56,10 @@ public final class LokiDatabaseUtilities : NSObject { public static func objc_getDeviceLinks(for masterHexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> Set { return OWSPrimaryStorage.shared().getDeviceLinks(for: masterHexEncodedPublicKey, in: transaction) } - - // MARK: Public Chats + + + + // MARK: - Open Groups private static let publicChatCollection = "LokiPublicChatCollection" @objc(getAllPublicChats:) diff --git a/SignalServiceKit/src/Loki/Protocol/Mentions/MentionsManager.swift b/SignalServiceKit/src/Loki/Protocol/Mentions/MentionsManager.swift index fb6491f17..b256adeab 100644 --- a/SignalServiceKit/src/Loki/Protocol/Mentions/MentionsManager.swift +++ b/SignalServiceKit/src/Loki/Protocol/Mentions/MentionsManager.swift @@ -4,7 +4,7 @@ public final class MentionsManager : NSObject { private static var _userHexEncodedPublicKeyCache: [String:Set] = [:] /// A mapping from thread ID to set of user hex encoded public keys. - @objc public static var userHexEncodedPublicKeyCache: [String:Set] { + @objc public static var userPublicKeyCache: [String:Set] { get { LokiAPI.stateQueue.sync { _userHexEncodedPublicKeyCache } } set { LokiAPI.stateQueue.sync { _userHexEncodedPublicKeyCache = newValue } } } @@ -21,16 +21,16 @@ public final class MentionsManager : NSObject { // MARK: Implementation @objc public static func cache(_ hexEncodedPublicKey: String, for threadID: String) { - if let cache = userHexEncodedPublicKeyCache[threadID] { - userHexEncodedPublicKeyCache[threadID] = cache.union([ hexEncodedPublicKey ]) + if let cache = userPublicKeyCache[threadID] { + userPublicKeyCache[threadID] = cache.union([ hexEncodedPublicKey ]) } else { - userHexEncodedPublicKeyCache[threadID] = [ hexEncodedPublicKey ] + userPublicKeyCache[threadID] = [ hexEncodedPublicKey ] } } @objc public static func getMentionCandidates(for query: String, in threadID: String) -> [Mention] { // Prepare - guard let cache = userHexEncodedPublicKeyCache[threadID] else { return [] } + guard let cache = userPublicKeyCache[threadID] else { return [] } var candidates: [Mention] = [] // Gather candidates var publicChat: LokiPublicChat? @@ -65,14 +65,14 @@ public final class MentionsManager : NSObject { return candidates } - @objc public static func populateUserHexEncodedPublicKeyCacheIfNeeded(for threadID: String, in transaction: YapDatabaseReadTransaction? = nil) { + @objc public static func populateUserPublicKeyCacheIfNeeded(for threadID: String, in transaction: YapDatabaseReadTransaction? = nil) { var result: Set = [] func populate(in transaction: YapDatabaseReadTransaction) { guard let thread = TSThread.fetch(uniqueId: threadID, transaction: transaction) else { return } if let groupThread = thread as? TSGroupThread, groupThread.groupModel.groupType == .closedGroup { result = result.union(groupThread.groupModel.groupMemberIds) } else { - guard userHexEncodedPublicKeyCache[threadID] == nil else { return } + guard userPublicKeyCache[threadID] == nil else { return } let interactions = transaction.ext(TSMessageDatabaseViewExtensionName) as! YapDatabaseViewTransaction interactions.enumerateKeysAndObjects(inGroup: threadID) { _, _, object, index, _ in guard let message = object as? TSIncomingMessage, index < userIDScanLimit else { return } @@ -88,6 +88,6 @@ public final class MentionsManager : NSObject { } } result.insert(getUserHexEncodedPublicKey()) - userHexEncodedPublicKeyCache[threadID] = result + userPublicKeyCache[threadID] = result } } diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index 43cc7420f..38b50102d 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -1466,7 +1466,7 @@ NS_ASSUME_NONNULL_BEGIN // Loki: Cache the user hex encoded public key (for mentions) dispatch_async(dispatch_get_main_queue(), ^{ [self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { - [LKMentionsManager populateUserHexEncodedPublicKeyCacheIfNeededFor:oldGroupThread.uniqueId in:transaction]; + [LKMentionsManager populateUserPublicKeyCacheIfNeededFor:oldGroupThread.uniqueId in:transaction]; [LKMentionsManager cache:incomingMessage.authorId for:oldGroupThread.uniqueId]; }]; });