|
| 1 | +using Avalonia.Controls; |
| 2 | +using Avalonia.Input.Platform; |
| 3 | +using Avalonia.Interactivity; |
| 4 | +using Avalonia.Media; |
| 5 | +using FluentAvalonia.UI.Controls; |
| 6 | +using SnapX.Core.Upload; |
| 7 | +using SnapX.Core.Upload.OAuth; |
| 8 | +using SnapX.Core.Upload.Text; |
| 9 | +using SnapX.Core.Utils; |
| 10 | +using SnapX.Core.Utils.Extensions; |
| 11 | + |
| 12 | +namespace SnapX.Avalonia.Views.Settings.Views.TextUploaders; |
| 13 | + |
| 14 | +public partial class GithubGistUploaderSettingsView : UserControl |
| 15 | +{ |
| 16 | + public GithubGistUploaderSettingsView() |
| 17 | + { |
| 18 | + InitializeComponent(); |
| 19 | + } |
| 20 | + |
| 21 | + protected override void OnDataContextChanged(EventArgs e) |
| 22 | + { |
| 23 | + base.OnDataContextChanged(e); |
| 24 | + |
| 25 | + if (DataContext is not GitHubGist) return; |
| 26 | + var config = SnapX.Core.SnapXL.UploadersConfig; |
| 27 | + CalculateAccessibility(); |
| 28 | + cbGistUseRawURL.IsChecked = config.GistRawURL; |
| 29 | + cbGistUseRawURL.IsCheckedChanged += (Sender, Args) => config.GistRawURL = cbGistUseRawURL.IsChecked ?? false; |
| 30 | + cbGistPublishPublic.IsChecked = config.GistPublishPublic; |
| 31 | + cbGistPublishPublic.IsCheckedChanged += (Sender, Args) => config.GistRawURL = cbGistPublishPublic.IsChecked ?? false; |
| 32 | + |
| 33 | + txtGistCustomURL.Text = config.GistCustomURL; |
| 34 | + txtGistCustomURL.TextChanged += (Sender, Args) => config.GistCustomURL = txtGistCustomURL.Text; |
| 35 | + } |
| 36 | + |
| 37 | + private async void ConnectButton_OnClick(object? Sender, RoutedEventArgs E) |
| 38 | + { |
| 39 | + var control = Sender as Control; |
| 40 | + var oauth = new OAuth2Info(APIKeys.GitHubID, APIKeys.GitHubSecret); |
| 41 | + var gist = new GitHubGist(oauth); |
| 42 | + var config = SnapX.Core.SnapXL.UploadersConfig; |
| 43 | + config.GistOAuth2Info = oauth; |
| 44 | + DataContext = gist; |
| 45 | + var url = await Task.Run(gist.GetAuthorizationURL); |
| 46 | + URLHelpers.OpenURL(url); |
| 47 | + } |
| 48 | + void CalculateAccessibility() |
| 49 | + { |
| 50 | + var Config = SnapX.Core.SnapXL.UploadersConfig; |
| 51 | + var oAuthSuccess = OAuth2Info.CheckOAuth(Config.GistOAuth2Info); |
| 52 | + |
| 53 | + ConnectButton.Content = oAuthSuccess ? "Disconnect" : "Connect..."; |
| 54 | + LoginStatus.Text = oAuthSuccess ? "Status: Logged in." : $"Status: {OAuthLoginStatus.LoginRequired}"; |
| 55 | + LoginStatus.Foreground = oAuthSuccess ? Brushes.LawnGreen : Brushes.Red; |
| 56 | + } |
| 57 | + private async void PasswordTextBox_OnPastingFromClipboard(object? Sender, RoutedEventArgs E) |
| 58 | + { |
| 59 | + try |
| 60 | + { |
| 61 | + var control = Sender as Control; |
| 62 | + var gist = control!.DataContext as GitHubGist; |
| 63 | + var topLevel = TopLevel.GetTopLevel(this); |
| 64 | + if (topLevel?.Clipboard == null) return; |
| 65 | + var code = await (topLevel?.Clipboard).TryGetTextAsync(); |
| 66 | + if (string.IsNullOrWhiteSpace(code)) return; |
| 67 | + var config = SnapX.Core.SnapXL.UploadersConfig; |
| 68 | + // config.GistOAuth2Info |
| 69 | + var success = await Task.Run(() => gist!.GetAccessToken(code)); |
| 70 | + if (success) config.GistOAuth2Info = gist!.AuthInfo; |
| 71 | + CalculateAccessibility(); |
| 72 | + var dialog = new ContentDialog |
| 73 | + { |
| 74 | + Title = success ? "Success" : "Login Failed", |
| 75 | + Content = success ? "Login successful!" : "Please check your username and password.", |
| 76 | + CloseButtonText = "OK", |
| 77 | + DefaultButton = ContentDialogButton.Close |
| 78 | + }; |
| 79 | + |
| 80 | + PasswordTextBox.Text = code; |
| 81 | + E.Handled = true; |
| 82 | + if (topLevel != null) |
| 83 | + { |
| 84 | + await dialog.ShowAsync(topLevel); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + await dialog.ShowAsync(); |
| 89 | + } |
| 90 | + } |
| 91 | + catch (Exception e) |
| 92 | + { |
| 93 | + e.ShowError(); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments