118 lines
3.5 KiB
C#

using System;
using System.Globalization;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace Billing
{
internal static class Helper
{
#if DEBUG
public static void Debug(string message)
{
var time = DateTime.Now.ToString("HH:mm:ss.fff");
System.Diagnostics.Debug.WriteLine($"[{time}] - {message}");
}
public static void Error(string category, Exception ex)
{
Error(category, ex?.Message ?? "unknown error");
}
public static void Error(string category, string message)
{
var time = DateTime.Now.ToString("HH:mm:ss.fff");
System.Diagnostics.Debug.Fail($"[{time}] - {category}", message);
}
#else
#pragma warning disable IDE0060 // Remove unused parameter
public static void Debug(string message)
{
}
public static void Error(string category, Exception ex)
{
}
public static void Error(string category, string message)
{
}
#pragma warning restore IDE0060 // Remove unused parameter
#endif
public static bool NetworkAvailable
{
get
{
try
{
return Connectivity.NetworkAccess == NetworkAccess.Internet
|| Connectivity.NetworkAccess == NetworkAccess.ConstrainedInternet;
}
catch
{
return false;
}
}
}
public const string DEFAULT_COLOR = "#183153";
public static string WrapColorString(string color)
{
if (color == null)
{
return DEFAULT_COLOR;
}
if (color.Length > 7)
{
var alpha = color[1..3];
if (int.TryParse(alpha, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int a) && a == 255)
{
return "#" + color[3..];
}
}
return color;
}
public static bool IsSameDay(DateTime day1, DateTime day2)
{
return day1.Year == day2.Year && day1.DayOfYear == day2.DayOfYear;
}
public static DateTime FirstDay(DateTime date)
{
var week = date.DayOfWeek;
if (week == DayOfWeek.Sunday)
{
return date;
}
return date.AddDays(-(int)week);
}
public static BindableProperty Create<TResult, TOwner>(string name, TResult defaultValue = default, PropertyValueChanged<TResult, TOwner> propertyChanged = null)
where TOwner : BindableObject
{
if (propertyChanged == null)
{
return BindableProperty.Create(name, typeof(TResult), typeof(TOwner), defaultValue: defaultValue);
}
return BindableProperty.Create(name, typeof(TResult), typeof(TOwner),
defaultValue: defaultValue,
propertyChanged: (obj, old, @new) =>
{
if (old != null && !(old is TResult))
{
return;
}
if (@new != null && !(@new is TResult))
{
return;
}
propertyChanged((TOwner)obj, (TResult)old, (TResult)@new);
});
}
public delegate void PropertyValueChanged<TResult, TOwner>(TOwner obj, TResult old, TResult @new);
}
}