Skip to content

Commit

Permalink
feat: Migrate Library Page to MVVM
Browse files Browse the repository at this point in the history
  • Loading branch information
Foxtrot47 committed Jan 18, 2025
1 parent 272c0ae commit b470c11
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 68 deletions.
1 change: 1 addition & 0 deletions Crimson/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public App()

services.AddTransient<SettingsViewModel>();
services.AddTransient<DownloadsViewModel>();
services.AddTransient<LibraryViewModel>();
}).
Build();
}
Expand Down
86 changes: 86 additions & 0 deletions Crimson/ViewModels/LibraryViewModel.cs
Original file line number Diff line number Diff line change
@@ -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<LibraryItem> 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<ILogger>();
_libraryManager = App.GetService<LibraryManager>();
_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<Game> games)
{
try
{
_log.Information("UpdateLibrary: Updating Library Page");
if (games == null) return;

_dispatcherQueue.TryEnqueue(() =>
{
S_gamesList = new List<LibraryItem>();
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());
}
}
}
10 changes: 8 additions & 2 deletions Crimson/Views/LibraryPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -28,9 +29,9 @@
</DataTemplate>
</Grid.Resources>


<StackPanel
x:Name="LoadingSection"
Visibility="{x:Bind ViewModel.ShowLoadingScreen, Converter={StaticResource BoolToVisibilityConverter}, Mode=TwoWay}"
VerticalAlignment="Center">
<ProgressRing
IsActive="True"
Expand All @@ -46,6 +47,11 @@
x:Name="ItemsRepeater"
Layout="{StaticResource UniformGridLayout}"
ItemTemplate="{StaticResource GameCardTemplate}"
Margin="0,20,0,20" />
Margin="0,20,0,20"
ItemsSource="{x:Bind ViewModel.S_gamesList, Mode=TwoWay}"/>
</Grid>

<Page.Resources>
<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
</Page.Resources>
</Page>
69 changes: 3 additions & 66 deletions Crimson/Views/LibraryPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,84 +1,21 @@
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;

/// <summary>
/// Library Page which shows list of currently installed games
/// </summary>
public sealed partial class LibraryPage : Page
{
public static List<LibraryItem> 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<ILogger>();
_libraryManager = App.GetService<LibraryManager>();
_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<Game> games)
{
try
{
_log.Information("UpdateLibrary: Updating Library Page");
if (games == null) return;
DispatcherQueue.TryEnqueue(() =>
{
GamesList = new List<LibraryItem>();
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<LibraryViewModel>();
}

private void GameButton_Click(object sender, RoutedEventArgs e)
Expand Down

0 comments on commit b470c11

Please sign in to comment.