Billing/Billing.Shared/Views/SettingPage.xaml.cs

159 lines
5.3 KiB
C#

using System.IO;
using Billing.Store;
using Billing.Themes;
using Billing.UI;
using Xamarin.Essentials;
using Xamarin.Forms;
using Resource = Billing.Languages.Resource;
namespace Billing.Views
{
public partial class SettingPage : BillingPage
{
private static readonly BindableProperty VersionProperty = Helper.Create<string, SettingPage>(nameof(Version));
private static readonly BindableProperty PrimaryColorProperty = Helper.Create<string, SettingPage>(nameof(PrimaryColor));
private static readonly BindableProperty ManyRecordsProperty = Helper.Create<string, SettingPage>(nameof(ManyRecords));
public string Version => (string)GetValue(VersionProperty);
public string PrimaryColor
{
get => (string)GetValue(PrimaryColorProperty);
set => SetValue(PrimaryColorProperty, value);
}
public string ManyRecords => (string)GetValue(ManyRecordsProperty);
public Command ShareCommand { get; }
public Command CategoryCommand { get; }
public Command ColorPickerCommand { get; }
public Command ShareLogsCommand { get; }
public SettingPage()
{
ShareCommand = new Command(OnShareCommand);
CategoryCommand = new Command(OnCategoryCommand);
ColorPickerCommand = new Command(OnColorPickerCommand);
ShareLogsCommand = new Command(OnShareLogsCommand);
InitializeComponent();
var (main, build) = Definition.GetVersion();
SetValue(VersionProperty, $"{main} ({build})");
}
protected override async void OnAppearing()
{
base.OnAppearing();
//SetValue(VersionProperty, $"{AppInfo.VersionString} ({AppInfo.BuildString})");
var colorString = Preferences.Get(Definition.PrimaryColorKey, Helper.DEFAULT_COLOR);
PrimaryColor = Helper.WrapColorString(colorString);
var count = await StoreHelper.GetLogsCount();
SetValue(ManyRecordsProperty, string.Format(Resource.ManyRecords, count));
}
protected override void OnDisappearing()
{
base.OnDisappearing();
Preferences.Set(Definition.PrimaryColorKey, PrimaryColor);
//Light.Instance.RefreshColor(Color.FromHex(color));
}
private async void OnShareCommand()
{
if (Tap.IsBusy)
{
return;
}
using (Tap.Start())
{
await Share.RequestAsync(new ShareFileRequest
{
File = new ShareFile(StoreHelper.DatabasePath, "application/vnd.sqlite3")
});
}
}
private async void OnCategoryCommand()
{
if (Tap.IsBusy)
{
return;
}
using (Tap.Start())
{
var page = new CategoryPage();
await Navigation.PushAsync(page);
}
}
private void OnColorPickerCommand(object o)
{
if (o is Color color)
{
PrimaryColor = Helper.WrapColorString(color.ToHex());
Light.Instance.RefreshColor(color);
}
}
private async void OnShareLogsCommand()
{
if (Tap.IsBusy)
{
return;
}
using (Tap.Start())
{
string file;
var count = await StoreHelper.GetLogsCount();
if (count > 0)
{
file = await StoreHelper.ExportLogs();
}
else
{
file = StoreHelper.GetLogFile();
}
if (file != null && File.Exists(file))
{
#if __IOS__
var sendEmail = Resource.SendEmail;
var shareLogs = Resource.ShareLogs;
var result = await DisplayActionSheet(Resource.HowToShareDiagnostic, Resource.Cancel, null, sendEmail, shareLogs);
if (result == sendEmail)
{
try
{
await Email.ComposeAsync(new EmailMessage
{
To = { "tsorgy@gmail.com " },
Subject = Resource.ShareLogs,
Attachments =
{
new(file, "text/csv")
}
});
}
catch (System.Exception ex)
{
Helper.Error("email.send", ex);
}
}
else if (result == shareLogs)
{
await Share.RequestAsync(new ShareFileRequest
{
File = new ShareFile(file, "text/csv")
});
}
#else
await Share.RequestAsync(new ShareFileRequest
{
File = new ShareFile(file, "text/csv")
});
#endif
}
}
}
}
}