UserEditor.axaml.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.Models;
  8. using UserProfile = Ryujinx.Ava.Ui.Models.UserProfile;
  9. namespace Ryujinx.Ava.Ui.Controls
  10. {
  11. public partial class UserEditor : UserControl
  12. {
  13. private NavigationDialogHost _parent;
  14. private UserProfile _profile;
  15. private bool _isNewUser;
  16. public TempProfile TempProfile { get; set; }
  17. public uint MaxProfileNameLength => 0x20;
  18. public UserEditor()
  19. {
  20. InitializeComponent();
  21. AddHandler(Frame.NavigatedToEvent, (s, e) =>
  22. {
  23. NavigatedTo(e);
  24. }, RoutingStrategies.Direct);
  25. }
  26. private void NavigatedTo(NavigationEventArgs arg)
  27. {
  28. if (Program.PreviewerDetached)
  29. {
  30. switch (arg.NavigationMode)
  31. {
  32. case NavigationMode.New:
  33. var args = ((NavigationDialogHost parent, UserProfile profile, bool isNewUser))arg.Parameter;
  34. _isNewUser = args.isNewUser;
  35. if (!_isNewUser)
  36. {
  37. _profile = args.profile;
  38. TempProfile = new TempProfile(_profile);
  39. }
  40. else
  41. {
  42. TempProfile = new TempProfile();
  43. }
  44. _parent = args.parent;
  45. break;
  46. }
  47. DataContext = TempProfile;
  48. AddPictureButton.IsVisible = _isNewUser;
  49. IdLabel.IsVisible = !_isNewUser;
  50. ChangePictureButton.IsVisible = !_isNewUser;
  51. }
  52. }
  53. private void CloseButton_Click(object sender, RoutedEventArgs e)
  54. {
  55. _parent?.GoBack();
  56. }
  57. private async void SaveButton_Click(object sender, RoutedEventArgs e)
  58. {
  59. DataValidationErrors.ClearErrors(NameBox);
  60. bool isInvalid = false;
  61. if (string.IsNullOrWhiteSpace(TempProfile.Name))
  62. {
  63. DataValidationErrors.SetError(NameBox, new DataValidationException(LocaleManager.Instance["UserProfileEmptyNameError"]));
  64. isInvalid = true;
  65. }
  66. if (TempProfile.Image == null)
  67. {
  68. await ContentDialogHelper.CreateWarningDialog(LocaleManager.Instance["UserProfileNoImageError"], "");
  69. isInvalid = true;
  70. }
  71. if(isInvalid)
  72. {
  73. return;
  74. }
  75. if (_profile != null)
  76. {
  77. _profile.Name = TempProfile.Name;
  78. _profile.Image = TempProfile.Image;
  79. _profile.UpdateState();
  80. _parent.AccountManager.SetUserName(_profile.UserId, _profile.Name);
  81. _parent.AccountManager.SetUserImage(_profile.UserId, _profile.Image);
  82. }
  83. else if (_isNewUser)
  84. {
  85. _parent.AccountManager.AddUser(TempProfile.Name, TempProfile.Image);
  86. }
  87. else
  88. {
  89. return;
  90. }
  91. _parent?.GoBack();
  92. }
  93. public void SelectProfileImage()
  94. {
  95. _parent.Navigate(typeof(ProfileImageSelectionDialog), (_parent, TempProfile));
  96. }
  97. private void ChangePictureButton_Click(object sender, RoutedEventArgs e)
  98. {
  99. if (_profile != null || _isNewUser)
  100. {
  101. SelectProfileImage();
  102. }
  103. }
  104. }
  105. }