using Billing.Languages; using Billing.Models; using Billing.UI; using System; using System.Collections.Generic; using System.Linq; using Xamarin.Forms; namespace Billing.Views { public partial class CategorySelectPage : BillingPage { private static readonly BindableProperty TopCategoriesProperty = Helper.Create, CategorySelectPage>(nameof(TopCategories)); private static readonly BindableProperty SubCategoriesProperty = Helper.Create, CategorySelectPage>(nameof(SubCategories)); public List TopCategories { get => (List)GetValue(TopCategoriesProperty); set => SetValue(TopCategoriesProperty, value); } public List SubCategories { get => (List)GetValue(SubCategoriesProperty); set => SetValue(SubCategoriesProperty, value); } public Command TapTopCategory { get; } public Command TapSubCategory { get; } public event EventHandler CategoryTapped; private readonly int categoryId; public CategorySelectPage(int id) { categoryId = id; TapTopCategory = new Command(OnTopCategoryTapped); TapSubCategory = new Command(OnSubCategoryTapped); TopCategories = new List { new(Resource.Spending, App.Categories.Where(c => c.Type == CategoryType.Spending && c.ParentId == null).Select(c => WrapCategory(c))), new(Resource.Income, App.Categories.Where(c => c.Type == CategoryType.Income && c.ParentId == null).Select(c => WrapCategory(c))) }; UICategory last; if (App.Categories.Any(c => c.LastUsed != null)) { last = new UICategory(null) { IsChecked = true, Icon = "rank", Name = Resource.LastSelected, TintColor = Definition.TransparentColor }; TopCategories.Insert(0, new(Resource.Recent) { last }); } else { last = null; } UICategory cat; var category = App.Categories.FirstOrDefault(c => c.Id == categoryId); if (category == null) { cat = TopCategories.Where(g => g.Count > 0).Select(g => g.First()).FirstOrDefault(); } else if (category.ParentId == null) { cat = TopCategories.SelectMany(g => g).FirstOrDefault(c => c.Category == category); } else { cat = TopCategories.SelectMany(g => g).FirstOrDefault(c => c.Category?.Id == category.ParentId) ?? last; } DoRefreshSubCategories(cat); InitializeComponent(); } private UICategory WrapCategory(Category c) { return new UICategory(c) { IsChecked = c.Id == categoryId, Icon = c.Icon, Name = c.Name, TintColor = c.TintColor }; } private void DoRefreshSubCategories(UICategory category) { var many = TopCategories.SelectMany(g => g); foreach (var m in many) { m.IsChecked = m == category; } if (category == null) { return; } if (category.Category == null) { SubCategories = App.Categories.Where(c => c.ParentId != null && c.LastUsed != null).OrderByDescending(c => c.LastUsed).Take(10).Select(c => WrapCategory(c)).ToList(); } else { SubCategories = App.Categories.Where(c => c.ParentId == category.Category.Id).Select(c => WrapCategory(c)).ToList(); } } private async void OnTopCategoryTapped(object o) { if (Tap.IsBusy) { return; } using (Tap.Start()) { if (o is UICategory category) { DoRefreshSubCategories(category); if (SubCategories?.Count == 0) { CategoryTapped?.Invoke(this, category); await Navigation.PopAsync(); } } } } private async void OnSubCategoryTapped(object o) { if (Tap.IsBusy) { return; } using (Tap.Start()) { if (o is UICategory category) { CategoryTapped?.Invoke(this, category); await Navigation.PopAsync(); } } } } }