155 lines
4.7 KiB
C#

using Billing.Models;
using Billing.UI;
using Billing.Views;
using System;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
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}");
}
#else
#pragma warning disable IDE0060 // Remove unused parameter
public static void Debug(string message)
{
}
#pragma warning restore IDE0060 // Remove unused parameter
#endif
public static void Error(string category, Exception ex)
{
Error(category, ex?.ToString() ?? "unknown error");
}
public static void Error(string category, string message)
{
#if DEBUG
var time = DateTime.Now.ToString("HH:mm:ss.fff");
System.Diagnostics.Debug.WriteLine($"[{time}] - {category}", message);
MainThread.BeginInvokeOnMainThread(async () => await Shell.Current.DisplayAlert(category, message, "Ok"));
#endif
_ = Store.StoreHelper.SaveLogItemAsync(new Logs()
{
LogTime = DateTime.Now,
Category = category,
Detail = message
});
}
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 UIBill WrapBill(Bill b)
{
var category = App.Categories.FirstOrDefault(c => c.Id == b.CategoryId);
return new UIBill(b)
{
Icon = category?.Icon ?? Definition.DefaultIcon,
TintColor = category?.TintColor ?? Definition.TransparentColor,
Name = b.Name,
DateCreation = b.CreateTime,
Amount = b.Amount,
Wallet = App.Accounts.FirstOrDefault(a => a.Id == b.WalletId)?.Name
};
}
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);
}
public class AsyncLazy<T>
{
private readonly Lazy<Task<T>> instance;
public AsyncLazy(Func<Task<T>> factory)
{
instance = new Lazy<Task<T>>(() => Task.Run(factory));
}
public TaskAwaiter<T> GetAwaiter()
{
return instance.Value.GetAwaiter();
}
}
}