Files
mapo-iob-man/IOB-MAN.Core/Utils.cs
T
2025-06-17 09:15:06 +02:00

97 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IOB_MAN.Core
{
public class Utils
{
#region Public Methods
/// <summary>
/// Comparazione tra 2 dizionari
/// </summary>
/// <param name="dict1"></param>
/// <param name="dict2"></param>
/// <returns></returns>
public static bool CompareDict(Dictionary<string, string> dict1, Dictionary<string, string> dict2)
{
bool areEqual = true;
if (dict1.Count != dict2.Count)
{
areEqual = false;
}
else
{
// ciclo da dict1 ogni valore che so essere in ugual numero in dict2
foreach (var kvp in dict1)
{
if (!dict2.TryGetValue(kvp.Key, out string? value) || value != kvp.Value)
{
areEqual = false;
break;
}
}
}
return areEqual;
}
/// <summary>
/// Helper formattazione durata human readable
/// </summary>
/// <param name="timeSpan"></param>
/// <param name="textMode">textMode = come linguaggio scritto</param>
/// <returns></returns>
public static string FormatTimeSpan(TimeSpan timeSpan, bool textMode = false)
{
if (textMode)
{
//Func<Tuple<int, string>, string> tupleFormatter = t => $"{t.Item1} {t.Item2}{(t.Item1 == 1 ? string.Empty : "s")}";
//Func<Tuple<int, string>, string> tupleFormatter = t => $"{t.Item1}{t.Item2}";
Func<Tuple<int, string>, string> tupleFormatter = t => $"{t.Item1} {(t.Item1 == 1 ? t.Item2 : t.Item2.Replace("giorno", "giorni").Replace("ora", "ore"))}";
var components = new List<Tuple<int, string>>
{
Tuple.Create((int) timeSpan.TotalDays, "giorno"),
Tuple.Create(timeSpan.Hours, "ora"),
Tuple.Create(timeSpan.Minutes, "min"),
Tuple.Create(timeSpan.Seconds, "sec"),
};
components.RemoveAll(i => i.Item1 == 0);
string extra = "";
if (components.Count > 1)
{
var finalComponent = components[components.Count - 1];
components.RemoveAt(components.Count - 1);
extra = $" e {tupleFormatter(finalComponent)}";
}
return $"{string.Join(", ", components.Select(tupleFormatter))}{extra}";
}
else
{
//Func<Tuple<int, string>, string> tupleFormatter = t => $"{t.Item1} {t.Item2}{(t.Item1 == 1 ? string.Empty : "s")}";
//Func<Tuple<int, string>, string> tupleFormatter = t => $"{t.Item1}{t.Item2}";
Func<Tuple<int, string>, string> tupleFormatter = t => $"{t.Item1:00}";
var components = new List<Tuple<int, string>>
{
Tuple.Create((int) timeSpan.TotalDays, "g"),
Tuple.Create(timeSpan.Hours, ""),
Tuple.Create(timeSpan.Minutes, ""),
Tuple.Create(timeSpan.Seconds, ""),
};
//components.RemoveAll(i => i.Item1 == 0);
return $"{string.Join(":", components.Select(tupleFormatter))}";
}
}
#endregion Public Methods
}
}