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
@@ -24,43 +24,22 @@ enum PillType: Codable, Equatable {
case allUsers
}
struct PillTextAttachmentData {
// MARK: - Properties
struct PillTextAttachmentData: Codable, Equatable {
struct Font: Codable, Equatable {
let descender: CGFloat
let lineHeight: CGFloat
}
/// Pill type
let type: PillType
/// Font for the display name
let font: UIFont
let fontData: Font
}
extension PillTextAttachmentData: Codable {
// MARK: - Codable
enum CodingKeys: String, CodingKey {
case type
case font
}
enum PillTextAttachmentDataError: Error {
case noFontData
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
type = try container.decode(PillType.self, forKey: .type)
let fontData = try container.decode(Data.self, forKey: .font)
if let font = try NSKeyedUnarchiver.unarchivedObject(ofClass: UIFont.self, from: fontData) {
self.font = font
} else {
throw PillTextAttachmentDataError.noFontData
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(type, forKey: .type)
let fontData = try NSKeyedArchiver.archivedData(withRootObject: font, requiringSecureCoding: false)
try container.encode(fontData, forKey: .font)
extension PillTextAttachmentData {
init(type: PillType, font: UIFont) {
self.type = type
fontData = Font(descender: font.descender, lineHeight: font.lineHeight)
}
}