|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Security.Authentication; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.UI; |
| 8 | +using Microsoft.UI.Xaml; |
| 9 | +using Microsoft.UI.Xaml.Controls; |
| 10 | +using Microsoft.UI.Xaml.Controls.Primitives; |
| 11 | +using Microsoft.UI.Xaml.Media; |
| 12 | +using Microsoft.UI.Xaml.Media.Imaging; |
| 13 | +using UniGetUI.Core.Logging; |
| 14 | +using UniGetUI.Core.Tools; |
| 15 | +using UniGetUI.Pages.DialogPages; |
| 16 | +using UniGetUI.Pages.SettingsPages.GeneralPages; |
| 17 | + |
| 18 | +namespace UniGetUI.Services |
| 19 | +{ |
| 20 | + public partial class UserAvatar: UserControl |
| 21 | + { |
| 22 | + public UserAvatar() |
| 23 | + { |
| 24 | + VerticalContentAlignment = VerticalAlignment.Center; |
| 25 | + HorizontalContentAlignment = HorizontalAlignment.Center; |
| 26 | + _ = RefreshStatus(); |
| 27 | + } |
| 28 | + |
| 29 | + public async Task RefreshStatus() |
| 30 | + { |
| 31 | + SetLoading(); |
| 32 | + var client = new GitHubAuthService(); |
| 33 | + await Task.Delay(1000); |
| 34 | + if (await client.IsAuthenticatedAsync()) |
| 35 | + { |
| 36 | + Content = await GenerateLogoutControl(); |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + Content = GenerateLoginControl(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private async void LoginButton_Click(object sender, RoutedEventArgs e) |
| 45 | + { |
| 46 | + SetLoading(); |
| 47 | + try |
| 48 | + { |
| 49 | + var client = new GitHubAuthService(); |
| 50 | + if (await client.IsAuthenticatedAsync()) |
| 51 | + { |
| 52 | + Logger.Warn("Login invoked when the client was already logged in!"); |
| 53 | + await RefreshStatus(); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + await client.SignInAsync(); |
| 58 | + await RefreshStatus(); |
| 59 | + } |
| 60 | + catch (Exception ex) |
| 61 | + { |
| 62 | + DialogHelper.ShowDismissableBalloon( |
| 63 | + CoreTools.Translate("Error"), |
| 64 | + CoreTools.Translate("Log in failed: ") + ex.Message |
| 65 | + ); |
| 66 | + await RefreshStatus(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private async void LogoutButton_Click(object sender, RoutedEventArgs e) |
| 71 | + { |
| 72 | + SetLoading(); |
| 73 | + try |
| 74 | + { |
| 75 | + var client = new GitHubAuthService(); |
| 76 | + if (await client.IsAuthenticatedAsync()) |
| 77 | + { |
| 78 | + await client.SignOutAsync(); |
| 79 | + } |
| 80 | + |
| 81 | + await RefreshStatus(); |
| 82 | + } |
| 83 | + catch (Exception ex) |
| 84 | + { |
| 85 | + DialogHelper.ShowDismissableBalloon( |
| 86 | + CoreTools.Translate("Error"), |
| 87 | + CoreTools.Translate("Log out failed: ") + ex.Message |
| 88 | + ); |
| 89 | + await RefreshStatus(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private void SetLoading() |
| 94 | + { |
| 95 | + this.Content = new ProgressRing() { IsIndeterminate = true, Width = 24, Height = 24 }; |
| 96 | + } |
| 97 | + |
| 98 | + private Button GenerateLoginControl() |
| 99 | + { |
| 100 | + var personPicture = new PersonPicture |
| 101 | + { |
| 102 | + Width = 36, |
| 103 | + Height = 36, |
| 104 | + Initials = "?" |
| 105 | + }; |
| 106 | + |
| 107 | + var translatedTextBlock = new TextBlock |
| 108 | + { |
| 109 | + Margin = new Thickness(4), |
| 110 | + TextWrapping = TextWrapping.WrapWholeWords, |
| 111 | + Text = CoreTools.Translate("Log in with GitHub to enable cloud package backup.") |
| 112 | + }; |
| 113 | + |
| 114 | + var hyperlinkButton = new HyperlinkButton |
| 115 | + { |
| 116 | + Padding = new Thickness(0), |
| 117 | + HorizontalAlignment = HorizontalAlignment.Stretch, |
| 118 | + Content = CoreTools.Translate("More details"), |
| 119 | + NavigateUri = new Uri("https://www.marticliment.com/unigetui/help/cloud-backup-overview/"), |
| 120 | + FontSize = 12 |
| 121 | + }; |
| 122 | + |
| 123 | + var loginButton = new Button |
| 124 | + { |
| 125 | + HorizontalAlignment = HorizontalAlignment.Stretch, |
| 126 | + Content = CoreTools.Translate("Log in") |
| 127 | + }; |
| 128 | + loginButton.Click += LoginButton_Click; |
| 129 | + |
| 130 | + var stackPanel = new StackPanel |
| 131 | + { |
| 132 | + MaxWidth = 200, |
| 133 | + Margin = new Thickness(-8), |
| 134 | + Orientation = Orientation.Vertical, |
| 135 | + Spacing = 8 |
| 136 | + }; |
| 137 | + stackPanel.Children.Add(translatedTextBlock); |
| 138 | + stackPanel.Children.Add(hyperlinkButton); |
| 139 | + stackPanel.Children.Add(loginButton); |
| 140 | + |
| 141 | + var flyout = new Flyout |
| 142 | + { |
| 143 | + LightDismissOverlayMode = LightDismissOverlayMode.Off, |
| 144 | + Placement = FlyoutPlacementMode.Bottom, |
| 145 | + Content = stackPanel |
| 146 | + }; |
| 147 | + |
| 148 | + return new Button |
| 149 | + { |
| 150 | + Margin = new Thickness(0), |
| 151 | + Padding = new Thickness(4), |
| 152 | + Background = new SolidColorBrush(Colors.Transparent), |
| 153 | + BorderThickness = new Thickness(0), |
| 154 | + CornerRadius = new CornerRadius(100), |
| 155 | + Content = personPicture, |
| 156 | + Flyout = flyout |
| 157 | + }; |
| 158 | + } |
| 159 | + |
| 160 | + private async Task<Button> GenerateLogoutControl() |
| 161 | + { |
| 162 | + var authClient = new GitHubAuthService(); |
| 163 | + var token = await authClient.GetAccessTokenAsync(); |
| 164 | + var GHClient = await authClient.CreateGitHubClientAsync(); |
| 165 | + if(GHClient is null) |
| 166 | + { |
| 167 | + Logger.Error("Client did not report valid authentication"); |
| 168 | + return GenerateLoginControl(); |
| 169 | + } |
| 170 | + |
| 171 | + var user = await GHClient.User.Current(); |
| 172 | + |
| 173 | + var personPicture = new PersonPicture |
| 174 | + { |
| 175 | + Width = 36, |
| 176 | + Height = 36, |
| 177 | + ProfilePicture = new BitmapImage(new Uri(user.AvatarUrl)) |
| 178 | + }; |
| 179 | + |
| 180 | + var text1 = new TextBlock |
| 181 | + { |
| 182 | + Margin = new Thickness(4), |
| 183 | + TextWrapping = TextWrapping.WrapWholeWords, |
| 184 | + Text = CoreTools.Translate("You are logged in as {0} (@{1})", user.Name, user.Login) |
| 185 | + }; |
| 186 | + |
| 187 | + var text2 = new TextBlock |
| 188 | + { |
| 189 | + Margin = new Thickness(4), |
| 190 | + TextWrapping = TextWrapping.WrapWholeWords, |
| 191 | + FontSize = 12, |
| 192 | + FontWeight = new(500), |
| 193 | + Text = CoreTools.Translate("If you have cloud backup enabled, it will be saved as a GitHub Gist on this account") |
| 194 | + }; |
| 195 | + |
| 196 | + var hyperlinkButton = new HyperlinkButton |
| 197 | + { |
| 198 | + Padding = new Thickness(0), |
| 199 | + HorizontalAlignment = HorizontalAlignment.Stretch, |
| 200 | + Content = "Backup settings", |
| 201 | + FontSize = 12 |
| 202 | + }; |
| 203 | + hyperlinkButton.Click += (_, _) => MainApp.Instance.MainWindow.NavigationPage.OpenSettingsPage(typeof(Backup)); |
| 204 | + |
| 205 | + var loginButton = new Button |
| 206 | + { |
| 207 | + HorizontalAlignment = HorizontalAlignment.Stretch, |
| 208 | + Content = "Log out", |
| 209 | + Background = new SolidColorBrush(ActualTheme is ElementTheme.Dark? Colors.DarkRed: Colors.Red), |
| 210 | + BorderThickness = new(0) |
| 211 | + }; |
| 212 | + loginButton.Click += LogoutButton_Click; |
| 213 | + |
| 214 | + var stackPanel = new StackPanel |
| 215 | + { |
| 216 | + MaxWidth = 200, |
| 217 | + Margin = new Thickness(-8), |
| 218 | + Orientation = Orientation.Vertical, |
| 219 | + Spacing = 8 |
| 220 | + }; |
| 221 | + stackPanel.Children.Add(text1); |
| 222 | + stackPanel.Children.Add(text2); |
| 223 | + stackPanel.Children.Add(hyperlinkButton); |
| 224 | + stackPanel.Children.Add(loginButton); |
| 225 | + |
| 226 | + var flyout = new Flyout |
| 227 | + { |
| 228 | + LightDismissOverlayMode = LightDismissOverlayMode.Off, |
| 229 | + Placement = FlyoutPlacementMode.Bottom, |
| 230 | + Content = stackPanel |
| 231 | + }; |
| 232 | + |
| 233 | + return new Button |
| 234 | + { |
| 235 | + Margin = new Thickness(0), |
| 236 | + Padding = new Thickness(4), |
| 237 | + Background = new SolidColorBrush(Colors.Transparent), |
| 238 | + BorderThickness = new Thickness(0), |
| 239 | + CornerRadius = new CornerRadius(100), |
| 240 | + Content = personPicture, |
| 241 | + Flyout = flyout |
| 242 | + }; |
| 243 | + } |
| 244 | + |
| 245 | + |
| 246 | + } |
| 247 | +} |
0 commit comments