Ali 2005a3a18a Merge branch 'experimental-3'
# Conflicts:
#	Telegram/Telegram-iOS/en.lproj/Localizable.strings
#	submodules/ChatListUI/Sources/ChatListControllerNode.swift
#	submodules/ChatListUI/Sources/Node/ChatListNode.swift
#	submodules/ChatListUI/Sources/Node/ChatListStorageInfoItem.swift
#	submodules/GalleryUI/Sources/SecretMediaPreviewController.swift
#	submodules/JoinLinkPreviewUI/Sources/JoinLinkPreviewController.swift
#	submodules/LegacyComponents/Sources/TGMediaPickerGalleryInterfaceView.m
#	submodules/LegacyMediaPickerUI/Sources/LegacyICloudFilePicker.swift
#	submodules/LegacyMediaPickerUI/Sources/LegacyMediaPickers.swift
#	submodules/TelegramApi/Sources/Api0.swift
#	submodules/TelegramApi/Sources/Api21.swift
#	submodules/TelegramApi/Sources/Api31.swift
#	submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift
#	submodules/TelegramCore/Sources/State/Serialization.swift
#	submodules/TelegramCore/Sources/TelegramEngine/Messages/AttachMenuBots.swift
#	submodules/TelegramUI/BUILD
#	submodules/TelegramUI/Components/LegacyMessageInputPanel/Sources/LegacyMessageInputPanel.swift
#	submodules/TelegramUI/Components/MediaEditorScreen/Sources/MediaEditorScreen.swift
#	submodules/TelegramUI/Components/MessageInputPanelComponent/BUILD
#	submodules/TelegramUI/Components/MessageInputPanelComponent/Sources/MessageInputPanelComponent.swift
#	submodules/TelegramUI/Components/Settings/NewSessionInfoScreen/Sources/NewSessionInfoContentComponent.swift
#	submodules/TelegramUI/Components/Settings/NewSessionInfoScreen/Sources/NewSessionInfoScreen.swift
#	submodules/TelegramUI/Components/Stories/StoryContainerScreen/Sources/StoryItemSetContainerComponent.swift
#	submodules/TelegramUI/Components/Stories/StoryContainerScreen/Sources/StoryItemSetContainerViewSendMessage.swift
#	submodules/TelegramUI/Sources/AccountContext.swift
#	submodules/TelegramUI/Sources/ChatController.swift
#	submodules/TelegramUI/Sources/OpenResolvedUrl.swift
#	submodules/TelegramUI/Sources/PeerInfo/PeerInfoScreen.swift
#	submodules/TelegramUI/Sources/SharedAccountContext.swift
#	submodules/WebUI/Sources/WebAppTermsAlertController.swift
#	versions.json
2023-09-08 13:32:46 +04:00

199 lines
6.9 KiB
Swift

import Foundation
import Postbox
public enum MediaArea: Codable, Equatable {
private enum CodingKeys: CodingKey {
case type
case coordinates
case value
case flags
}
public struct Coordinates: Codable, Equatable {
private enum CodingKeys: CodingKey {
case x
case y
case width
case height
case rotation
}
public var x: Double
public var y: Double
public var width: Double
public var height: Double
public var rotation: Double
public init(
x: Double,
y: Double,
width: Double,
height: Double,
rotation: Double
) {
self.x = x
self.y = y
self.width = width
self.height = height
self.rotation = rotation
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.x = try container.decode(Double.self, forKey: .x)
self.y = try container.decode(Double.self, forKey: .y)
self.width = try container.decode(Double.self, forKey: .width)
self.height = try container.decode(Double.self, forKey: .height)
self.rotation = try container.decode(Double.self, forKey: .rotation)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.x, forKey: .x)
try container.encode(self.y, forKey: .y)
try container.encode(self.width, forKey: .width)
try container.encode(self.height, forKey: .height)
try container.encode(self.rotation, forKey: .rotation)
}
}
public struct Venue: Codable, Equatable {
private enum CodingKeys: CodingKey {
case latitude
case longitude
case venue
case queryId
case resultId
}
public let latitude: Double
public let longitude: Double
public let venue: MapVenue?
public let queryId: Int64?
public let resultId: String?
public init(
latitude: Double,
longitude: Double,
venue: MapVenue?,
queryId: Int64?,
resultId: String?
) {
self.latitude = latitude
self.longitude = longitude
self.venue = venue
self.queryId = queryId
self.resultId = resultId
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.latitude = try container.decode(Double.self, forKey: .latitude)
self.longitude = try container.decode(Double.self, forKey: .longitude)
if let venueData = try container.decodeIfPresent(Data.self, forKey: .venue) {
self.venue = PostboxDecoder(buffer: MemoryBuffer(data: venueData)).decodeRootObject() as? MapVenue
} else {
self.venue = nil
}
self.queryId = try container.decodeIfPresent(Int64.self, forKey: .queryId)
self.resultId = try container.decodeIfPresent(String.self, forKey: .resultId)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.latitude, forKey: .latitude)
try container.encode(self.longitude, forKey: .longitude)
if let venue = self.venue {
let encoder = PostboxEncoder()
encoder.encodeRootObject(venue)
let venueData = encoder.makeData()
try container.encode(venueData, forKey: .venue)
}
try container.encodeIfPresent(self.queryId, forKey: .queryId)
try container.encodeIfPresent(self.resultId, forKey: .resultId)
}
}
case venue(coordinates: Coordinates, venue: Venue)
case reaction(coordinates: Coordinates, reaction: MessageReaction.Reaction, flags: ReactionFlags)
public struct ReactionFlags: OptionSet {
public var rawValue: Int32
public init(rawValue: Int32) {
self.rawValue = rawValue
}
public init() {
self.rawValue = 0
}
public static let isDark = ReactionFlags(rawValue: 1 << 0)
public static let isFlipped = ReactionFlags(rawValue: 1 << 1)
}
private enum MediaAreaType: Int32 {
case venue
case reaction
}
public enum DecodingError: Error {
case generic
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
guard let type = MediaAreaType(rawValue: try container.decode(Int32.self, forKey: .type)) else {
throw DecodingError.generic
}
switch type {
case .venue:
let coordinates = try container.decode(MediaArea.Coordinates.self, forKey: .coordinates)
let venue = try container.decode(MediaArea.Venue.self, forKey: .value)
self = .venue(coordinates: coordinates, venue: venue)
case .reaction:
let coordinates = try container.decode(MediaArea.Coordinates.self, forKey: .coordinates)
let reaction = try container.decode(MessageReaction.Reaction.self, forKey: .value)
let flags = ReactionFlags(rawValue: try container.decodeIfPresent(Int32.self, forKey: .flags) ?? 0)
self = .reaction(coordinates: coordinates, reaction: reaction, flags: flags)
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case let .venue(coordinates, venue):
try container.encode(MediaAreaType.venue.rawValue, forKey: .type)
try container.encode(coordinates, forKey: .coordinates)
try container.encode(venue, forKey: .value)
case let .reaction(coordinates, reaction, flags):
try container.encode(MediaAreaType.reaction.rawValue, forKey: .type)
try container.encode(coordinates, forKey: .coordinates)
try container.encode(reaction, forKey: .value)
try container.encode(flags.rawValue, forKey: .flags)
}
}
}
public extension MediaArea {
var coordinates: Coordinates {
switch self {
case let .venue(coordinates, _):
return coordinates
case let .reaction(coordinates, _, _):
return coordinates
}
}
}