您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
134 行
3.9 KiB
134 行
3.9 KiB
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<object> props => new List<object>
|
|
{description, image, link, title};
|
|
|
|
/// Creates preview data from a map (decoded JSON).
|
|
public static PreviewData fromJson(Dictionary<string, object> json)
|
|
{
|
|
return new PreviewData(
|
|
json["description"] as string,
|
|
json["image"] == null
|
|
? null
|
|
: PreviewDataImage.fromJson(json["image"] as Dictionary<string, object>),
|
|
json["link"] as string,
|
|
json["title"] as string
|
|
);
|
|
}
|
|
|
|
/// Converts preview data to the map representation, encodable to JSON.
|
|
public Dictionary<string, object> toJson()
|
|
{
|
|
return new Dictionary<string, object>
|
|
{
|
|
{"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<object> props => new List<object>
|
|
{height, url, width};
|
|
|
|
/// Creates preview data image from a map (decoded JSON).
|
|
public static PreviewDataImage fromJson(Dictionary<string, object> 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<string, object> toJson()
|
|
{
|
|
return new Dictionary<string, object>
|
|
{
|
|
{"height", height},
|
|
{"url", url},
|
|
{"width", width}
|
|
};
|
|
}
|
|
}
|
|
}
|