UserEditorView.axaml.cs 5.7 KB

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