Billing/Billing.Shared/Views/AddCategoryPage.xaml.cs
2022-03-11 22:25:31 +08:00

148 lines
4.8 KiB
C#

using Billing.Languages;
using Billing.Models;
using Billing.Themes;
using Billing.UI;
using System;
using System.Linq;
using Xamarin.Forms;
namespace Billing.Views
{
public partial class AddCategoryPage : BillingPage
{
private static readonly BindableProperty CategoryNameProperty = Helper.Create<string, AddCategoryPage>(nameof(CategoryName));
private static readonly BindableProperty CategoryIconProperty = Helper.Create<string, AddCategoryPage>(nameof(CategoryIcon));
private static readonly BindableProperty TintColorProperty = Helper.Create<Color, AddCategoryPage>(nameof(TintColor));
private static readonly BindableProperty TintColorStringProperty = Helper.Create<string, AddCategoryPage>(nameof(TintColorString));
public string CategoryName
{
get => (string)GetValue(CategoryNameProperty);
set => SetValue(CategoryNameProperty, value);
}
public string CategoryIcon
{
get => (string)GetValue(CategoryIconProperty);
set => SetValue(CategoryIconProperty, value);
}
public Color TintColor
{
get => (Color)GetValue(TintColorProperty);
set => SetValue(TintColorProperty, value);
}
public string TintColorString
{
get => (string)GetValue(TintColorStringProperty);
set => SetValue(TintColorStringProperty, value);
}
public Command CheckCategory { get; }
public Command SelectIcon { get; }
public Command ColorPickerCommand { get; }
public event EventHandler<Category> CategoryChecked;
private readonly int categoryId;
private readonly Category parent;
public AddCategoryPage(int id = -1, Category parent = null)
{
categoryId = id;
this.parent = parent;
var category = App.Categories.FirstOrDefault(c => c.Id == id);
Title = category?.Name ?? Resource.AddCategory;
if (category != null)
{
CategoryName = category.Name;
CategoryIcon = category.Icon;
if (category.TintColor.IsTransparent())
{
TintColor = BaseTheme.CurrentPrimaryColor;
}
else
{
TintColor = category.TintColor.ToColor();
}
}
else
{
TintColor = BaseTheme.CurrentPrimaryColor;
}
TintColorString = Helper.WrapColorString(TintColor.ToHex());
CheckCategory = new Command(OnCheckCategory);
SelectIcon = new Command(OnSelectIcon);
ColorPickerCommand = new Command(OnColorPickerCommand);
InitializeComponent();
}
protected override void OnLoaded()
{
editorName.SetFocus();
}
private void OnColorPickerCommand(object o)
{
if (o is Color color)
{
TintColor = color;
TintColorString = Helper.WrapColorString(color.ToHex());
}
}
private async void OnCheckCategory()
{
if (Tap.IsBusy)
{
return;
}
using (Tap.Start())
{
var currentColor = BaseTheme.CurrentPrimaryColor;
var tintColor = TintColor;
var color = (tintColor == currentColor ? Color.Transparent : tintColor).ToLong();
var category = App.Categories.FirstOrDefault(c => c.Id == categoryId);
if (category == null)
{
CategoryChecked?.Invoke(this, new Category
{
Name = CategoryName,
Icon = CategoryIcon,
TintColor = color,
ParentId = parent?.Id,
Type = parent?.Type ?? CategoryType.Spending
});
}
else
{
category.Name = CategoryName;
category.Icon = CategoryIcon;
category.TintColor = color;
CategoryChecked?.Invoke(this, category);
}
await Navigation.PopAsync();
}
}
private async void OnSelectIcon()
{
if (Tap.IsBusy)
{
return;
}
using (Tap.Start())
{
var page = new IconSelectPage(CategoryIcon);
page.IconChecked += Category_IconChecked;
await Navigation.PushAsync(page);
}
}
private void Category_IconChecked(object sender, string icon)
{
CategoryIcon = icon;
}
}
}