2020-12-25 10:49:51 -05:00
|
|
|
using System;
|
2020-12-28 09:27:37 -05:00
|
|
|
using System.Collections.ObjectModel;
|
2020-12-25 10:49:51 -05:00
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using auto_creamapi.Models;
|
2020-12-28 09:27:37 -05:00
|
|
|
using auto_creamapi.Utils;
|
2020-12-25 10:49:51 -05:00
|
|
|
using MvvmCross.Converters;
|
2020-12-28 09:27:37 -05:00
|
|
|
using MvvmCross.Platforms.Wpf.Converters;
|
2020-12-25 10:49:51 -05:00
|
|
|
|
|
|
|
namespace auto_creamapi.Converters
|
|
|
|
{
|
2020-12-28 09:27:37 -05:00
|
|
|
public class ListOfDLcToStringNativeConverter : MvxNativeValueConverter<ListOfDLcToStringConverter>
|
2020-12-25 10:49:51 -05:00
|
|
|
{
|
2020-12-28 09:27:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public class ListOfDLcToStringConverter : MvxValueConverter<ObservableCollection<SteamApp>, string>
|
|
|
|
{
|
|
|
|
protected override string Convert(ObservableCollection<SteamApp> value, Type targetType, object parameter,
|
|
|
|
CultureInfo culture)
|
2020-12-25 10:49:51 -05:00
|
|
|
{
|
2020-12-28 09:27:37 -05:00
|
|
|
MyLogger.Log.Debug("ListOfDLcToStringConverter: Convert");
|
2020-12-25 10:49:51 -05:00
|
|
|
var dlcListToString = DlcListToString(value);
|
|
|
|
return dlcListToString.GetType() == targetType ? dlcListToString : "";
|
|
|
|
}
|
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
protected override ObservableCollection<SteamApp> ConvertBack(string value, Type targetType, object parameter,
|
|
|
|
CultureInfo culture)
|
2020-12-25 10:49:51 -05:00
|
|
|
{
|
2020-12-28 09:27:37 -05:00
|
|
|
MyLogger.Log.Debug("ListOfDLcToStringConverter: ConvertBack");
|
2020-12-25 10:49:51 -05:00
|
|
|
var stringToDlcList = StringToDlcList(value);
|
2020-12-28 09:27:37 -05:00
|
|
|
return stringToDlcList.GetType() == targetType ? stringToDlcList : new ObservableCollection<SteamApp>();
|
2020-12-25 10:49:51 -05:00
|
|
|
}
|
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
private static ObservableCollection<SteamApp> StringToDlcList(string value)
|
2020-12-25 10:49:51 -05:00
|
|
|
{
|
2020-12-28 09:27:37 -05:00
|
|
|
var result = new ObservableCollection<SteamApp>();
|
2020-12-25 10:49:51 -05:00
|
|
|
var expression = new Regex(@"(?<id>.*) *= *(?<name>.*)");
|
|
|
|
using var reader = new StringReader(value);
|
|
|
|
string line;
|
|
|
|
while ((line = reader.ReadLine()) != null)
|
|
|
|
{
|
|
|
|
var match = expression.Match(line);
|
|
|
|
if (match.Success)
|
|
|
|
result.Add(new SteamApp
|
|
|
|
{
|
|
|
|
AppId = int.Parse(match.Groups["id"].Value),
|
|
|
|
Name = match.Groups["name"].Value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
private static string DlcListToString(ObservableCollection<SteamApp> value)
|
2020-12-25 10:49:51 -05:00
|
|
|
{
|
|
|
|
var result = "";
|
2020-12-28 09:27:37 -05:00
|
|
|
//value.ForEach(x => result += $"{x}\n");
|
|
|
|
foreach (var steamApp in value) result += $"{steamApp}\n";
|
2020-12-25 10:49:51 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|