Răsfoiți Sursa

Update ChatViewModel

Sergey 2 ani în urmă
părinte
comite
cc5747720c
1 a modificat fișierele cu 18 adăugiri și 8 ștergeri
  1. 18 8
      Chat/UI/Chat/ChatViewModel.swift

+ 18 - 8
Chat/UI/Chat/ChatViewModel.swift

@@ -9,10 +9,6 @@ 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
@@ -38,6 +34,8 @@ final class ChatViewModel: NSObject, ObservableObject {
 
 		self.room.addDelegate(self, delegateQueue: DispatchQueue.main)
 		self.mam.addDelegate(self, delegateQueue: DispatchQueue.main)
+
+		fetchMessages()
     }
 
     func fetchMessages() {
@@ -63,14 +61,23 @@ final class ChatViewModel: NSObject, ObservableObject {
 			} else {
 				XMPPController.shared.xmppStream.send(xmppMessage)
 			}
+			self.messages.append(.init(
+				id: UUID().uuidString,
+				body: messageText,
+				fromID: XMPPController.shared.xmppStream.myJID?.bare ?? "")
+			)
             self.messageText = ""
         }
-    }
+	}
 }
 
 extension ChatViewModel: XMPPRoomDelegate {
-    func xmppRoom(_ sender: XMPPRoom, didReceive message: XMPPMessage, fromOccupant occupantJID: XMPPJID) {
-        print(message.debugDescription)
+	func xmppRoom(_ sender: XMPPRoom, didReceive message: XMPPMessage, fromOccupant occupantJID: XMPPJID) {
+		if let body = message.body,
+		   let id = message.originId,
+		   let fromID = message.from?.bare {
+			self.messages.append(.init(id: id, body: body, fromID: fromID))
+		}
     }
 }
 
@@ -102,7 +109,10 @@ extension ChatViewModel: XMPPMessageArchiveManagementDelegate {
 		}
 	}
 
-	func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didFinishReceivingMessagesWith resultSet: XMPPResultSet) {
+	func xmppMessageArchiveManagement(
+		_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement,
+		didFinishReceivingMessagesWith resultSet: XMPPResultSet
+	) {
 		debugPrint("FINISH MAM")
 	}
 }