2022-12-02 14:16:43 +01:00
|
|
|
using Avalonia;
|
2022-07-12 00:25:33 +02:00
|
|
|
using Avalonia.Threading;
|
2022-12-02 14:16:43 +01:00
|
|
|
using FluentAvalonia.UI.Controls;
|
|
|
|
using LibHac.Common;
|
|
|
|
using LibHac.Fs;
|
|
|
|
using LibHac.Fs.Shim;
|
2022-07-08 20:47:11 +02:00
|
|
|
using Ryujinx.Ava.Common.Locale;
|
2022-12-29 15:24:05 +01:00
|
|
|
using Ryujinx.Ava.UI.Controls;
|
|
|
|
using Ryujinx.Ava.UI.Helpers;
|
2022-07-08 20:47:11 +02:00
|
|
|
using Ryujinx.HLE.HOS.Services.Account.Acc;
|
|
|
|
using System;
|
2022-12-02 14:16:43 +01:00
|
|
|
using System.Collections.Generic;
|
2022-07-08 20:47:11 +02:00
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using System.Linq;
|
2022-12-29 15:24:05 +01:00
|
|
|
using UserId = Ryujinx.HLE.HOS.Services.Account.Acc.UserId;
|
|
|
|
using UserProfile = Ryujinx.Ava.UI.Models.UserProfile;
|
2022-07-08 20:47:11 +02:00
|
|
|
|
2022-12-29 15:24:05 +01:00
|
|
|
namespace Ryujinx.Ava.UI.ViewModels
|
2022-07-08 20:47:11 +02:00
|
|
|
{
|
|
|
|
public class UserProfileViewModel : BaseModel, IDisposable
|
|
|
|
{
|
2022-07-24 19:38:38 +02:00
|
|
|
private readonly NavigationDialogHost _owner;
|
2022-07-08 20:47:11 +02:00
|
|
|
|
|
|
|
private UserProfile _selectedProfile;
|
2022-07-24 19:38:38 +02:00
|
|
|
private UserProfile _highlightedProfile;
|
2022-07-08 20:47:11 +02:00
|
|
|
|
|
|
|
public UserProfileViewModel()
|
|
|
|
{
|
|
|
|
Profiles = new ObservableCollection<UserProfile>();
|
2022-12-02 14:16:43 +01:00
|
|
|
LostProfiles = new ObservableCollection<UserProfile>();
|
2022-07-08 20:47:11 +02:00
|
|
|
}
|
|
|
|
|
2022-07-24 19:38:38 +02:00
|
|
|
public UserProfileViewModel(NavigationDialogHost owner) : this()
|
2022-07-08 20:47:11 +02:00
|
|
|
{
|
|
|
|
_owner = owner;
|
|
|
|
|
|
|
|
LoadProfiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ObservableCollection<UserProfile> Profiles { get; set; }
|
|
|
|
|
2022-12-02 14:16:43 +01:00
|
|
|
public ObservableCollection<UserProfile> LostProfiles { get; set; }
|
|
|
|
|
2022-07-08 20:47:11 +02:00
|
|
|
public UserProfile SelectedProfile
|
|
|
|
{
|
|
|
|
get => _selectedProfile;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_selectedProfile = value;
|
|
|
|
|
2022-12-29 15:24:05 +01:00
|
|
|
OnPropertyChanged();
|
2022-07-24 19:38:38 +02:00
|
|
|
OnPropertyChanged(nameof(IsHighlightedProfileDeletable));
|
|
|
|
OnPropertyChanged(nameof(IsHighlightedProfileEditable));
|
2022-07-08 20:47:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 19:16:23 +02:00
|
|
|
public bool IsHighlightedProfileEditable => _highlightedProfile != null;
|
2022-07-24 19:38:38 +02:00
|
|
|
|
2022-07-28 19:16:23 +02:00
|
|
|
public bool IsHighlightedProfileDeletable => _highlightedProfile != null && _highlightedProfile.UserId != AccountManager.DefaultUserId;
|
2022-07-24 19:38:38 +02:00
|
|
|
|
|
|
|
public UserProfile HighlightedProfile
|
|
|
|
{
|
|
|
|
get => _highlightedProfile;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_highlightedProfile = value;
|
|
|
|
|
2022-12-29 15:24:05 +01:00
|
|
|
OnPropertyChanged();
|
2022-07-24 19:38:38 +02:00
|
|
|
OnPropertyChanged(nameof(IsHighlightedProfileDeletable));
|
|
|
|
OnPropertyChanged(nameof(IsHighlightedProfileEditable));
|
|
|
|
}
|
|
|
|
}
|
2022-07-08 20:47:11 +02:00
|
|
|
|
2022-07-28 19:16:23 +02:00
|
|
|
public void Dispose() { }
|
2022-07-08 20:47:11 +02:00
|
|
|
|
|
|
|
public void LoadProfiles()
|
|
|
|
{
|
|
|
|
Profiles.Clear();
|
2022-12-02 14:16:43 +01:00
|
|
|
LostProfiles.Clear();
|
2022-07-08 20:47:11 +02:00
|
|
|
|
2022-07-28 19:16:23 +02:00
|
|
|
var profiles = _owner.AccountManager.GetAllUsers().OrderByDescending(x => x.AccountState == AccountState.Open);
|
2022-07-08 20:47:11 +02:00
|
|
|
|
|
|
|
foreach (var profile in profiles)
|
|
|
|
{
|
2022-12-02 14:16:43 +01:00
|
|
|
Profiles.Add(new UserProfile(profile, _owner));
|
2022-07-08 20:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SelectedProfile = Profiles.FirstOrDefault(x => x.UserId == _owner.AccountManager.LastOpenedUser.UserId);
|
|
|
|
|
|
|
|
if (SelectedProfile == null)
|
|
|
|
{
|
|
|
|
SelectedProfile = Profiles.First();
|
|
|
|
|
|
|
|
if (SelectedProfile != null)
|
|
|
|
{
|
|
|
|
_owner.AccountManager.OpenUser(_selectedProfile.UserId);
|
|
|
|
}
|
|
|
|
}
|
2022-12-02 14:16:43 +01:00
|
|
|
|
|
|
|
var saveDataFilter = SaveDataFilter.Make(programId: default, saveType: SaveDataType.Account,
|
|
|
|
default, saveDataId: default, index: default);
|
|
|
|
|
|
|
|
using var saveDataIterator = new UniqueRef<SaveDataIterator>();
|
|
|
|
|
|
|
|
_owner.HorizonClient.Fs.OpenSaveDataIterator(ref saveDataIterator.Ref(), SaveDataSpaceId.User, in saveDataFilter).ThrowIfFailure();
|
|
|
|
|
|
|
|
Span<SaveDataInfo> saveDataInfo = stackalloc SaveDataInfo[10];
|
|
|
|
|
2022-12-29 15:24:05 +01:00
|
|
|
HashSet<UserId> lostAccounts = new HashSet<UserId>();
|
2022-12-02 14:16:43 +01:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
saveDataIterator.Get.ReadSaveDataInfo(out long readCount, saveDataInfo).ThrowIfFailure();
|
|
|
|
|
|
|
|
if (readCount == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < readCount; i++)
|
|
|
|
{
|
|
|
|
var save = saveDataInfo[i];
|
2022-12-29 15:24:05 +01:00
|
|
|
var id = new UserId((long)save.UserId.Id.Low, (long)save.UserId.Id.High);
|
2022-12-02 14:16:43 +01:00
|
|
|
if (Profiles.FirstOrDefault( x=> x.UserId == id) == null)
|
|
|
|
{
|
|
|
|
lostAccounts.Add(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(var account in lostAccounts)
|
|
|
|
{
|
|
|
|
LostProfiles.Add(new UserProfile(new HLE.HOS.Services.Account.Acc.UserProfile(account, "", null), _owner));
|
|
|
|
}
|
2022-07-08 20:47:11 +02:00
|
|
|
}
|
|
|
|
|
2022-07-24 19:38:38 +02:00
|
|
|
public void AddUser()
|
2022-07-08 20:47:11 +02:00
|
|
|
{
|
2022-07-24 19:38:38 +02:00
|
|
|
UserProfile userProfile = null;
|
2022-07-28 19:16:23 +02:00
|
|
|
|
2022-07-24 19:38:38 +02:00
|
|
|
_owner.Navigate(typeof(UserEditor), (this._owner, userProfile, true));
|
2022-07-08 20:47:11 +02:00
|
|
|
}
|
|
|
|
|
2022-12-02 14:16:43 +01:00
|
|
|
public async void ManageSaves()
|
|
|
|
{
|
|
|
|
UserProfile userProfile = _highlightedProfile ?? SelectedProfile;
|
|
|
|
|
|
|
|
SaveManager manager = new SaveManager(userProfile, _owner.HorizonClient, _owner.VirtualFileSystem);
|
|
|
|
|
|
|
|
ContentDialog contentDialog = new ContentDialog
|
|
|
|
{
|
2023-01-03 19:45:08 +01:00
|
|
|
Title = string.Format(LocaleManager.Instance[LocaleKeys.SaveManagerHeading], userProfile.Name),
|
2022-12-02 14:16:43 +01:00
|
|
|
PrimaryButtonText = "",
|
|
|
|
SecondaryButtonText = "",
|
2023-01-03 19:45:08 +01:00
|
|
|
CloseButtonText = LocaleManager.Instance[LocaleKeys.UserProfilesClose],
|
2022-12-02 14:16:43 +01:00
|
|
|
Content = manager,
|
|
|
|
Padding = new Thickness(0)
|
|
|
|
};
|
|
|
|
|
|
|
|
await contentDialog.ShowAsync();
|
|
|
|
}
|
|
|
|
|
2022-07-24 19:38:38 +02:00
|
|
|
public void EditUser()
|
2022-07-08 20:47:11 +02:00
|
|
|
{
|
2022-07-24 19:38:38 +02:00
|
|
|
_owner.Navigate(typeof(UserEditor), (this._owner, _highlightedProfile ?? SelectedProfile, false));
|
2022-07-08 20:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async void DeleteUser()
|
|
|
|
{
|
2022-07-24 19:38:38 +02:00
|
|
|
if (_highlightedProfile != null)
|
2022-07-08 20:47:11 +02:00
|
|
|
{
|
|
|
|
var lastUserId = _owner.AccountManager.LastOpenedUser.UserId;
|
|
|
|
|
2022-07-24 19:38:38 +02:00
|
|
|
if (_highlightedProfile.UserId == lastUserId)
|
2022-07-08 20:47:11 +02:00
|
|
|
{
|
|
|
|
// If we are deleting the currently open profile, then we must open something else before deleting.
|
|
|
|
var profile = Profiles.FirstOrDefault(x => x.UserId != lastUserId);
|
|
|
|
|
|
|
|
if (profile == null)
|
|
|
|
{
|
2022-07-12 00:25:33 +02:00
|
|
|
Dispatcher.UIThread.Post(async () =>
|
|
|
|
{
|
2023-01-03 19:45:08 +01:00
|
|
|
await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogUserProfileDeletionWarningMessage]);
|
2022-07-12 00:25:33 +02:00
|
|
|
});
|
|
|
|
|
2022-07-08 20:47:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_owner.AccountManager.OpenUser(profile.UserId);
|
|
|
|
}
|
|
|
|
|
|
|
|
var result =
|
2023-01-03 19:45:08 +01:00
|
|
|
await ContentDialogHelper.CreateConfirmationDialog(LocaleManager.Instance[LocaleKeys.DialogUserProfileDeletionConfirmMessage], "",
|
|
|
|
LocaleManager.Instance[LocaleKeys.InputDialogYes], LocaleManager.Instance[LocaleKeys.InputDialogNo], "");
|
2022-07-08 20:47:11 +02:00
|
|
|
|
|
|
|
if (result == UserResult.Yes)
|
|
|
|
{
|
2022-07-24 19:38:38 +02:00
|
|
|
_owner.AccountManager.DeleteUser(_highlightedProfile.UserId);
|
2022-07-08 20:47:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadProfiles();
|
|
|
|
}
|
2022-12-02 14:16:43 +01:00
|
|
|
|
|
|
|
public void GoBack()
|
|
|
|
{
|
|
|
|
_owner.GoBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RecoverLostAccounts()
|
|
|
|
{
|
|
|
|
_owner.Navigate(typeof(UserRecoverer), (this._owner, this));
|
|
|
|
}
|
2022-07-08 20:47:11 +02:00
|
|
|
}
|
|
|
|
}
|