From f1b5d7ed83d219781abd61130b38267b35ddc2d9 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Tue, 12 Sep 2017 16:00:09 -0400 Subject: [PATCH] Fixes to update the publish view only once --- .../Editor/GitHub.Unity/UI/PopupWindow.cs | 20 +++++-- .../Editor/GitHub.Unity/UI/PublishView.cs | 56 ++++++++++++++----- 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/PopupWindow.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/PopupWindow.cs index cb5edff0e..5596711ca 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/PopupWindow.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/PopupWindow.cs @@ -41,9 +41,9 @@ public static PopupWindow Open(PopupViewType popupViewType, Action onClose popupWindow.ActiveViewType = popupViewType; popupWindow.titleContent = new GUIContent(popupWindow.ActiveView.Title, Styles.SmallLogo); - - popupWindow.InitializeWindow(EntryPoint.ApplicationManager); + popupWindow.OnEnable(); popupWindow.Show(); + popupWindow.Refresh(); return popupWindow; } @@ -64,7 +64,6 @@ public override void Initialize(IApplicationManager applicationManager) public override void OnEnable() { base.OnEnable(); - minSize = maxSize = ActiveView.Size; ActiveView.OnEnable(); } @@ -75,6 +74,12 @@ public override void OnDisable() ActiveView.OnDisable(); } + public override void OnDataUpdate() + { + base.OnDataUpdate(); + ActiveView.OnDataUpdate(); + } + public override void OnUI() { base.OnUI(); @@ -132,7 +137,14 @@ private Subview ActiveView private PopupViewType ActiveViewType { get { return activeViewType; } - set { activeViewType = value; } + set + { + if (activeViewType != value) + { + ActiveView.OnDisable(); + activeViewType = value; + } + } } } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/PublishView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/PublishView.cs index 103df35bf..a963520c6 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/PublishView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/PublishView.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using Octokit; using UnityEditor; @@ -23,6 +24,7 @@ class PublishView : Subview [SerializeField] private string username; [SerializeField] private string[] owners = { OwnersDefaultText }; + [SerializeField] private IList organizations; [SerializeField] private int selectedOwner; [SerializeField] private string repoName = String.Empty; [SerializeField] private string repoDescription = ""; @@ -31,6 +33,7 @@ class PublishView : Subview [NonSerialized] private IApiClient client; [NonSerialized] private bool isBusy; [NonSerialized] private string error; + [NonSerialized] private bool organizationsNeedLoading; public IApiClient Client { @@ -56,23 +59,43 @@ public IApiClient Client } } + public override void OnEnable() + { + base.OnEnable(); + organizationsNeedLoading = organizations == null && !isBusy; + } + + public override void OnDataUpdate() + { + base.OnDataUpdate(); + MaybeUpdateData(); + } + + private void MaybeUpdateData() + { + if (organizationsNeedLoading) + { + organizationsNeedLoading = false; + LoadOrganizations(); + } + } + public override void InitializeView(IView parent) { base.InitializeView(parent); Title = WindowTitle; Size = viewSize; - PopulateView(); } - private void PopulateView() + private void LoadOrganizations() { - try + var keychainConnections = Platform.Keychain.Connections; + //TODO: ONE_USER_LOGIN This assumes only ever one user can login + if (keychainConnections.Any()) { - var keychainConnections = Platform.Keychain.Connections; - //TODO: ONE_USER_LOGIN This assumes only ever one user can login - if (keychainConnections.Any()) + try { - Logger.Trace("GetCurrentUser"); + Logger.Trace("Loading Keychain"); isBusy = true; @@ -87,12 +110,14 @@ private void PopulateView() //TODO: ONE_USER_LOGIN This assumes only ever one user can login username = keychainConnections.First().Username; - Client.GetOrganizations(organizations => { + Logger.Trace("Loading Organizations"); + + Client.GetOrganizations(orgs => { + organizations = orgs; Logger.Trace("Loaded {0} organizations", organizations.Count); var organizationLogins = organizations - .OrderBy(organization => organization.Login) - .Select(organization => organization.Login); + .OrderBy(organization => organization.Login).Select(organization => organization.Login); owners = new[] { OwnersDefaultText, username }.Union(organizationLogins).ToArray(); @@ -100,15 +125,15 @@ private void PopulateView() }); }); } - else + catch (Exception e) { - Logger.Warning("No Keychain connections to use"); + Logger.Error(e, "Error PopulateView & GetOrganizations"); + isBusy = false; } } - catch (Exception e) + else { - Logger.Error(e, "Error PopulateView & GetOrganizations"); - throw; + Logger.Warning("No Keychain connections to use"); } } @@ -196,6 +221,7 @@ public override void OnGUI() EditorGUI.BeginDisabledGroup(!IsFormValid); if (GUILayout.Button(PublishViewCreateButton)) { + GUI.FocusControl(null); isBusy = true; var organization = owners[selectedOwner] == username ? null : owners[selectedOwner];