TitleUpdateViewModel.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using Avalonia.Collections;
  2. using Avalonia.Platform.Storage;
  3. using Avalonia.Threading;
  4. using CommunityToolkit.Mvvm.ComponentModel;
  5. using FluentAvalonia.UI.Controls;
  6. using Ryujinx.Ava.Common.Locale;
  7. using Ryujinx.Ava.Common.Models;
  8. using Ryujinx.Ava.UI.Helpers;
  9. using Ryujinx.Ava.Utilities.AppLibrary;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading.Tasks;
  14. namespace Ryujinx.Ava.UI.ViewModels
  15. {
  16. public record TitleUpdateViewModelNoUpdate;
  17. public partial class TitleUpdateViewModel : BaseModel
  18. {
  19. private ApplicationLibrary ApplicationLibrary { get; }
  20. private ApplicationData ApplicationData { get; }
  21. [ObservableProperty] private AvaloniaList<TitleUpdateModel> _titleUpdates = [];
  22. [ObservableProperty] private AvaloniaList<object> _views = [];
  23. [ObservableProperty] private object _selectedUpdate = new TitleUpdateViewModelNoUpdate();
  24. [ObservableProperty] private bool _showBundledContentNotice;
  25. private readonly IStorageProvider _storageProvider;
  26. public TitleUpdateViewModel(ApplicationLibrary applicationLibrary, ApplicationData applicationData)
  27. {
  28. ApplicationLibrary = applicationLibrary;
  29. ApplicationData = applicationData;
  30. _storageProvider = RyujinxApp.MainWindow.StorageProvider;
  31. LoadUpdates();
  32. }
  33. private void LoadUpdates()
  34. {
  35. IEnumerable<(TitleUpdateModel TitleUpdate, bool IsSelected)> updates = ApplicationLibrary.TitleUpdates.Items
  36. .Where(it => it.TitleUpdate.TitleIdBase == ApplicationData.IdBase);
  37. bool hasBundledContent = false;
  38. SelectedUpdate = new TitleUpdateViewModelNoUpdate();
  39. foreach ((TitleUpdateModel update, bool isSelected) in updates)
  40. {
  41. TitleUpdates.Add(update);
  42. hasBundledContent = hasBundledContent || update.IsBundled;
  43. if (isSelected)
  44. {
  45. SelectedUpdate = update;
  46. }
  47. }
  48. ShowBundledContentNotice = hasBundledContent;
  49. SortUpdates();
  50. }
  51. public void SortUpdates()
  52. {
  53. IOrderedEnumerable<TitleUpdateModel> sortedUpdates = TitleUpdates.OrderByDescending(update => update.Version);
  54. // NOTE(jpr): this works around a bug where calling Views.Clear also clears SelectedUpdate for
  55. // some reason. so we save the item here and restore it after
  56. object selected = SelectedUpdate;
  57. Views.Clear();
  58. Views.Add(new TitleUpdateViewModelNoUpdate());
  59. Views.AddRange(sortedUpdates);
  60. SelectedUpdate = selected;
  61. if (SelectedUpdate is TitleUpdateViewModelNoUpdate)
  62. {
  63. SelectedUpdate = Views[0];
  64. }
  65. // this is mainly to handle a scenario where the user removes the selected update
  66. else if (!TitleUpdates.Contains((TitleUpdateModel)SelectedUpdate))
  67. {
  68. SelectedUpdate = Views.Count > 1 ? Views[1] : Views[0];
  69. }
  70. }
  71. private bool AddUpdate(string path, out int numUpdatesAdded)
  72. {
  73. numUpdatesAdded = 0;
  74. if (!File.Exists(path))
  75. {
  76. return false;
  77. }
  78. if (!ApplicationLibrary.TryGetTitleUpdatesFromFile(path, out List<TitleUpdateModel> updates))
  79. {
  80. return false;
  81. }
  82. List<TitleUpdateModel> updatesForThisGame = updates.Where(it => it.TitleIdBase == ApplicationData.Id).ToList();
  83. if (updatesForThisGame.Count == 0)
  84. {
  85. return false;
  86. }
  87. foreach (TitleUpdateModel update in updatesForThisGame)
  88. {
  89. if (!TitleUpdates.Contains(update))
  90. {
  91. TitleUpdates.Add(update);
  92. SelectedUpdate = update;
  93. numUpdatesAdded++;
  94. }
  95. }
  96. if (numUpdatesAdded > 0)
  97. {
  98. SortUpdates();
  99. }
  100. return true;
  101. }
  102. public void RemoveUpdate(TitleUpdateModel update)
  103. {
  104. if (!update.IsBundled)
  105. {
  106. TitleUpdates.Remove(update);
  107. }
  108. else if (update == SelectedUpdate as TitleUpdateModel)
  109. {
  110. SelectedUpdate = new TitleUpdateViewModelNoUpdate();
  111. }
  112. SortUpdates();
  113. }
  114. public async Task Add()
  115. {
  116. IReadOnlyList<IStorageFile> result = await _storageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
  117. {
  118. AllowMultiple = true,
  119. FileTypeFilter = new List<FilePickerFileType>
  120. {
  121. new(LocaleManager.Instance[LocaleKeys.AllSupportedFormats])
  122. {
  123. Patterns = ["*.nsp"],
  124. AppleUniformTypeIdentifiers = ["com.ryujinx.nsp"],
  125. MimeTypes = ["application/x-nx-nsp"],
  126. },
  127. },
  128. });
  129. int totalUpdatesAdded = 0;
  130. foreach (IStorageFile file in result)
  131. {
  132. if (!AddUpdate(file.Path.LocalPath, out int newUpdatesAdded))
  133. {
  134. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogUpdateAddUpdateErrorMessage]);
  135. }
  136. totalUpdatesAdded += newUpdatesAdded;
  137. }
  138. if (totalUpdatesAdded > 0)
  139. {
  140. await ShowNewUpdatesAddedDialog(totalUpdatesAdded);
  141. }
  142. }
  143. public void Save()
  144. {
  145. List<(TitleUpdateModel it, bool)> updates = TitleUpdates.Select(it => (it, it == SelectedUpdate as TitleUpdateModel)).ToList();
  146. ApplicationLibrary.SaveTitleUpdatesForGame(ApplicationData, updates);
  147. }
  148. private Task ShowNewUpdatesAddedDialog(int numAdded)
  149. {
  150. string msg = string.Format(LocaleManager.Instance[LocaleKeys.UpdateWindowUpdateAddedMessage], numAdded);
  151. return Dispatcher.UIThread.InvokeAsync(async () =>
  152. await ContentDialogHelper.ShowTextDialog(
  153. LocaleManager.Instance[LocaleKeys.DialogConfirmationTitle],
  154. msg,
  155. string.Empty,
  156. string.Empty,
  157. string.Empty,
  158. LocaleManager.Instance[LocaleKeys.InputDialogOk],
  159. (int)Symbol.Checkmark
  160. ));
  161. }
  162. }
  163. }