using Avalonia.Media.Imaging;
using CommunityToolkit.Mvvm.ComponentModel;
using QRCoder;
using ZonyLrcTools.Common.MusicScanner;
using ZonyLrcTools.Desktop.Infrastructure.Localization;
namespace ZonyLrcTools.Desktop.ViewModels;
///
/// 网易云扫码登录对话框。
///
public partial class QrCodeLoginViewModel : ObservableObject
{
private readonly IUILocalizationService? _localization;
private bool _isClosed;
[ObservableProperty]
private Bitmap? _qrCodeImage;
[ObservableProperty]
private string _statusText = string.Empty;
[ObservableProperty]
private bool _hasImage;
///
/// 请求关闭登录对话框。
///
public event EventHandler? CloseRequested;
///
/// 登录完成前关闭对话框时触发。
///
public event EventHandler? Cancelled;
///
/// 是否已登录成功。
///
public bool IsCompleted { get; private set; }
/// 登录窗口标题。
public string LoginTitle => _localization?["NetEase_LoginTitle"] ?? "Sign in to NetEase Cloud Music";
/// 扫码登录提示。
public string LoginTip => _localization?["NetEase_LoginTip"] ?? "Scan the QR code with the NetEase Cloud Music app on your phone.";
/// 取消按钮文案。
public string CancelText => _localization?["Common_Cancel"] ?? "Cancel";
///
/// 创建扫码登录对话框状态。
///
public QrCodeLoginViewModel(IUILocalizationService? localization = null)
{
_localization = localization;
_statusText = WaitText;
}
private string WaitText => _localization?["NetEase_LoginWaiting"] ?? "Waiting for you to scan...";
private string ScannedText => _localization?["NetEase_LoginScanned"] ?? "Scanned, please confirm on your phone.";
private string SuccessText => _localization?["NetEase_LoginSucceeded"] ?? "Signed in.";
///
/// 显示扫描器提供的二维码。
///
public void SetPayload(NetEaseQrCodePayload payload)
{
if (_isClosed) return;
var previousImage = QrCodeImage;
QrCodeImage = RenderQrCode(payload.Content);
HasImage = QrCodeImage != null;
previousImage?.Dispose();
StatusText = HasImage
? WaitText
: _localization?["NetEase_LoginFailed"] ?? "Login did not complete. Close this dialog and try again.";
}
///
/// 更新扫码登录状态。
///
public void SetState(NetEaseLoginState state)
{
if (_isClosed) return;
StatusText = state switch
{
NetEaseLoginState.ScanConfirmed => ScannedText,
NetEaseLoginState.Succeeded => SuccessText,
NetEaseLoginState.Expired => _localization?["NetEase_LoginExpired"] ?? "The QR code expired. Close this dialog and try again.",
_ => WaitText
};
}
///
/// 显示登录错误。
///
public void SetError(string message)
{
if (_isClosed) return;
StatusText = message;
}
///
/// 标记登录完成并关闭对话框。
///
public void Complete()
{
if (_isClosed) return;
IsCompleted = true;
RequestClose();
}
///
/// 请求关闭对话框。
///
public void RequestClose()
{
CloseRequested?.Invoke(this, EventArgs.Empty);
}
///
/// 窗口关闭后停止登录并释放二维码。
///
public void NotifyClosed()
{
if (_isClosed) return;
_isClosed = true;
var image = QrCodeImage;
QrCodeImage = null;
HasImage = false;
image?.Dispose();
if (!IsCompleted)
{
Cancelled?.Invoke(this, EventArgs.Empty);
}
}
private static Bitmap? RenderQrCode(string content)
{
try
{
using var generator = new QRCodeGenerator();
using var data = generator.CreateQrCode(content, QRCodeGenerator.ECCLevel.M);
using var qrCode = new PngByteQRCode(data);
using var stream = new MemoryStream(qrCode.GetGraphic(12));
return new Bitmap(stream);
}
catch (Exception)
{
// 二维码无法生成时,由界面显示登录失败提示。
return null;
}
}
}