123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- //
- // ChatViewModel.swift
- // Chat
- //
- // Created by Sergey Tarasov on 10.08.2022.
- //
- import Foundation
- import XMPPFramework
- import XMPPFrameworkSwift
- final class ChatViewModelExample {
- 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."]
- }
- final class ChatViewModel: NSObject, ObservableObject {
- let room: XMPPRoom
- let storage: XMPPRoomMemoryStorage
- let mam: XMPPMessageArchiveManagement
- let jid: XMPPJID?
- @Published var messageText: String = ""
- @Published var messages: [Message] = []
- init(with jidString: String) {
- self.storage = XMPPRoomMemoryStorage()
- self.room = XMPPRoom(roomStorage: self.storage, jid: XMPPJID(string: jidString)!)
- self.mam = XMPPMessageArchiveManagement()
- self.jid = XMPPJID(string: jidString)
- super.init()
- self.room.activate(XMPPController.shared.xmppStream)
- self.mam.activate(XMPPController.shared.xmppStream)
- self.room.join(usingNickname: XMPPController.shared.xmppStream.myJID?.user ?? "Test", history: nil)
- self.room.addDelegate(self, delegateQueue: DispatchQueue.main)
- self.mam.addDelegate(self, delegateQueue: DispatchQueue.main)
- }
- func fetchMessages() {
- self.messages.removeAll()
- if self.room.isJoined {
- self.mam.retrieveMessageArchive(at: self.room.roomJID.bareJID, withFields: nil, with: nil)
- } else {
- let field = DDXMLElement(name: "field")
- field.addAttribute(withName: "var", stringValue: "with")
- field.addChild(DDXMLElement(name: "value", stringValue: self.room.roomJID.bare))
- self.mam.retrieveMessageArchive(at: nil, withFields: [field], with: nil)
- }
- }
- func sendMessage() {
- if !self.messageText.isEmpty {
- let xmppMessage = XMPPMessage(messageType: room.isJoined ? .chat : .normal, to: self.room.roomJID.bareJID)
- xmppMessage.addBody(messageText)
- if self.room.isJoined {
- self.room.send(xmppMessage)
- } else {
- XMPPController.shared.xmppStream.send(xmppMessage)
- }
- self.messageText = ""
- }
- }
- }
- extension ChatViewModel: XMPPRoomDelegate {
- func xmppRoom(_ sender: XMPPRoom, didReceive message: XMPPMessage, fromOccupant occupantJID: XMPPJID) {
- print(message.debugDescription)
- }
- }
- extension ChatViewModel: XMPPRoomMemoryStorageDelegate {
- func xmppRoomMemoryStorage(
- _ sender: XMPPRoomMemoryStorage!,
- didReceiveMessage message: XMPPRoomMessageMemoryStorageObject!,
- fromOccupant occupant: XMPPRoomOccupantMemoryStorageObject!,
- at index: UInt,
- in allMessages: [Any]!
- ) {
- debugPrint(message.debugDescription)
- }
- }
- extension ChatViewModel: XMPPMessageArchiveManagementDelegate {
- func xmppMessageArchiveManagement(
- _ xmppMessageArchiveManagement: XMPPMessageArchiveManagement,
- didReceiveMAMMessage message: XMPPMessage
- ) {
- if let result = message.children?.first,
- let forwarded = result.children?.first,
- let msg = forwarded.children?.first as? DDXMLElement {
- let xmppmsg = XMPPMessage(from: msg)
- if xmppmsg.isMessageWithBody, let body = xmppmsg.body, let from = xmppmsg.from?.bare {
- let readyMessage = Message(id: UUID().uuidString, body: body, fromID: from)
- self.messages.append(readyMessage)
- }
- }
- }
- func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didFinishReceivingMessagesWith resultSet: XMPPResultSet) {
- debugPrint("FINISH MAM")
- }
- }
|