diff --git a/submodules/TelegramCore/Sources/Statistics/PeerStatistics.swift b/submodules/TelegramCore/Sources/Statistics/PeerStatistics.swift index c0a080c056..8bb3a41bee 100644 --- a/submodules/TelegramCore/Sources/Statistics/PeerStatistics.swift +++ b/submodules/TelegramCore/Sources/Statistics/PeerStatistics.swift @@ -19,7 +19,21 @@ public struct StatsPercentValue: Equatable { public let total: Double } -public enum StatsGraph: Equatable { +public enum StatsGraph: Equatable, Codable { + private enum CodingKeys: String, CodingKey { + case type + case token + case error + case data + } + + private enum TypeKey: Int32 { + case onDemand + case failed + case loaded + case empty + } + case OnDemand(token: String) case Failed(error: String) case Loaded(token: String?, data: String) @@ -46,6 +60,41 @@ public enum StatsGraph: Equatable { return nil } } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + + let type = TypeKey(rawValue: try container.decode(Int32.self, forKey: .type)) ?? .empty + switch type { + case .onDemand: + self = .OnDemand(token: try container.decode(String.self, forKey: .token)) + case .failed: + self = .Failed(error: try container.decode(String.self, forKey: .error)) + case .loaded: + self = .Loaded(token: try container.decodeIfPresent(String.self, forKey: .token), data: try container.decode(String.self, forKey: .data)) + case .empty: + self = .Empty + } + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + + switch self { + case let .OnDemand(token): + try container.encode(TypeKey.empty.rawValue, forKey: .type) + try container.encode(token, forKey: .token) + case let .Failed(error): + try container.encode(TypeKey.failed.rawValue, forKey: .type) + try container.encode(error, forKey: .error) + case let .Loaded(token, data): + try container.encode(TypeKey.loaded.rawValue, forKey: .type) + try container.encodeIfPresent(token, forKey: .token) + try container.encode(data, forKey: .data) + case .Empty: + try container.encode(TypeKey.empty.rawValue, forKey: .type) + } + } } public struct ChannelStatsPostInteractions: Equatable {