swiftswiftuimessagekit

How to fix Type 'ChatViewController' does not conform to protocol 'MessagesDataSource'


I get an error: Type 'ChatViewController' does not conform to protocol 'MessagesDataSource' when running this swiftui code but I don't know what I'm missing.

extension ChatViewController: MessagesDataSource, MessagesLayoutDelegate, MessagesDisplayDelegate {

    func currentSender() -> SenderType {
        if let sender = selfSender {
            return sender
        }
        
        fatalError("Self Sender is nil, email should be cached")
    }
    
    func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
        return messages[indexPath.section]
    }
    
    func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
        return messages.count
    }
    
    func configureMediaMessageImageView(_ imageView: UIImageView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) {
        guard let message = message as? Message else {
            return
        }
        
        switch message.kind {
        case .photo(let media):
            guard let imageUrl = media.url else {
                return
            }
            
            imageView.sd_setImage(with: imageUrl)
            
        default:
            break
        }
    }
    
    func backgroundColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor {
        let sender = message.sender
        if sender.senderId == selfSender?.senderId {
            // our message thar we've sent
            return .link
        }
        
        return .secondarySystemBackground
    }
    
    func configureAvatarView(_ avatarView: AvatarView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) {
        let sender = message.sender
        if sender.senderId == selfSender?.senderId {
            // show our profile image
            if let currentUserImageUrl = self.senderPhotoUrl {
                avatarView.sd_setImage(with: currentUserImageUrl)
            } else {
                //images/safeemail_profile_picture.png
                guard let email = UserDefaults.standard.value(forKey: Keys.email.rawValue) as? String else {
                    return
                }
                
                let safeEmail = DatabaseManager.safeEmail(emailAddress: email)
                let path = "images/\(safeEmail)_profile_picture.png"
                
                //fetch url
                StorageManager.shared.downloadURL(for: path) { [weak self] result in
                    switch result {
                    case .success(let url):
                        DispatchQueue.main.async {
                            self?.senderPhotoUrl = url
                            avatarView.sd_setImage(with: url)
                        }
                    case .failure(let error):
                        print("Failed to download image url: \(error)")
                    }
                }
            }
        } else {
            // show other user profile image
            if let otherUserImageUrl = self.otherUserPhotoUrl {
                avatarView.sd_setImage(with: otherUserImageUrl)
            } else {
                //fetch url
                let email = self.otherUserEmail
                
                let safeEmail = DatabaseManager.safeEmail(emailAddress: email)
                let path = "images/\(safeEmail)_profile_picture.png"
                
                //fetch url
                StorageManager.shared.downloadURL(for: path) { [weak self] result in
                    switch result {
                    case .success(let url):
                        DispatchQueue.main.async {
                            self?.otherUserPhotoUrl = url
                            avatarView.sd_setImage(with: url)
                        }
                    case .failure(let error):
                        print("Failed to download image url: \(error)")
                    }
                }
            }
        }
    }
}

I tried the automatic fix of xcode: Do you want to add protocol stubs? which does this:

extension ChatViewController: MessagesDataSource, MessagesLayoutDelegate, MessagesDisplayDelegate {
    var currentSender: MessageKit.SenderType {
        <#code#>
    }

But then I get a new error: Invalid redeclaration of 'currentSender()'


Solution

  • The MessagesDataSource protocol has var currentSender: SenderType { get } as one of its requirements.

    This means you must implement currentSender as a var or let property, not as a func.