Entity input: improved animation cache and rendering

This commit is contained in:
Ali
2022-06-24 02:06:02 +01:00
parent 0f1b382265
commit c112bc5146
37 changed files with 2557 additions and 383 deletions

View File

@@ -12,7 +12,7 @@ private let typeHintFileIsLarge: Int32 = 7
private let typeHintIsValidated: Int32 = 8
private let typeNoPremium: Int32 = 9
public enum StickerPackReference: PostboxCoding, Hashable, Equatable {
public enum StickerPackReference: PostboxCoding, Hashable, Equatable, Codable {
case id(id: Int64, accessHash: Int64)
case name(String)
case animatedEmoji
@@ -37,6 +37,27 @@ public enum StickerPackReference: PostboxCoding, Hashable, Equatable {
}
}
public init(decoder: Decoder) throws {
let container = try decoder.container(keyedBy: StringCodingKey.self)
let discriminator = try container.decode(Int32.self, forKey: "r")
switch discriminator {
case 0:
self = .id(id: try container.decode(Int64.self, forKey: "i"), accessHash: try container.decode(Int64.self, forKey: "h"))
case 1:
self = .name(try container.decode(String.self, forKey: "n"))
case 2:
self = .animatedEmoji
case 3:
self = .dice((try? container.decode(String.self, forKey: "e")) ?? "🎲")
case 4:
self = .animatedEmojiAnimations
default:
self = .name("")
assertionFailure()
}
}
public func encode(_ encoder: PostboxEncoder) {
switch self {
case let .id(id, accessHash):
@@ -56,6 +77,27 @@ public enum StickerPackReference: PostboxCoding, Hashable, Equatable {
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: StringCodingKey.self)
switch self {
case let .id(id, accessHash):
try container.encode(0 as Int32, forKey: "r")
try container.encode(id, forKey: "i")
try container.encode(accessHash, forKey: "h")
case let .name(name):
try container.encode(1 as Int32, forKey: "r")
try container.encode(name, forKey: "n")
case .animatedEmoji:
try container.encode(2 as Int32, forKey: "r")
case let .dice(emoji):
try container.encode(3 as Int32, forKey: "r")
try container.encode(emoji, forKey: "e")
case .animatedEmojiAnimations:
try container.encode(4 as Int32, forKey: "r")
}
}
public static func ==(lhs: StickerPackReference, rhs: StickerPackReference) -> Bool {
switch lhs {
case let .id(id, accessHash):

View File

@@ -21,7 +21,7 @@ public enum MessageTextEntityType: Equatable {
case Underline
case BankCard
case Spoiler
case AnimatedEmoji(MediaId?)
case CustomEmoji(stickerPack: StickerPackReference, fileId: Int64)
case Custom(type: CustomEntityType)
}
@@ -73,7 +73,11 @@ public struct MessageTextEntity: PostboxCoding, Codable, Equatable {
case 17:
self.type = .Spoiler
case 18:
self.type = .AnimatedEmoji(decoder.decodeObjectForKey("mediaId") as? MediaId)
if let stickerPack = decoder.decodeObjectForKey("s", decoder: { StickerPackReference(decoder: $0) }) as? StickerPackReference {
self.type = .CustomEmoji(stickerPack: stickerPack, fileId: decoder.decodeInt64ForKey("f", orElse: 0))
} else {
self.type = .Unknown
}
case Int32.max:
self.type = .Custom(type: decoder.decodeInt32ForKey("type", orElse: 0))
default:
@@ -130,7 +134,7 @@ public struct MessageTextEntity: PostboxCoding, Codable, Equatable {
case 17:
self.type = .Spoiler
case 18:
self.type = .AnimatedEmoji(try? container.decode(MediaId.self, forKey: "mediaId"))
self.type = .CustomEmoji(stickerPack: try container.decode(StickerPackReference.self, forKey: "s"), fileId: try container.decode(Int64.self, forKey: "f"))
case Int32.max:
let customType: Int32 = (try? container.decode(Int32.self, forKey: "type")) ?? 0
self.type = .Custom(type: customType)
@@ -181,13 +185,10 @@ public struct MessageTextEntity: PostboxCoding, Codable, Equatable {
encoder.encodeInt32(16, forKey: "_rawValue")
case .Spoiler:
encoder.encodeInt32(17, forKey: "_rawValue")
case let .AnimatedEmoji(mediaId):
case let .CustomEmoji(stickerPack, fileId):
encoder.encodeInt32(18, forKey: "_rawValue")
if let mediaId = mediaId {
encoder.encodeObject(mediaId, forKey: "mediaId")
} else {
encoder.encodeNil(forKey: "mediaId")
}
encoder.encodeObject(stickerPack, forKey: "s")
encoder.encodeInt64(fileId, forKey: "f")
case let .Custom(type):
encoder.encodeInt32(Int32.max, forKey: "_rawValue")
encoder.encodeInt32(type, forKey: "type")
@@ -238,11 +239,10 @@ public struct MessageTextEntity: PostboxCoding, Codable, Equatable {
try container.encode(16 as Int32, forKey: "_rawValue")
case .Spoiler:
try container.encode(17 as Int32, forKey: "_rawValue")
case let .AnimatedEmoji(mediaId):
case let .CustomEmoji(stickerPack, fileId):
try container.encode(18 as Int32, forKey: "_rawValue")
if let mediaId = mediaId {
try container.encode(mediaId, forKey: "mediaId")
}
try container.encode(stickerPack, forKey: "s")
try container.encode(fileId, forKey: "f")
case let .Custom(type):
try container.encode(Int32.max as Int32, forKey: "_rawValue")
try container.encode(type as Int32, forKey: "type")