Skip to content

Commit b969ac1

Browse files
goran-wlove-linger
authored andcommitted
enhance: unify sorting of RepositoryNode tree, unconditional sort & save after rescan
1 parent 88c38b4 commit b969ac1

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/ViewModels/Preferences.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -436,16 +436,26 @@ public void AddNode(RepositoryNode node, RepositoryNode to, bool save)
436436
{
437437
var collection = to == null ? RepositoryNodes : to.SubNodes;
438438
collection.Add(node);
439-
collection.Sort((l, r) =>
439+
SortNodes(collection);
440+
441+
if (save)
442+
Save();
443+
}
444+
445+
public void SortNodes(List<RepositoryNode> collection)
446+
{
447+
collection?.Sort((l, r) =>
440448
{
441449
if (l.IsRepository != r.IsRepository)
442450
return l.IsRepository ? 1 : -1;
443451

444452
return string.Compare(l.Name, r.Name, StringComparison.Ordinal);
445453
});
454+
}
446455

447-
if (save)
448-
Save();
456+
public void SortAllNodes()
457+
{
458+
SortNodesRecursive(RepositoryNodes);
449459
}
450460

451461
public RepositoryNode FindNode(string id)
@@ -503,22 +513,14 @@ public void RemoveNode(RepositoryNode node, bool save)
503513
public void SortByRenamedNode(RepositoryNode node)
504514
{
505515
var container = FindNodeContainer(node, RepositoryNodes);
506-
container?.Sort((l, r) =>
507-
{
508-
if (l.IsRepository != r.IsRepository)
509-
return l.IsRepository ? 1 : -1;
510-
511-
return string.Compare(l.Name, r.Name, StringComparison.Ordinal);
512-
});
516+
SortNodes(container);
513517

514518
Save();
515519
}
516520

517-
public void AutoRemoveInvalidNode()
521+
public bool AutoRemoveInvalidNode()
518522
{
519-
var changed = RemoveInvalidRepositoriesRecursive(RepositoryNodes);
520-
if (changed)
521-
Save();
523+
return RemoveInvalidRepositoriesRecursive(RepositoryNodes);
522524
}
523525

524526
public void Save()
@@ -588,6 +590,13 @@ private void PrepareWorkspaces()
588590
}
589591
}
590592

593+
private void SortNodesRecursive(List<RepositoryNode> collection)
594+
{
595+
SortNodes(collection);
596+
foreach (var node in collection)
597+
SortNodesRecursive(node.SubNodes);
598+
}
599+
591600
private RepositoryNode FindNodeRecursive(string id, List<RepositoryNode> collection)
592601
{
593602
foreach (var node in collection)

src/ViewModels/ScanRepositories.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public override Task<bool> Sure()
6464
}
6565

6666
Preferences.Instance.AutoRemoveInvalidNode();
67+
68+
// Sort & Save unconditionally after a complete rescan.
69+
Preferences.Instance.SortAllNodes();
70+
Preferences.Instance.Save();
71+
6772
Welcome.Instance.Refresh();
6873
});
6974

@@ -151,13 +156,7 @@ private RepositoryNode FindOrCreateGroup(List<RepositoryNode> collection, string
151156
IsExpanded = true,
152157
};
153158
collection.Add(added);
154-
collection.Sort((l, r) =>
155-
{
156-
if (l.IsRepository != r.IsRepository)
157-
return l.IsRepository ? 1 : -1;
158-
159-
return string.Compare(l.Name, r.Name, StringComparison.Ordinal);
160-
});
159+
Preferences.Instance.SortNodes(collection);
161160

162161
return added;
163162
}

0 commit comments

Comments
 (0)