using CommunityToolkit.Mvvm.ComponentModel; using Microsoft.UI.Xaml; namespace XFEWarframe.Player.ViewModels; public sealed record IdentityOperation(string Key, string Label); public partial class IdentityViewModel : ObservableObject { public IReadOnlyList Operations { get; } = [new("name", "修改游戏昵称"), new("email", "修改邮箱"), new("password", "修改密码"), new("delete", "删除账号")]; [ObservableProperty, NotifyPropertyChangedFor(nameof(ValueLabel), nameof(ValueVisibility), nameof(PasswordVisibility), nameof(Warning), nameof(SubmitLabel))] public partial IdentityOperation? Operation { get; set; } [ObservableProperty] public partial string Value { get; set; } = ""; [ObservableProperty] public partial string Password { get; set; } = ""; [ObservableProperty] public partial string NewPassword { get; set; } = ""; [ObservableProperty] public partial string Confirmation { get; set; } = ""; [ObservableProperty] public partial string Error { get; set; } = ""; public string ValueLabel => Operation?.Key == "email" ? "新邮箱" : Operation?.Key == "delete" ? "输入当前游戏昵称以确认删除" : "新游戏昵称"; public string SubmitLabel => Operation?.Key == "delete" ? "永久删除账号" : "保存更改"; public Visibility ValueVisibility => Operation?.Key != "password" ? Visibility.Visible : Visibility.Collapsed; public Visibility PasswordVisibility => Operation?.Key == "password" ? Visibility.Visible : Visibility.Collapsed; public string Warning => Operation?.Key == "delete" ? "此操作将永久删除账号及存档。若你是氏族创始人,氏族也会删除。" : Operation?.Key == "password" ? "更改密码后会注销游戏与其他桌面会话。本启动器会使用新密码重新登录。" : "请输入当前密码确认修改。"; public IdentityViewModel() => Operation = Operations[0]; partial void OnOperationChanged(IdentityOperation? value) { Error = ""; Value = value?.Key == "name" ? App.Accounts.State.DisplayName : value?.Key == "email" ? App.Accounts.State.Email : ""; Clear(); } public async Task SubmitAsync() { Error = ""; try { if (Operation is null) return false; if (Operation.Key == "password" && (NewPassword.Length < 8 || NewPassword != Confirmation)) throw new InvalidOperationException("新密码至少 8 位,两次输入必须一致。"); await App.Accounts.EditIdentityAsync(Operation.Key, Password, Operation.Key == "password" ? NewPassword : Value.Trim()); Clear(); return true; } catch (Exception ex) { Error = ex.Message; return false; } } public void Clear() => Password = NewPassword = Confirmation = ""; }