Hello. It's still a small fish, but I'm only thinking about programming. Under such circumstances, my understanding of MessageKit deepened my understanding of programming. I wrote this article.
About MessageKit used for portfolio creation such as chat apps Articles to explain. I think that the field of view will expand if you pay particular attention to the mold.
The information you need to chat ・ Sender information -Message type, information With just these two, MessageKit can be implemented once these two are in place.
-UserId (unique ID) You can register as a user If you do not want to register, temporarily implement with the unique ID (UDID) of the terminal. (Practical) ・ UserName (displayed on the display)
-MessageId (used for deleting messages with a unique ID) ・ MessageType (text, photo, video, etc.) -Date (required for chat, also for sorting) ・ MessageContent
MessageKit can be implemented with about this much information.
SenderType This is a protocol for sender information. The protocol describes the properties and functions described therein when it complies. If you don't write it, you'll get angry and it will help improve the maintainability of your code.
public protocol SenderType {
/// The unique String identifier for the sender.
///
/// Note: This value must be unique across all senders.
var senderId: String { get }
/// The display name of a sender.
var displayName: String { get }
}
The contents of the SenderType protocol look like this. Write SenderType as import MessageKit and command + hover over ↓ Jump to Definition You can check it by jumping to the description location with.
The contents of the SenderType protocol include The senderId and displayName are described. We will implement a structure Sender that conforms to this protocol here.
struct Sender: SenderType {
var senderId: String
var displayName: String
}
Now you have a structure for sender information. In the chat implementation, in the process related to the sender We will use this structure as a mold. Oh, if you want to implement chat, write this code.
MessageType The other of the minimum required information mentioned at the beginning, the type and information of the message. Take a peek inside.
public protocol MessageType {
/// The sender of the message.
var sender: SenderType { get }
/// The unique identifier for the message.
var messageId: String { get }
/// The date the message was sent.
var sentDate: Date { get }
/// The kind of message and its underlying kind.
var kind: MessageKind { get }
}
The sender type is SenderType type. The Sender implemented earlier is a MessageType-compliant structure. It is used by assigning it to the sender property.
The messageId has already been explained, but the ID of the message. Used to delete individual messages.
sentDate is date information.
kind determines whether the message is text, a photo, or a video. Take a peek inside.
public enum MessageKind {
/// A standard text message.
///
/// - Note: The font used for this message will be the value of the
/// `messageLabelFont` property in the `MessagesCollectionViewFlowLayout` object.
///
/// Using `MessageKind.attributedText(NSAttributedString)` doesn't require you
/// to set this property and results in higher performance.
case text(String)
/// A message with attributed text.
case attributedText(NSAttributedString)
/// A photo message.
case photo(MediaItem)
/// A video message.
case video(MediaItem)
/// A location message.
case location(LocationItem)
/// An emoji message.
case emoji(String)
//Omitted below
You can see that it is divided into cases by enum. text, attributedText, photo, etc.
If you write a structure that conforms to MessageType, it will be like this.
struct Message: MessageType {
var sender: SenderType
var messageId: String
var sentDate: Date
var kind: MessageKind
private init(kind: MessageKind, sender: Sender, messageId: String, date: Date) {
self.kind = kind
self.sender = sender
self.messageId = messageId
self.sentDate = date
}
init(attributedText: NSAttributedString, sender: Sender, messageId: String, date: Date) {
self.init(kind: .attributedText(attributedText), sender: sender, messageId: messageId, date: date)
}
}
It can be implemented with at least this. There are private init and init without private. This separates the initialization process accessed from the outside by MessageKind, Initialize the Message structure through private init.
If you expect to send photos and videos as well, use this init without private I will add that many, but since it is supposed to be text only, I have prepared only one.
I will not write the next article unless there is a particular response. No, what is the demand for MessageKit? Explanation of decoration system?