From b470c112f2eb112363c75f5f632128c73a4805c0 Mon Sep 17 00:00:00 2001 From: foxtrot47 Date: Sun, 12 Jan 2025 21:30:51 +0530 Subject: [PATCH] feat: Migrate Library Page to MVVM --- Crimson/App.xaml.cs | 1 + Crimson/ViewModels/LibraryViewModel.cs | 86 ++++++++++++++++++++++++++ Crimson/Views/LibraryPage.xaml | 10 ++- Crimson/Views/LibraryPage.xaml.cs | 69 +-------------------- 4 files changed, 98 insertions(+), 68 deletions(-) create mode 100644 Crimson/ViewModels/LibraryViewModel.cs diff --git a/Crimson/App.xaml.cs b/Crimson/App.xaml.cs index 152db23..4132035 100644 --- a/Crimson/App.xaml.cs +++ b/Crimson/App.xaml.cs @@ -88,6 +88,7 @@ public App() services.AddTransient(); services.AddTransient(); + services.AddTransient(); }). Build(); } diff --git a/Crimson/ViewModels/LibraryViewModel.cs b/Crimson/ViewModels/LibraryViewModel.cs new file mode 100644 index 0000000..36489e8 --- /dev/null +++ b/Crimson/ViewModels/LibraryViewModel.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using CommunityToolkit.Mvvm.ComponentModel; +using Crimson.Core; +using Crimson.Models; +using Crimson.Views; +using Serilog; + +namespace Crimson.ViewModels; + +public partial class LibraryViewModel : ObservableObject +{ + [ObservableProperty] + private static List s_gamesList; + + [ObservableProperty] + private bool _loadingFinished = false; + + [ObservableProperty] + private bool _showLoadingScreen = true; + + [ObservableProperty] + private bool _showAppGrid = false; + + [ObservableProperty] + private bool _showQueueItems = false; + + private readonly ILogger _log; + private readonly LibraryManager _libraryManager; + private readonly Windows.System.DispatcherQueue _dispatcherQueue; + + public LibraryViewModel() + { + _log = App.GetService(); + _libraryManager = App.GetService(); + _log.Information("LibraryPage: Loading Page"); + + + Task.Run(async () => + { + var games = await _libraryManager.GetLibraryData(); + UpdateLibrary(games); + }); + + _libraryManager.LibraryUpdated += UpdateLibrary; + _log.Information("LibraryPage: Loading finished"); + _dispatcherQueue = Windows.System.DispatcherQueue.GetForCurrentThread(); + } + + private void UpdateLibrary(IEnumerable games) + { + try + { + _log.Information("UpdateLibrary: Updating Library Page"); + if (games == null) return; + + _dispatcherQueue.TryEnqueue(() => + { + S_gamesList = new List(); + foreach (var game in games) + { + if (game.IsDlc()) continue; + var item = new LibraryItem + { + Name = game.AppName, + Title = game.AppTitle, + //InstallState = game.State, + Image = Util.GetBitmapImage(game.Metadata.KeyImages.FirstOrDefault(image => image.Type == "DieselGameBoxTall")?.Url) + }; + _log.Information($"UpdateLibrary: Adding {item.Name} to Library"); + S_gamesList.Add(item); + } + S_gamesList = S_gamesList.OrderBy(item => item.Title).ToList(); + ShowLoadingScreen = false; + ShowAppGrid = true; + }); + _log.Information("UpdateLibrary: Updated Library Page"); + } + catch (Exception ex) + { + _log.Error(ex.ToString()); + } + } +} diff --git a/Crimson/Views/LibraryPage.xaml b/Crimson/Views/LibraryPage.xaml index a885f2f..e2ef5b6 100644 --- a/Crimson/Views/LibraryPage.xaml +++ b/Crimson/Views/LibraryPage.xaml @@ -3,6 +3,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Crimson.Views" + xmlns:converters="using:CommunityToolkit.WinUI.Converters" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> @@ -28,9 +29,9 @@ - + Margin="0,20,0,20" + ItemsSource="{x:Bind ViewModel.S_gamesList, Mode=TwoWay}"/> + + + + \ No newline at end of file diff --git a/Crimson/Views/LibraryPage.xaml.cs b/Crimson/Views/LibraryPage.xaml.cs index 3591517..d53c5e7 100644 --- a/Crimson/Views/LibraryPage.xaml.cs +++ b/Crimson/Views/LibraryPage.xaml.cs @@ -1,15 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Crimson.Core; -using Crimson.Models; +using Crimson.ViewModels; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Media.Imaging; -using Serilog; - namespace Crimson.Views; /// @@ -17,68 +10,12 @@ namespace Crimson.Views; /// public sealed partial class LibraryPage : Page { - public static List GamesList { get; set; } - public bool LoadingFinished = false; - - // Get logger instance from MainWindow window class - private readonly ILogger _log; - private readonly LibraryManager _libraryManager; + public LibraryViewModel ViewModel => (LibraryViewModel)DataContext; public LibraryPage() { InitializeComponent(); - - _log = App.GetService(); - _libraryManager = App.GetService(); - _log.Information("LibraryPage: Loading Page"); - - LoadingSection.Visibility = Visibility.Visible; - GamesGrid.Visibility = Visibility.Collapsed; - - Task.Run(async () => - { - var games = await _libraryManager.GetLibraryData(); - UpdateLibrary(games); - }); - - DataContext = this; - _libraryManager.LibraryUpdated += UpdateLibrary; - _log.Information("LibraryPage: Loading finished"); - } - - private void UpdateLibrary(IEnumerable games) - { - try - { - _log.Information("UpdateLibrary: Updating Library Page"); - if (games == null) return; - DispatcherQueue.TryEnqueue(() => - { - GamesList = new List(); - foreach (var game in games) - { - if (game.IsDlc()) continue; - var item = new LibraryItem - { - Name = game.AppName, - Title = game.AppTitle, - //InstallState = game.State, - Image = Util.GetBitmapImage(game.Metadata.KeyImages.FirstOrDefault(image => image.Type == "DieselGameBoxTall")?.Url) - }; - _log.Information($"UpdateLibrary: Adding {item.Name} to Library"); - GamesList.Add(item); - } - GamesList = GamesList.OrderBy(item => item.Title).ToList(); - ItemsRepeater.ItemsSource = GamesList; - LoadingSection.Visibility = Visibility.Collapsed; - GamesGrid.Visibility = Visibility.Visible; - }); - _log.Information("UpdateLibrary: Updated Library Page"); - } - catch (Exception ex) - { - _log.Error(ex.ToString()); - } + DataContext = App.GetService(); } private void GameButton_Click(object sender, RoutedEventArgs e)