using System; using System.Collections.Generic; using JetBrains.Annotations; using UIWidgetsSample; using Unity.UIWidgets.foundation; namespace ChatComponents { public enum MessageType { custom, file, image, text, unsupported } /// Extension with one [toShortString] method public class MessageTypeToShortString { /// Converts enum to the string equal to enum's name public MessageType messageType; private string toShortString() { return ToString().Split('.').last(); } } /// All possible statuses message can have. public enum Status { delivered, error, seen, sending, sent } /// Extension with one [toShortString] method public class StatusToShortString { public Status status; /// Converts enum to the string equal to enum's name private string toShortString() { return ToString().Split('.').last(); } } /// An abstract class that contains all variables and methods /// every message will have. public abstract class Message : Equatable { /// User who sent this message public readonly User author; /// Created message timestamp, in ms public readonly int? createdAt; /// Unique ID of the message public readonly string id; /// Additional custom metadata or attributes related to the message public readonly Dictionary metadata; /// ID of the room where this message is sent public readonly string roomId; /// Message [Status] public readonly Status? status; /// [MessageType] public readonly MessageType type; public Message( User author, int? createdAt, string id, Dictionary metadata, [CanBeNull] string roomId, Status? status, MessageType type ) { this.author = author; this.createdAt = createdAt; this.id = id; this.metadata = metadata; this.roomId = roomId; this.status = status; this.type = type; } /// Creates a particular message from a map (decoded JSON). /// Type is determined by the `type` field. public static Message fromJson(Dictionary json) { var type = json["type"] as string; switch (type) { case "custom": return CustomMessage.fromJson(json); case "file": return FileMessage.fromJson(json); case "image": return ImageMessage.fromJson(json); case "text": return TextMessage.fromJson(json); default: return UnsupportedMessage.fromJson(json); } } /// Creates a copy of the message with an updated data /// [metadata] with null value will nullify existing metadata, otherwise /// both metadatas will be merged into one Map, where keys from a passed /// metadata will overwite keys from the previous one. /// [previewData] will be only set for the text message type. /// [status] with null value will be overwritten by the previous status. /// [text] will be only set for the text message type. Null value will be /// overwritten by the previous text (can't be empty). public abstract Message copyWith( Dictionary metadata = null, [CanBeNull] PreviewData previewData = null, Status? status = default, string text = null ); /// Converts a particular message to the map representation, encodable to JSON.\ public abstract Dictionary toJson(); } }