From 023b931d8c8cfa559c5cf3493bb698a3dd3198e0 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Tue, 21 Jul 2020 13:49:41 +1000 Subject: [PATCH] Implement blocking --- .../Loki/Components/ConversationCell.swift | 32 ++++++++++++------- Signal/src/Loki/View Controllers/HomeVC.swift | 31 ++++++++++-------- .../ConversationViewController.m | 12 +++---- SignalMessaging/utils/BlockListUIUtils.m | 2 +- 4 files changed, 43 insertions(+), 34 deletions(-) diff --git a/Signal/src/Loki/Components/ConversationCell.swift b/Signal/src/Loki/Components/ConversationCell.swift index 2c7842ac6..791f3ac43 100644 --- a/Signal/src/Loki/Components/ConversationCell.swift +++ b/Signal/src/Loki/Components/ConversationCell.swift @@ -5,11 +5,7 @@ final class ConversationCell : UITableViewCell { static let reuseIdentifier = "ConversationCell" // MARK: Components - private lazy var unreadMessagesIndicatorView: UIView = { - let result = UIView() - result.backgroundColor = Colors.accent - return result - }() + private let accentView = UIView() private lazy var profilePictureView = ProfilePictureView() @@ -67,9 +63,9 @@ final class ConversationCell : UITableViewCell { let selectedBackgroundView = UIView() selectedBackgroundView.backgroundColor = Colors.cellSelected self.selectedBackgroundView = selectedBackgroundView - // Set up the unread messages indicator view - unreadMessagesIndicatorView.set(.width, to: Values.accentLineThickness) - unreadMessagesIndicatorView.set(.height, to: cellHeight) + // Set up the accent view + accentView.set(.width, to: Values.accentLineThickness) + accentView.set(.height, to: cellHeight) // Set up the profile picture view let profilePictureViewSize = Values.mediumProfilePictureSize profilePictureView.set(.width, to: profilePictureViewSize) @@ -93,14 +89,14 @@ final class ConversationCell : UITableViewCell { labelContainerView.addSubview(topLabelStackView) labelContainerView.addSubview(bottomLabelStackView) // Set up the main stack view - let stackView = UIStackView(arrangedSubviews: [ unreadMessagesIndicatorView, profilePictureView, labelContainerView ]) + let stackView = UIStackView(arrangedSubviews: [ accentView, profilePictureView, labelContainerView ]) stackView.axis = .horizontal stackView.alignment = .center stackView.spacing = Values.mediumSpacing contentView.addSubview(stackView) // Set up the constraints - unreadMessagesIndicatorView.pin(.top, to: .top, of: contentView) - unreadMessagesIndicatorView.pin(.bottom, to: .bottom, of: contentView) + accentView.pin(.top, to: .top, of: contentView) + accentView.pin(.bottom, to: .bottom, of: contentView) // The three lines below are part of a workaround for a weird layout bug topLabelStackView.set(.width, to: UIScreen.main.bounds.width - Values.accentLineThickness - Values.mediumSpacing - profilePictureViewSize - Values.mediumSpacing - Values.mediumSpacing) topLabelStackView.set(.height, to: 20) @@ -136,7 +132,19 @@ final class ConversationCell : UITableViewCell { private func update() { AssertIsOnMainThread() 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 + let isBlocked: Bool + if let thread = threadViewModel.threadRecord as? TSContactThread { + isBlocked = SSKEnvironment.shared.blockingManager.isRecipientIdBlocked(thread.contactIdentifier()) + } else { + isBlocked = false + } + if isBlocked { + accentView.backgroundColor = Colors.destructive + accentView.alpha = 1 + } else { + accentView.backgroundColor = Colors.accent + accentView.alpha = threadViewModel.hasUnreadMessages ? 1 : 0.0001 // Setting the alpha to exactly 0 causes an issue on iOS 12 + } profilePictureView.openGroupProfilePicture = nil if threadViewModel.isGroupThread { if threadViewModel.name == "Loki Public Chat" diff --git a/Signal/src/Loki/View Controllers/HomeVC.swift b/Signal/src/Loki/View Controllers/HomeVC.swift index b209e44cd..d0c9bca82 100644 --- a/Signal/src/Loki/View Controllers/HomeVC.swift +++ b/Signal/src/Loki/View Controllers/HomeVC.swift @@ -358,16 +358,7 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIScrol } func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { - guard let threadID = self.thread(at: indexPath.row)?.uniqueId else { return false } - var publicChat: PublicChat? - OWSPrimaryStorage.shared().dbReadConnection.read { transaction in - publicChat = LokiDatabaseUtilities.getPublicChat(for: threadID, in: transaction) - } - if let publicChat = publicChat { - return publicChat.isDeletable - } else { - return true - } + return true } func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { @@ -376,7 +367,7 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIScrol OWSPrimaryStorage.shared().dbReadConnection.read { transaction in publicChat = LokiDatabaseUtilities.getPublicChat(for: thread.uniqueId!, in: transaction) } - let delete = UITableViewRowAction(style: .destructive, title: NSLocalizedString("Delete", comment: "")) { [weak self] action, indexPath in + let delete = UITableViewRowAction(style: .destructive, title: NSLocalizedString("Delete", comment: "")) { [weak self] _, _ in let alert = UIAlertController(title: NSLocalizedString("CONVERSATION_DELETE_CONFIRMATION_ALERT_TITLE", comment: ""), message: NSLocalizedString("CONVERSATION_DELETE_CONFIRMATION_ALERT_MESSAGE", comment: ""), preferredStyle: .alert) alert.addAction(UIAlertAction(title: NSLocalizedString("TXT_DELETE_TITLE", comment: ""), style: .destructive) { _ in try! Storage.writeSync { transaction in @@ -400,8 +391,22 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIScrol self.present(alert, animated: true, completion: nil) } delete.backgroundColor = Colors.destructive - if let publicChat = publicChat { - return publicChat.isDeletable ? [ delete ] : [] + if thread is TSContactThread { + var publicKey: String! + Storage.read { transaction in + publicKey = OWSPrimaryStorage.shared().getMasterHexEncodedPublicKey(for: thread.contactIdentifier()!, in: transaction) ?? thread.contactIdentifier()! + } + let blockingManager = SSKEnvironment.shared.blockingManager + let isBlocked = blockingManager.isRecipientIdBlocked(publicKey) + let block = UITableViewRowAction(style: .normal, title: NSLocalizedString("BLOCK_LIST_BLOCK_BUTTON", comment: "")) { _, _ in + blockingManager.addBlockedPhoneNumber(publicKey) + tableView.reloadRows(at: [ indexPath ], with: UITableView.RowAnimation.fade) + } + let unblock = UITableViewRowAction(style: .normal, title: NSLocalizedString("BLOCK_LIST_UNBLOCK_BUTTON", comment: "")) { _, _ in + blockingManager.removeBlockedPhoneNumber(publicKey) + tableView.reloadRows(at: [ indexPath ], with: UITableView.RowAnimation.fade) + } + return [ delete, (isBlocked ? unblock : block) ] } else { return [ delete ] } diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index f0d80cb87..f26634bbb 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -1128,7 +1128,7 @@ typedef enum : NSUInteger { if (blockStateMessage) { [self createBannerWithTitle:blockStateMessage - bannerColor:[UIColor ows_destructiveRedColor] + bannerColor:LKColors.destructive tapSelector:@selector(blockBannerViewWasTapped:)]; return; } @@ -1315,19 +1315,15 @@ typedef enum : NSUInteger { { self.userHasScrolled = NO; - // To avoid "noisy" animations (hiding the keyboard before showing - // the action sheet, re-showing it after), hide the keyboard before - // showing the "unblock" action sheet. - // - // Unblocking is a rare interaction, so it's okay to leave the keyboard - // hidden. - [self dismissKeyBoard]; + [UIView setAnimationsEnabled:NO]; [BlockListUIUtils showUnblockThreadActionSheet:self.thread fromViewController:self blockingManager:self.blockingManager contactsManager:self.contactsManager completionBlock:completionBlock]; + + [UIView setAnimationsEnabled:YES]; } - (BOOL)isBlockedConversation diff --git a/SignalMessaging/utils/BlockListUIUtils.m b/SignalMessaging/utils/BlockListUIUtils.m index db07e760f..833724dc8 100644 --- a/SignalMessaging/utils/BlockListUIUtils.m +++ b/SignalMessaging/utils/BlockListUIUtils.m @@ -291,7 +291,7 @@ typedef void (^BlockAlertCompletionBlock)(UIAlertAction *action); contactsManager:(OWSContactsManager *)contactsManager completionBlock:(nullable BlockActionCompletionBlock)completionBlock { - NSString *displayName = [contactsManager displayNameForPhoneIdentifier:phoneNumber]; + NSString *displayName = [LKUserDisplayNameUtilities getPrivateChatDisplayNameFor:phoneNumber]; [self showUnblockPhoneNumbersActionSheet:@[ phoneNumber ] displayName:displayName fromViewController:fromViewController