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

Commit 7bbc76f

Browse files
authored
Merge pull request #275 from github-for-unity/fixes/settings-update-on-enable
Refactor views to update settings in MaybeUpdateData
2 parents a893f1b + db45ff1 commit 7bbc76f

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class BranchesView : Subview
4343
[NonSerialized] private List<GitBranch> newLocalBranches;
4444
[NonSerialized] private BranchTreeNode newNodeSelection;
4545
[NonSerialized] private BranchesMode targetMode;
46+
[NonSerialized] private bool favouritesHasChanged;
4647

4748
[SerializeField] private BranchTreeNode activeBranchNode;
4849
[SerializeField] private BranchTreeNode localRoot;
@@ -51,6 +52,7 @@ class BranchesView : Subview
5152
[SerializeField] private List<Remote> remotes = new List<Remote>();
5253
[SerializeField] private Vector2 scroll;
5354
[SerializeField] private BranchTreeNode selectedNode;
55+
private List<string> favouritesList;
5456

5557
public override void InitializeView(IView parent)
5658
{
@@ -62,6 +64,7 @@ public override void OnEnable()
6264
{
6365
base.OnEnable();
6466
AttachHandlers(Repository);
67+
favouritesHasChanged = true;
6568
}
6669

6770
public override void OnDisable()
@@ -70,6 +73,21 @@ public override void OnDisable()
7073
DetachHandlers(Repository);
7174
}
7275

76+
public override void OnDataUpdate()
77+
{
78+
base.OnDataUpdate();
79+
MaybeUpdateData();
80+
}
81+
82+
private void MaybeUpdateData()
83+
{
84+
if (favouritesHasChanged)
85+
{
86+
favouritesList = Manager.LocalSettings.Get(FavoritesSetting, new List<string>());
87+
favouritesHasChanged = false;
88+
}
89+
}
90+
7391
public override void OnRepositoryChanged(IRepository oldRepository)
7492
{
7593
base.OnRepositoryChanged(oldRepository);
@@ -246,12 +264,12 @@ public void OnEmbeddedGUI()
246264

247265
private int CompareBranches(GitBranch a, GitBranch b)
248266
{
249-
if (GetFavourite(a.Name))
267+
if (IsFavourite(a.Name))
250268
{
251269
return -1;
252270
}
253271

254-
if (GetFavourite(b.Name))
272+
if (IsFavourite(b.Name))
255273
{
256274
return 1;
257275
}
@@ -269,19 +287,9 @@ private int CompareBranches(GitBranch a, GitBranch b)
269287
return 0;
270288
}
271289

272-
private bool GetFavourite(BranchTreeNode branch)
273-
{
274-
return GetFavourite(branch.Name);
275-
}
276-
277-
private bool GetFavourite(string branchName)
290+
private bool IsFavourite(string branchName)
278291
{
279-
if (string.IsNullOrEmpty(branchName))
280-
{
281-
return false;
282-
}
283-
284-
return Manager.LocalSettings.Get(FavoritesSetting, new List<string>()).Contains(branchName);
292+
return !String.IsNullOrEmpty(branchName) && favouritesList.Contains(branchName);
285293
}
286294

287295
private void OnLocalBranchesUpdate(IEnumerable<GitBranch> list)
@@ -312,7 +320,6 @@ private void BuildTree(IEnumerable<GitBranch> local, IEnumerable<GitBranch> remo
312320

313321
// Prepare for updated favourites listing
314322
favourites.Clear();
315-
var cachedFavs = Manager.LocalSettings.Get<List<string>>(FavoritesSetting, new List<string>());
316323

317324
// Just build directly on the local root, keep track of active branch
318325
localRoot = new BranchTreeNode("", NodeType.Folder, false);
@@ -342,7 +349,7 @@ private void BuildTree(IEnumerable<GitBranch> local, IEnumerable<GitBranch> remo
342349
}
343350

344351
// Add to favourites
345-
if (cachedFavs.Contains(branch.Name))
352+
if (favouritesList.Contains(branch.Name))
346353
{
347354
favourites.Add(node);
348355
}
@@ -386,7 +393,7 @@ private void BuildTree(IEnumerable<GitBranch> local, IEnumerable<GitBranch> remo
386393
}
387394

388395
// Add to favourites
389-
if (cachedFavs.Contains(branch.Name))
396+
if (favouritesList.Contains(branch.Name))
390397
{
391398
favourites.Add(node);
392399
}
@@ -597,7 +604,7 @@ private void OnTreeNodeGUI(BranchTreeNode node)
597604

598605
if (node.Type != NodeType.Folder)
599606
{
600-
var favourite = GetFavourite(node);
607+
var favourite = IsFavourite(node.Name);
601608
if (Event.current.type == EventType.Repaint)
602609
{
603610
GUI.DrawTexture(favouriteRect, favourite ? Styles.FavouriteIconOn : Styles.FavouriteIconOff);
@@ -610,7 +617,7 @@ private void OnTreeNodeGUI(BranchTreeNode node)
610617
}
611618
}
612619
// Favourite status
613-
else if (Event.current.type == EventType.Repaint && node.Type != NodeType.Folder && GetFavourite(node.Name))
620+
else if (Event.current.type == EventType.Repaint && node.Type != NodeType.Folder && IsFavourite(node.Name))
614621
{
615622
GUI.DrawTexture(favouriteRect, Styles.FavouriteIconOn);
616623
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,24 @@ class SettingsView : Subview
4545
[SerializeField] private bool isBusy;
4646
[SerializeField] private int lockedFileSelection = -1;
4747
[SerializeField] private bool hasRemote;
48-
[SerializeField] private bool remoteHasChanged;
48+
[NonSerialized] private bool remoteHasChanged;
4949
[NonSerialized] private bool userDataHasChanged;
5050

5151
[SerializeField] private string newGitName;
5252
[SerializeField] private string newGitEmail;
5353
[SerializeField] private string newRepositoryRemoteUrl;
5454
[SerializeField] private User cachedUser;
55+
56+
[SerializeField] private bool metricsEnabled;
57+
[NonSerialized] private bool metricsHasChanged;
5558

5659
public override void OnEnable()
5760
{
5861
base.OnEnable();
5962
AttachHandlers(Repository);
6063

6164
remoteHasChanged = true;
65+
metricsHasChanged = true;
6266
}
6367

6468
public override void OnDisable()
@@ -141,6 +145,12 @@ public override void OnGUI()
141145

142146
private void MaybeUpdateData()
143147
{
148+
if (metricsHasChanged)
149+
{
150+
metricsEnabled = Manager.UsageTracker.Enabled;
151+
metricsHasChanged = false;
152+
}
153+
144154
if (lockedFiles == null)
145155
lockedFiles = new List<GitLock>();
146156

@@ -478,13 +488,10 @@ private void OnInstallPathGUI()
478488

479489
private void OnPrivacyGui()
480490
{
481-
var service = Manager != null ? Manager.UsageTracker : null;
482-
483491
GUILayout.Label(PrivacyTitle, EditorStyles.boldLabel);
484492

485-
EditorGUI.BeginDisabledGroup(isBusy || service == null);
493+
EditorGUI.BeginDisabledGroup(isBusy);
486494
{
487-
var metricsEnabled = service != null && service.Enabled;
488495
EditorGUI.BeginChangeCheck();
489496
{
490497
metricsEnabled = GUILayout.Toggle(metricsEnabled, MetricsOptInLabel);
@@ -493,7 +500,6 @@ private void OnPrivacyGui()
493500
{
494501
Manager.UsageTracker.Enabled = metricsEnabled;
495502
}
496-
497503
}
498504
EditorGUI.EndDisabledGroup();
499505
}

0 commit comments

Comments
 (0)