Message Screen perform message count and if message read then not count and show message

 It seems like you want to implement a message count functionality in your message screen, and display messages as counted or not counted based on whether the user has viewed them or not. Additionally, you want to show a typing indicator when another user is typing a message.

To implement this functionality, you can first add a flag to each message in your real-time database to indicate whether it has been viewed by the user or not. You can set this flag to false when a new message is sent, and set it to true when the message is viewed by the user.

Next, you can fetch the messages from the real-time database and display them in your message screen. For each message, you can check the flag to determine whether to show it as counted or not counted.

To show a typing indicator when another user is typing, you can add a listener to the real-time database that listens for changes to a "typing" node under the conversation between the two users. When this node is set to true, you can display a typing indicator in the message screen.

Here's some example code to get you started:

// Add flag to each message to indicate whether it has been viewed database.child("Chats").child("(String(describing: dict.object(forKey: "userid")!))").child(Auth.auth().currentUser!.uid).observe(.childAdded, with: { (snapshot) in let messageData = snapshot.value as! [String: Any] var message = Message() message.text = messageData["text"] as? String ?? "" message.isViewed = messageData["isViewed"] as? Bool ?? false // ... Add message to message array and reload table view })

// Show messages as counted or not counted based on isViewed flag func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MessageCell", for: indexPath) as! MessageCell let message = messages[indexPath.row] cell.messageLabel.text = message.text if message.isViewed { cell.countLabel.isHidden = true } else { cell.countLabel.isHidden = false } return cell }

// Add listener for typing indicator database.child("Chats").child("(String(describing: dict.object(forKey: "userid")!))").child(Auth.auth().currentUser!.uid).child("typing").observe(.value, with: { (snapshot) in if let isTyping = snapshot.value as? Bool, isTyping { // Display typing indicator } else { // Hide typing indicator } })

Note that this is just example code and you will need to adapt it to fit your specific use case.

Comments