UserEditor.axaml.cs 3.6 KB

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