mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 16:10:20 +00:00
Fixes #1270 - Parse the latest room message instead of directly using the body. Add an app wide attributed string caching layer.
This commit is contained in:
committed by
Stefan Ceriu
parent
56544d6604
commit
0511bef33a
@@ -16,6 +16,7 @@
|
||||
|
||||
import DTCoreText
|
||||
import Foundation
|
||||
import LRUCache
|
||||
|
||||
struct AttributedStringBuilder: AttributedStringBuilderProtocol {
|
||||
private let temporaryBlockquoteMarkingColor = UIColor.magenta
|
||||
@@ -23,6 +24,8 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
|
||||
private let linkColor = UIColor.blue
|
||||
private let permalinkBaseURL: URL
|
||||
|
||||
private static var cache = LRUCache<String, AttributedString>(countLimit: 1000)
|
||||
|
||||
init(permalinkBaseURL: URL) {
|
||||
self.permalinkBaseURL = permalinkBaseURL
|
||||
}
|
||||
@@ -31,12 +34,18 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
|
||||
guard let string else {
|
||||
return nil
|
||||
}
|
||||
|
||||
if let cached = Self.cache.value(forKey: string) {
|
||||
return cached
|
||||
}
|
||||
|
||||
let mutableAttributedString = NSMutableAttributedString(string: string)
|
||||
addLinks(mutableAttributedString)
|
||||
removeLinkColors(mutableAttributedString)
|
||||
|
||||
return try? AttributedString(mutableAttributedString, including: \.elementX)
|
||||
let result = try? AttributedString(mutableAttributedString, including: \.elementX)
|
||||
Self.cache.setValue(result, forKey: string)
|
||||
return result
|
||||
}
|
||||
|
||||
// Do not use the default HTML renderer of NSAttributedString because this method
|
||||
@@ -52,6 +61,10 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
|
||||
return nil
|
||||
}
|
||||
|
||||
if let cached = Self.cache.value(forKey: htmlString) {
|
||||
return cached
|
||||
}
|
||||
|
||||
let defaultFont = UIFont.preferredFont(forTextStyle: .body)
|
||||
|
||||
let parsingOptions: [String: Any] = [
|
||||
@@ -83,7 +96,9 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
|
||||
replaceMarkedCodeBlocks(mutableAttributedString)
|
||||
removeDTCoreTextArtifacts(mutableAttributedString)
|
||||
|
||||
return try? AttributedString(mutableAttributedString, including: \.elementX)
|
||||
let result = try? AttributedString(mutableAttributedString, including: \.elementX)
|
||||
Self.cache.setValue(result, forKey: htmlString)
|
||||
return result
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
Reference in New Issue
Block a user