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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

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

namespace XFEEDToolBox.Core.Presentation;

public static class SystemLocationUpdates
{
    public static ListDelta ApplyLocations(this LiveCollection<SystemLocationLiveRow,SystemLocation,string> rows,IReadOnlyList<SystemLocation> sorted,bool sortExisting=false)
    {
        if(sortExisting||rows.Count==0)return rows.Apply(sorted);
        var incoming=sorted.ToDictionary(x=>x.Identity,StringComparer.Ordinal);
        // Distances and selection bind to the existing row. Automatic samples
        // do not reshuffle surviving items underneath the user's scroll/click.
        var ordered=rows.Where(x=>incoming.ContainsKey(x.Record.Identity)).Select(x=>incoming[x.Record.Identity]).ToList();
        var kept=ordered.Select(x=>x.Identity).ToHashSet(StringComparer.Ordinal);
        foreach(var added in sorted.Where(x=>!kept.Contains(x.Identity)))
        {
            var position=ordered.FindIndex(x=>(x.DistanceMetres??double.MaxValue)>(added.DistanceMetres??double.MaxValue));
            if(position<0)ordered.Add(added);else ordered.Insert(position,added);
        }
        return rows.Apply(ordered);
    }
}