Fixed a bug that prevented the mentions to be shown as plain text in the room list cell (#1835)

* fix a bug that made the room list not render mentions as plain text

* better naming
This commit is contained in:
Mauro
2023-09-29 16:49:20 +02:00
committed by GitHub
parent b7c46ceac3
commit 6bc5a440b2
7 changed files with 46 additions and 32 deletions
@@ -660,7 +660,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
@objc
private func didChangeContentSizeCategory() {
AttributedStringBuilder.invalidateCache()
AttributedStringBuilder.invalidateCaches()
}
@objc
@@ -19,19 +19,29 @@ import Foundation
import LRUCache
struct AttributedStringBuilder: AttributedStringBuilderProtocol {
private let cacheKey: String
private let temporaryBlockquoteMarkingColor = UIColor.magenta
private let temporaryCodeBlockMarkingColor = UIColor.cyan
private let linkColor = UIColor.blue
private let permalinkBaseURL: URL
private let mentionBuilder: MentionBuilderProtocol
private static var cache = LRUCache<String, AttributedString>(countLimit: 1000)
private static let defaultKey = "default"
private static var caches: [String: LRUCache<String, AttributedString>] = [:]
static func invalidateCache() {
cache.removeAllValues()
static func invalidateCaches() {
caches.removeAll()
}
init(permalinkBaseURL: URL, mentionBuilder: MentionBuilderProtocol) {
static func invalidateCache(for key: String = defaultKey) {
caches[key]?.removeAllValues()
}
init(cacheKey: String = defaultKey, permalinkBaseURL: URL, mentionBuilder: MentionBuilderProtocol) {
self.cacheKey = cacheKey
if Self.caches[cacheKey] == nil {
Self.caches[cacheKey] = LRUCache<String, AttributedString>(countLimit: 1000)
}
self.permalinkBaseURL = permalinkBaseURL
self.mentionBuilder = mentionBuilder
}
@@ -41,7 +51,7 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
return nil
}
if let cached = Self.cache.value(forKey: string) {
if let cached = Self.caches[cacheKey]?.value(forKey: string) {
return cached
}
@@ -51,7 +61,7 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
removeLinkColors(mutableAttributedString)
let result = try? AttributedString(mutableAttributedString, including: \.elementX)
Self.cache.setValue(result, forKey: string)
Self.caches[cacheKey]?.setValue(result, forKey: string)
return result
}
@@ -67,7 +77,7 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
return nil
}
if let cached = Self.cache.value(forKey: htmlString) {
if let cached = Self.caches[cacheKey]?.value(forKey: htmlString) {
return cached
}
@@ -107,7 +117,7 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
removeDTCoreTextArtifacts(mutableAttributedString)
let result = try? AttributedString(mutableAttributedString, including: \.elementX)
Self.cache.setValue(result, forKey: htmlString)
Self.caches[cacheKey]?.setValue(result, forKey: htmlString)
return result
}
@@ -0,0 +1,24 @@
//
// Copyright 2023 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
// In the future we might use this to do some customisation in what is plain text used to represent mentions.
struct PlainMentionBuilder: MentionBuilderProtocol {
func handleAllUsersMention(for attributedString: NSMutableAttributedString, in range: NSRange) { }
func handleUserMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, userID: String) { }
}
@@ -443,8 +443,9 @@ class ClientProxy: ClientProxyProtocol {
.finish()
let roomListService = syncService.roomListService()
let roomMessageEventStringBuilder = RoomMessageEventStringBuilder(attributedStringBuilder: AttributedStringBuilder(permalinkBaseURL: appSettings.permalinkBaseURL,
mentionBuilder: MentionBuilder(mentionsEnabled: appSettings.mentionsEnabled)))
let roomMessageEventStringBuilder = RoomMessageEventStringBuilder(attributedStringBuilder: AttributedStringBuilder(cacheKey: "roomList",
permalinkBaseURL: appSettings.permalinkBaseURL,
mentionBuilder: PlainMentionBuilder()))
let eventStringBuilder = RoomEventStringBuilder(stateEventStringBuilder: RoomStateEventStringBuilder(userID: userID),
messageEventStringBuilder: roomMessageEventStringBuilder)
roomSummaryProvider = RoomSummaryProvider(roomListService: roomListService,