From 7fb6726d43da131e08cecbc959076710e2fc010f Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Thu, 17 Mar 2022 16:08:45 +1100 Subject: [PATCH] refactor on media+text message cell --- Session.xcodeproj/project.pbxproj | 4 - .../ConversationVC+Interaction.swift | 9 +-- .../Content Views/MediaTextOverlayView.swift | 74 ------------------- .../Message Cells/VisibleMessageCell.swift | 13 ++-- 4 files changed, 7 insertions(+), 93 deletions(-) delete mode 100644 Session/Conversations/Message Cells/Content Views/MediaTextOverlayView.swift diff --git a/Session.xcodeproj/project.pbxproj b/Session.xcodeproj/project.pbxproj index 19150c39c..405c2d94b 100644 --- a/Session.xcodeproj/project.pbxproj +++ b/Session.xcodeproj/project.pbxproj @@ -196,7 +196,6 @@ B8566C6C256F60F50045A0B9 /* OWSUserProfile.m in Sources */ = {isa = PBXBuildFile; fileRef = C38EF2D1255B6DAF007E1867 /* OWSUserProfile.m */; }; B8566C7D256F62030045A0B9 /* OWSUserProfile.h in Headers */ = {isa = PBXBuildFile; fileRef = C38EF2D3255B6DAF007E1867 /* OWSUserProfile.h */; settings = {ATTRIBUTES = (Public, ); }; }; B8569AC325CB5D2900DBA3DB /* ConversationVC+Interaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8569AC225CB5D2900DBA3DB /* ConversationVC+Interaction.swift */; }; - B8569AD325CBA13D00DBA3DB /* MediaTextOverlayView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8569AD225CBA13D00DBA3DB /* MediaTextOverlayView.swift */; }; B8569AE325CBB19A00DBA3DB /* DocumentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8569AE225CBB19A00DBA3DB /* DocumentView.swift */; }; B866CE112581C1A900535CC4 /* Sodium+Conversion.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3E7134E251C867C009649BB /* Sodium+Conversion.swift */; }; B86BD08423399ACF000F5AE3 /* Modal.swift in Sources */ = {isa = PBXBuildFile; fileRef = B86BD08323399ACF000F5AE3 /* Modal.swift */; }; @@ -1226,7 +1225,6 @@ B8544E3223D50E4900299F14 /* SNAppearance.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SNAppearance.swift; sourceTree = ""; }; B8566C62256F55930045A0B9 /* OWSLinkPreview+Conversion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OWSLinkPreview+Conversion.swift"; sourceTree = ""; }; B8569AC225CB5D2900DBA3DB /* ConversationVC+Interaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ConversationVC+Interaction.swift"; sourceTree = ""; }; - B8569AD225CBA13D00DBA3DB /* MediaTextOverlayView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediaTextOverlayView.swift; sourceTree = ""; }; B8569AE225CBB19A00DBA3DB /* DocumentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentView.swift; sourceTree = ""; }; B86BD08323399ACF000F5AE3 /* Modal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Modal.swift; sourceTree = ""; }; B86BD08523399CEF000F5AE3 /* SeedModal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedModal.swift; sourceTree = ""; }; @@ -2175,7 +2173,6 @@ isa = PBXGroup; children = ( 34A8B3502190A40E00218A25 /* MediaAlbumView.swift */, - B8569AD225CBA13D00DBA3DB /* MediaTextOverlayView.swift */, 3488F9352191CC4000E524CC /* MediaView.swift */, B8041A9425C8FA1D003C2166 /* MediaLoaderView.swift */, B8F5F71925F1B35C003BF8D4 /* MediaPlaceholderView.swift */, @@ -4949,7 +4946,6 @@ 341341EF2187467A00192D59 /* ConversationViewModel.m in Sources */, 4C21D5D8223AC60F00EF8A77 /* PhotoCapture.swift in Sources */, C331FFF32558FF0300070591 /* PathStatusView.swift in Sources */, - B8569AD325CBA13D00DBA3DB /* MediaTextOverlayView.swift in Sources */, 4CC1ECFB211A553000CC13BE /* AppUpdateNag.swift in Sources */, B848A4C5269EAAA200617031 /* UserDetailsSheet.swift in Sources */, 34B6A903218B3F63007C4606 /* TypingIndicatorView.swift in Sources */, diff --git a/Session/Conversations/ConversationVC+Interaction.swift b/Session/Conversations/ConversationVC+Interaction.swift index cc41b44e4..7e3aec685 100644 --- a/Session/Conversations/ConversationVC+Interaction.swift +++ b/Session/Conversations/ConversationVC+Interaction.swift @@ -544,14 +544,7 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc } else { guard let albumView = cell.albumView else { return } let locationInCell = gestureRecognizer.location(in: cell) - // Figure out whether the "read more" button was tapped - if let overlayView = cell.mediaTextOverlayView { - let locationInOverlayView = cell.convert(locationInCell, to: overlayView) - if let readMoreButton = overlayView.readMoreButton, readMoreButton.frame.contains(locationInOverlayView) { - return showFullText(viewItem) // HACK: This is a dirty way to do this - } - } - // Otherwise, figure out which of the media views was tapped + // Figure out which of the media views was tapped let locationInAlbumView = cell.convert(locationInCell, to: albumView) guard let mediaView = albumView.mediaView(forLocation: locationInAlbumView) else { return } if albumView.isMoreItemsView(mediaView: mediaView) && viewItem.mediaAlbumHasFailedAttachment() { diff --git a/Session/Conversations/Message Cells/Content Views/MediaTextOverlayView.swift b/Session/Conversations/Message Cells/Content Views/MediaTextOverlayView.swift deleted file mode 100644 index a0c655a5f..000000000 --- a/Session/Conversations/Message Cells/Content Views/MediaTextOverlayView.swift +++ /dev/null @@ -1,74 +0,0 @@ -import UIKit - -/// Shown over a media message if it has a message body. -final class MediaTextOverlayView : UIView { - private let viewItem: ConversationViewItem - private let albumViewWidth: CGFloat - private let delegate: MessageCellDelegate - private let textColor: UIColor - var readMoreButton: UIButton? - - // MARK: Settings - private static let maxHeight: CGFloat = 88; - - // MARK: Lifecycle - init(viewItem: ConversationViewItem, albumViewWidth: CGFloat, textColor: UIColor, delegate: MessageCellDelegate) { - self.viewItem = viewItem - self.albumViewWidth = albumViewWidth - self.delegate = delegate - self.textColor = textColor - super.init(frame: CGRect.zero) - setUpViewHierarchy() - } - - override init(frame: CGRect) { - preconditionFailure("Use init(text:) instead.") - } - - required init?(coder: NSCoder) { - preconditionFailure("Use init(text:) instead.") - } - - private func setUpViewHierarchy() { - guard let message = viewItem.interaction as? TSMessage, let body = message.body, body.count > 0 else { return } - // Body label - let bodyLabel = UILabel() - bodyLabel.numberOfLines = 0 - bodyLabel.lineBreakMode = .byTruncatingTail - bodyLabel.text = given(body) { MentionUtilities.highlightMentions(in: $0, threadID: viewItem.interaction.uniqueThreadId) } - bodyLabel.textColor = self.textColor - bodyLabel.font = .systemFont(ofSize: Values.mediumFontSize) - // Content stack view - let contentStackView = UIStackView(arrangedSubviews: [ bodyLabel ]) - contentStackView.axis = .horizontal - contentStackView.spacing = Values.smallSpacing - addSubview(contentStackView) - let inset: CGFloat = 12 - contentStackView.pin(.left, to: .left, of: self, withInset: inset) - contentStackView.pin(.top, to: .top, of: self) - contentStackView.pin(.right, to: .right, of: self, withInset: -inset) - // Max height - bodyLabel.heightAnchor.constraint(lessThanOrEqualToConstant: MediaTextOverlayView.maxHeight).isActive = true - // Overflow button - let bodyLabelTargetSize = bodyLabel.sizeThatFits(CGSize(width: albumViewWidth - 2 * inset, height: .greatestFiniteMagnitude)) - if bodyLabelTargetSize.height > MediaTextOverlayView.maxHeight { - let readMoreButton = UIButton() - self.readMoreButton = readMoreButton - readMoreButton.setTitle("Read More", for: UIControl.State.normal) - readMoreButton.titleLabel!.font = .boldSystemFont(ofSize: Values.smallFontSize) - readMoreButton.setTitleColor(self.textColor, for: UIControl.State.normal) - readMoreButton.addTarget(self, action: #selector(readMore), for: UIControl.Event.touchUpInside) - addSubview(readMoreButton) - readMoreButton.pin(.left, to: .left, of: self, withInset: inset) - readMoreButton.pin(.top, to: .bottom, of: contentStackView, withInset: Values.smallSpacing) - readMoreButton.pin(.bottom, to: .bottom, of: self, withInset: -Values.smallSpacing) - } else { - contentStackView.pin(.bottom, to: .bottom, of: self, withInset: -inset) - } - } - - // MARK: Interaction - @objc private func readMore() { - delegate.showFullText(viewItem) - } -} diff --git a/Session/Conversations/Message Cells/VisibleMessageCell.swift b/Session/Conversations/Message Cells/VisibleMessageCell.swift index 22e4f8a2a..a707858e3 100644 --- a/Session/Conversations/Message Cells/VisibleMessageCell.swift +++ b/Session/Conversations/Message Cells/VisibleMessageCell.swift @@ -4,7 +4,6 @@ final class VisibleMessageCell : MessageCell, LinkPreviewViewDelegate { private var previousX: CGFloat = 0 var albumView: MediaAlbumView? var bodyTextView: UITextView? - var mediaTextOverlayView: MediaTextOverlayView? // Constraints private lazy var headerViewTopConstraint = headerView.pin(.top, to: .top, of: self, withInset: 1) private lazy var authorLabelHeightConstraint = authorLabel.set(.height, to: 0) @@ -312,7 +311,6 @@ final class VisibleMessageCell : MessageCell, LinkPreviewViewDelegate { } albumView = nil bodyTextView = nil - mediaTextOverlayView = nil let isOutgoing = (viewItem.interaction.interactionType() == .outgoingMessage) switch viewItem.messageCellType { case .textOnlyMessage: @@ -373,11 +371,12 @@ final class VisibleMessageCell : MessageCell, LinkPreviewViewDelegate { albumView.layer.mask = bubbleViewMaskLayer stackView.addArrangedSubview(albumView) // Body text view - if let message = viewItem.interaction as? TSMessage, let body = message.body, body.count > 0, - let delegate = delegate { // delegate should always be set at this point - let overlayView = MediaTextOverlayView(viewItem: viewItem, albumViewWidth: size.width, textColor: bodyLabelTextColor, delegate: delegate) - self.mediaTextOverlayView = overlayView - stackView.addArrangedSubview(overlayView) + if let message = viewItem.interaction as? TSMessage, let body = message.body, body.count > 0 { + let inset: CGFloat = 12 + let maxWidth = size.width - 2 * inset + let bodyTextView = VisibleMessageCell.getBodyTextView(for: viewItem, with: maxWidth, textColor: bodyLabelTextColor, searchText: delegate?.lastSearchedText, delegate: self) + self.bodyTextView = bodyTextView + stackView.addArrangedSubview(UIView(wrapping: bodyTextView, withInsets: UIEdgeInsets(top: 0, left: inset, bottom: inset, right: inset))) } unloadContent = { albumView.unloadMedia() } // Constraints