-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Avalonia; | ||
using Avalonia.Controls.ApplicationLifetimes; | ||
using Avalonia.Controls.Notifications; | ||
using Avalonia.Threading; | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.Input; | ||
using CommunityToolkit.Mvvm.Messaging; | ||
using DialogHostAvalonia; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using WonderLab.Infrastructure.Models.Messaging; | ||
using WonderLab.Services.Accounts; | ||
|
||
namespace WonderLab.ViewModels.Dialog.Auth; | ||
|
||
public sealed partial class MicrosoftAuthDialogViewModel : ObservableObject { | ||
private readonly AccountService _accountService; | ||
|
||
private CancellationTokenSource _cancellationTokenSource; | ||
Check warning on line 20 in WonderLab/ViewModels/Dialog/Auth/MicrosoftAuthDialogViewModel.cs GitHub Actions / test_build
Check warning on line 20 in WonderLab/ViewModels/Dialog/Auth/MicrosoftAuthDialogViewModel.cs GitHub Actions / build_MacOS
Check warning on line 20 in WonderLab/ViewModels/Dialog/Auth/MicrosoftAuthDialogViewModel.cs GitHub Actions / build_Windows
Check warning on line 20 in WonderLab/ViewModels/Dialog/Auth/MicrosoftAuthDialogViewModel.cs GitHub Actions / build_Linux (x64)
Check warning on line 20 in WonderLab/ViewModels/Dialog/Auth/MicrosoftAuthDialogViewModel.cs GitHub Actions / build_Linux (arm)
|
||
|
||
[ObservableProperty] private string _verificationUrl; | ||
|
||
[ObservableProperty] | ||
[NotifyPropertyChangedFor(nameof(IsCodeLoaded))] | ||
private string _userCode; | ||
|
||
public bool IsCodeLoaded => !string.IsNullOrEmpty(UserCode); | ||
|
||
public MicrosoftAuthDialogViewModel(AccountService accountService) { | ||
_accountService = accountService; | ||
} | ||
|
||
private bool CanCopy() => !string.IsNullOrEmpty(UserCode); | ||
|
||
[RelayCommand] | ||
private Task OnLoaded() => Task.Run(async () => { | ||
var account = await _accountService.CreateMicrosoftAccount(x => { | ||
UserCode = x.UserCode; | ||
VerificationUrl = x.VerificationUrl; | ||
}, _cancellationTokenSource); | ||
|
||
Close(); | ||
WeakReferenceMessenger.Default.Send(new NotificationMessage($"已将微软账户\"{account.Name}\"添加至 WonderLab!", NotificationType.Warning)); | ||
}); | ||
|
||
[RelayCommand] | ||
private void Close() => Dispatcher.UIThread.InvokeAsync(() => { | ||
DialogHost.Close("PART_DialogHost"); | ||
|
||
try { | ||
_cancellationTokenSource.Cancel(); | ||
} catch (System.Exception) {} | ||
}); | ||
|
||
[RelayCommand] | ||
private void OpenUrl() => Dispatcher.UIThread.InvokeAsync(() => { | ||
Process.Start(new ProcessStartInfo(VerificationUrl) { | ||
UseShellExecute = true, | ||
Verb = "open" | ||
}).Dispose(); | ||
}); | ||
|
||
[RelayCommand] | ||
private void CopyCode() => Dispatcher.UIThread.InvokeAsync(async () => { | ||
if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { | ||
var clipboard = desktop.MainWindow?.Clipboard; | ||
|
||
if (clipboard != null) { | ||
await clipboard.SetTextAsync(UserCode); | ||
} | ||
} | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<UserControl xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" | ||
xmlns:wc="using:WonderLab.Controls" | ||
x:Class="WonderLab.Views.Dialog.Auth.MicrosoftAuthDialog"> | ||
<Interaction.Behaviors> | ||
<EventTriggerBehavior EventName="Loaded"> | ||
<InvokeCommandAction Command="{Binding LoadedCommand}"/> | ||
</EventTriggerBehavior> | ||
</Interaction.Behaviors> | ||
|
||
<Grid Margin="16 24 16 0" | ||
RowDefinitions="Auto, Auto, Auto"> | ||
<TextBlock Classes="Subtitle" | ||
Text="创建新账户"/> | ||
|
||
<wc:ProgressRing Width="45" | ||
Height="45" | ||
Grid.Row="1" | ||
Margin="0 16 0 24" | ||
BorderThickness="3" | ||
IsIndeterminate="True" | ||
Background="Transparent" | ||
VerticalAlignment="Center" | ||
HorizontalAlignment="Center" | ||
IsVisible="{Binding IsCodeLoaded, Converter={StaticResource BooleanReverseConverter}}"/> | ||
|
||
<StackPanel Grid.Row="1" | ||
Spacing="8" | ||
Margin="0 16 0 24" | ||
HorizontalAlignment="Center" | ||
IsVisible="{Binding IsCodeLoaded}"> | ||
<TextBlock Classes="Body" | ||
Text="使用一次性代码" | ||
TextAlignment="Center"/> | ||
|
||
<Button HorizontalAlignment="Center" | ||
Command="{Binding CopyCodeCommand}"> | ||
<TextBlock Classes="BodyStrong" | ||
TextAlignment="Center" | ||
Text="{Binding UserCode}"/> | ||
</Button> | ||
|
||
<TextBlock Classes="Body" | ||
Text="访问验证网址以继续下一步验证操作" | ||
TextAlignment="Center"/> | ||
|
||
<TextBlock Classes="Caption" | ||
Text="网址访问速度可能比较慢,但请务必在规定的时间内完成验证操作,否则此验证码将失效!" | ||
TextAlignment="Center"/> | ||
|
||
<ProgressBar IsIndeterminate="True"/> | ||
</StackPanel> | ||
|
||
<Border Grid.Row="2" | ||
Padding="16" | ||
Margin="-16 0" | ||
CornerRadius="0 0 12 12" | ||
BorderThickness="0 1.5 0 0" | ||
Background="{DynamicResource ContentColor2}" | ||
BorderBrush="{DynamicResource ContentColor3}"> | ||
<Grid ColumnDefinitions="Auto, 1*, Auto"> | ||
<Button Grid.Column="0" | ||
Content="打开验证网页" | ||
ToolTip.Tip="{Binding UserCode}" | ||
Command="{Binding OpenUrlCommand}"/> | ||
|
||
<Button Grid.Column="2" | ||
Content="取消" | ||
Command="{Binding CloseCommand}"/> | ||
</Grid> | ||
</Border> | ||
</Grid> | ||
</UserControl> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Avalonia.Controls; | ||
|
||
namespace WonderLab.Views.Dialog.Auth; | ||
|
||
public partial class MicrosoftAuthDialog : UserControl { | ||
public MicrosoftAuthDialog() { | ||
InitializeComponent(); | ||
} | ||
} |