UserProfileViewModel.cs 5.4 KB

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