mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-24 07:05:35 +00:00
Merge branch 'unlockable-media'
This commit is contained in:
@@ -16,6 +16,77 @@ public struct TelegramMediaInvoiceFlags: OptionSet {
|
||||
public static let shippingAddressRequested = TelegramMediaInvoiceFlags(rawValue: 1 << 1)
|
||||
}
|
||||
|
||||
public enum TelegramExtendedMedia: PostboxCoding, Equatable {
|
||||
public static func == (lhs: TelegramExtendedMedia, rhs: TelegramExtendedMedia) -> Bool {
|
||||
switch lhs {
|
||||
case let .preview(lhsDimensions, lhsImmediateThumbnailData, lhsVideoDuration):
|
||||
if case let .preview(rhsDimensions, rhsImmediateThumbnailData, rhsVideoDuration) = rhs, lhsDimensions == rhsDimensions, lhsImmediateThumbnailData == rhsImmediateThumbnailData, lhsVideoDuration == rhsVideoDuration {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
case let .full(lhsMedia):
|
||||
if case let .full(rhsMedia) = rhs, lhsMedia.isEqual(to: rhsMedia) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case preview(dimensions: PixelDimensions?, immediateThumbnailData: Data?, videoDuration: Int32?)
|
||||
case full(media: Media)
|
||||
|
||||
public init(decoder: PostboxDecoder) {
|
||||
let type = decoder.decodeInt32ForKey("t", orElse: 0)
|
||||
switch type {
|
||||
case 0:
|
||||
let width = decoder.decodeOptionalInt32ForKey("width")
|
||||
let height = decoder.decodeOptionalInt32ForKey("width")
|
||||
var dimensions: PixelDimensions?
|
||||
if let width = width, let height = height {
|
||||
dimensions = PixelDimensions(width: width, height: height)
|
||||
}
|
||||
let immediateThumbnailData = decoder.decodeDataForKey("thumb")
|
||||
let videoDuration = decoder.decodeOptionalInt32ForKey("duration")
|
||||
self = .preview(dimensions: dimensions, immediateThumbnailData: immediateThumbnailData, videoDuration: videoDuration)
|
||||
case 1:
|
||||
let media = decoder.decodeObjectForKey("media") as! Media
|
||||
self = .full(media: media)
|
||||
default:
|
||||
self = .preview(dimensions: nil, immediateThumbnailData: nil, videoDuration: nil)
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(_ encoder: PostboxEncoder) {
|
||||
switch self {
|
||||
case let .preview(dimensions, immediateThumbnailData, videoDuration):
|
||||
encoder.encodeInt32(0, forKey: "t")
|
||||
if let dimensions = dimensions {
|
||||
encoder.encodeInt32(dimensions.width, forKey: "width")
|
||||
encoder.encodeInt32(dimensions.height, forKey: "height")
|
||||
} else {
|
||||
encoder.encodeNil(forKey: "width")
|
||||
encoder.encodeNil(forKey: "height")
|
||||
}
|
||||
if let immediateThumbnailData = immediateThumbnailData {
|
||||
encoder.encodeData(immediateThumbnailData, forKey: "thumb")
|
||||
} else {
|
||||
encoder.encodeNil(forKey: "thumb")
|
||||
}
|
||||
if let videoDuration = videoDuration {
|
||||
encoder.encodeInt32(videoDuration, forKey: "duration")
|
||||
} else {
|
||||
encoder.encodeNil(forKey: "duration")
|
||||
}
|
||||
case let .full(media):
|
||||
encoder.encodeInt32(1, forKey: "t")
|
||||
encoder.encodeObject(media, forKey: "media")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final class TelegramMediaInvoice: Media {
|
||||
public var peerIds: [PeerId] = []
|
||||
|
||||
@@ -29,8 +100,9 @@ public final class TelegramMediaInvoice: Media {
|
||||
public let startParam: String
|
||||
public let photo: TelegramMediaWebFile?
|
||||
public let flags: TelegramMediaInvoiceFlags
|
||||
public let extendedMedia: TelegramExtendedMedia?
|
||||
|
||||
public init(title: String, description: String, photo: TelegramMediaWebFile?, receiptMessageId: MessageId?, currency: String, totalAmount: Int64, startParam: String, flags: TelegramMediaInvoiceFlags) {
|
||||
public init(title: String, description: String, photo: TelegramMediaWebFile?, receiptMessageId: MessageId?, currency: String, totalAmount: Int64, startParam: String, extendedMedia: TelegramExtendedMedia?, flags: TelegramMediaInvoiceFlags) {
|
||||
self.title = title
|
||||
self.description = description
|
||||
self.photo = photo
|
||||
@@ -39,6 +111,7 @@ public final class TelegramMediaInvoice: Media {
|
||||
self.totalAmount = totalAmount
|
||||
self.startParam = startParam
|
||||
self.flags = flags
|
||||
self.extendedMedia = extendedMedia
|
||||
}
|
||||
|
||||
public init(decoder: PostboxDecoder) {
|
||||
@@ -49,6 +122,7 @@ public final class TelegramMediaInvoice: Media {
|
||||
self.startParam = decoder.decodeStringForKey("sp", orElse: "")
|
||||
self.photo = decoder.decodeObjectForKey("p") as? TelegramMediaWebFile
|
||||
self.flags = TelegramMediaInvoiceFlags(rawValue: decoder.decodeInt32ForKey("f", orElse: 0))
|
||||
self.extendedMedia = decoder.decodeObjectForKey("m", decoder: { TelegramExtendedMedia(decoder: $0) }) as? TelegramExtendedMedia
|
||||
|
||||
if let receiptMessageIdPeerId = decoder.decodeOptionalInt64ForKey("r.p") as Int64?, let receiptMessageIdNamespace = decoder.decodeOptionalInt32ForKey("r.n") as Int32?, let receiptMessageIdId = decoder.decodeOptionalInt32ForKey("r.i") as Int32? {
|
||||
self.receiptMessageId = MessageId(peerId: PeerId(receiptMessageIdPeerId), namespace: receiptMessageIdNamespace, id: receiptMessageIdId)
|
||||
@@ -65,12 +139,18 @@ public final class TelegramMediaInvoice: Media {
|
||||
encoder.encodeString(self.startParam, forKey: "sp")
|
||||
encoder.encodeInt32(self.flags.rawValue, forKey: "f")
|
||||
|
||||
if let photo = photo {
|
||||
if let photo = self.photo {
|
||||
encoder.encodeObject(photo, forKey: "p")
|
||||
} else {
|
||||
encoder.encodeNil(forKey: "p")
|
||||
}
|
||||
|
||||
if let extendedMedia = self.extendedMedia {
|
||||
encoder.encodeObject(extendedMedia, forKey: "m")
|
||||
} else {
|
||||
encoder.encodeNil(forKey: "m")
|
||||
}
|
||||
|
||||
if let receiptMessageId = self.receiptMessageId {
|
||||
encoder.encodeInt64(receiptMessageId.peerId.toInt64(), forKey: "r.p")
|
||||
encoder.encodeInt32(receiptMessageId.namespace, forKey: "r.n")
|
||||
@@ -121,4 +201,17 @@ public final class TelegramMediaInvoice: Media {
|
||||
public func isSemanticallyEqual(to other: Media) -> Bool {
|
||||
return self.isEqual(to: other)
|
||||
}
|
||||
|
||||
public func withUpdatedExtendedMedia(_ extendedMedia: TelegramExtendedMedia) -> TelegramMediaInvoice {
|
||||
return TelegramMediaInvoice(
|
||||
title: self.title,
|
||||
description: self.description,
|
||||
photo: self.photo,
|
||||
receiptMessageId: self.receiptMessageId,
|
||||
currency: self.currency,
|
||||
totalAmount: self.totalAmount,
|
||||
startParam: self.startParam,
|
||||
extendedMedia: extendedMedia,
|
||||
flags: self.flags)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user