UserProfileViewModel.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 => _highlightedProfile != null;
  38. public bool IsHighlightedProfileDeletable => _highlightedProfile != null && _highlightedProfile.UserId != AccountManager.DefaultUserId;
  39. public UserProfile HighlightedProfile
  40. {
  41. get => _highlightedProfile;
  42. set
  43. {
  44. _highlightedProfile = value;
  45. OnPropertyChanged(nameof(HighlightedProfile));
  46. OnPropertyChanged(nameof(IsHighlightedProfileDeletable));
  47. OnPropertyChanged(nameof(IsHighlightedProfileEditable));
  48. }
  49. }
  50. public void Dispose() { }
  51. public void LoadProfiles()
  52. {
  53. Profiles.Clear();
  54. var profiles = _owner.AccountManager.GetAllUsers().OrderByDescending(x => x.AccountState == AccountState.Open);
  55. foreach (var profile in profiles)
  56. {
  57. Profiles.Add(new UserProfile(profile));
  58. }
  59. SelectedProfile = Profiles.FirstOrDefault(x => x.UserId == _owner.AccountManager.LastOpenedUser.UserId);
  60. if (SelectedProfile == null)
  61. {
  62. SelectedProfile = Profiles.First();
  63. if (SelectedProfile != null)
  64. {
  65. _owner.AccountManager.OpenUser(_selectedProfile.UserId);
  66. }
  67. }
  68. }
  69. public void AddUser()
  70. {
  71. UserProfile userProfile = null;
  72. _owner.Navigate(typeof(UserEditor), (this._owner, userProfile, true));
  73. }
  74. public void EditUser()
  75. {
  76. _owner.Navigate(typeof(UserEditor), (this._owner, _highlightedProfile ?? SelectedProfile, false));
  77. }
  78. public async void DeleteUser()
  79. {
  80. if (_highlightedProfile != null)
  81. {
  82. var lastUserId = _owner.AccountManager.LastOpenedUser.UserId;
  83. if (_highlightedProfile.UserId == lastUserId)
  84. {
  85. // If we are deleting the currently open profile, then we must open something else before deleting.
  86. var profile = Profiles.FirstOrDefault(x => x.UserId != lastUserId);
  87. if (profile == null)
  88. {
  89. Dispatcher.UIThread.Post(async () =>
  90. {
  91. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance["DialogUserProfileDeletionWarningMessage"]);
  92. });
  93. return;
  94. }
  95. _owner.AccountManager.OpenUser(profile.UserId);
  96. }
  97. var result =
  98. await ContentDialogHelper.CreateConfirmationDialog(LocaleManager.Instance["DialogUserProfileDeletionConfirmMessage"], "",
  99. LocaleManager.Instance["InputDialogYes"], LocaleManager.Instance["InputDialogNo"], "");
  100. if (result == UserResult.Yes)
  101. {
  102. _owner.AccountManager.DeleteUser(_highlightedProfile.UserId);
  103. }
  104. }
  105. LoadProfiles();
  106. }
  107. }
  108. }