// // ChatViewModel.swift // Chat // // Created by Sergey Tarasov on 10.08.2022. // import Foundation import XMPPFramework import XMPPFrameworkSwift 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."] } class ChatViewModel: NSObject, ObservableObject { let manager: RoomManager @Published var messageText: String = "" @Published var messages: [XMPPMessage] = [] init(with jidString: String) { self.manager = RoomManager(with: jidString) super.init() } func fetchMessages() { self.manager.fetchMessages() DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in self?.messages = self?.manager.messages ?? ChatViewModelExample.messages.map { text in let message = XMPPMessage() message.addBody(text) return message } } } func sendMessage() { if !self.messageText.isEmpty { self.manager.room.sendMessage(withBody: self.messageText) } } } extension ChatViewModel: XMPPRoomDelegate { func xmppRoom(_ sender: XMPPRoom, didReceive message: XMPPMessage, fromOccupant occupantJID: XMPPJID) { print(message.debugDescription) } }