From 2b89721aa0df9ccc6647dcf65f103418b0106a9b Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Wed, 18 Oct 2023 11:51:03 +0200 Subject: [PATCH] reverting some of the performance improvements --- .../Pills/PillAttachmentViewProvider.swift | 4 +--- .../Other/Pills/PillTextAttachment.swift | 18 +----------------- ElementX/Sources/Other/Pills/PillView.swift | 3 +-- 3 files changed, 3 insertions(+), 22 deletions(-) diff --git a/ElementX/Sources/Other/Pills/PillAttachmentViewProvider.swift b/ElementX/Sources/Other/Pills/PillAttachmentViewProvider.swift index 7ad0cb3b6..353a3e1d6 100644 --- a/ElementX/Sources/Other/Pills/PillAttachmentViewProvider.swift +++ b/ElementX/Sources/Other/Pills/PillAttachmentViewProvider.swift @@ -64,8 +64,7 @@ final class PillAttachmentViewProvider: NSTextAttachmentViewProvider { return } - let view = PillView(imageProvider: imageProvider, context: context) { [weak self, weak textAttachment] in - textAttachment?.invalidateLastBounds() + let view = PillView(imageProvider: imageProvider, context: context) { [weak self] in self?.delegate?.invalidateTextAttachmentsDisplay() } let controller = UIHostingController(rootView: view) @@ -73,7 +72,6 @@ final class PillAttachmentViewProvider: NSTextAttachmentViewProvider { // This allows the text view to handle it as a link controller.view.isUserInteractionEnabled = false self.view = controller.view - textAttachment.view = controller.view delegate?.registerPillView(controller.view) } } diff --git a/ElementX/Sources/Other/Pills/PillTextAttachment.swift b/ElementX/Sources/Other/Pills/PillTextAttachment.swift index 42745b1ec..666172439 100644 --- a/ElementX/Sources/Other/Pills/PillTextAttachment.swift +++ b/ElementX/Sources/Other/Pills/PillTextAttachment.swift @@ -26,29 +26,13 @@ final class PillTextAttachment: NSTextAttachment { } private(set) var pillData: PillTextAttachmentData! - weak var view: UIView? - private var lastBounds: CGRect? - - func invalidateLastBounds() { - lastBounds = nil - } override func attachmentBounds(for textContainer: NSTextContainer?, proposedLineFragment lineFrag: CGRect, glyphPosition position: CGPoint, characterIndex charIndex: Int) -> CGRect { - var rect: CGRect - if let lastBounds { - return lastBounds - } else if let view { - rect = CGRect(origin: .zero, size: view.sizeThatFits(UIView.layoutFittingExpandedSize)) - } else { - rect = super.attachmentBounds(for: textContainer, proposedLineFragment: lineFrag, glyphPosition: position, characterIndex: charIndex) - } + var rect = super.attachmentBounds(for: textContainer, proposedLineFragment: lineFrag, glyphPosition: position, characterIndex: charIndex) let fontData = pillData.fontData // Align the pill text vertically with the surrounding text. rect.origin.y = fontData.descender + (fontData.lineHeight - rect.height) / 2.0 - // Set the max width of the pill - rect.size.width = min(rect.size.width, PillConstants.maxWidth) - lastBounds = rect return rect } } diff --git a/ElementX/Sources/Other/Pills/PillView.swift b/ElementX/Sources/Other/Pills/PillView.swift index 3d7509355..c942cbf6a 100644 --- a/ElementX/Sources/Other/Pills/PillView.swift +++ b/ElementX/Sources/Other/Pills/PillView.swift @@ -34,6 +34,7 @@ struct PillView: View { .padding(.vertical, 4) // for now design has defined no color so we will just use gray .background(Capsule().foregroundColor(context.viewState.isOwnMention ? .compound.bgCriticalPrimary : .gray)) + .frame(maxWidth: PillConstants.maxWidth) .onChange(of: context.viewState.displayText) { _ in didChangeText() } @@ -53,13 +54,11 @@ struct PillView_Previews: PreviewProvider, TestablePreview { PillView(imageProvider: mockMediaProvider, context: PillContext.mock(type: .loadedUser(isOwn: false))) { } // To simulate the trimming that happens at UIKit level - .frame(maxWidth: PillConstants.maxWidth) .previewDisplayName("Loaded Long") PillView(imageProvider: mockMediaProvider, context: PillContext.mock(type: .loadedUser(isOwn: true))) { } // To simulate the trimming that happens at UIKit level .frame(maxWidth: PillConstants.maxWidth) - .previewDisplayName("Loaded Long Own") PillView(imageProvider: mockMediaProvider, context: PillContext.mock(type: .allUsers)) { } .previewDisplayName("All Users")