-
Notifications
You must be signed in to change notification settings - Fork 43
/
LoginUIController.cs
390 lines (368 loc) · 16.1 KB
/
LoginUIController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright (c) Microsoft Corporation and Contributors
// Licensed under the MIT license.
using Microsoft.Windows.ApplicationModel.Resources;
using Microsoft.Windows.DevHome.SDK;
namespace GitHubPlugin.DeveloperId;
internal class LoginUIController : IPluginAdaptiveCardController
{
// _loginEntryPoint stores the calling component on Dev Home (like "Settings", "SetupTool" etc).
private readonly string _loginEntryPoint;
private IPluginAdaptiveCard? _loginUI;
private static readonly LoginUITemplate _loginUITemplate = new ();
public LoginUIController(string loginEntryPoint)
{
_loginEntryPoint = loginEntryPoint;
}
public void Dispose()
{
Log.Logger()?.ReportDebug($"Dispose");
_loginUI?.Update(null, null, null);
}
public void Initialize(IPluginAdaptiveCard pluginUI)
{
Log.Logger()?.ReportDebug($"Initialize");
_loginUI = pluginUI;
_loginUI.Update(_loginUITemplate.GetLoginUITemplate(LoginUIState.LoginPage), null, LoginUIState.LoginPage);
}
public async void OnAction(string action, string inputs)
{
Log.Logger()?.ReportInfo($"OnAction() called with state:{_loginUI?.State}");
Log.Logger()?.ReportDebug($"action: {action}");
switch (_loginUI?.State)
{
case LoginUIState.LoginPage:
{
// Inputs are validated at this point.
_loginUI.Update(_loginUITemplate.GetLoginUITemplate(LoginUIState.WaitingPage), null, LoginUIState.WaitingPage);
Log.Logger()?.ReportDebug($"inputs: {inputs}");
try
{
var devId = await (DeveloperIdProvider.GetInstance() as IDeveloperIdProvider).LoginNewDeveloperIdAsync();
if (devId != null)
{
var resourceLoader = new ResourceLoader(ResourceLoader.GetDefaultResourceFilePath(), "GitHubPlugin/Resources");
_loginUI.Update(_loginUITemplate.GetLoginUITemplate(LoginUIState.LoginSucceededPage).Replace("${message}", $"{devId.LoginId()} {resourceLoader.GetString("LoginUI_LoginSuccededPage_text")}"), null, LoginUIState.LoginSucceededPage);
}
else
{
Log.Logger()?.ReportError($"Unable to create DevId");
_loginUI.Update(_loginUITemplate.GetLoginUITemplate(LoginUIState.LoginFailedPage), null, LoginUIState.LoginFailedPage);
}
}
catch (Exception ex)
{
Log.Logger()?.ReportError($"Error: {ex}");
_loginUI.Update(_loginUITemplate.GetLoginUITemplate(LoginUIState.LoginFailedPage), null, LoginUIState.LoginFailedPage);
}
break;
}
// These pages only have close actions.
case LoginUIState.LoginSucceededPage:
case LoginUIState.LoginFailedPage:
{
Log.Logger()?.ReportInfo($"State:{_loginUI.State}");
_loginUI.Update(null, null, LoginUIState.End);
break;
}
// These pages do not have any actions. We should never be here.
case LoginUIState.WaitingPage:
default:
{
Log.Logger()?.ReportError($"Unexpected state:{_loginUI?.State}");
break;
}
}
}
// Adaptive Card Templates for LoginUI.
private class LoginUITemplate
{
internal string GetLoginUITemplate(string loginUIState)
{
var loader = new ResourceLoader(ResourceLoader.GetDefaultResourceFilePath(), "GitHubPlugin/Resources");
var loginPage = @"
{
""type"": ""AdaptiveCard"",
""body"": [
{
""type"": ""ColumnSet"",
""spacing"": ""Large"",
""columns"": [
{
""type"": ""Column"",
""items"": [
{
""type"": ""Image"",
""style"": ""Person"",
""url"": ""https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"",
""size"": ""Medium"",
""horizontalAlignment"": ""Center"",
""spacing"": ""None""
},
{
""type"": ""TextBlock"",
""weight"": ""Bolder"",
""text"": """ + $"{loader.GetString("LoginUI_LoginPage_Heading")}" + @""",
""wrap"": true,
""horizontalAlignment"": ""Center"",
""spacing"": ""Small"",
""style"": ""heading""
},
{
""type"": ""TextBlock"",
""text"": """ + $"{loader.GetString("LoginUI_LoginPage_Subheading")}" + @""",
""isSubtle"": true,
""wrap"": true,
""horizontalAlignment"": ""Center"",
""spacing"": ""None""
},
{
""type"": ""TextBlock"",
""text"": """",
""wrap"": true,
""spacing"": ""Large"",
""horizontalAlignment"": ""Center"",
""isSubtle"": true
}
],
""width"": ""stretch"",
""separator"": true,
""spacing"": ""Medium""
}
]
},
{
""type"": ""Table"",
""columns"": [
{
""width"": 1
}
],
""rows"": [
{
""type"": ""TableRow"",
""cells"": [
{
""type"": ""TableCell"",
""items"": [
{
""type"": ""ActionSet"",
""actions"": [
{
""type"": ""Action.Submit"",
""title"": """ + $"{loader.GetString("LoginUI_LoginPage_Button1Text")}" + @""",
""tooltip"": """ + $"{loader.GetString("LoginUI_LoginPage_Button1ToolTip")}" + @""",
""style"": ""positive"",
""isEnabled"": true,
""id"": ""Personal""
}
],
""horizontalAlignment"": ""Center"",
""height"": ""stretch"",
""spacing"": ""None""
}
],
""isVisible"": true,
""verticalContentAlignment"": ""Center"",
""height"": ""stretch"",
""spacing"": ""None"",
""horizontalAlignment"": ""Center""
}
],
""horizontalAlignment"": ""Center"",
""height"": ""stretch"",
""horizontalCellContentAlignment"": ""Center"",
""verticalCellContentAlignment"": ""Center"",
""spacing"": ""None""
},
{
""type"": ""TableRow"",
""cells"": [
{
""type"": ""TableCell"",
""items"": [
{
""type"": ""ActionSet"",
""actions"": [
{
""type"": ""Action.ShowCard"",
""title"": """ + $"{loader.GetString("LoginUI_LoginPage_Button2Text")}" + @""",
""isEnabled"": false,
""tooltip"": """ + $"{loader.GetString("LoginUI_LoginPage_Button2ToolTip")}" + @""",
""id"": ""Enterprise"",
""card"": {
""type"": ""AdaptiveCard"",
""body"": [
{
""type"": ""Input.Text"",
""placeholder"": """ + $"{loader.GetString("LoginUI_LoginPage_Button2Flyout_Text_PlaceHolder")}" + @""",
""style"": ""Url"",
""isRequired"": true,
""id"": ""Enterprise.server"",
""label"": """ + $"{loader.GetString("LoginUI_LoginPage_Button2Flyout_Text_Label")}" + @""",
""value"": ""github.com"",
""errorMessage"": """ + $"{loader.GetString("LoginUI_LoginPage_Button2Flyout_Text_ErrorMessage")}" + @"""
},
{
""type"": ""ActionSet"",
""actions"": [
{
""type"": ""Action.Submit"",
""title"": """ + $"{loader.GetString("LoginUI_LoginPage_Button2Flyout_Button")}" + @""",
""style"": ""positive"",
""associatedInputs"": ""auto""
}
]
}
]
}
}
],
""spacing"": ""None"",
""horizontalAlignment"": ""Center"",
""height"": ""stretch""
}
],
""verticalContentAlignment"": ""Center"",
""spacing"": ""None"",
""horizontalAlignment"": ""Center"",
""height"": ""stretch""
}
],
""spacing"": ""None"",
""horizontalAlignment"": ""Center"",
""height"": ""stretch"",
""horizontalCellContentAlignment"": ""Center"",
""verticalCellContentAlignment"": ""Center""
}
],
""firstRowAsHeaders"": false,
""spacing"": ""Medium"",
""horizontalAlignment"": ""Center"",
""height"": ""stretch"",
""horizontalCellContentAlignment"": ""Center"",
""verticalCellContentAlignment"": ""Center"",
""showGridLines"": false
}
],
""$schema"": ""http://adaptivecards.io/schemas/adaptive-card.json"",
""version"": ""1.5"",
""minHeight"": ""350px"",
""verticalContentAlignment"": ""Top"",
""rtl"": false
}
";
var waitingPage = @"
{
""type"": ""AdaptiveCard"",
""body"": [
{
""type"": ""TextBlock"",
""text"": """ + $"{loader.GetString("LoginUI_WaitingPage_Text")}" + @""",
""isSubtle"": false,
""wrap"": true,
""horizontalAlignment"": ""Center"",
""spacing"": ""ExtraLarge"",
""size"": ""Large"",
""weight"": ""Lighter"",
""height"": ""stretch"",
""style"": ""heading""
},
{
""type"" : ""TextBlock"",
""text"": """ + $"{loader.GetString("LoginUI_WaitingPageBrowserLaunch_Text")}" + @""",
""isSubtle"": false,
""horizontalAlignment"": ""Center"",
""weight"": ""Lighter""
}
],
""$schema"": ""http://adaptivecards.io/schemas/adaptive-card.json"",
""version"": ""1.5"",
""minHeight"": ""100px""
}
";
var loginSucceededPage = @"
{
""type"": ""AdaptiveCard"",
""body"": [
{
""type"": ""TextBlock"",
""text"": ""${message}"",
""isSubtle"": false,
""wrap"": true,
""horizontalAlignment"": ""Center"",
""spacing"": ""ExtraLarge"",
""size"": ""Large"",
""weight"": ""Lighter"",
""style"": ""heading""
}
],
""$schema"": ""http://adaptivecards.io/schemas/adaptive-card.json"",
""version"": ""1.5"",
""minHeight"": ""200px""
}
";
var loginFailedPage = @"
{
""type"": ""AdaptiveCard"",
""body"": [
{
""type"": ""TextBlock"",
""text"": """ + $"{loader.GetString("LoginUI_LoginFailedPage_text1")}" + @""",
""isSubtle"": false,
""wrap"": true,
""horizontalAlignment"": ""Center"",
""spacing"": ""ExtraLarge"",
""size"": ""Large"",
""weight"": ""Lighter"",
""style"": ""heading""
},
{
""type"": ""TextBlock"",
""text"": """ + $"{loader.GetString("LoginUI_LoginFailedPage_text2")}" + @""",
""isSubtle"": true,
""wrap"": true,
""horizontalAlignment"": ""Center"",
""size"": ""medium"",
""weight"": ""Lighter""
}
],
""$schema"": ""http://adaptivecards.io/schemas/adaptive-card.json"",
""version"": ""1.5"",
""minHeight"": ""200px""
}
";
switch (loginUIState)
{
case LoginUIState.LoginPage:
{
return loginPage;
}
case LoginUIState.WaitingPage:
{
return waitingPage;
}
case LoginUIState.LoginFailedPage:
{
return loginFailedPage;
}
case LoginUIState.LoginSucceededPage:
{
return loginSucceededPage;
}
default:
{
throw new InvalidOperationException();
}
}
}
}
// This class cannot be an enum, since we are passing this to the core app as State parameter.
private class LoginUIState
{
internal const string LoginPage = "LoginPage";
internal const string WaitingPage = "WaitingPage";
internal const string LoginFailedPage = "LoginFailedPage";
internal const string LoginSucceededPage = "LoginSucceededPage";
internal const string End = "End";
}
}