Merge pull request #6152 from vector-im/aringenbach/clear_pills_artifacts

Flush pills on text view updates
This commit is contained in:
ismailgulek 2022-05-16 11:30:51 +03:00 committed by GitHub
commit 5da4330732
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 7 deletions

View file

@ -26,6 +26,14 @@ NS_ASSUME_NONNULL_BEGIN
// The last hit test location received by the view.
@property (nonatomic, readonly) CGPoint lastHitTestLocation;
/// Register a view that has been added as a pill to this text view.
/// This is needed in order to flush pills that are not always removed properly by the system.
/// All registered views will be manually removed from hierarchy on attributedText or text updates.
///
/// @param pillView pill view to register
- (void)registerPillView:(UIView *)pillView API_AVAILABLE(ios(15));
@end
NS_ASSUME_NONNULL_END

View file

@ -20,6 +20,7 @@
@interface MXKMessageTextView()
@property (nonatomic, readwrite) CGPoint lastHitTestLocation;
@property (nonatomic) NSHashTable *pillViews;
@end
@ -54,4 +55,38 @@
return [self isThereALinkNearLocation:point];
}
#pragma mark - Pills Flushing
- (void)setText:(NSString *)text
{
if (@available(iOS 15.0, *)) {
[self flushPills];
}
[super setText:text];
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
if (@available(iOS 15.0, *)) {
[self flushPills];
}
[super setAttributedText:attributedText];
}
- (void)registerPillView:(UIView *)pillView
{
[self.pillViews addObject:pillView];
}
/// Flushes all previously registered Pills from their hierarchy.
- (void)flushPills API_AVAILABLE(ios(15))
{
for (UIView* view in self.pillViews)
{
view.alpha = 0.0;
[view removeFromSuperview];
}
self.pillViews = [NSHashTable weakObjectsHashTable];
}
@end

View file

@ -90,6 +90,7 @@ class PillAttachmentView: UIView {
pillBackgroundView.layer.cornerRadius = sizes.pillBackgroundHeight / 2.0
self.addSubview(pillBackgroundView)
self.alpha = pillData.alpha
}
// MARK: - Override

View file

@ -19,9 +19,18 @@ import UIKit
/// Provider for mention Pills attachment view.
@available(iOS 15.0, *)
@objc class PillAttachmentViewProvider: NSTextAttachmentViewProvider {
// MARK: - Properties
private static let pillAttachmentViewSizes = PillAttachmentView.Sizes(verticalMargin: 2.0,
horizontalMargin: 4.0,
avatarSideLength: 16.0)
private weak var messageTextView: MXKMessageTextView?
// MARK: - Override
override init(textAttachment: NSTextAttachment, parentView: UIView?, textLayoutManager: NSTextLayoutManager?, location: NSTextLocation) {
super.init(textAttachment: textAttachment, parentView: parentView, textLayoutManager: textLayoutManager, location: location)
self.messageTextView = parentView?.superview as? MXKMessageTextView
}
override func loadView() {
super.loadView()
@ -38,13 +47,14 @@ import UIKit
let mainSession = AppDelegate.theDelegate().mxSessions.first as? MXSession
view = PillAttachmentView(frame: CGRect(origin: .zero, size: Self.size(forDisplayText: pillData.displayText,
andFont: pillData.font)),
sizes: Self.pillAttachmentViewSizes,
theme: ThemeService.shared().theme,
mediaManager: mainSession?.mediaManager,
andPillData: pillData)
view?.alpha = pillData.alpha
let pillView = PillAttachmentView(frame: CGRect(origin: .zero, size: Self.size(forDisplayText: pillData.displayText,
andFont: pillData.font)),
sizes: Self.pillAttachmentViewSizes,
theme: ThemeService.shared().theme,
mediaManager: mainSession?.mediaManager,
andPillData: pillData)
view = pillView
messageTextView?.registerPillView(pillView)
}
}