ChatViewModel.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // ChatViewModel.swift
  3. // Chat
  4. //
  5. // Created by Sergey Tarasov on 10.08.2022.
  6. //
  7. import Foundation
  8. import XMPPFramework
  9. import XMPPFrameworkSwift
  10. final class ChatViewModelExample {
  11. static let messages: [String] = ["Display a short text message, called an alert, that draws attention to something new in your app.", "Play a notification sound.", "Set a badge number on the app’s icon to let the user know there are new items.", "Provide actions the user can take without opening the app.", "Show a media attachment.", "Be silent, allowing the app to perform a task in the background.", "No", "Group notifications into threads.", "Yes", "Edit or remove delivered notifications.", "Run code to change your notification before displaying it.", "Display a custom, interactive UI for your notification.", "And probably more.", "Yes", "Edit or remove delivered notifications.", "Run code to change your notification before displaying it.", "Display a custom, interactive UI for your notification.", "And probably more."]
  12. }
  13. final class ChatViewModel: NSObject, ObservableObject {
  14. let room: XMPPRoom
  15. let storage: XMPPRoomMemoryStorage
  16. let mam: XMPPMessageArchiveManagement
  17. let jid: XMPPJID?
  18. @Published var messageText: String = ""
  19. @Published var messages: [Message] = []
  20. init(with jidString: String) {
  21. self.storage = XMPPRoomMemoryStorage()
  22. self.room = XMPPRoom(roomStorage: self.storage, jid: XMPPJID(string: jidString)!)
  23. self.mam = XMPPMessageArchiveManagement()
  24. self.jid = XMPPJID(string: jidString)
  25. super.init()
  26. self.room.activate(XMPPController.shared.xmppStream)
  27. self.mam.activate(XMPPController.shared.xmppStream)
  28. self.room.join(usingNickname: XMPPController.shared.xmppStream.myJID?.user ?? "Test", history: nil)
  29. self.room.addDelegate(self, delegateQueue: DispatchQueue.main)
  30. self.mam.addDelegate(self, delegateQueue: DispatchQueue.main)
  31. }
  32. func fetchMessages() {
  33. self.messages.removeAll()
  34. if self.room.isJoined {
  35. self.mam.retrieveMessageArchive(at: self.room.roomJID.bareJID, withFields: nil, with: nil)
  36. } else {
  37. let field = DDXMLElement(name: "field")
  38. field.addAttribute(withName: "var", stringValue: "with")
  39. field.addChild(DDXMLElement(name: "value", stringValue: self.room.roomJID.bare))
  40. self.mam.retrieveMessageArchive(at: nil, withFields: [field], with: nil)
  41. }
  42. }
  43. func sendMessage() {
  44. if !self.messageText.isEmpty {
  45. let xmppMessage = XMPPMessage(messageType: room.isJoined ? .chat : .normal, to: self.room.roomJID.bareJID)
  46. xmppMessage.addBody(messageText)
  47. if self.room.isJoined {
  48. self.room.send(xmppMessage)
  49. } else {
  50. XMPPController.shared.xmppStream.send(xmppMessage)
  51. }
  52. self.messageText = ""
  53. }
  54. }
  55. }
  56. extension ChatViewModel: XMPPRoomDelegate {
  57. func xmppRoom(_ sender: XMPPRoom, didReceive message: XMPPMessage, fromOccupant occupantJID: XMPPJID) {
  58. print(message.debugDescription)
  59. }
  60. }
  61. extension ChatViewModel: XMPPRoomMemoryStorageDelegate {
  62. func xmppRoomMemoryStorage(
  63. _ sender: XMPPRoomMemoryStorage!,
  64. didReceiveMessage message: XMPPRoomMessageMemoryStorageObject!,
  65. fromOccupant occupant: XMPPRoomOccupantMemoryStorageObject!,
  66. at index: UInt,
  67. in allMessages: [Any]!
  68. ) {
  69. debugPrint(message.debugDescription)
  70. }
  71. }
  72. extension ChatViewModel: XMPPMessageArchiveManagementDelegate {
  73. func xmppMessageArchiveManagement(
  74. _ xmppMessageArchiveManagement: XMPPMessageArchiveManagement,
  75. didReceiveMAMMessage message: XMPPMessage
  76. ) {
  77. if let result = message.children?.first,
  78. let forwarded = result.children?.first,
  79. let msg = forwarded.children?.first as? DDXMLElement {
  80. let xmppmsg = XMPPMessage(from: msg)
  81. if xmppmsg.isMessageWithBody, let body = xmppmsg.body, let from = xmppmsg.from?.bare {
  82. let readyMessage = Message(id: UUID().uuidString, body: body, fromID: from)
  83. self.messages.append(readyMessage)
  84. }
  85. }
  86. }
  87. func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didFinishReceivingMessagesWith resultSet: XMPPResultSet) {
  88. debugPrint("FINISH MAM")
  89. }
  90. }