Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 8edfbde

Browse files
Start of an independent UserSettingsView
1 parent ccfeef3 commit 8edfbde

File tree

3 files changed

+177
-129
lines changed

3 files changed

+177
-129
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/GitHub.Unity.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
<Compile Include="UI\HistoryView.cs" />
103103
<Compile Include="UI\IView.cs" />
104104
<Compile Include="UI\PublishView.cs" />
105+
<Compile Include="UI\UserSettingsView.cs" />
105106
<Compile Include="UI\SettingsView.cs" />
106107
<Compile Include="UI\Subview.cs" />
107108
<Compile Include="UI\Window.cs" />

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/SettingsView.cs

Lines changed: 21 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ class SettingsView : Subview
5252
private const string GitIgnoreRulesDescription = "Description";
5353
private const string NewGitIgnoreRuleButton = "New";
5454
private const string DeleteGitIgnoreRuleButton = "Delete";
55-
private const string GitConfigTitle = "Git Configuration";
56-
private const string GitConfigNameLabel = "Name";
57-
private const string GitConfigEmailLabel = "Email";
58-
private const string GitConfigUserSave = "Save User";
59-
private const string GitConfigUserSaved = "Saved";
6055
private const string GitRepositoryTitle = "Repository Configuration";
6156
private const string GitRepositoryRemoteLabel = "Remote";
6257
private const string GitRepositorySave = "Save Repository";
@@ -68,9 +63,6 @@ class SettingsView : Subview
6863

6964
[NonSerialized] private int newGitIgnoreRulesSelection = -1;
7065

71-
[SerializeField] private string gitName;
72-
[SerializeField] private string gitEmail;
73-
7466
[SerializeField] private int gitIgnoreRulesSelection = 0;
7567
[SerializeField] private string initDirectory;
7668
[SerializeField] private List<GitLock> lockedFiles = new List<GitLock>();
@@ -82,16 +74,23 @@ class SettingsView : Subview
8274
[SerializeField] private int lockedFileSelection = -1;
8375
[SerializeField] private bool hasRemote;
8476
[SerializeField] private bool remoteHasChanged;
85-
[NonSerialized] private bool userDataHasChanged;
8677

8778
[SerializeField] private string newGitName;
8879
[SerializeField] private string newGitEmail;
8980
[SerializeField] private string newRepositoryRemoteUrl;
9081
[SerializeField] private User cachedUser;
82+
[SerializeField] private UserSettingsView userSettingsView = new UserSettingsView();
83+
84+
public override void InitializeView(IView parent)
85+
{
86+
base.InitializeView(parent);
87+
userSettingsView.InitializeView(this);
88+
}
9189

9290
public override void OnEnable()
9391
{
9492
base.OnEnable();
93+
userSettingsView.OnEnable();
9594
AttachHandlers(Repository);
9695

9796
remoteHasChanged = true;
@@ -100,6 +99,7 @@ public override void OnEnable()
10099
public override void OnDisable()
101100
{
102101
base.OnDisable();
102+
userSettingsView.OnDisable();
103103
DetachHandlers(Repository);
104104
}
105105

@@ -112,6 +112,7 @@ public override void OnDataUpdate()
112112
public override void OnRepositoryChanged(IRepository oldRepository)
113113
{
114114
base.OnRepositoryChanged(oldRepository);
115+
userSettingsView.OnRepositoryChanged(oldRepository);
115116

116117
DetachHandlers(oldRepository);
117118
AttachHandlers(Repository);
@@ -152,7 +153,7 @@ public override void OnGUI()
152153
{
153154
scroll = GUILayout.BeginScrollView(scroll);
154155
{
155-
OnUserSettingsGUI();
156+
userSettingsView.OnGUI();
156157

157158
GUILayout.Space(EditorGUIUtility.standardVerticalSpacing);
158159

@@ -182,72 +183,27 @@ private void MaybeUpdateData()
182183

183184
if (Repository == null)
184185
{
185-
if ((cachedUser == null || String.IsNullOrEmpty(cachedUser.Name)) && GitClient != null)
186-
{
187-
var user = new User();
188-
GitClient.GetConfig("user.name", GitConfigSource.User)
189-
.Then((success, value) => user.Name = value).Then(
190-
GitClient.GetConfig("user.email", GitConfigSource.User)
191-
.Then((success, value) => user.Email = value))
192-
.FinallyInUI((success, ex) =>
193-
{
194-
if (success && !String.IsNullOrEmpty(user.Name))
195-
{
196-
cachedUser = user;
197-
userDataHasChanged = true;
198-
Redraw();
199-
}
200-
})
201-
.Start();
202-
}
203-
204-
if (userDataHasChanged)
205-
{
206-
newGitName = gitName = cachedUser.Name;
207-
newGitEmail = gitEmail = cachedUser.Email;
208-
userDataHasChanged = false;
209-
}
210186
return;
211187
}
212188

213-
userDataHasChanged = Repository.User.Name != gitName || Repository.User.Email != gitEmail;
214-
215-
if (!remoteHasChanged && !userDataHasChanged)
189+
if (!remoteHasChanged)
216190
return;
217191

218-
if (userDataHasChanged)
192+
remoteHasChanged = false;
193+
var activeRemote = Repository.CurrentRemote;
194+
hasRemote = activeRemote.HasValue && !String.IsNullOrEmpty(activeRemote.Value.Url);
195+
if (!hasRemote)
219196
{
220-
userDataHasChanged = false;
221-
newGitName = gitName = Repository.User.Name;
222-
newGitEmail = gitEmail = Repository.User.Email;
197+
repositoryRemoteName = DefaultRepositoryRemoteName;
198+
newRepositoryRemoteUrl = repositoryRemoteUrl = string.Empty;
223199
}
224-
225-
if (remoteHasChanged)
200+
else
226201
{
227-
remoteHasChanged = false;
228-
var activeRemote = Repository.CurrentRemote;
229-
hasRemote = activeRemote.HasValue && !String.IsNullOrEmpty(activeRemote.Value.Url);
230-
if (!hasRemote)
231-
{
232-
repositoryRemoteName = DefaultRepositoryRemoteName;
233-
newRepositoryRemoteUrl = repositoryRemoteUrl = string.Empty;
234-
}
235-
else
236-
{
237-
repositoryRemoteName = activeRemote.Value.Name;
238-
newRepositoryRemoteUrl = repositoryRemoteUrl = activeRemote.Value.Url;
239-
}
202+
repositoryRemoteName = activeRemote.Value.Name;
203+
newRepositoryRemoteUrl = repositoryRemoteUrl = activeRemote.Value.Url;
240204
}
241205
}
242206

243-
private void ResetToDefaults()
244-
{
245-
gitName = Repository != null ? Repository.User.Name : String.Empty;
246-
gitEmail = Repository != null ? Repository.User.Email : String.Empty;
247-
repositoryRemoteName = DefaultRepositoryRemoteName;
248-
repositoryRemoteUrl = string.Empty;
249-
}
250-
251207
private void Repository_OnActiveRemoteChanged(string remote)
252208
{
253209
remoteHasChanged = true;
@@ -273,70 +229,6 @@ private void OnLocksUpdate(IEnumerable<GitLock> update)
273229
Redraw();
274230
}
275231

276-
private void OnUserSettingsGUI()
277-
{
278-
GUILayout.Label(GitConfigTitle, EditorStyles.boldLabel);
279-
280-
EditorGUI.BeginDisabledGroup(isBusy);
281-
{
282-
newGitName = EditorGUILayout.TextField(GitConfigNameLabel, newGitName);
283-
newGitEmail = EditorGUILayout.TextField(GitConfigEmailLabel, newGitEmail);
284-
285-
var needsSaving = newGitName != gitName || newGitEmail != gitEmail;
286-
EditorGUI.BeginDisabledGroup(!needsSaving);
287-
{
288-
if (GUILayout.Button(GitConfigUserSave, GUILayout.ExpandWidth(false)))
289-
{
290-
GitClient.SetConfig("user.name", newGitName, GitConfigSource.User)
291-
.Then((success, value) =>
292-
{
293-
if (success)
294-
{
295-
if (Repository != null)
296-
{
297-
Repository.User.Name = value;
298-
}
299-
else
300-
{
301-
if (cachedUser == null)
302-
{
303-
cachedUser = new User();
304-
}
305-
cachedUser.Name = value;
306-
}
307-
}
308-
})
309-
.Then(
310-
GitClient.SetConfig("user.email", newGitEmail, GitConfigSource.User)
311-
.Then((success, value) =>
312-
{
313-
if (success)
314-
{
315-
if (Repository != null)
316-
{
317-
Repository.User.Email = value;
318-
}
319-
else
320-
{
321-
cachedUser.Email = value;
322-
userDataHasChanged = true;
323-
}
324-
}
325-
}))
326-
.FinallyInUI((_, __) =>
327-
{
328-
isBusy = false;
329-
Redraw();
330-
})
331-
.Start();
332-
isBusy = true;
333-
}
334-
}
335-
EditorGUI.EndDisabledGroup();
336-
}
337-
EditorGUI.EndDisabledGroup();
338-
}
339-
340232
private void OnRepositorySettingsGUI()
341233
{
342234
GUILayout.Label(GitRepositoryTitle, EditorStyles.boldLabel);
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using UnityEditor;
7+
using UnityEngine;
8+
9+
namespace GitHub.Unity
10+
{
11+
[Serializable]
12+
class UserSettingsView : Subview
13+
{
14+
private const string GitConfigTitle = "Git Configuration";
15+
private const string GitConfigNameLabel = "Name";
16+
private const string GitConfigEmailLabel = "Email";
17+
private const string GitConfigUserSave = "Save User";
18+
19+
[SerializeField]
20+
private string gitName;
21+
[SerializeField]
22+
private string gitEmail;
23+
24+
[SerializeField]
25+
private bool isBusy;
26+
[NonSerialized]
27+
private bool userDataHasChanged;
28+
29+
[SerializeField]
30+
private string newGitName;
31+
[SerializeField]
32+
private string newGitEmail;
33+
[SerializeField]
34+
private User cachedUser;
35+
36+
public override void OnDataUpdate()
37+
{
38+
base.OnDataUpdate();
39+
MaybeUpdateData();
40+
}
41+
42+
public override void OnRepositoryChanged(IRepository oldRepository)
43+
{
44+
base.OnRepositoryChanged(oldRepository);
45+
46+
Refresh();
47+
}
48+
49+
public override void OnGUI()
50+
{
51+
GUILayout.Label(GitConfigTitle, EditorStyles.boldLabel);
52+
53+
EditorGUI.BeginDisabledGroup(isBusy);
54+
{
55+
newGitName = EditorGUILayout.TextField(GitConfigNameLabel, newGitName);
56+
newGitEmail = EditorGUILayout.TextField(GitConfigEmailLabel, newGitEmail);
57+
58+
var needsSaving = newGitName != gitName || newGitEmail != gitEmail;
59+
EditorGUI.BeginDisabledGroup(!needsSaving);
60+
{
61+
if (GUILayout.Button(GitConfigUserSave, GUILayout.ExpandWidth(false)))
62+
{
63+
GitClient.SetConfig("user.name", newGitName, GitConfigSource.User)
64+
.Then((success, value) =>
65+
{
66+
if (success)
67+
{
68+
if (Repository != null)
69+
{
70+
Repository.User.Name = value;
71+
}
72+
else
73+
{
74+
if (cachedUser == null)
75+
{
76+
cachedUser = new User();
77+
}
78+
cachedUser.Name = value;
79+
}
80+
}
81+
})
82+
.Then(
83+
GitClient.SetConfig("user.email", newGitEmail, GitConfigSource.User)
84+
.Then((success, value) =>
85+
{
86+
if (success)
87+
{
88+
if (Repository != null)
89+
{
90+
Repository.User.Email = value;
91+
}
92+
else
93+
{
94+
cachedUser.Email = value;
95+
userDataHasChanged = true;
96+
}
97+
}
98+
}))
99+
.FinallyInUI((_, __) =>
100+
{
101+
isBusy = false;
102+
Redraw();
103+
})
104+
.Start();
105+
isBusy = true;
106+
}
107+
}
108+
EditorGUI.EndDisabledGroup();
109+
}
110+
EditorGUI.EndDisabledGroup();
111+
}
112+
113+
private void MaybeUpdateData()
114+
{
115+
if (Repository != null)
116+
{
117+
if ((cachedUser == null || String.IsNullOrEmpty(cachedUser.Name)) && GitClient != null)
118+
{
119+
var user = new User();
120+
GitClient.GetConfig("user.name", GitConfigSource.User)
121+
.Then((success, value) => user.Name = value).Then(
122+
GitClient.GetConfig("user.email", GitConfigSource.User)
123+
.Then((success, value) => user.Email = value))
124+
.FinallyInUI((success, ex) =>
125+
{
126+
if (success && !String.IsNullOrEmpty(user.Name))
127+
{
128+
cachedUser = user;
129+
userDataHasChanged = true;
130+
Redraw();
131+
}
132+
})
133+
.Start();
134+
}
135+
136+
if (userDataHasChanged)
137+
{
138+
newGitName = gitName = cachedUser.Name;
139+
newGitEmail = gitEmail = cachedUser.Email;
140+
userDataHasChanged = false;
141+
}
142+
return;
143+
}
144+
145+
userDataHasChanged = Repository.User.Name != gitName || Repository.User.Email != gitEmail;
146+
147+
if (!userDataHasChanged)
148+
return;
149+
150+
userDataHasChanged = false;
151+
newGitName = gitName = Repository.User.Name;
152+
newGitEmail = gitEmail = Repository.User.Email;
153+
}
154+
}
155+
}

0 commit comments

Comments
 (0)