using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text.RegularExpressions; using auto_creamapi.Models; using MvvmCross.Converters; namespace auto_creamapi.Converters { public class ListOfDLcToStringConverter : MvxValueConverter, string> { protected override string Convert(List value, Type targetType, object parameter, CultureInfo culture) { var dlcListToString = DlcListToString(value); return dlcListToString.GetType() == targetType ? dlcListToString : ""; } protected override List ConvertBack(string value, Type targetType, object parameter, CultureInfo culture) { var stringToDlcList = StringToDlcList(value); return stringToDlcList.GetType() == targetType ? stringToDlcList : new List(); } private static List StringToDlcList(string value) { var result = new List(); var expression = new Regex(@"(?.*) *= *(?.*)"); 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; } private static string DlcListToString(List value) { var result = ""; value.ForEach(x => result += $"{x}\n"); return result; } } }