UserProfileViewModel.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using Avalonia;
  2. using Avalonia.Threading;
  3. using FluentAvalonia.UI.Controls;
  4. using LibHac.Common;
  5. using LibHac.Fs;
  6. using LibHac.Fs.Shim;
  7. using Ryujinx.Ava.Common.Locale;
  8. using Ryujinx.Ava.UI.Controls;
  9. using Ryujinx.Ava.UI.Helpers;
  10. using Ryujinx.HLE.HOS.Services.Account.Acc;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.Linq;
  15. using UserId = Ryujinx.HLE.HOS.Services.Account.Acc.UserId;
  16. using UserProfile = Ryujinx.Ava.UI.Models.UserProfile;
  17. namespace Ryujinx.Ava.UI.ViewModels
  18. {
  19. public class UserProfileViewModel : BaseModel, IDisposable
  20. {
  21. private readonly NavigationDialogHost _owner;
  22. private UserProfile _selectedProfile;
  23. private UserProfile _highlightedProfile;
  24. public UserProfileViewModel()
  25. {
  26. Profiles = new ObservableCollection<UserProfile>();
  27. LostProfiles = new ObservableCollection<UserProfile>();
  28. }
  29. public UserProfileViewModel(NavigationDialogHost owner) : this()
  30. {
  31. _owner = owner;
  32. LoadProfiles();
  33. }
  34. public ObservableCollection<UserProfile> Profiles { get; set; }
  35. public ObservableCollection<UserProfile> LostProfiles { get; set; }
  36. public UserProfile SelectedProfile
  37. {
  38. get => _selectedProfile;
  39. set
  40. {
  41. _selectedProfile = value;
  42. OnPropertyChanged();
  43. OnPropertyChanged(nameof(IsHighlightedProfileDeletable));
  44. OnPropertyChanged(nameof(IsHighlightedProfileEditable));
  45. }
  46. }
  47. public bool IsHighlightedProfileEditable => _highlightedProfile != null;
  48. public bool IsHighlightedProfileDeletable => _highlightedProfile != null && _highlightedProfile.UserId != AccountManager.DefaultUserId;
  49. public UserProfile HighlightedProfile
  50. {
  51. get => _highlightedProfile;
  52. set
  53. {
  54. _highlightedProfile = value;
  55. OnPropertyChanged();
  56. OnPropertyChanged(nameof(IsHighlightedProfileDeletable));
  57. OnPropertyChanged(nameof(IsHighlightedProfileEditable));
  58. }
  59. }
  60. public void Dispose() { }
  61. public void LoadProfiles()
  62. {
  63. Profiles.Clear();
  64. LostProfiles.Clear();
  65. var profiles = _owner.AccountManager.GetAllUsers().OrderByDescending(x => x.AccountState == AccountState.Open);
  66. foreach (var profile in profiles)
  67. {
  68. Profiles.Add(new UserProfile(profile, _owner));
  69. }
  70. SelectedProfile = Profiles.FirstOrDefault(x => x.UserId == _owner.AccountManager.LastOpenedUser.UserId);
  71. if (SelectedProfile == null)
  72. {
  73. SelectedProfile = Profiles.First();
  74. if (SelectedProfile != null)
  75. {
  76. _owner.AccountManager.OpenUser(_selectedProfile.UserId);
  77. }
  78. }
  79. var saveDataFilter = SaveDataFilter.Make(programId: default, saveType: SaveDataType.Account,
  80. default, saveDataId: default, index: default);
  81. using var saveDataIterator = new UniqueRef<SaveDataIterator>();
  82. _owner.HorizonClient.Fs.OpenSaveDataIterator(ref saveDataIterator.Ref(), SaveDataSpaceId.User, in saveDataFilter).ThrowIfFailure();
  83. Span<SaveDataInfo> saveDataInfo = stackalloc SaveDataInfo[10];
  84. HashSet<UserId> lostAccounts = new HashSet<UserId>();
  85. while (true)
  86. {
  87. saveDataIterator.Get.ReadSaveDataInfo(out long readCount, saveDataInfo).ThrowIfFailure();
  88. if (readCount == 0)
  89. {
  90. break;
  91. }
  92. for (int i = 0; i < readCount; i++)
  93. {
  94. var save = saveDataInfo[i];
  95. var id = new UserId((long)save.UserId.Id.Low, (long)save.UserId.Id.High);
  96. if (Profiles.FirstOrDefault( x=> x.UserId == id) == null)
  97. {
  98. lostAccounts.Add(id);
  99. }
  100. }
  101. }
  102. foreach(var account in lostAccounts)
  103. {
  104. LostProfiles.Add(new UserProfile(new HLE.HOS.Services.Account.Acc.UserProfile(account, "", null), _owner));
  105. }
  106. }
  107. public void AddUser()
  108. {
  109. UserProfile userProfile = null;
  110. _owner.Navigate(typeof(UserEditor), (this._owner, userProfile, true));
  111. }
  112. public async void ManageSaves()
  113. {
  114. UserProfile userProfile = _highlightedProfile ?? SelectedProfile;
  115. SaveManager manager = new SaveManager(userProfile, _owner.HorizonClient, _owner.VirtualFileSystem);
  116. ContentDialog contentDialog = new ContentDialog
  117. {
  118. Title = string.Format(LocaleManager.Instance[LocaleKeys.SaveManagerHeading], userProfile.Name),
  119. PrimaryButtonText = "",
  120. SecondaryButtonText = "",
  121. CloseButtonText = LocaleManager.Instance[LocaleKeys.UserProfilesClose],
  122. Content = manager,
  123. Padding = new Thickness(0)
  124. };
  125. await contentDialog.ShowAsync();
  126. }
  127. public void EditUser()
  128. {
  129. _owner.Navigate(typeof(UserEditor), (this._owner, _highlightedProfile ?? SelectedProfile, false));
  130. }
  131. public async void DeleteUser()
  132. {
  133. if (_highlightedProfile != null)
  134. {
  135. var lastUserId = _owner.AccountManager.LastOpenedUser.UserId;
  136. if (_highlightedProfile.UserId == lastUserId)
  137. {
  138. // If we are deleting the currently open profile, then we must open something else before deleting.
  139. var profile = Profiles.FirstOrDefault(x => x.UserId != lastUserId);
  140. if (profile == null)
  141. {
  142. Dispatcher.UIThread.Post(async () =>
  143. {
  144. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogUserProfileDeletionWarningMessage]);
  145. });
  146. return;
  147. }
  148. _owner.AccountManager.OpenUser(profile.UserId);
  149. }
  150. var result =
  151. await ContentDialogHelper.CreateConfirmationDialog(LocaleManager.Instance[LocaleKeys.DialogUserProfileDeletionConfirmMessage], "",
  152. LocaleManager.Instance[LocaleKeys.InputDialogYes], LocaleManager.Instance[LocaleKeys.InputDialogNo], "");
  153. if (result == UserResult.Yes)
  154. {
  155. _owner.AccountManager.DeleteUser(_highlightedProfile.UserId);
  156. }
  157. }
  158. LoadProfiles();
  159. }
  160. public void GoBack()
  161. {
  162. _owner.GoBack();
  163. }
  164. public void RecoverLostAccounts()
  165. {
  166. _owner.Navigate(typeof(UserRecoverer), (this._owner, this));
  167. }
  168. }
  169. }