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

Commit b0cc696

Browse files
Initial routine in PopupWindow to handle authentication before presenting specific views
1 parent 682fb99 commit b0cc696

File tree

6 files changed

+98
-13
lines changed

6 files changed

+98
-13
lines changed

src/GitHub.Api/Application/ApiClient.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ public async Task GetOrganizations(Action<IList<Organization>> onSuccess, Action
7474
await GetOrganizationInternal(onSuccess, onError);
7575
}
7676

77+
public async Task ValidateCurrentUser(Action onSuccess, Action<Exception> onError = null)
78+
{
79+
Guard.ArgumentNotNull(onSuccess, nameof(onSuccess));
80+
try
81+
{
82+
await ValidateCurrentUserInternal();
83+
onSuccess();
84+
}
85+
catch (Exception e)
86+
{
87+
onError?.Invoke(e);
88+
}
89+
}
90+
7791
public async Task GetCurrentUser(Action<Octokit.User> callback)
7892
{
7993
Guard.ArgumentNotNull(callback, "callback");

src/GitHub.Api/Application/IApiClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ interface IApiClient
1616
Task<bool> LoginAsync(string username, string password, Func<LoginResult, string> need2faCode);
1717
Task Logout(UriString host);
1818
Task GetCurrentUser(Action<Octokit.User> callback);
19+
Task ValidateCurrentUser(Action onSuccess, Action<Exception> onError = null);
1920
}
2021
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public void OnEmbeddedGUI()
287287
var publishedClicked = GUILayout.Button(PublishButton, Styles.HistoryToolbarButtonStyle);
288288
if (publishedClicked)
289289
{
290-
PopupWindow.Open(PopupWindow.PopupViewType.PublishView);
290+
PopupWindow.OpenWindow(PopupWindow.PopupViewType.PublishView);
291291
}
292292
}
293293
}

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

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,100 @@ public enum PopupViewType
2020
[SerializeField] private PublishView publishView;
2121
[SerializeField] private LoadingView loadingView;
2222

23+
[NonSerialized] private IApiClient client;
24+
2325
public event Action<bool> OnClose;
2426

2527
[MenuItem("GitHub/Authenticate")]
2628
public static void Launch()
2729
{
28-
Open(PopupViewType.AuthenticationView);
30+
OpenWindow(PopupViewType.AuthenticationView);
2931
}
3032

31-
public static PopupWindow Open(PopupViewType popupViewType, Action<bool> onClose = null)
33+
public static PopupWindow OpenWindow(PopupViewType popupViewType, Action<bool> onClose = null)
3234
{
3335
var popupWindow = GetWindow<PopupWindow>(true);
3436

35-
popupWindow.OnClose.SafeInvoke(false);
37+
popupWindow.Open(popupViewType, onClose);
38+
39+
return popupWindow;
40+
}
41+
42+
private void Open(PopupViewType popupViewType, Action<bool> onClose)
43+
{
44+
OnClose.SafeInvoke(false);
45+
OnClose = null;
46+
47+
Logger.Trace("OpenView: {0}", popupViewType.ToString());
48+
49+
var viewNeedsAuthentication = popupViewType == PopupViewType.PublishView;
50+
if (viewNeedsAuthentication)
51+
{
52+
Logger.Trace("Validating to open view");
53+
54+
Client.ValidateCurrentUser(() => {
55+
56+
Logger.Trace("User validated opening view");
57+
58+
OpenInternal(popupViewType, onClose);
59+
60+
}, exception => {
61+
62+
Logger.Trace("User required validation opening AuthenticationView");
63+
64+
Open(PopupViewType.AuthenticationView, completedAuthentication => {
3665

66+
if (completedAuthentication)
67+
{
68+
Logger.Trace("User completed validation opening view: {0}", popupViewType.ToString());
69+
70+
Open(popupViewType, onClose);
71+
}
72+
});
73+
});
74+
}
75+
else
76+
{
77+
OpenInternal(popupViewType, onClose);
78+
}
79+
}
80+
81+
private void OpenInternal(PopupViewType popupViewType, Action<bool> onClose)
82+
{
3783
if (onClose != null)
3884
{
39-
popupWindow.OnClose += onClose;
85+
OnClose += onClose;
4086
}
4187

42-
popupWindow.ActiveViewType = popupViewType;
43-
popupWindow.titleContent = new GUIContent(popupWindow.ActiveView.Title, Styles.SmallLogo);
44-
popupWindow.OnEnable();
45-
popupWindow.Show();
46-
popupWindow.Refresh();
88+
ActiveViewType = popupViewType;
89+
titleContent = new GUIContent(ActiveView.Title, Styles.SmallLogo);
90+
OnEnable();
91+
Show();
92+
Refresh();
93+
}
4794

48-
return popupWindow;
95+
public IApiClient Client
96+
{
97+
get
98+
{
99+
if (client == null)
100+
{
101+
var repository = Environment.Repository;
102+
UriString host;
103+
if (repository != null && !string.IsNullOrEmpty(repository.CloneUrl))
104+
{
105+
host = repository.CloneUrl.ToRepositoryUrl();
106+
}
107+
else
108+
{
109+
host = UriString.ToUriString(HostAddress.GitHubDotComHostAddress.WebUri);
110+
}
111+
112+
client = ApiClient.Create(host, Platform.Keychain);
113+
}
114+
115+
return client;
116+
}
49117
}
50118

51119
public override void Initialize(IApplicationManager applicationManager)
@@ -59,6 +127,8 @@ public override void Initialize(IApplicationManager applicationManager)
59127
publishView.InitializeView(this);
60128
authenticationView.InitializeView(this);
61129
loadingView.InitializeView(this);
130+
131+
titleContent = new GUIContent(ActiveView.Title, Styles.SmallLogo);
62132
}
63133

64134
public override void OnEnable()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private void LoadOwners()
146146
if (keychainEmptyException != null)
147147
{
148148
Logger.Trace("Keychain empty");
149-
PopupWindow.Open(PopupWindow.PopupViewType.AuthenticationView);
149+
PopupWindow.OpenWindow(PopupWindow.PopupViewType.AuthenticationView);
150150
return;
151151
}
152152

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ private void DoAccountDropdown()
351351

352352
private void SignIn(object obj)
353353
{
354-
PopupWindow.Open(PopupWindow.PopupViewType.AuthenticationView);
354+
PopupWindow.OpenWindow(PopupWindow.PopupViewType.AuthenticationView);
355355
}
356356

357357
private void GoToProfile(object obj)

0 commit comments

Comments
 (0)