UserEditorView.axaml.cs
1 using Avalonia.Controls; 2 using Avalonia.Data; 3 using Avalonia.Interactivity; 4 using FluentAvalonia.UI.Controls; 5 using FluentAvalonia.UI.Navigation; 6 using Ryujinx.Ava.Common.Locale; 7 using Ryujinx.Ava.UI.Controls; 8 using Ryujinx.Ava.UI.Helpers; 9 using Ryujinx.Ava.UI.Models; 10 using Ryujinx.HLE.HOS.Services.Account.Acc; 11 using System; 12 using UserProfile = Ryujinx.Ava.UI.Models.UserProfile; 13 14 namespace Ryujinx.Ava.UI.Views.User 15 { 16 public partial class UserEditorView : UserControl 17 { 18 private NavigationDialogHost _parent; 19 private UserProfile _profile; 20 private bool _isNewUser; 21 22 public TempProfile TempProfile { get; set; } 23 public static uint MaxProfileNameLength => 0x20; 24 public bool IsDeletable => _profile.UserId != AccountManager.DefaultUserId; 25 26 public UserEditorView() 27 { 28 InitializeComponent(); 29 AddHandler(Frame.NavigatedToEvent, (s, e) => 30 { 31 NavigatedTo(e); 32 }, RoutingStrategies.Direct); 33 } 34 35 private void NavigatedTo(NavigationEventArgs arg) 36 { 37 if (Program.PreviewerDetached) 38 { 39 switch (arg.NavigationMode) 40 { 41 case NavigationMode.New: 42 var (parent, profile, isNewUser) = ((NavigationDialogHost parent, UserProfile profile, bool isNewUser))arg.Parameter; 43 _isNewUser = isNewUser; 44 _profile = profile; 45 TempProfile = new TempProfile(_profile); 46 47 _parent = parent; 48 break; 49 } 50 51 ((ContentDialog)_parent.Parent).Title = $"{LocaleManager.Instance[LocaleKeys.UserProfileWindowTitle]} - " + 52 $"{(_isNewUser ? LocaleManager.Instance[LocaleKeys.UserEditorTitleCreate] : LocaleManager.Instance[LocaleKeys.UserEditorTitle])}"; 53 54 DataContext = TempProfile; 55 56 AddPictureButton.IsVisible = _isNewUser; 57 ChangePictureButton.IsVisible = !_isNewUser; 58 IdLabel.IsVisible = _profile != null; 59 IdText.IsVisible = _profile != null; 60 if (!_isNewUser && IsDeletable) 61 { 62 DeleteButton.IsVisible = true; 63 } 64 else 65 { 66 DeleteButton.IsVisible = false; 67 } 68 } 69 } 70 71 private async void BackButton_Click(object sender, RoutedEventArgs e) 72 { 73 if (_isNewUser) 74 { 75 if (TempProfile.Name != String.Empty || TempProfile.Image != null) 76 { 77 if (await ContentDialogHelper.CreateChoiceDialog( 78 LocaleManager.Instance[LocaleKeys.DialogUserProfileUnsavedChangesTitle], 79 LocaleManager.Instance[LocaleKeys.DialogUserProfileUnsavedChangesMessage], 80 LocaleManager.Instance[LocaleKeys.DialogUserProfileUnsavedChangesSubMessage])) 81 { 82 _parent?.GoBack(); 83 } 84 } 85 else 86 { 87 _parent?.GoBack(); 88 } 89 } 90 else 91 { 92 if (_profile.Name != TempProfile.Name || _profile.Image != TempProfile.Image) 93 { 94 if (await ContentDialogHelper.CreateChoiceDialog( 95 LocaleManager.Instance[LocaleKeys.DialogUserProfileUnsavedChangesTitle], 96 LocaleManager.Instance[LocaleKeys.DialogUserProfileUnsavedChangesMessage], 97 LocaleManager.Instance[LocaleKeys.DialogUserProfileUnsavedChangesSubMessage])) 98 { 99 _parent?.GoBack(); 100 } 101 } 102 else 103 { 104 _parent?.GoBack(); 105 } 106 } 107 } 108 109 private void DeleteButton_Click(object sender, RoutedEventArgs e) 110 { 111 _parent.DeleteUser(_profile); 112 } 113 114 private void SaveButton_Click(object sender, RoutedEventArgs e) 115 { 116 DataValidationErrors.ClearErrors(NameBox); 117 118 if (string.IsNullOrWhiteSpace(TempProfile.Name)) 119 { 120 DataValidationErrors.SetError(NameBox, new DataValidationException(LocaleManager.Instance[LocaleKeys.UserProfileEmptyNameError])); 121 122 return; 123 } 124 125 if (TempProfile.Image == null) 126 { 127 _parent.Navigate(typeof(UserProfileImageSelectorView), (_parent, TempProfile)); 128 129 return; 130 } 131 132 if (_profile != null && !_isNewUser) 133 { 134 _profile.Name = TempProfile.Name; 135 _profile.Image = TempProfile.Image; 136 _profile.UpdateState(); 137 _parent.AccountManager.SetUserName(_profile.UserId, _profile.Name); 138 _parent.AccountManager.SetUserImage(_profile.UserId, _profile.Image); 139 } 140 else if (_isNewUser) 141 { 142 _parent.AccountManager.AddUser(TempProfile.Name, TempProfile.Image, TempProfile.UserId); 143 } 144 else 145 { 146 return; 147 } 148 149 _parent?.GoBack(); 150 } 151 152 public void SelectProfileImage() 153 { 154 _parent.Navigate(typeof(UserProfileImageSelectorView), (_parent, TempProfile)); 155 } 156 157 private void ChangePictureButton_Click(object sender, RoutedEventArgs e) 158 { 159 if (_profile != null || _isNewUser) 160 { 161 SelectProfileImage(); 162 } 163 } 164 } 165 }