diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/AuthenticationView.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/AuthenticationView.cs index 0bb15fb25..c7795c05a 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/AuthenticationView.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/AuthenticationView.cs @@ -9,6 +9,8 @@ class AuthenticationView : Subview { private static readonly Vector2 viewSize = new Vector2(290, 290); + private const string CredentialsNeedRefreshMessage = "We've detected that your stored credentials are out of sync with your current user. This can happen if you have signed in to git outside of Unity. Sign in again to refresh your credentials."; + private const string NeedAuthenticationMessage = "We need you to authenticate first"; private const string WindowTitle = "Authenticate"; private const string UsernameLabel = "Username"; private const string PasswordLabel = "Password"; @@ -24,10 +26,10 @@ class AuthenticationView : Subview [SerializeField] private string username = string.Empty; [SerializeField] private string two2fa = string.Empty; [SerializeField] private string message; + [SerializeField] private string errorMessage; + [SerializeField] private bool need2fa; - [NonSerialized] private bool need2fa; [NonSerialized] private bool isBusy; - [NonSerialized] private string errorMessage; [NonSerialized] private bool enterPressed; [NonSerialized] private string password = string.Empty; [NonSerialized] private AuthenticationService authenticationService; @@ -37,18 +39,30 @@ public override void InitializeView(IView parent) { base.InitializeView(parent); need2fa = isBusy = false; + message = errorMessage = null; Title = WindowTitle; Size = viewSize; } - public override void OnEnable() + public void Initialize(Exception exception) { - base.OnEnable(); - } + var usernameMismatchException = exception as TokenUsernameMismatchException; + if (usernameMismatchException != null) + { + message = CredentialsNeedRefreshMessage; + username = usernameMismatchException.CachedUsername; + } - public override void OnDisable() - { - base.OnDisable(); + var keychainEmptyException = exception as KeychainEmptyException; + if (keychainEmptyException != null) + { + message = NeedAuthenticationMessage; + } + + if (usernameMismatchException == null && keychainEmptyException == null) + { + message = exception.Message; + } } public override void OnGUI() @@ -81,27 +95,7 @@ public override void OnGUI() } GUILayout.EndScrollView(); } - - public void SetMessage(string value) - { - message = value; - } - - public void ClearMessage() - { - message = null; - } - - public void SetUsername(string value) - { - username = value; - } - - public void ClearUsername() - { - username = string.Empty; - } - + private void HandleEnterPressed() { if (Event.current.type != EventType.KeyDown) @@ -174,8 +168,7 @@ private void OnGUI2FA() if (GUILayout.Button(BackButton)) { GUI.FocusControl(null); - need2fa = false; - Redraw(); + Clear(); } if (GUILayout.Button(TwofaButton) || (!isBusy && enterPressed)) @@ -196,7 +189,7 @@ private void OnGUI2FA() private void DoRequire2fa(string msg) { - Logger.Trace("Strating 2FA - Message:\"{0}\"", msg); + Logger.Trace("Starting 2FA - Message:\"{0}\"", msg); need2fa = true; errorMessage = msg; @@ -204,19 +197,28 @@ private void DoRequire2fa(string msg) Redraw(); } + private void Clear() + { + need2fa = false; + errorMessage = null; + isBusy = false; + Redraw(); + } + private void DoResult(bool success, string msg) { Logger.Trace("DoResult - Success:{0} Message:\"{1}\"", success, msg); - errorMessage = msg; isBusy = false; if (success == true) { + Clear(); Finish(true); } else { + errorMessage = msg; Redraw(); } } diff --git a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/PopupWindow.cs b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/PopupWindow.cs index 80a626517..86a62cb75 100644 --- a/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/PopupWindow.cs +++ b/src/UnityExtension/Assets/Editor/GitHub.Unity/UI/PopupWindow.cs @@ -7,9 +7,6 @@ namespace GitHub.Unity [Serializable] class PopupWindow : BaseWindow { - private const string CredentialsNeedRefreshMessage = "We've detected that your stored credentials are out of sync with your current user. This can happen if you have signed in to git outside of Unity. Sign in again to refresh your credentials."; - private const string NeedAuthenticationMessage = "We need you to authenticate first"; - public enum PopupViewType { None, @@ -63,32 +60,8 @@ private void Open(PopupViewType popupViewType, Action onClose) }, exception => { Logger.Trace("User required validation opening AuthenticationView"); - - string message = null; - string username = null; - - var usernameMismatchException = exception as TokenUsernameMismatchException; - if (usernameMismatchException != null) - { - message = CredentialsNeedRefreshMessage; - username = usernameMismatchException.CachedUsername; - } - - var keychainEmptyException = exception as KeychainEmptyException; - if (keychainEmptyException != null) - { - message = NeedAuthenticationMessage; - } - - if (usernameMismatchException == null && keychainEmptyException == null) - { - message = exception.Message; - } - + authenticationView.Initialize(exception); OpenInternal(PopupViewType.AuthenticationView, completedAuthentication => { - authenticationView.ClearMessage(); - authenticationView.ClearUsername(); - if (completedAuthentication) { Logger.Trace("User completed validation opening view: {0}", popupViewType.ToString()); @@ -98,11 +71,6 @@ private void Open(PopupViewType popupViewType, Action onClose) }); shouldCloseOnFinish = false; - authenticationView.SetMessage(message); - if (username != null) - { - authenticationView.SetUsername(username); - } }); } else @@ -120,10 +88,7 @@ private void OpenInternal(PopupViewType popupViewType, Action onClose) } ActiveViewType = popupViewType; - titleContent = new GUIContent(ActiveView.Title, Styles.SmallLogo); - OnEnable(); Show(); - Refresh(); } public IApiClient Client