-
Notifications
You must be signed in to change notification settings - Fork 821
Expand file tree
/
Copy pathUserAvatar.cs
More file actions
312 lines (285 loc) · 10.6 KB
/
UserAvatar.cs
File metadata and controls
312 lines (285 loc) · 10.6 KB
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
using Microsoft.UI;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Octokit;
using UniGetUI.Core.Logging;
using UniGetUI.Core.Tools;
using UniGetUI.Interface.Widgets;
using UniGetUI.Pages.DialogPages;
using UniGetUI.Pages.SettingsPages.GeneralPages;
using Windows.UI;
namespace UniGetUI.Services
{
public partial class PointButton : Button
{
public PointButton()
{
ProtectedCursor = InputSystemCursor.Create(InputSystemCursorShape.Hand);
}
}
public partial class UserAvatar : UserControl
{
public UserAvatar()
{
VerticalContentAlignment = VerticalAlignment.Center;
HorizontalContentAlignment = HorizontalAlignment.Center;
Microsoft.UI.Xaml.Automation.AutomationProperties.SetName(this, CoreTools.Translate("User profile"));
ToolTipService.SetToolTip(this, CoreTools.Translate("User profile"));
_ = RefreshStatus();
GitHubAuthService.AuthStatusChanged += GitHubAuthService_AuthStatusChanged;
}
private void GitHubAuthService_AuthStatusChanged(object? sender, EventArgs e)
{
_ = RefreshStatus();
}
public async Task RefreshStatus()
{
SetLoading();
var client = new GitHubAuthService();
// await Task.Delay(1000);
if (client.IsAuthenticated())
{
Content = await GenerateLogoutControl();
}
else
{
Content = GenerateLoginControl();
}
}
private void LoginButton_Click(object sender, RoutedEventArgs e) =>
_ = _loginButton_Click();
private async Task _loginButton_Click()
{
SetLoading();
try
{
var client = new GitHubAuthService();
if (client.IsAuthenticated())
{
Logger.Warn("Login invoked when the client was already logged in!");
return;
}
bool success = await client.SignInAsync();
if (!success)
{
DialogHelper.ShowDismissableBalloon(
CoreTools.Translate("Failed"),
CoreTools.Translate("An error occurred while logging in: ")
);
}
}
catch (Exception ex)
{
DialogHelper.ShowDismissableBalloon(
CoreTools.Translate("Error"),
CoreTools.Translate("Log in failed: ") + ex.Message
);
}
}
private void LogoutButton_Click(object sender, RoutedEventArgs e)
{
SetLoading();
try
{
var client = new GitHubAuthService();
if (client.IsAuthenticated())
{
client.SignOut();
}
}
catch (Exception ex)
{
DialogHelper.ShowDismissableBalloon(
CoreTools.Translate("Error"),
CoreTools.Translate("Log out failed: ") + ex.Message
);
}
}
private void SetLoading()
{
this.Content = new ProgressRing()
{
IsIndeterminate = true,
Width = 24,
Height = 24,
};
}
private PointButton GenerateLoginControl()
{
var personPicture = new PersonPicture { Width = 32, Height = 32 };
var translatedTextBlock = new TextBlock
{
Margin = new Thickness(4),
TextWrapping = TextWrapping.Wrap,
Text = CoreTools.Translate("Log in with GitHub to enable cloud package backup."),
};
var hyperlinkButton = new HyperlinkButton
{
Padding = new Thickness(0),
HorizontalAlignment = HorizontalAlignment.Stretch,
Content = new TextBlock()
{
Text = CoreTools.Translate("More details"),
TextWrapping = TextWrapping.Wrap,
},
FontSize = 12,
};
hyperlinkButton.Click += (_, _) =>
CoreTools.Launch("https://devolutions.net/unigetui");
var loginButton = new PointButton
{
HorizontalAlignment = HorizontalAlignment.Stretch,
Content = CoreTools.Translate("Log in"),
};
loginButton.Click += LoginButton_Click;
var stackPanel = new StackPanel
{
MaxWidth = 200,
Margin = new Thickness(-8),
Orientation = Orientation.Vertical,
Spacing = 8,
};
stackPanel.Children.Add(translatedTextBlock);
stackPanel.Children.Add(hyperlinkButton);
stackPanel.Children.Add(loginButton);
var flyout = new BetterFlyout()
{
LightDismissOverlayMode = LightDismissOverlayMode.Off,
Placement = FlyoutPlacementMode.Bottom,
Content = stackPanel,
};
var btn = new PointButton
{
Margin = new Thickness(0),
Padding = new Thickness(4),
Background = new SolidColorBrush(Colors.Transparent),
BorderThickness = new Thickness(0),
CornerRadius = new CornerRadius(100),
Content = personPicture,
Flyout = flyout,
};
Microsoft.UI.Xaml.Automation.AutomationProperties.SetName(btn, CoreTools.Translate("User profile"));
ToolTipService.SetToolTip(btn, CoreTools.Translate("User profile"));
return btn;
}
private async Task<PointButton> GenerateLogoutControl()
{
await Task.Run(CoreTools.WaitForInternetConnection);
User user;
try
{
var authClient = new GitHubAuthService();
var GHClient = authClient.CreateGitHubClient();
if (GHClient is null)
{
Logger.Error("Client did not report valid authentication");
return GenerateLoginControl();
}
user = await GHClient.User.Current();
}
catch (Exception ex)
{
Logger.Error("An error occurred while retrieving user's logged in data.");
Logger.Error(ex);
return GenerateLoginControl();
}
var personPicture = new PersonPicture
{
Width = 32,
Height = 32,
ProfilePicture = new BitmapImage(new Uri(user.AvatarUrl)),
};
var text1 = new TextBlock
{
Margin = new Thickness(4),
TextWrapping = TextWrapping.Wrap,
Text = CoreTools.Translate(
"You are logged in as {0} (@{1})",
user.Name,
user.Login
),
};
var text2 = new TextBlock
{
Margin = new Thickness(4),
TextWrapping = TextWrapping.Wrap,
FontSize = 12,
FontWeight = new(500),
Text = CoreTools.Translate(
"If you have cloud backup enabled, it will be saved as a GitHub Gist on this account"
),
};
var hyperlinkButton = new HyperlinkButton
{
Padding = new Thickness(0),
HorizontalAlignment = HorizontalAlignment.Stretch,
Content = new TextBlock()
{
Text = CoreTools.Translate("More details"),
TextWrapping = TextWrapping.Wrap,
},
FontSize = 12,
};
hyperlinkButton.Click += (_, _) =>
CoreTools.Launch("https://devolutions.net/unigetui");
var hyperlinkButton2 = new HyperlinkButton
{
Padding = new Thickness(0),
HorizontalAlignment = HorizontalAlignment.Stretch,
Content = new TextBlock()
{
Text = CoreTools.Translate("Package backup settings"),
TextWrapping = TextWrapping.Wrap,
},
FontSize = 12,
};
hyperlinkButton2.Click += (_, _) =>
MainApp.Instance.MainWindow.NavigationPage.OpenSettingsPage(typeof(Backup));
var loginButton = new PointButton
{
HorizontalAlignment = HorizontalAlignment.Stretch,
Content = CoreTools.Translate("Log out"),
Background = new SolidColorBrush(
ActualTheme is ElementTheme.Dark ? Colors.DarkRed : Colors.PaleVioletRed
),
BorderThickness = new(0),
};
loginButton.Click += LogoutButton_Click;
var stackPanel = new StackPanel
{
MaxWidth = 200,
Margin = new Thickness(-8),
Orientation = Orientation.Vertical,
Spacing = 8,
};
stackPanel.Children.Add(text1);
stackPanel.Children.Add(text2);
stackPanel.Children.Add(hyperlinkButton);
stackPanel.Children.Add(hyperlinkButton2);
stackPanel.Children.Add(loginButton);
var flyout = new BetterFlyout()
{
LightDismissOverlayMode = LightDismissOverlayMode.Off,
Placement = FlyoutPlacementMode.Bottom,
Content = stackPanel,
};
var btn = new PointButton
{
Margin = new Thickness(0),
Padding = new Thickness(4),
Background = new SolidColorBrush(Colors.Transparent),
BorderThickness = new Thickness(0),
CornerRadius = new CornerRadius(100),
Content = personPicture,
Flyout = flyout,
};
Microsoft.UI.Xaml.Automation.AutomationProperties.SetName(btn, CoreTools.Translate("User profile"));
ToolTipService.SetToolTip(btn, CoreTools.Translate("User profile"));
return btn;
}
}
}