UserProfileViewModel.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Avalonia.Threading;
  2. using Ryujinx.Ava.Common.Locale;
  3. using Ryujinx.Ava.Ui.Controls;
  4. using Ryujinx.HLE.HOS.Services.Account.Acc;
  5. using System;
  6. using System.Collections.ObjectModel;
  7. using System.Linq;
  8. using UserProfile = Ryujinx.Ava.Ui.Models.UserProfile;
  9. namespace Ryujinx.Ava.Ui.ViewModels
  10. {
  11. public class UserProfileViewModel : BaseModel, IDisposable
  12. {
  13. private readonly NavigationDialogHost _owner;
  14. private UserProfile _selectedProfile;
  15. private UserProfile _highlightedProfile;
  16. public UserProfileViewModel()
  17. {
  18. Profiles = new ObservableCollection<UserProfile>();
  19. }
  20. public UserProfileViewModel(NavigationDialogHost owner) : this()
  21. {
  22. _owner = owner;
  23. LoadProfiles();
  24. }
  25. public ObservableCollection<UserProfile> Profiles { get; set; }
  26. public UserProfile SelectedProfile
  27. {
  28. get => _selectedProfile;
  29. set
  30. {
  31. _selectedProfile = value;
  32. OnPropertyChanged(nameof(SelectedProfile));
  33. OnPropertyChanged(nameof(IsHighlightedProfileDeletable));
  34. OnPropertyChanged(nameof(IsHighlightedProfileEditable));
  35. }
  36. }
  37. public bool IsHighlightedProfileEditable =>
  38. _highlightedProfile != null;
  39. public bool IsHighlightedProfileDeletable =>
  40. _highlightedProfile != null && _highlightedProfile.UserId != AccountManager.DefaultUserId;
  41. public UserProfile HighlightedProfile
  42. {
  43. get => _highlightedProfile;
  44. set
  45. {
  46. _highlightedProfile = value;
  47. OnPropertyChanged(nameof(HighlightedProfile));
  48. OnPropertyChanged(nameof(IsHighlightedProfileDeletable));
  49. OnPropertyChanged(nameof(IsHighlightedProfileEditable));
  50. }
  51. }
  52. public void Dispose()
  53. {
  54. }
  55. public void LoadProfiles()
  56. {
  57. Profiles.Clear();
  58. var profiles = _owner.AccountManager.GetAllUsers()
  59. .OrderByDescending(x => x.AccountState == AccountState.Open);
  60. foreach (var profile in profiles)
  61. {
  62. Profiles.Add(new UserProfile(profile));
  63. }
  64. SelectedProfile = Profiles.FirstOrDefault(x => x.UserId == _owner.AccountManager.LastOpenedUser.UserId);
  65. if (SelectedProfile == null)
  66. {
  67. SelectedProfile = Profiles.First();
  68. if (SelectedProfile != null)
  69. {
  70. _owner.AccountManager.OpenUser(_selectedProfile.UserId);
  71. }
  72. }
  73. }
  74. public void AddUser()
  75. {
  76. UserProfile userProfile = null;
  77. _owner.Navigate(typeof(UserEditor), (this._owner, userProfile, true));
  78. }
  79. public void EditUser()
  80. {
  81. _owner.Navigate(typeof(UserEditor), (this._owner, _highlightedProfile ?? SelectedProfile, false));
  82. }
  83. public async void DeleteUser()
  84. {
  85. if (_highlightedProfile != null)
  86. {
  87. var lastUserId = _owner.AccountManager.LastOpenedUser.UserId;
  88. if (_highlightedProfile.UserId == lastUserId)
  89. {
  90. // If we are deleting the currently open profile, then we must open something else before deleting.
  91. var profile = Profiles.FirstOrDefault(x => x.UserId != lastUserId);
  92. if (profile == null)
  93. {
  94. Dispatcher.UIThread.Post(async () =>
  95. {
  96. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance["DialogUserProfileDeletionWarningMessage"]);
  97. });
  98. return;
  99. }
  100. _owner.AccountManager.OpenUser(profile.UserId);
  101. }
  102. var result =
  103. await ContentDialogHelper.CreateConfirmationDialog(LocaleManager.Instance["DialogUserProfileDeletionConfirmMessage"], "",
  104. LocaleManager.Instance["InputDialogYes"], LocaleManager.Instance["InputDialogNo"], "");
  105. if (result == UserResult.Yes)
  106. {
  107. _owner.AccountManager.DeleteUser(_highlightedProfile.UserId);
  108. }
  109. }
  110. LoadProfiles();
  111. }
  112. }
  113. }