session-ios/SessionMessagingKit/Messages/Control Messages/ExpirationTimerUpdate.swift

73 lines
2.4 KiB
Swift
Raw Normal View History

2020-11-09 00:58:47 +01:00
import SessionUtilitiesKit
2020-11-05 23:17:05 +01:00
@objc(SNExpirationTimerUpdate)
public final class ExpirationTimerUpdate : ControlMessage {
2020-11-06 00:59:04 +01:00
public var duration: UInt32?
2020-11-05 23:17:05 +01:00
2020-11-06 00:59:04 +01:00
// MARK: Initialization
2020-11-26 03:30:30 +01:00
public override init() { super.init() }
2020-11-06 04:58:47 +01:00
internal init(duration: UInt32) {
2020-11-06 00:59:04 +01:00
super.init()
self.duration = duration
}
2020-11-06 06:28:06 +01:00
// MARK: Validation
2020-11-17 06:23:13 +01:00
public override var isValid: Bool {
guard super.isValid else { return false }
return duration != nil
}
2020-11-06 06:28:06 +01:00
2020-11-06 00:59:04 +01:00
// MARK: Coding
public required init?(coder: NSCoder) {
super.init(coder: coder)
2020-11-08 22:15:44 +01:00
if let duration = coder.decodeObject(forKey: "durationSeconds") as! UInt32? { self.duration = duration }
2020-11-06 00:59:04 +01:00
}
public override func encode(with coder: NSCoder) {
super.encode(with: coder)
2020-11-08 22:15:44 +01:00
coder.encode(duration, forKey: "durationSeconds")
2020-11-06 00:59:04 +01:00
}
// MARK: Proto Conversion
public override class func fromProto(_ proto: SNProtoContent) -> ExpirationTimerUpdate? {
2020-11-06 01:41:01 +01:00
guard let dataMessageProto = proto.dataMessage else { return nil }
let isExpirationTimerUpdate = (dataMessageProto.flags & UInt32(SNProtoDataMessage.SNProtoDataMessageFlags.expirationTimerUpdate.rawValue)) != 0
2020-11-06 00:59:04 +01:00
guard isExpirationTimerUpdate else { return nil }
2020-11-06 01:41:01 +01:00
let duration = dataMessageProto.expireTimer
return ExpirationTimerUpdate(duration: duration)
2020-11-06 00:59:04 +01:00
}
public override func toProto() -> SNProtoContent? {
2020-11-06 01:41:01 +01:00
guard let duration = duration else {
SNLog("Couldn't construct expiration timer update proto from: \(self).")
return nil
}
let dataMessageProto = SNProtoDataMessage.builder()
dataMessageProto.setFlags(UInt32(SNProtoDataMessage.SNProtoDataMessageFlags.expirationTimerUpdate.rawValue))
dataMessageProto.setExpireTimer(duration)
2020-11-06 00:59:04 +01:00
let contentProto = SNProtoContent.builder()
do {
2020-11-06 01:41:01 +01:00
contentProto.setDataMessage(try dataMessageProto.build())
2020-11-06 00:59:04 +01:00
return try contentProto.build()
} catch {
SNLog("Couldn't construct expiration timer update proto from: \(self).")
return nil
}
}
2020-11-26 03:30:30 +01:00
2020-12-07 05:11:49 +01:00
// MARK: Description
public override var description: String {
"""
ExpirationTimerUpdate(
duration: \(duration?.description ?? "null")
)
"""
}
2020-11-26 03:30:30 +01:00
// MARK: Convenience
@objc public func setDuration(_ duration: UInt32) {
2020-11-26 05:51:12 +01:00
self.duration = duration
2020-11-26 03:30:30 +01:00
}
2020-11-05 23:17:05 +01:00
}