mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			76 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			76 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Swift
		
	
| 
											5 years ago
										 | import SessionUtilitiesKit | ||
| 
											5 years ago
										 | 
 | ||
|  | @objc(SNTypingIndicator) | ||
|  | public final class TypingIndicator : ControlMessage { | ||
| 
											5 years ago
										 |     public var kind: Kind? | ||
| 
											5 years ago
										 | 
 | ||
| 
											5 years ago
										 |     public override class var ttl: UInt64 { 30 * 1000 } | ||
|  | 
 | ||
| 
											5 years ago
										 |     // MARK: Kind | ||
| 
											5 years ago
										 |     public enum Kind : Int { | ||
| 
											5 years ago
										 |         case started, stopped | ||
|  | 
 | ||
|  |         static func fromProto(_ proto: SNProtoTypingMessage.SNProtoTypingMessageAction) -> Kind { | ||
|  |             switch proto { | ||
|  |             case .started: return .started | ||
|  |             case .stopped: return .stopped | ||
|  |             } | ||
|  |         } | ||
|  | 
 | ||
|  |         func toProto() -> SNProtoTypingMessage.SNProtoTypingMessageAction { | ||
|  |             switch self { | ||
|  |             case .started: return .started | ||
|  |             case .stopped: return .stopped | ||
|  |             } | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
| 
											5 years ago
										 |     // MARK: Validation | ||
| 
											5 years ago
										 |     public override var isValid: Bool { | ||
|  |         guard super.isValid else { return false } | ||
|  |         return kind != nil | ||
|  |     } | ||
| 
											5 years ago
										 | 
 | ||
| 
											5 years ago
										 |     // MARK: Initialization | ||
| 
											5 years ago
										 |     public override init() { super.init() } | ||
|  | 
 | ||
| 
											5 years ago
										 |     internal init(kind: Kind) { | ||
| 
											5 years ago
										 |         super.init() | ||
| 
											5 years ago
										 |         self.kind = kind | ||
|  |     } | ||
|  | 
 | ||
| 
											5 years ago
										 |     // MARK: Coding | ||
|  |     public required init?(coder: NSCoder) { | ||
|  |         super.init(coder: coder) | ||
| 
											5 years ago
										 |         if let rawKind = coder.decodeObject(forKey: "action") as! Int? { kind = Kind(rawValue: rawKind) } | ||
| 
											5 years ago
										 |     } | ||
|  | 
 | ||
|  |     public override func encode(with coder: NSCoder) { | ||
|  |         super.encode(with: coder) | ||
| 
											5 years ago
										 |         coder.encode(kind?.rawValue, forKey: "action") | ||
| 
											5 years ago
										 |     } | ||
|  | 
 | ||
|  |     // MARK: Proto Conversion | ||
|  |     public override class func fromProto(_ proto: SNProtoContent) -> TypingIndicator? { | ||
| 
											5 years ago
										 |         guard let typingIndicatorProto = proto.typingMessage else { return nil } | ||
| 
											5 years ago
										 |         let kind = Kind.fromProto(typingIndicatorProto.action) | ||
| 
											5 years ago
										 |         return TypingIndicator(kind: kind) | ||
| 
											5 years ago
										 |     } | ||
|  | 
 | ||
| 
											5 years ago
										 |     public override func toProto() -> SNProtoContent? { | ||
| 
											5 years ago
										 |         guard let timestamp = sentTimestamp, let kind = kind else { | ||
|  |             SNLog("Couldn't construct typing indicator proto from: \(self).") | ||
|  |             return nil | ||
|  |         } | ||
| 
											5 years ago
										 |         let typingIndicatorProto = SNProtoTypingMessage.builder(timestamp: timestamp, action: kind.toProto()) | ||
|  |         let contentProto = SNProtoContent.builder() | ||
|  |         do { | ||
|  |             contentProto.setTypingMessage(try typingIndicatorProto.build()) | ||
| 
											5 years ago
										 |             return try contentProto.build() | ||
| 
											5 years ago
										 |         } catch { | ||
|  |             SNLog("Couldn't construct typing indicator proto from: \(self).") | ||
|  |             return nil | ||
|  |         } | ||
|  |     } | ||
| 
											5 years ago
										 | } |