2020-12-25 10:49:51 -05:00
|
|
|
using System;
|
2021-01-13 07:04:35 -05:00
|
|
|
using System.Collections.Generic;
|
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
|
|
|
{
|
2021-01-13 07:04:35 -05:00
|
|
|
if (value == null) return "";
|
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);
|
2023-12-23 16:55:26 -05:00
|
|
|
return stringToDlcList.GetType() == targetType ? stringToDlcList : [];
|
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>();
|
2023-12-23 16:55:26 -05:00
|
|
|
var expression = new Regex("(?<id>.*) *= *(?<name>.*)");
|
2020-12-25 10:49:51 -05:00
|
|
|
using var reader = new StringReader(value);
|
|
|
|
string line;
|
|
|
|
while ((line = reader.ReadLine()) != null)
|
|
|
|
{
|
|
|
|
var match = expression.Match(line);
|
|
|
|
if (match.Success)
|
2023-12-23 16:55:26 -05:00
|
|
|
{
|
2020-12-25 10:49:51 -05:00
|
|
|
result.Add(new SteamApp
|
|
|
|
{
|
|
|
|
AppId = int.Parse(match.Groups["id"].Value),
|
|
|
|
Name = match.Groups["name"].Value
|
|
|
|
});
|
2023-12-23 16:55:26 -05:00
|
|
|
}
|
2020-12-25 10:49:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-13 07:04:35 -05:00
|
|
|
private static string DlcListToString(IEnumerable<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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|