Pills performance improvements (#1891)

* big performance improvement for pills

* small overall performance improvement

* code improvements
This commit is contained in:
Mauro
2023-10-13 14:40:18 +02:00
committed by GitHub
parent f97a7c8cce
commit 8e03b89641
6 changed files with 50 additions and 58 deletions
+18 -14
View File
@@ -19,6 +19,7 @@ import SwiftUI
final class MessageTextView: UITextView, PillAttachmentViewProviderDelegate {
var roomContext: RoomScreenViewModel.Context?
var updateClosure: (() -> Void)?
private var pillViews = NSHashTable<UIView>.weakObjects()
// This prevents the magnifying glass from showing up
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
@@ -38,21 +39,23 @@ final class MessageTextView: UITextView, PillAttachmentViewProviderDelegate {
}
}
}
// Required to setup the first rendering of the pill view
override func layoutSubviews() {
invalidateTextAttachmentsDisplay(update: false)
super.layoutSubviews()
}
func registerPillView(_ pillView: UIView) {
// No need to be implemented in this view
pillViews.add(pillView)
}
func flushPills() {
for view in pillViews.allObjects {
view.alpha = 0.0
view.removeFromSuperview()
}
pillViews.removeAllObjects()
}
}
struct MessageText: UIViewRepresentable {
@Environment(\.openURL) private var openURLAction: OpenURLAction
@EnvironmentObject private var viewModel: RoomScreenViewModel.Context
@Environment(\.openURL) private var openURLAction
@Environment(\.roomContext) private var viewModel
@State private var computedSizes = [Double: CGSize]()
@State var attributedString: AttributedString {
@@ -92,7 +95,11 @@ struct MessageText: UIViewRepresentable {
}
func updateUIView(_ uiView: MessageTextView, context: Context) {
uiView.attributedText = NSAttributedString(attributedString)
let newAttributedText = NSAttributedString(attributedString)
if uiView.attributedText != newAttributedText {
uiView.flushPills()
uiView.attributedText = newAttributedText
}
context.coordinator.openURLAction = openURLAction
}
@@ -177,7 +184,6 @@ struct MessageText_Previews: PreviewProvider, TestablePreview {
MessageText(attributedString: attributedString)
.border(Color.purple)
.previewDisplayName("Custom Text")
.environmentObject(RoomScreenViewModel.mock.context)
// For comparison
Text(attributedString)
.border(Color.purple)
@@ -188,13 +194,11 @@ struct MessageText_Previews: PreviewProvider, TestablePreview {
MessageText(attributedString: attributedString)
.border(Color.purple)
.previewDisplayName("With block quote")
.environmentObject(RoomScreenViewModel.mock.context)
}
if let attributedString = attributedStringBuilder.fromHTML(htmlStringWithList) {
MessageText(attributedString: attributedString)
.border(Color.purple)
.previewDisplayName("With list")
.environmentObject(RoomScreenViewModel.mock.context)
}
}
}