UserProfileViewModel.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using Ryujinx.Ava.Common.Locale;
  2. using Ryujinx.Ava.Ui.Controls;
  3. using Ryujinx.Ava.Ui.Windows;
  4. using Ryujinx.HLE.HOS.Services.Account.Acc;
  5. using System;
  6. using System.Collections.ObjectModel;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using UserProfile = Ryujinx.Ava.Ui.Models.UserProfile;
  10. namespace Ryujinx.Ava.Ui.ViewModels
  11. {
  12. public class UserProfileViewModel : BaseModel, IDisposable
  13. {
  14. private const uint MaxProfileNameLength = 0x20;
  15. private readonly UserProfileWindow _owner;
  16. private UserProfile _selectedProfile;
  17. private string _tempUserName;
  18. public UserProfileViewModel()
  19. {
  20. Profiles = new ObservableCollection<UserProfile>();
  21. }
  22. public UserProfileViewModel(UserProfileWindow owner) : this()
  23. {
  24. _owner = owner;
  25. LoadProfiles();
  26. }
  27. public ObservableCollection<UserProfile> Profiles { get; set; }
  28. public UserProfile SelectedProfile
  29. {
  30. get => _selectedProfile;
  31. set
  32. {
  33. _selectedProfile = value;
  34. OnPropertyChanged(nameof(SelectedProfile));
  35. OnPropertyChanged(nameof(IsSelectedProfileDeletable));
  36. }
  37. }
  38. public bool IsSelectedProfileDeletable =>
  39. _selectedProfile != null && _selectedProfile.UserId != AccountManager.DefaultUserId;
  40. public void Dispose()
  41. {
  42. }
  43. public void LoadProfiles()
  44. {
  45. Profiles.Clear();
  46. var profiles = _owner.AccountManager.GetAllUsers()
  47. .OrderByDescending(x => x.AccountState == AccountState.Open);
  48. foreach (var profile in profiles)
  49. {
  50. Profiles.Add(new UserProfile(profile));
  51. }
  52. SelectedProfile = Profiles.FirstOrDefault(x => x.UserId == _owner.AccountManager.LastOpenedUser.UserId);
  53. if (SelectedProfile == null)
  54. {
  55. SelectedProfile = Profiles.First();
  56. if (SelectedProfile != null)
  57. {
  58. _owner.AccountManager.OpenUser(_selectedProfile.UserId);
  59. }
  60. }
  61. }
  62. public async void ChooseProfileImage()
  63. {
  64. await SelectProfileImage();
  65. }
  66. public async Task SelectProfileImage(bool isNewUser = false)
  67. {
  68. ProfileImageSelectionDialog selectionDialog = new(_owner.ContentManager);
  69. await selectionDialog.ShowDialog(_owner);
  70. if (selectionDialog.BufferImageProfile != null)
  71. {
  72. if (isNewUser)
  73. {
  74. if (!string.IsNullOrWhiteSpace(_tempUserName))
  75. {
  76. _owner.AccountManager.AddUser(_tempUserName, selectionDialog.BufferImageProfile);
  77. }
  78. }
  79. else if (SelectedProfile != null)
  80. {
  81. _owner.AccountManager.SetUserImage(SelectedProfile.UserId, selectionDialog.BufferImageProfile);
  82. SelectedProfile.Image = selectionDialog.BufferImageProfile;
  83. SelectedProfile = null;
  84. }
  85. LoadProfiles();
  86. }
  87. }
  88. public async void AddUser()
  89. {
  90. var dlgTitle = LocaleManager.Instance["InputDialogAddNewProfileTitle"];
  91. var dlgMainText = LocaleManager.Instance["InputDialogAddNewProfileHeader"];
  92. var dlgSubText = string.Format(LocaleManager.Instance["InputDialogAddNewProfileSubtext"],
  93. MaxProfileNameLength);
  94. _tempUserName =
  95. await ContentDialogHelper.CreateInputDialog(dlgTitle, dlgMainText, dlgSubText, _owner,
  96. MaxProfileNameLength);
  97. if (!string.IsNullOrWhiteSpace(_tempUserName))
  98. {
  99. await SelectProfileImage(true);
  100. }
  101. _tempUserName = String.Empty;
  102. }
  103. public async void DeleteUser()
  104. {
  105. if (_selectedProfile != null)
  106. {
  107. var lastUserId = _owner.AccountManager.LastOpenedUser.UserId;
  108. if (_selectedProfile.UserId == lastUserId)
  109. {
  110. // If we are deleting the currently open profile, then we must open something else before deleting.
  111. var profile = Profiles.FirstOrDefault(x => x.UserId != lastUserId);
  112. if (profile == null)
  113. {
  114. ContentDialogHelper.CreateErrorDialog(_owner,
  115. LocaleManager.Instance["DialogUserProfileDeletionWarningMessage"]);
  116. return;
  117. }
  118. _owner.AccountManager.OpenUser(profile.UserId);
  119. }
  120. var result =
  121. await ContentDialogHelper.CreateConfirmationDialog(_owner,
  122. LocaleManager.Instance["DialogUserProfileDeletionConfirmMessage"], "",
  123. LocaleManager.Instance["InputDialogYes"], LocaleManager.Instance["InputDialogNo"], "");
  124. if (result == UserResult.Yes)
  125. {
  126. _owner.AccountManager.DeleteUser(_selectedProfile.UserId);
  127. }
  128. }
  129. LoadProfiles();
  130. }
  131. }
  132. }