Archived
1
0
This repository has been archived on 2026-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
FoxTube/FoxTube.Core/Extensions.cs
T
2020-05-09 23:16:19 +03:00

114 lines
3.1 KiB
C#

using FoxTube.Utils;
using SQLitePCL;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Xml;
using Windows.UI;
namespace FoxTube
{
public static class Extensions
{
public static Uri ToUri(this string url)
{
Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out Uri result);
return result;
}
public static string ReplaceInvalidChars(this string str, char newValue)
{
foreach (char i in Path.GetInvalidFileNameChars())
str = str.Replace(i, newValue);
return str;
}
public static Windows.Data.Xml.Dom.XmlDocument ToXml(this string text)
{
Windows.Data.Xml.Dom.XmlDocument doc = new Windows.Data.Xml.Dom.XmlDocument();
try
{
doc.LoadXml(text);
return doc;
}
catch { return null; }
}
public static bool Belongs<T>(this T obj, params T[] args) =>
args.Contains(obj);
public static string ToHex(this Color color) =>
$"#{color.R:X}{color.G:X}{color.B:X}";
public static Color FromHex(this Color parent, string hex)
{
hex = hex.Replace("#", "");
List<byte> values = new List<byte>();
for (int k = 0; k < hex.Length; k++)
values.Add(byte.Parse(string.Join("", hex[k], hex[++k]), System.Globalization.NumberStyles.HexNumber));
return hex.Length switch
{
6 => Color.FromArgb(255, values[0], values[1], values[2]),
8 => Color.FromArgb(values[0], values[1], values[2], values[3]),
_ => Colors.Black
};
}
public static TimeSpan GetDuration(this string rawDuration)
{
try
{
return XmlConvert.ToTimeSpan(rawDuration);
}
catch (FormatException)
{
TimeSpan time = XmlConvert.ToTimeSpan("PT" + rawDuration.Split('T')[1]);
TimeSpan date = TimeSpan.FromDays(int.Parse(rawDuration.Split('W')[0].Remove('P')) * 7);
date.Add(time);
return date;
}
catch (Exception e)
{
Metrics.SendReport(new Exception("Failed to parse duration", e), null, ("RawDuration", rawDuration));
return TimeSpan.FromMilliseconds(0);
}
}
public static string GetFriendlyDate(this DateTime date)
{
TimeSpan span = DateTime.Now - date;
if (span.TotalMinutes < 1)
return "Just now";
else if (Math.Round(span.TotalMinutes) == 1)
return "Minute ago";
else if (span.TotalMinutes < 60)
return Math.Round(span.TotalMinutes) + " " + "minutes ago";
else if (Math.Round(span.TotalHours) == 1)
return "Hour ago";
else if (span.TotalHours < 24)
return Math.Round(span.TotalHours) + " " + "hours ago";
else if (Math.Round(span.TotalDays) == 1)
return "Day ago";
else if (span.TotalDays < 7)
return Math.Round(span.TotalDays) + " " + "days ago";
else if (Math.Round(span.TotalDays) == 7)
return "Week ago";
else if (span.TotalDays < 30)
return Math.Round(span.TotalDays / 7) + " " + "weeks ago";
else if (Math.Round(span.TotalDays) == 30)
return "Month ago";
else if (Math.Round(span.TotalDays) < 365)
return Math.Round(span.TotalDays / 30) + " " + "months ago";
else if (Math.Round(span.TotalDays / 365) == 365)
return "Year ago";
else
return Math.Round(span.TotalDays / 365) + " " + "years ago";
}
}
}