/// <summary>Occurs on the background thread immediately before <see cref="RootNode{TChild}.ReloadNodes"/> is called.</summary> protected override void OnReloading(ICollection <BaseBranchNode> olds, ICollection <BaseBranchNode> news) { base.OnReloading(olds, news); // recursive get branches... List <string> branches = new List <string>(); Func <List <string>, BaseBranchNode, List <string> > getChildBranches = ((seed, node) => { BranchPathNode branchPath = node as BranchPathNode; if (branchPath != null) {// BranchPath return(branchPath.Recurse( seed, (bbp, count) => (from branchNode in branchPath.Children let branch = branchNode as BranchNode where branch != null select branch.FullPath).ToList())); }// else Branch return(new List <string> { node.FullPath }); }); foreach (BaseBranchNode baseBranchNode in news) { branches.AddRange(getChildBranches(branches, baseBranchNode)); } Branches = branches; SetFavorites(news); }
public T Recurse <T>(T seed, Func <BaseBranchNode, T, T> aggregate) { T egg = seed; foreach (BaseBranchNode baseBranchNode in Children) { egg = aggregate(baseBranchNode, seed); BranchPathNode branchPath = baseBranchNode as BranchPathNode; if (branchPath != null) { egg = branchPath.Recurse(egg, aggregate); } } return(egg); }
void SetFavorites(ICollection <BaseBranchNode> news) { favorites.Clear(); var favs = (from section in UiCommands.Module.GetLocalConfig().ConfigSections where Equals(section.SectionName, "branch") let value = section.GetValue("fav") where value.IsNotNullOrWhitespace() select section.SubSection).ToList(); if (favs.Any() == false) {// no branches config'd -> return return; } // else... Func <List <BranchNode>, BaseBranchNode, List <BranchNode> > getFavBranches = ((seed, node) => { BranchPathNode branchPath = node as BranchPathNode; if (branchPath != null) { // BranchPath return(branchPath.Recurse( seed, (bbp, favors) => (from branchNode in branchPath.Children let branch = branchNode as BranchNode where branch != null let isFav = favs.Any(fav => Equals(fav, branchNode.FullPath)) where isFav select branch).ToList())); } // else (Branch) BranchNode branchNod = node as BranchNode; if (branchNod != null && favs.Any(fav => Equals(fav, branchNod.FullPath))) {// branch AND return(new List <BranchNode> { branchNod }); } return(new List <BranchNode>()); }); foreach (BaseBranchNode baseBranchNode in news) { favorites.AddRange(getFavBranches(favorites, baseBranchNode)); } }