mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-24 07:05:35 +00:00
Refactoring
This commit is contained in:
@@ -8,12 +8,12 @@ public final class SynchronizeChatInputStateOperation: PostboxCoding {
|
||||
}
|
||||
|
||||
public init(decoder: PostboxDecoder) {
|
||||
self.previousState = decoder.decodeObjectForKey("p", decoder: { SynchronizeableChatInputState(decoder: $0) }) as? SynchronizeableChatInputState
|
||||
self.previousState = decoder.decode(SynchronizeableChatInputState.self, forKey: "p")
|
||||
}
|
||||
|
||||
public func encode(_ encoder: PostboxEncoder) {
|
||||
if let previousState = self.previousState {
|
||||
encoder.encodeObject(previousState, forKey: "p")
|
||||
encoder.encode(previousState, forKey: "p")
|
||||
} else {
|
||||
encoder.encodeNil(forKey: "p")
|
||||
}
|
||||
|
||||
@@ -1,42 +1,49 @@
|
||||
import Foundation
|
||||
import Postbox
|
||||
|
||||
public struct SynchronizeableChatInputState: PostboxCoding, Equatable {
|
||||
public struct SynchronizeableChatInputState: Codable, Equatable {
|
||||
public let replyToMessageId: MessageId?
|
||||
public let text: String
|
||||
public let entities: [MessageTextEntity]
|
||||
public let timestamp: Int32
|
||||
public let textSelection: Range<Int>?
|
||||
|
||||
public init(replyToMessageId: MessageId?, text: String, entities: [MessageTextEntity], timestamp: Int32) {
|
||||
public init(replyToMessageId: MessageId?, text: String, entities: [MessageTextEntity], timestamp: Int32, textSelection: Range<Int>?) {
|
||||
self.replyToMessageId = replyToMessageId
|
||||
self.text = text
|
||||
self.entities = entities
|
||||
self.timestamp = timestamp
|
||||
self.textSelection = textSelection
|
||||
}
|
||||
|
||||
public init(decoder: PostboxDecoder) {
|
||||
self.text = decoder.decodeStringForKey("t", orElse: "")
|
||||
self.entities = decoder.decodeObjectArrayWithDecoderForKey("e")
|
||||
self.timestamp = decoder.decodeInt32ForKey("s", orElse: 0)
|
||||
if let messageIdPeerId = decoder.decodeOptionalInt64ForKey("m.p"), let messageIdNamespace = decoder.decodeOptionalInt32ForKey("m.n"), let messageIdId = decoder.decodeOptionalInt32ForKey("m.i") {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: StringCodingKey.self)
|
||||
self.text = (try? container.decode(String.self, forKey: "t")) ?? ""
|
||||
self.entities = (try? container.decode([MessageTextEntity].self, forKey: "e")) ?? []
|
||||
self.timestamp = (try? container.decode(Int32.self, forKey: "s")) ?? 0
|
||||
|
||||
if let messageIdPeerId = try? container.decodeIfPresent(Int64.self, forKey: "m.p"), let messageIdNamespace = try? container.decodeIfPresent(Int32.self, forKey: "m.n"), let messageIdId = try? container.decodeIfPresent(Int32.self, forKey: "m.i") {
|
||||
self.replyToMessageId = MessageId(peerId: PeerId(messageIdPeerId), namespace: messageIdNamespace, id: messageIdId)
|
||||
} else {
|
||||
self.replyToMessageId = nil
|
||||
}
|
||||
self.textSelection = nil
|
||||
}
|
||||
|
||||
public func encode(_ encoder: PostboxEncoder) {
|
||||
encoder.encodeString(self.text, forKey: "t")
|
||||
encoder.encodeObjectArray(self.entities, forKey: "e")
|
||||
encoder.encodeInt32(self.timestamp, forKey: "s")
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: StringCodingKey.self)
|
||||
|
||||
try container.encode(self.text, forKey: "t")
|
||||
try container.encode(self.entities, forKey: "e")
|
||||
try container.encode(self.timestamp, forKey: "s")
|
||||
if let replyToMessageId = self.replyToMessageId {
|
||||
encoder.encodeInt64(replyToMessageId.peerId.toInt64(), forKey: "m.p")
|
||||
encoder.encodeInt32(replyToMessageId.namespace, forKey: "m.n")
|
||||
encoder.encodeInt32(replyToMessageId.id, forKey: "m.i")
|
||||
try container.encode(replyToMessageId.peerId.toInt64(), forKey: "m.p")
|
||||
try container.encode(replyToMessageId.namespace, forKey: "m.n")
|
||||
try container.encode(replyToMessageId.id, forKey: "m.i")
|
||||
} else {
|
||||
encoder.encodeNil(forKey: "m.p")
|
||||
encoder.encodeNil(forKey: "m.n")
|
||||
encoder.encodeNil(forKey: "m.i")
|
||||
try container.encodeNil(forKey: "m.p")
|
||||
try container.encodeNil(forKey: "m.n")
|
||||
try container.encodeNil(forKey: "m.i")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,11 +60,46 @@ public struct SynchronizeableChatInputState: PostboxCoding, Equatable {
|
||||
if lhs.timestamp != rhs.timestamp {
|
||||
return false
|
||||
}
|
||||
if lhs.textSelection != rhs.textSelection {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
public protocol SynchronizeableChatInterfaceState: PeerChatInterfaceState {
|
||||
var synchronizeableInputState: SynchronizeableChatInputState? { get }
|
||||
func withUpdatedSynchronizeableInputState(_ state: SynchronizeableChatInputState?) -> SynchronizeableChatInterfaceState
|
||||
class InternalChatInterfaceState: Codable {
|
||||
let synchronizeableInputState: SynchronizeableChatInputState?
|
||||
let historyScrollMessageIndex: MessageIndex?
|
||||
let opaqueData: Data?
|
||||
|
||||
init(
|
||||
synchronizeableInputState: SynchronizeableChatInputState?,
|
||||
historyScrollMessageIndex: MessageIndex?,
|
||||
opaqueData: Data?
|
||||
) {
|
||||
self.synchronizeableInputState = synchronizeableInputState
|
||||
self.historyScrollMessageIndex = historyScrollMessageIndex
|
||||
self.opaqueData = opaqueData
|
||||
}
|
||||
}
|
||||
|
||||
func _internal_updateChatInputState(transaction: Transaction, peerId: PeerId, inputState: SynchronizeableChatInputState?) {
|
||||
var previousState: InternalChatInterfaceState?
|
||||
if let peerChatInterfaceState = transaction.getPeerChatInterfaceState(peerId), let data = peerChatInterfaceState.data {
|
||||
previousState = (try? AdaptedPostboxDecoder().decode(InternalChatInterfaceState.self, from: data))
|
||||
}
|
||||
|
||||
if let updatedStateData = try? AdaptedPostboxEncoder().encode(InternalChatInterfaceState(
|
||||
synchronizeableInputState: inputState,
|
||||
historyScrollMessageIndex: previousState?.historyScrollMessageIndex,
|
||||
opaqueData: previousState?.opaqueData
|
||||
)) {
|
||||
let storedState = StoredPeerChatInterfaceState(
|
||||
overrideChatTimestamp: inputState?.timestamp,
|
||||
historyScrollMessageIndex: previousState?.historyScrollMessageIndex,
|
||||
associatedMessageIds: (inputState?.replyToMessageId).flatMap({ [$0] }) ?? [],
|
||||
data: updatedStateData
|
||||
)
|
||||
transaction.setPeerChatInterfaceState(peerId, state: storedState)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public enum MessageTextEntityType: Equatable {
|
||||
case Custom(type: CustomEntityType)
|
||||
}
|
||||
|
||||
public struct MessageTextEntity: PostboxCoding, Equatable {
|
||||
public struct MessageTextEntity: PostboxCoding, Codable, Equatable {
|
||||
public let range: Range<Int>
|
||||
public let type: MessageTextEntityType
|
||||
|
||||
@@ -74,6 +74,60 @@ public struct MessageTextEntity: PostboxCoding, Equatable {
|
||||
self.type = .Unknown
|
||||
}
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: StringCodingKey.self)
|
||||
|
||||
let rangeStart: Int32 = (try? container.decode(Int32.self, forKey: "start")) ?? 0
|
||||
var rangeEnd: Int32 = (try? container.decode(Int32.self, forKey: "end")) ?? 0
|
||||
rangeEnd = max(rangeEnd, rangeStart)
|
||||
|
||||
let type: Int32 = (try? container.decode(Int32.self, forKey: "_rawValue")) ?? 0
|
||||
|
||||
self.range = Int(rangeStart) ..< Int(rangeEnd)
|
||||
|
||||
switch type {
|
||||
case 1:
|
||||
self.type = .Mention
|
||||
case 2:
|
||||
self.type = .Hashtag
|
||||
case 3:
|
||||
self.type = .BotCommand
|
||||
case 4:
|
||||
self.type = .Url
|
||||
case 5:
|
||||
self.type = .Email
|
||||
case 6:
|
||||
self.type = .Bold
|
||||
case 7:
|
||||
self.type = .Italic
|
||||
case 8:
|
||||
self.type = .Code
|
||||
case 9:
|
||||
self.type = .Pre
|
||||
case 10:
|
||||
let url = (try? container.decode(String.self, forKey: "url")) ?? ""
|
||||
self.type = .TextUrl(url: url)
|
||||
case 11:
|
||||
let peerId = (try? container.decode(Int64.self, forKey: "peerId")) ?? 0
|
||||
self.type = .TextMention(peerId: PeerId(peerId))
|
||||
case 12:
|
||||
self.type = .PhoneNumber
|
||||
case 13:
|
||||
self.type = .Strikethrough
|
||||
case 14:
|
||||
self.type = .BlockQuote
|
||||
case 15:
|
||||
self.type = .Underline
|
||||
case 16:
|
||||
self.type = .BankCard
|
||||
case Int32.max:
|
||||
let customType: Int32 = (try? container.decode(Int32.self, forKey: "type")) ?? 0
|
||||
self.type = .Custom(type: customType)
|
||||
default:
|
||||
self.type = .Unknown
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(_ encoder: PostboxEncoder) {
|
||||
encoder.encodeInt32(Int32(self.range.lowerBound), forKey: "start")
|
||||
@@ -120,6 +174,54 @@ public struct MessageTextEntity: PostboxCoding, Equatable {
|
||||
encoder.encodeInt32(type, forKey: "type")
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: StringCodingKey.self)
|
||||
|
||||
try container.encode(Int32(self.range.lowerBound), forKey: "start")
|
||||
try container.encode(Int32(self.range.upperBound), forKey: "end")
|
||||
switch self.type {
|
||||
case .Unknown:
|
||||
try container.encode(0 as Int32, forKey: "_rawValue")
|
||||
case .Mention:
|
||||
try container.encode(1 as Int32, forKey: "_rawValue")
|
||||
case .Hashtag:
|
||||
try container.encode(2 as Int32, forKey: "_rawValue")
|
||||
case .BotCommand:
|
||||
try container.encode(3 as Int32, forKey: "_rawValue")
|
||||
case .Url:
|
||||
try container.encode(4 as Int32, forKey: "_rawValue")
|
||||
case .Email:
|
||||
try container.encode(5 as Int32, forKey: "_rawValue")
|
||||
case .Bold:
|
||||
try container.encode(6 as Int32, forKey: "_rawValue")
|
||||
case .Italic:
|
||||
try container.encode(7 as Int32, forKey: "_rawValue")
|
||||
case .Code:
|
||||
try container.encode(8 as Int32, forKey: "_rawValue")
|
||||
case .Pre:
|
||||
try container.encode(9 as Int32, forKey: "_rawValue")
|
||||
case let .TextUrl(url):
|
||||
try container.encode(10 as Int32, forKey: "_rawValue")
|
||||
try container.encode(url, forKey: "url")
|
||||
case let .TextMention(peerId):
|
||||
try container.encode(11 as Int32, forKey: "_rawValue")
|
||||
try container.encode(peerId.toInt64(), forKey: "peerId")
|
||||
case .PhoneNumber:
|
||||
try container.encode(12 as Int32, forKey: "_rawValue")
|
||||
case .Strikethrough:
|
||||
try container.encode(13 as Int32, forKey: "_rawValue")
|
||||
case .BlockQuote:
|
||||
try container.encode(14 as Int32, forKey: "_rawValue")
|
||||
case .Underline:
|
||||
try container.encode(15 as Int32, forKey: "_rawValue")
|
||||
case .BankCard:
|
||||
try container.encode(16 as Int32, forKey: "_rawValue")
|
||||
case let .Custom(type):
|
||||
try container.encode(Int32.max as Int32, forKey: "_rawValue")
|
||||
try container.encode(type as Int32, forKey: "type")
|
||||
}
|
||||
}
|
||||
|
||||
public static func ==(lhs: MessageTextEntity, rhs: MessageTextEntity) -> Bool {
|
||||
return lhs.range == rhs.range && lhs.type == rhs.type
|
||||
|
||||
Reference in New Issue
Block a user