forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepositoryNode.cs
More file actions
182 lines (155 loc) · 4.73 KB
/
RepositoryNode.cs
File metadata and controls
182 lines (155 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class RepositoryNode : ObservableObject
{
public string Id
{
get => _id;
set
{
var normalized = value.Replace('\\', '/').TrimEnd('/');
SetProperty(ref _id, normalized);
}
}
public string Name
{
get => _name;
set => SetProperty(ref _name, value);
}
public int Bookmark
{
get => _bookmark;
set => SetProperty(ref _bookmark, value);
}
public bool IsRepository
{
get => _isRepository;
set => SetProperty(ref _isRepository, value);
}
public bool IsExpanded
{
get => _isExpanded;
set => SetProperty(ref _isExpanded, value);
}
[JsonIgnore]
public bool IsVisible
{
get => _isVisible;
set => SetProperty(ref _isVisible, value);
}
[JsonIgnore]
public bool IsInvalid
{
get => _isRepository && !Directory.Exists(_id);
}
[JsonIgnore]
public int Depth
{
get;
set;
} = 0;
public Models.RepositoryStatus Status
{
get => _status;
set => SetProperty(ref _status, value);
}
public List<RepositoryNode> SubNodes
{
get;
set;
} = [];
public void Open()
{
if (IsRepository)
{
App.GetLauncher().OpenRepositoryInTab(this, null);
return;
}
foreach (var subNode in SubNodes)
subNode.Open();
}
public void Edit()
{
var activePage = App.GetLauncher().ActivePage;
if (activePage != null && activePage.CanCreatePopup())
activePage.Popup = new EditRepositoryNode(this);
}
public void AddSubFolder()
{
var activePage = App.GetLauncher().ActivePage;
if (activePage != null && activePage.CanCreatePopup())
activePage.Popup = new CreateGroup(this);
}
public void Move()
{
var activePage = App.GetLauncher().ActivePage;
if (activePage != null && activePage.CanCreatePopup())
activePage.Popup = new MoveRepositoryNode(this);
}
public void OpenInFileManager()
{
if (!IsRepository)
return;
Native.OS.OpenInFileManager(_id);
}
public void OpenTerminal()
{
if (!IsRepository)
return;
Native.OS.OpenTerminal(_id);
}
public void Delete()
{
var activePage = App.GetLauncher().ActivePage;
if (activePage != null && activePage.CanCreatePopup())
activePage.Popup = new DeleteRepositoryNode(this);
}
public async Task UpdateStatusAsync(bool force, CancellationToken? token)
{
if (token is { IsCancellationRequested: true })
return;
if (!_isRepository)
{
Status = null;
if (SubNodes.Count > 0)
{
// avoid collection was modified while enumerating.
var nodes = new List<RepositoryNode>();
nodes.AddRange(SubNodes);
foreach (var node in nodes)
await node.UpdateStatusAsync(force, token);
}
return;
}
if (!Directory.Exists(_id))
{
_lastUpdateStatus = DateTime.Now;
Status = null;
return;
}
if (!force)
{
var passed = DateTime.Now - _lastUpdateStatus;
if (passed.TotalSeconds < 10.0)
return;
}
_lastUpdateStatus = DateTime.Now;
Status = await new Commands.QueryRepositoryStatus(_id).GetResultAsync();
}
private string _id = string.Empty;
private string _name = string.Empty;
private bool _isRepository = false;
private int _bookmark = 0;
private bool _isExpanded = false;
private bool _isVisible = true;
private Models.RepositoryStatus _status = null;
private DateTime _lastUpdateStatus = DateTime.UnixEpoch.ToLocalTime();
}
}