diff --git a/Riot/Modules/Pills/PillAttachmentView.swift b/Riot/Modules/Pills/PillAttachmentView.swift index ec6809a0e..8e88906f5 100644 --- a/Riot/Modules/Pills/PillAttachmentView.swift +++ b/Riot/Modules/Pills/PillAttachmentView.swift @@ -49,14 +49,18 @@ class PillAttachmentView: UIView { } // MARK: - Init - convenience init(withRoomMember roomMember: MXRoomMember, isCurrentUser: Bool) { + /// Create a Mention Pill view for given room member. + /// + /// - Parameters: + /// - roomMember: the room member + /// - isHighlighted: whether this pill should be highlighted + convenience init(withRoomMember roomMember: MXRoomMember, isHighlighted: Bool) { self.init(frame: CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: Self.size(forRoomMember: roomMember))) - let label = UILabel(frame: .zero) label.text = roomMember.displayname.count > 0 ? roomMember.displayname : roomMember.userId label.font = Constants.pillLabelFont - label.textColor = isCurrentUser ? ThemeService.shared().theme.baseTextPrimaryColor : ThemeService.shared().theme.textPrimaryColor + label.textColor = isHighlighted ? ThemeService.shared().theme.baseTextPrimaryColor : ThemeService.shared().theme.textPrimaryColor let labelSize = label.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)) label.frame = CGRect(x: Constants.displaynameLabelLeading, @@ -88,7 +92,7 @@ class PillAttachmentView: UIView { pillBackgroundView.addSubview(imageView) pillBackgroundView.addSubview(label) - pillBackgroundView.backgroundColor = isCurrentUser ? ThemeService.shared().theme.colors.alert : ThemeService.shared().theme.colors.quinaryContent + pillBackgroundView.backgroundColor = isHighlighted ? ThemeService.shared().theme.colors.alert : ThemeService.shared().theme.colors.quinaryContent pillBackgroundView.layer.cornerRadius = Constants.pillBackgroundHeight / 2.0 self.addSubview(pillBackgroundView) diff --git a/Riot/Modules/Pills/PillAttachmentViewProvider.swift b/Riot/Modules/Pills/PillAttachmentViewProvider.swift index ac8360dea..c21432823 100644 --- a/Riot/Modules/Pills/PillAttachmentViewProvider.swift +++ b/Riot/Modules/Pills/PillAttachmentViewProvider.swift @@ -33,7 +33,7 @@ import UIKit } view = PillAttachmentView(withRoomMember: roomMember, - isCurrentUser: textAttachment.isCurrentUser) + isHighlighted: textAttachment.isHighlighted) view?.alpha = textAttachment.alpha } } diff --git a/Riot/Modules/Pills/PillTextAttachment.swift b/Riot/Modules/Pills/PillTextAttachment.swift index 3469cb17f..0a16bf48a 100644 --- a/Riot/Modules/Pills/PillTextAttachment.swift +++ b/Riot/Modules/Pills/PillTextAttachment.swift @@ -23,13 +23,13 @@ import MatrixSDK class PillTextAttachment: NSTextAttachment { // MARK: - Internal Properties var roomMember: MXRoomMember? - var isCurrentUser: Bool = false + var isHighlighted: Bool = false var alpha: CGFloat = 1.0 // MARK: - Constants private enum Constants { static let roomMemberKey: String = "roomMember" - static let isCurrentUserKey: String = "isCurrentUser" + static let isHighlightedKey: String = "isHighlighted" static let alphaKey: String = "alpha" static let pillVerticalOffset: CGFloat = -7.5 } @@ -39,10 +39,15 @@ class PillTextAttachment: NSTextAttachment { super.init(data: contentData, ofType: uti) } - init(withRoomMember roomMember: MXRoomMember, isCurrentUser: Bool) { + /// Create a Mention Pill text attachment for given room member. + /// + /// - Parameters: + /// - roomMember: the room member + /// - isHighlighted: whether this pill should be highlighted + init(withRoomMember roomMember: MXRoomMember, isHighlighted: Bool) { super.init(data: nil, ofType: StringPillsUtils.pillUTType) self.roomMember = roomMember - self.isCurrentUser = isCurrentUser + self.isHighlighted = isHighlighted let pillSize = PillAttachmentView.size(forRoomMember: roomMember) self.bounds = CGRect(origin: CGPoint(x: 0.0, y: Constants.pillVerticalOffset), size: pillSize) } @@ -57,7 +62,7 @@ class PillTextAttachment: NSTextAttachment { self.fileType = StringPillsUtils.pillUTType self.roomMember = roomMember - self.isCurrentUser = coder.decodeBool(forKey: Constants.isCurrentUserKey) + self.isHighlighted = coder.decodeBool(forKey: Constants.isHighlightedKey) self.alpha = CGFloat(coder.decodeFloat(forKey: Constants.alphaKey)) let pillSize = PillAttachmentView.size(forRoomMember: roomMember) @@ -68,7 +73,7 @@ class PillTextAttachment: NSTextAttachment { super.encode(with: coder) coder.encode(roomMember, forKey: Constants.roomMemberKey) - coder.encode(isCurrentUser, forKey: Constants.isCurrentUserKey) + coder.encode(isHighlighted, forKey: Constants.isHighlightedKey) coder.encode(Float(alpha), forKey: Constants.alphaKey) } } diff --git a/Riot/Modules/Pills/StringPillsUtils.swift b/Riot/Modules/Pills/StringPillsUtils.swift index 6240f03f5..183f7f5ee 100644 --- a/Riot/Modules/Pills/StringPillsUtils.swift +++ b/Riot/Modules/Pills/StringPillsUtils.swift @@ -46,10 +46,10 @@ class StringPillsUtils: NSObject { newAttr.vc_enumerateAttribute(.link, in: totalRange) { (url: URL, range: NSRange, _) in if let userId = userIdFromPermalink(url.absoluteString), let roomMember = roomState.members.member(withUserId: userId) { - let isCurrentUser = roomMember.userId == session.myUserId && event.sender != session.myUserId + let isHighlighted = roomMember.userId == session.myUserId && event.sender != session.myUserId let attachmentString = mentionPill(withRoomMember: roomMember, andUrl: isEditMode ? nil : url, - isCurrentUser: isCurrentUser) + isHighlighted: isHighlighted) newAttr.replaceCharacters(in: range, with: attachmentString) } } @@ -84,12 +84,12 @@ class StringPillsUtils: NSObject { /// - Parameters: /// - roomMember: the room member /// - url: URL to room member profile. Should be provided to make pill act as a link. - /// - isCurrentUser: true to indicate that the room member is the current user + /// - isHighlighted: true to indicate that the pill should be highlighted /// - Returns: attributed string with a pill attachment and an optional link static func mentionPill(withRoomMember roomMember: MXRoomMember, andUrl url: URL? = nil, - isCurrentUser: Bool) -> NSAttributedString { - let attachment = PillTextAttachment(withRoomMember: roomMember, isCurrentUser: isCurrentUser) + isHighlighted: Bool) -> NSAttributedString { + let attachment = PillTextAttachment(withRoomMember: roomMember, isHighlighted: isHighlighted) let string = NSMutableAttributedString(attachment: attachment) string.addAttribute(.font, value: UIFont.systemFont(ofSize: 15.0), range: .init(location: 0, length: string.length)) if let url = url { diff --git a/Riot/Modules/Room/RoomViewController.swift b/Riot/Modules/Room/RoomViewController.swift index 0aeeb615f..cec9e9b0f 100644 --- a/Riot/Modules/Room/RoomViewController.swift +++ b/Riot/Modules/Room/RoomViewController.swift @@ -28,7 +28,7 @@ extension RoomViewController { if inputToolbar.attributedTextMessage.length > 0 { if #available(iOS 15.0, *) { newAttributedString.append(StringPillsUtils.mentionPill(withRoomMember: roomMember, - isCurrentUser: false)) + isHighlighted: false)) } else { newAttributedString.appendString(roomMember.displayname.count > 0 ? roomMember.displayname : roomMember.userId) } @@ -42,7 +42,7 @@ extension RoomViewController { } else { if #available(iOS 15.0, *) { newAttributedString.append(StringPillsUtils.mentionPill(withRoomMember: roomMember, - isCurrentUser: false)) + isHighlighted: false)) } else { newAttributedString.appendString(roomMember.displayname.count > 0 ? roomMember.displayname : roomMember.userId) } diff --git a/RiotTests/StringPillsUtilsTests.swift b/RiotTests/StringPillsUtilsTests.swift index ebc630b5a..3c88bcd2e 100644 --- a/RiotTests/StringPillsUtilsTests.swift +++ b/RiotTests/StringPillsUtilsTests.swift @@ -37,7 +37,7 @@ class StringPillsUtilsTests: XCTestCase { XCTAssert(messageWithPills.attribute(.attachment, at: messageWithPills.length - 1, effectiveRange: nil) is PillTextAttachment) let pillTextAttachment = messageWithPills.attribute(.attachment, at: messageWithPills.length - 1, effectiveRange: nil) as? PillTextAttachment - XCTAssert(pillTextAttachment?.isCurrentUser == true) // Alice is highlighted + XCTAssert(pillTextAttachment?.isHighlighted == true) // Alice is highlighted XCTAssert(pillTextAttachment?.fileType == StringPillsUtils.pillUTType) }