Add ProviderIdModelExtensions

This commit is contained in:
xjasonlyu
2022-06-09 14:30:08 +08:00
parent 13534ce152
commit b8c19c7df7
3 changed files with 68 additions and 62 deletions

View File

@@ -0,0 +1,67 @@
using Jellyfin.Plugin.JavTube.Models;
namespace Jellyfin.Plugin.JavTube.Extensions;
internal static class ProviderIdModelExtensions
{
private const char Separator = ':';
private static double? ParseDouble(string s)
{
try
{
return double.Parse(s);
}
catch (Exception)
{
return null;
}
}
private static bool? ParseBool(string s)
{
switch (s)
{
case "1":
case "t":
case "T":
case "true":
case "True":
case "TRUE":
return true;
case "0":
case "f":
case "F":
case "false":
case "False":
case "FALSE":
return false;
}
return null;
}
public static ProviderIdModel Deserialize(string rawPid)
{
var providerIds = rawPid?.Split(Separator);
return new ProviderIdModel
{
Provider = providerIds?.Length > 0 ? providerIds[0] : string.Empty,
Id = providerIds?.Length > 1 ? providerIds[1] : string.Empty,
Position = providerIds?.Length > 2 ? ParseDouble(providerIds[2]) : null,
UpdateInfo = providerIds?.Length > 3 ? ParseBool(providerIds[3]) : null
};
}
public static string Serialize(this ProviderIdModel pid)
{
//var pid = this;
var values = new List<string>
{
pid.Provider, pid.Id
};
if (pid.Position.HasValue) values.Add(pid.Position.ToString());
if (pid.UpdateInfo.HasValue) values.Add(pid.UpdateInfo.ToString());
return string.Join(Separator, values);
}
}

View File

@@ -9,7 +9,7 @@ internal static class ProviderIdsExtensions
{
return !instance.ProviderIds.Any()
? new ProviderIdModel()
: ProviderIdModel.Deserialize(instance.GetProviderId(name));
: ProviderIdModelExtensions.Deserialize(instance.GetProviderId(name));
}
public static void SetProviderIdModel(this IHasProviderIds instance, string name, ProviderIdModel pid)

View File

@@ -19,65 +19,4 @@ public class ProviderIdModel : ProviderModel
[JsonIgnore] public double? Position { get; set; }
[JsonIgnore] public bool? UpdateInfo { get; set; }
public string Serialize()
{
var pid = this;
var values = new List<string>
{
pid.Provider, pid.Id
};
if (pid.Position.HasValue) values.Add(pid.Position.ToString());
if (pid.UpdateInfo.HasValue) values.Add(pid.UpdateInfo.ToString());
return string.Join(Separator, values);
}
public static ProviderIdModel Deserialize(string rawPid)
{
var providerIds = rawPid?.Split(Separator);
return new ProviderIdModel
{
Provider = providerIds?.Length > 0 ? providerIds[0] : string.Empty,
Id = providerIds?.Length > 1 ? providerIds[1] : string.Empty,
Position = providerIds?.Length > 2 ? ParseDouble(providerIds[2]) : null,
UpdateInfo = providerIds?.Length > 3 ? ParseBool(providerIds[3]) : null
};
}
private const char Separator = ':';
private static double? ParseDouble(string s)
{
try
{
return double.Parse(s);
}
catch (Exception)
{
return null;
}
}
private static bool? ParseBool(string s)
{
switch (s)
{
case "1":
case "t":
case "T":
case "true":
case "True":
case "TRUE":
return true;
case "0":
case "f":
case "F":
case "false":
case "False":
case "FALSE":
return false;
}
return null;
}
}