|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
3 | 4 | using System.Threading; |
4 | 5 | using System.Threading.Tasks; |
5 | 6 |
|
@@ -28,9 +29,12 @@ public string SearchFilter |
28 | 29 | } |
29 | 30 | } |
30 | 31 |
|
| 32 | + public CancellationToken StatusCheckCancellationToken => _statusCheckCancellation.Token; |
| 33 | + |
31 | 34 | public Welcome() |
32 | 35 | { |
33 | 36 | Refresh(); |
| 37 | + Preferences.Instance.PropertyChanged += OnPreferencesPropertyChanged; |
34 | 38 | } |
35 | 39 |
|
36 | 40 | public void Refresh() |
@@ -59,14 +63,41 @@ public async Task UpdateStatusAsync(bool force, CancellationToken? token) |
59 | 63 |
|
60 | 64 | _isUpdatingStatus = true; |
61 | 65 |
|
62 | | - // avoid collection was modified while enumerating. |
63 | | - var nodes = new List<RepositoryNode>(); |
64 | | - nodes.AddRange(Preferences.Instance.RepositoryNodes); |
| 66 | + try |
| 67 | + { |
| 68 | + // avoid collection was modified while enumerating. |
| 69 | + var nodes = new List<RepositoryNode>(); |
| 70 | + nodes.AddRange(Preferences.Instance.RepositoryNodes); |
65 | 71 |
|
66 | | - foreach (var node in nodes) |
67 | | - await node.UpdateStatusAsync(force, token); |
| 72 | + foreach (var node in nodes) |
| 73 | + await node.UpdateStatusAsync(force, token); |
| 74 | + } |
| 75 | + finally |
| 76 | + { |
| 77 | + _isUpdatingStatus = false; |
| 78 | + } |
| 79 | + } |
68 | 80 |
|
69 | | - _isUpdatingStatus = false; |
| 81 | + private async void OnPreferencesPropertyChanged(object sender, PropertyChangedEventArgs e) |
| 82 | + { |
| 83 | + if (e.PropertyName != nameof(Preferences.CheckRepositoryStatusOnWelcome)) |
| 84 | + return; |
| 85 | + |
| 86 | + if (Preferences.Instance.CheckRepositoryStatusOnWelcome) |
| 87 | + { |
| 88 | + while (_isUpdatingStatus) |
| 89 | + await Task.Delay(50); |
| 90 | + |
| 91 | + if (!Preferences.Instance.CheckRepositoryStatusOnWelcome) |
| 92 | + return; |
| 93 | + |
| 94 | + await UpdateStatusAsync(false, _statusCheckCancellation.Token); |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + _statusCheckCancellation.Cancel(); |
| 99 | + _statusCheckCancellation = new CancellationTokenSource(); |
| 100 | + } |
70 | 101 | } |
71 | 102 |
|
72 | 103 | public void ToggleNodeIsExpanded(RepositoryNode node) |
@@ -274,5 +305,6 @@ private void MakeTreeRows(List<RepositoryNode> rows, List<RepositoryNode> nodes, |
274 | 305 |
|
275 | 306 | private string _searchFilter = string.Empty; |
276 | 307 | private bool _isUpdatingStatus = false; |
| 308 | + private CancellationTokenSource _statusCheckCancellation = new(); |
277 | 309 | } |
278 | 310 | } |
0 commit comments