VoIPCallRingingMessage.swift 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. @objc class VoIPCallRingingMessage: NSObject {
  22. @objc var contact: Contact
  23. @objc var callId: VoIPCallId
  24. @objc var completion: (() -> Void)?
  25. @objc init(contact: Contact, callId: VoIPCallId, completion: (() -> Void)?) {
  26. self.contact = contact
  27. self.callId = callId
  28. self.completion = completion
  29. super.init()
  30. }
  31. }
  32. extension VoIPCallRingingMessage {
  33. enum VoIPCallRingingMessageError: Error {
  34. case generateJson(error: Error)
  35. }
  36. enum Keys: String {
  37. case callId = "callId"
  38. }
  39. @objc class func ringingFromJSONDictionary(_ dictionary: [AnyHashable: Any]?, contact: Contact) -> VoIPCallRingingMessage {
  40. guard let dictionary = dictionary else {
  41. return VoIPCallRingingMessage(contact: contact, callId: VoIPCallId(callId: nil), completion: nil)
  42. }
  43. let tmpCallId = VoIPCallId(callId: dictionary[Keys.callId.rawValue] as? UInt32)
  44. return VoIPCallRingingMessage(contact: contact, callId: tmpCallId, completion: nil)
  45. }
  46. @objc func jsonData() throws -> Data {
  47. let json = [Keys.callId.rawValue: callId.callId] as [AnyHashable : Any]
  48. do {
  49. return try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
  50. }
  51. catch let error {
  52. throw VoIPCallRingingMessageError.generateJson(error: error)
  53. }
  54. }
  55. }