Skip to content

Commit b15d714

Browse files
committed
enhance: update repository status immediately on the first time that it is added
Signed-off-by: leo <longshuang@msn.cn>
1 parent fb046d5 commit b15d714

File tree

7 files changed

+42
-11
lines changed

7 files changed

+42
-11
lines changed

src/ViewModels/Clone.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ public override async Task<bool> Sure()
150150
log.Complete();
151151

152152
var node = Preferences.Instance.FindOrAddNodeByRepositoryPath(path, null, true);
153+
await node.UpdateStatusAsync(false, null);
154+
153155
var launcher = App.GetLauncher();
154156
LauncherPage page = null;
155157
foreach (var one in launcher.Pages)

src/ViewModels/Init.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public override async Task<bool> Sure()
3939

4040
if (succ)
4141
{
42-
Preferences.Instance.FindOrAddNodeByRepositoryPath(_targetPath, _parentNode, true);
42+
var node = Preferences.Instance.FindOrAddNodeByRepositoryPath(_targetPath, _parentNode, true);
43+
await node.UpdateStatusAsync(false, null);
44+
4345
Welcome.Instance.Refresh();
4446
}
4547
return succ;

src/ViewModels/RepositoryNode.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,30 @@ public async Task UpdateStatusAsync(bool force, CancellationToken? token)
135135
if (token is { IsCancellationRequested: true })
136136
return;
137137

138-
if (!_isRepository || !Directory.Exists(_id))
138+
if (!_isRepository)
139139
{
140140
Status = null;
141141

142142
if (SubNodes.Count > 0)
143143
{
144-
foreach (var subNode in SubNodes)
145-
await subNode.UpdateStatusAsync(force, token);
144+
// avoid collection was modified while enumerating.
145+
var nodes = new List<RepositoryNode>();
146+
nodes.AddRange(SubNodes);
147+
148+
foreach (var node in nodes)
149+
await node.UpdateStatusAsync(force, token);
146150
}
147151

148152
return;
149153
}
150154

155+
if (!Directory.Exists(_id))
156+
{
157+
_lastUpdateStatus = DateTime.Now;
158+
Status = null;
159+
return;
160+
}
161+
151162
if (!force)
152163
{
153164
var passed = DateTime.Now - _lastUpdateStatus;

src/ViewModels/ScanRepositories.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,15 @@ public override async Task<bool> Sure()
9797
var parent = new DirectoryInfo(f).Parent!.FullName.Replace('\\', '/').TrimEnd('/');
9898
if (parent.Equals(normalizedRoot, StringComparison.Ordinal))
9999
{
100-
Preferences.Instance.FindOrAddNodeByRepositoryPath(f, null, false, false);
100+
var node = Preferences.Instance.FindOrAddNodeByRepositoryPath(f, null, false, false);
101+
await node.UpdateStatusAsync(false, null);
101102
}
102103
else if (parent.StartsWith(normalizedRoot, StringComparison.Ordinal))
103104
{
104105
var relative = parent.Substring(normalizedRoot.Length).TrimStart('/');
105106
var group = FindOrCreateGroupRecursive(Preferences.Instance.RepositoryNodes, relative);
106-
Preferences.Instance.FindOrAddNodeByRepositoryPath(f, group, false, false);
107+
var node = Preferences.Instance.FindOrAddNodeByRepositoryPath(f, group, false, false);
108+
await node.UpdateStatusAsync(false, null);
107109
}
108110
}
109111

src/ViewModels/Welcome.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,19 @@ public void Refresh()
5555

5656
public async Task UpdateStatusAsync(bool force, CancellationToken? token)
5757
{
58-
foreach (var node in Preferences.Instance.RepositoryNodes)
58+
if (_isUpdatingStatus)
59+
return;
60+
61+
_isUpdatingStatus = true;
62+
63+
// avoid collection was modified while enumerating.
64+
var nodes = new List<RepositoryNode>();
65+
nodes.AddRange(Preferences.Instance.RepositoryNodes);
66+
67+
foreach (var node in nodes)
5968
await node.UpdateStatusAsync(force, token);
69+
70+
_isUpdatingStatus = false;
6071
}
6172

6273
public void ToggleNodeIsExpanded(RepositoryNode node)
@@ -130,9 +141,11 @@ public void InitRepository(string path, RepositoryNode parent, string reason)
130141
activePage.Popup = new Init(activePage.Node.Id, path, parent, reason);
131142
}
132143

133-
public void AddRepository(string path, RepositoryNode parent, bool moveNode, bool open)
144+
public async Task AddRepositoryAsync(string path, RepositoryNode parent, bool moveNode, bool open)
134145
{
135146
var node = Preferences.Instance.FindOrAddNodeByRepositoryPath(path, parent, moveNode);
147+
await node.UpdateStatusAsync(false, null);
148+
136149
if (open)
137150
node.Open();
138151
}
@@ -277,5 +290,6 @@ private void MakeTreeRows(List<RepositoryNode> rows, List<RepositoryNode> nodes,
277290
}
278291

279292
private string _searchFilter = string.Empty;
293+
private bool _isUpdatingStatus = false;
280294
}
281295
}

src/Views/Welcome.axaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ private async void DropOnTreeView(object sender, DragEventArgs e)
279279
var path = await ViewModels.Welcome.Instance.GetRepositoryRootAsync(item.Path.LocalPath);
280280
if (!string.IsNullOrEmpty(path))
281281
{
282-
ViewModels.Welcome.Instance.AddRepository(path, null, true, false);
282+
await ViewModels.Welcome.Instance.AddRepositoryAsync(path, null, true, false);
283283
refresh = true;
284284
}
285285
}
@@ -338,7 +338,7 @@ private async void DropOnTreeNode(object sender, DragEventArgs e)
338338
var path = await ViewModels.Welcome.Instance.GetRepositoryRootAsync(item.Path.LocalPath);
339339
if (!string.IsNullOrEmpty(path))
340340
{
341-
ViewModels.Welcome.Instance.AddRepository(path, to, true, false);
341+
await ViewModels.Welcome.Instance.AddRepositoryAsync(path, to, true, false);
342342
refresh = true;
343343
}
344344
}

src/Views/WelcomeToolbar.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private async void OpenLocalRepository(object _1, RoutedEventArgs e)
4747
var repoPath = await ViewModels.Welcome.Instance.GetRepositoryRootAsync(folderPath);
4848
if (!string.IsNullOrEmpty(repoPath))
4949
{
50-
ViewModels.Welcome.Instance.AddRepository(repoPath, null, false, true);
50+
await ViewModels.Welcome.Instance.AddRepositoryAsync(repoPath, null, false, true);
5151
ViewModels.Welcome.Instance.Refresh();
5252
}
5353
else if (Directory.Exists(folderPath))

0 commit comments

Comments
 (0)