I have a function for avatars that's called in viewDidLoad
(this is the function if it helps although I think the problem is not the function itself: https://pastebin.com/ksHmucTX).
In viewDidLoad
I call it:
createAvatar(senderId: self.senderId, senderDisplayName: self.senderDisplayName, user: self.currentUser!, color: UIColor.lightGray)
But since I am only passing in my information (as the current user), when using the app I only see my avatar and not the avatars of everyone else in the chat.
In the avatarImageDataForItemAt
function, there is an index path to figure out which message is which:
override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {
let message = messages[indexPath.row]
return avatars[message.senderId]
}
So I'm wondering how to get the proper senderId
(i.e. the ID of whoever sent a message) in viewDidLoad so I can plug it into the createAvatars
call, as opposed to what I have now, where senderId
is ONLY my ID - thus making everyone's avatar appear, and not only mine (or whoever is the current user).
Thanks for any help!
EDIT:
func getParticipantInfo() {
let groupRef = databaseRef.child("groups").child(currentRoomIdGlobal)
groupRef.observe(.childAdded, with: { snapshot in
if let snapDict = snapshot.value as? [String : AnyObject] {
for each in snapDict {
let uid = each.key
let avatar = each.value["profilePicture"] as! String
let gender = each.value["gender"] as! String
let handle = each.value["handle"] as! String
let name = each.value["name"] as! String
let status = each.value["status"] as! String
// Set those to the dictionaries [UID : value]
self.avatarDictionary.setValue(avatar, forKey: uid)
self.nameDictionary.setValue(name, forKey: uid)
self.genderDictionary.setValue(gender, forKey: uid)
self.handleDictionary.setValue(handle, forKey: uid)
self.statusDictionary.setValue(status, forKey: uid)
DispatchQueue.main.async {
self.createAvatar(senderId: uid, senderDisplayName: name, photoUrl: avatar, color: UIColor.lightGray)
self.navCollectionView?.collectionView?.reloadData()
}
}
}
})
}
createAvatar:
func createAvatar(senderId: String, senderDisplayName: String, photoUrl: String, color: UIColor) {
if self.avatars[senderId] == nil {
let img = MyImageCache.sharedCache.object(forKey: senderId as AnyObject) as? UIImage
if img != nil {
self.avatars[senderId] = JSQMessagesAvatarImageFactory.avatarImage(with: img, diameter: 30)
} else if photoUrl != "" {
// the images are very small, so the following methods work just fine, no need for Alamofire here
if photoUrl.contains("https://firebasestorage.googleapis.com") {
self.storage.reference(forURL: photoUrl).data(withMaxSize: 1 * 1024 * 1024) { (data, error) -> Void in
if (error != nil) {
assertionFailure("Error with Firebase Storage")
}
else {
let newImage = UIImage(data: data!)
self.avatars[senderId] = JSQMessagesAvatarImageFactory.avatarImage(with: newImage, diameter: 30)
MyImageCache.sharedCache.setObject(newImage!, forKey: senderId as AnyObject, cost: data!.count)
}
}
} else if let data = NSData(contentsOf: NSURL(string:photoUrl)! as URL) {
let newImage = UIImage(data: data as Data)!
self.avatars[senderId] = JSQMessagesAvatarImageFactory.avatarImage(with: newImage, diameter: 30)
MyImageCache.sharedCache.setObject(newImage, forKey: senderId as AnyObject, cost: data.length)
} else {
// Initials
let senderName = nameDictionary.value(forKey: senderId) as! String
let initials = senderName.components(separatedBy: " ").reduce("") { ($0 == "" ? "" : "\($0.characters.first!)") + "\($1.characters.first!)" }
let placeholder = JSQMessagesAvatarImageFactory.avatarImage(withUserInitials: initials, backgroundColor: UIColor.gray, textColor: UIColor.white, font: UIFont.systemFont(ofSize: 14), diameter: UInt(kJSQMessagesCollectionViewAvatarSizeDefault))
MyImageCache.sharedCache.setObject(placeholder!, forKey: senderId as AnyObject)
}
}
} else {
// Initials
let senderName = nameDictionary.value(forKey: senderId) as! String
let initials = senderName.components(separatedBy: " ").reduce("") { ($0 == "" ? "" : "\($0.characters.first!)") + "\($1.characters.first!)" }
let placeholder = JSQMessagesAvatarImageFactory.avatarImage(withUserInitials: initials, backgroundColor: UIColor.gray, textColor: UIColor.white, font: UIFont.systemFont(ofSize: 14), diameter: UInt(kJSQMessagesCollectionViewAvatarSizeDefault))
MyImageCache.sharedCache.setObject(placeholder!, forKey: senderId as AnyObject)
}
}