I am utilizing MessageKit
right here:
struct MessagesView: UIViewControllerRepresentable {
@Binding var messages: [MockMessage]
func makeUIViewController(context: Context) -> MessagesViewController {
let messagesViewController = MessagesViewController()
messagesViewController.messagesCollectionView.messagesDataSource = context.coordinator
messagesViewController.messagesCollectionView.messagesLayoutDelegate = context.coordinator
messagesViewController.messagesCollectionView.messagesDisplayDelegate = context.coordinator
messagesViewController.messagesCollectionView.messageCellDelegate = context.coordinator
return messagesViewController
}
func updateUIViewController(_ uiViewController: MessagesViewController, context: Context) {
context.coordinator.messages = messages
uiViewController.messagesCollectionView.reloadData()
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
struct Sender: SenderType {
var senderId: String
var displayName: String
}
class Coordinator: NSObject, MessagesDataSource, MessagesLayoutDelegate, MessagesDisplayDelegate, MessageCellDelegate {
var currentSender: SenderType = Sender(senderId: "123", displayName: "John Doe")
var mum or dad: MessagesView
var messages: [MockMessage] = []
init(_ mum or dad: MessagesView) {
self.mum or dad = mum or dad
}
func messageStyle(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageStyle {
let tail: MessageStyle.TailCorner = (message.sender.senderId == currentSender.senderId) ? .bottomRight : .bottomLeft
return .bubbleTail(tail, .curved)
}
func backgroundColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor {
return message.sender.senderId == currentSender.senderId ? .systemBlue : .lightGray
}
func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
return messages[indexPath.section]
}
func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
return messages.rely
}
func avatarView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarView {
let avatarView = AvatarView()
avatarView.backgroundColor = .pink // Set a visual shade to check
avatarView.body = CGRect(x: 0, y: 0, width: 30, top: 30) // Set a body to check
return avatarView
}
func avatarSize(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGSize {
return CGSize(width: 30, top: 30) // Return a non-zero measurement to check
}
}
}
struct Sender: SenderType {
var senderId: String
var displayName: String
}
struct MockMessage: MessageType {
var messageId: String
var sender: SenderType
var sentDate: Date
var variety: MessageKind
var requiresLoadingIndicator: Bool {
// Add your logic right here to find out if the message ought to present a loading indicator
return false // Instance: return true if the message is being despatched
}
init(textual content: String, sender: SenderType, messageId: String, date: Date, picture: UIImage? = nil) {
self.messageId = messageId
self.sender = sender
self.sentDate = date
if let picture = picture {
let mediaItem = MockMediaItem(picture: picture)
self.variety = .picture(mediaItem)
} else {
self.variety = .textual content(textual content)
}
}
}
struct MockMediaItem: MediaItem {
var url: URL?
var picture: UIImage?
var placeholderImage: UIImage
var measurement: CGSize
init(picture: UIImage) {
self.picture = picture
self.placeholderImage = UIImage() // Use a correct placeholder picture
self.measurement = CGSize(width: 240, top: 240)
}
}
I exploit the MessageKit view right here:
var physique: some View {
MessagesView(messages: $messageManager.messages)
.onAppear {
messageManager.initializeMockMessages()
DispatchQueue.essential.asyncAfter(deadline: .now() + 8) {
if let photoMessage = messageManager.createImageMessage(picture: "Corn") {
self.messageManager.messages.append(photoMessage)
self.handleAudioForImageMessage(photoMessage)
}
}
}
}
The avatarView
and avatarSize
features usually are not being referred to as for some motive