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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using XFEEDToolBox.ViewModels;
using XFEEDToolBox.Core.Galaxy;
using Microsoft.UI.Xaml.Input;
namespace XFEEDToolBox.Views;
public sealed partial class PlannerPage : Page
{
    public ToolboxViewModel ViewModel => App.ViewModel;
    private CancellationTokenSource? regionSuggestions,regionCommit;
    private string committingCenter="";
    public PlannerPage() { InitializeComponent(); NavigationCacheMode = Microsoft.UI.Xaml.Navigation.NavigationCacheMode.Required; }
    protected override void OnNavigatedFrom(Microsoft.UI.Xaml.Navigation.NavigationEventArgs args)
    {regionSuggestions?.Cancel();regionCommit?.Cancel();base.OnNavigatedFrom(args);}
    private async void TradeRegionCenterChanged(AutoSuggestBox sender,AutoSuggestBoxTextChangedEventArgs args)
    {
        if(args.Reason!=AutoSuggestionBoxTextChangeReason.UserInput)return;
        regionCommit?.Cancel();ViewModel.TradeRegionCenterText=sender.Text;
        regionSuggestions?.Cancel();regionSuggestions?.Dispose();regionSuggestions=new();var stop=regionSuggestions;
        var query=sender.Text.Trim();
        if(query.Length<2){sender.ItemsSource=null;sender.IsSuggestionListOpen=false;return;}
        try
        {
            await Task.Delay(250,stop.Token);var found=await ViewModel.SuggestGalacticSystemsAsync(query,stop.Token);
            if(stop.IsCancellationRequested||sender.Text.Trim()!=query)return;
            sender.ItemsSource=found;sender.IsSuggestionListOpen=found.Count>0;
        }
        catch(OperationCanceledException){}
        catch(Exception error){App.Runtime.Log("跑商中心补全",error.ToString());}
    }
    private async void TradeRegionCenterChosen(AutoSuggestBox sender,AutoSuggestBoxSuggestionChosenEventArgs args)
    {if(args.SelectedItem is GalacticSystem system)await CommitCenterAsync(system.Name);}
    private async void TradeRegionCenterSubmitted(AutoSuggestBox sender,AutoSuggestBoxQuerySubmittedEventArgs args)
    {await CommitCenterAsync(args.ChosenSuggestion is GalacticSystem system?system.Name:sender.Text);}
    private async Task CommitCenterAsync(string name)
    {
        if(committingCenter==name&&ViewModel.TradeRegionBusy)return;
        if(!ViewModel.TradeRegionEditable)return;
        regionCommit?.Cancel();regionCommit?.Dispose();regionCommit=new();var stop=regionCommit;committingCenter=name;
        regionSuggestions?.Cancel();TradeRegionCenterInput.IsSuggestionListOpen=false;
        try{await ViewModel.CommitTradeRegionCenterAsync(name,stop.Token);}
        catch(OperationCanceledException){}
        catch(Exception error){ViewModel.ReportTradeRegionError(error);}
        finally{if(ReferenceEquals(regionCommit,stop))committingCenter="";}
    }
    private async void TradeRegionRadiusCommitted(object sender,RoutedEventArgs args)=>await CommitRadiusAsync();
    private async void TradeRegionRadiusKeyDown(object sender,KeyRoutedEventArgs args)
    {if(args.Key==Windows.System.VirtualKey.Enter){args.Handled=true;await CommitRadiusAsync();}}
    private async Task CommitRadiusAsync()
    {
        if(!ViewModel.TradeRegionEditable)return;
        try{await ViewModel.SaveTradeRegionRadiusAsync();}
        catch(OperationCanceledException){}
        catch(Exception error){ViewModel.ReportTradeRegionError(error);}
    }
}