XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using XFEEDToolBox.Core.Galaxy;

namespace XFEEDToolBox.Controls;

public sealed partial class GalaxyFilterPanel
{
    private IReadOnlyList<GalaxyCommodityGroup> historicalGroups=[];
    private readonly Dictionary<string,(CheckBox Check,GalaxyCommodityGroup Group)> historicalParents=[];
    private readonly Dictionary<string,bool> historicalExpanded=[];

    private void RefreshHistoricalCommodityTree(GalaxyMapFilter filter)
    {
        // Original TradeRoute's 13-category/123-commodity resource is separate
        // from Market taxonomy and runtime CommodityList. It supplies parents,
        // never additional cached trade records.
        historicalGroups=GalaxyOriginalTradeCategories.Build(HistoricalTradeChoices);
        var search=historicalTradeSearch.Text.Trim();
        bool Contains(string value)=>value.Contains(search,StringComparison.OrdinalIgnoreCase);
        var shown=historicalGroups.Select(g=>search.Length==0||Contains(g.Label)||Contains(g.Key)?g:g with{Children=g.Children.Where(c=>Contains(c.Label)||Contains(c.Key)).ToArray()}).Where(g=>g.Children.Count>0).ToArray();
        var signature=string.Join('|',shown.Select(g=>g.Key+":"+string.Join(',',g.Children.Select(c=>c.Key+"="+c.Label))));
        if(historicalCommoditySignature!=signature)
        {
            historicalCommoditySignature=signature;historicalTradeCommodities.Children.Clear();historicalCommodityChecks.Clear();historicalParents.Clear();
            foreach(var group in shown)
            {
                var complete=historicalGroups.First(g=>g.Key==group.Key);
                var content=new StackPanel{Spacing=1};var parent=new CheckBox{Content="全选此类",IsThreeState=true};content.Children.Add(parent);historicalParents[group.Key]=(parent,complete);
                parent.Click+=(_,_)=>
                {
                    if(updating)return;
                    Filter=Filter with{HistoricalTradeCommodities=GalaxyCommodityTree.SelectGroup(Filter.HistoricalTradeCommodities,historicalGroups.SelectMany(g=>g.Children).Select(c=>c.Key),complete.Children.Select(c=>c.Key),GalaxyCommodityTree.Selected(Filter.HistoricalTradeCommodities,complete.Children)!=true)};
                };
                foreach(var child in group.Children)
                {
                    var check=new CheckBox{Content=child.Label};historicalCommodityChecks[child.Key]=check;content.Children.Add(check);
                    void Choose(object sender,RoutedEventArgs args)
                    {
                        if(updating)return;
                        var selected=Filter.HistoricalTradeCommodities is {} set?new HashSet<string>(set):HistoricalTradeChoices.Select(c=>c.Key).ToHashSet();
                        if(check.IsChecked==true)selected.Add(child.Key);else selected.Remove(child.Key);
                        Filter=Filter with{HistoricalTradeCommodities=selected};
                    }
                    check.Checked+=Choose;check.Unchecked+=Choose;
                }
                var expander=new Expander{Header=group.Label+" · "+complete.Children.Count,Content=content,HorizontalAlignment=HorizontalAlignment.Stretch,HorizontalContentAlignment=HorizontalAlignment.Stretch,IsExpanded=search.Length>0||historicalExpanded.GetValueOrDefault(group.Key)};
                expander.Expanding+=(_,_)=>historicalExpanded[group.Key]=true;expander.Collapsed+=(_,_)=>historicalExpanded[group.Key]=false;historicalTradeCommodities.Children.Add(expander);
            }
        }
        foreach(var entry in historicalCommodityChecks){entry.Value.IsChecked=filter.HistoricalTradeCommodities is null||filter.HistoricalTradeCommodities.Contains(entry.Key);entry.Value.IsEnabled=filter.ShowHistoricalTradeFlows;}
        foreach(var entry in historicalParents){entry.Value.Check.IsChecked=GalaxyCommodityTree.Selected(filter.HistoricalTradeCommodities,entry.Value.Group.Children);entry.Value.Check.IsEnabled=filter.ShowHistoricalTradeFlows;}
    }
}