Thanks to the people that helped me on my other SA question, I was able to create a function that returns a boolean to see if a user already voted on a chat message. I want to print if the person voted on the chat message using the MessageKit messageBottomLabelAttributedText
function. However, I'm unable to use the returned boolean value to print the correct text.
Here's my current messageBottomLabelAttributedText function within MessagesDataSource:
func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
var bool = didAlreadyVote(message: message as! MessageType){_ in Bool.self}
if bool as? Bool == true {
let dateString = self.formatter.string(from: message.sentDate)
let likeString = "Voted"
return NSAttributedString(string: "\(dateString) | \(likeString)", attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)])
} else {
let dateString = self.formatter.string(from: message.sentDate)
return NSAttributedString(string: dateString, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)])
}
}
}
For reference, here's the didAlreadyVote function this community helped me out with earlier:
func didAlreadyVote(message: MessageType, completion: @escaping (Bool) -> Void) {
// check user votes collection to see if current message matches
guard let currentUser = Auth.auth().currentUser else {return}
let userID = currentUser.uid
let docRef = Firestore.firestore().collection("users").document(userID).collection("upvotes").whereField("messageId", isEqualTo: message.messageId)
docRef.getDocuments { querySnapshot, error in
if let error = error {
print("Error getting documents: \(error)")
completion(false)
} else {
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
completion(true) /// Note that this will get called multiple times if you have more the one document!
}
}
}
}
When I run the app, the bool variable doesn't return anything. How can I retrieve the boolean from the function and then use it within messageBottomLabelAttributedText?
Thanks!