UserEditor.axaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. _profile = args.profile;
  36. TempProfile = new TempProfile(_profile);
  37. _parent = args.parent;
  38. break;
  39. }
  40. DataContext = TempProfile;
  41. AddPictureButton.IsVisible = _isNewUser;
  42. IdLabel.IsVisible = _profile != null;
  43. IdText.IsVisible = _profile != null;
  44. ChangePictureButton.IsVisible = !_isNewUser;
  45. }
  46. }
  47. private void CloseButton_Click(object sender, RoutedEventArgs e)
  48. {
  49. _parent?.GoBack();
  50. }
  51. private async void SaveButton_Click(object sender, RoutedEventArgs e)
  52. {
  53. DataValidationErrors.ClearErrors(NameBox);
  54. bool isInvalid = false;
  55. if (string.IsNullOrWhiteSpace(TempProfile.Name))
  56. {
  57. DataValidationErrors.SetError(NameBox, new DataValidationException(LocaleManager.Instance["UserProfileEmptyNameError"]));
  58. isInvalid = true;
  59. }
  60. if (TempProfile.Image == null)
  61. {
  62. await ContentDialogHelper.CreateWarningDialog(LocaleManager.Instance["UserProfileNoImageError"], "");
  63. isInvalid = true;
  64. }
  65. if(isInvalid)
  66. {
  67. return;
  68. }
  69. if (_profile != null && !_isNewUser)
  70. {
  71. _profile.Name = TempProfile.Name;
  72. _profile.Image = TempProfile.Image;
  73. _profile.UpdateState();
  74. _parent.AccountManager.SetUserName(_profile.UserId, _profile.Name);
  75. _parent.AccountManager.SetUserImage(_profile.UserId, _profile.Image);
  76. }
  77. else if (_isNewUser)
  78. {
  79. _parent.AccountManager.AddUser(TempProfile.Name, TempProfile.Image, TempProfile.UserId);
  80. }
  81. else
  82. {
  83. return;
  84. }
  85. _parent?.GoBack();
  86. }
  87. public void SelectProfileImage()
  88. {
  89. _parent.Navigate(typeof(ProfileImageSelectionDialog), (_parent, TempProfile));
  90. }
  91. private void ChangePictureButton_Click(object sender, RoutedEventArgs e)
  92. {
  93. if (_profile != null || _isNewUser)
  94. {
  95. SelectProfileImage();
  96. }
  97. }
  98. }
  99. }