using System.Collections.Generic; using JetBrains.Annotations; using UIWidgetsSample; namespace ChatComponents { public class PreviewData : Equatable { /// Link description (usually og:description meta tag) public readonly string description; /// See [PreviewDataImage] public readonly PreviewDataImage image; /// Remote resource URL public readonly string link; /// Link title (usually og:title meta tag) public readonly string title; /// Creates preview data. public PreviewData( string description = null, [CanBeNull] PreviewDataImage image = null, string link = null, string title = null ) { this.description = description; this.image = image; this.link = link; this.title = title; } /// Equatable props public override List props => new List {description, image, link, title}; /// Creates preview data from a map (decoded JSON). public static PreviewData fromJson(Dictionary json) { return new PreviewData( json["description"] as string, json["image"] == null ? null : PreviewDataImage.fromJson(json["image"] as Dictionary), json["link"] as string, json["title"] as string ); } /// Converts preview data to the map representation, encodable to JSON. public Dictionary toJson() { return new Dictionary { {"description", description}, {"image", image?.toJson()}, {"link", link}, {"title", title} }; } /// Creates a copy of the preview data with an updated data. /// Null values will nullify existing values. private PreviewData copyWith( string description = null, PreviewDataImage image = null, string link = null, string title = null ) { return new PreviewData( description, image, link, title ); } } /// A utility class that forces image"s width and height to be stored /// alongside the url. /// /// See https://github.com/flyerhq/flutter_link_previewer public class PreviewDataImage : Equatable { /// Image height in pixels public readonly float height; /// Remote image URL public readonly string url; /// Image width in pixels public readonly float width; /// Creates preview data image. public PreviewDataImage( float height, string url, float width) { this.height = height; this.url = url; this.width = width; } /// Equatable props public override List props => new List {height, url, width}; /// Creates preview data image from a map (decoded JSON). public static PreviewDataImage fromJson(Dictionary json) { return new PreviewDataImage( json["height"] is float ? (float) json["height"] : 0, json["url"] as string, json["width"] is float ? (float) json["width"] : 0 ); } /// Converts preview data image to the map representation, encodable to JSON. public Dictionary toJson() { return new Dictionary { {"height", height}, {"url", url}, {"width", width} }; } } }