Persist view model between theme swaps

This commit is contained in:
Matt Nadareski
2021-07-05 23:09:22 -07:00
parent 0ae58ab21c
commit 7f4992d2d0
2 changed files with 22 additions and 6 deletions

View File

@@ -5,6 +5,7 @@ using System.Xml;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using RedBookPlayer.Common;
namespace RedBookPlayer.GUI namespace RedBookPlayer.GUI
{ {
@@ -30,10 +31,13 @@ namespace RedBookPlayer.GUI
if(string.IsNullOrWhiteSpace(theme)) if(string.IsNullOrWhiteSpace(theme))
return; return;
// If we already have a view, cache the view model
PlayerViewModel pvm = ((PlayerView)Instance.ContentControl.Content).PlayerViewModel;
// If the theme name is "default", we assume the internal theme is used // If the theme name is "default", we assume the internal theme is used
if(theme.Equals("default", StringComparison.CurrentCultureIgnoreCase)) if(theme.Equals("default", StringComparison.CurrentCultureIgnoreCase))
{ {
Instance.ContentControl.Content = new PlayerView(); Instance.ContentControl.Content = new PlayerView(pvm);
} }
else else
{ {
@@ -50,12 +54,12 @@ namespace RedBookPlayer.GUI
{ {
string xaml = File.ReadAllText(xamlPath); string xaml = File.ReadAllText(xamlPath);
xaml = xaml.Replace("Source=\"", $"Source=\"file://{themeDirectory}/"); xaml = xaml.Replace("Source=\"", $"Source=\"file://{themeDirectory}/");
Instance.ContentControl.Content = new PlayerView(xaml); Instance.ContentControl.Content = new PlayerView(xaml, pvm);
} }
catch(XmlException ex) catch(XmlException ex)
{ {
Console.WriteLine($"Error: invalid theme XAML ({ex.Message}), reverting to default"); Console.WriteLine($"Error: invalid theme XAML ({ex.Message}), reverting to default");
Instance.ContentControl.Content = new PlayerView(); Instance.ContentControl.Content = new PlayerView(pvm);
} }
} }

View File

@@ -30,15 +30,27 @@ namespace RedBookPlayer.GUI
/// <summary> /// <summary>
/// Initialize the UI based on the default theme /// Initialize the UI based on the default theme
/// </summary> /// </summary>
public PlayerView() : this(null) { } public PlayerView() : this(null, null) { }
/// <summary>
/// Initialize the UI based on the default theme with an existing view model
/// </summary>
/// <param name="xaml">XAML data representing the theme, null for default</param>
/// <param name="playerViewModel">Existing PlayerViewModel to load in instead of creating a new one</param>
public PlayerView(PlayerViewModel playerViewModel) : this(null, playerViewModel) { }
/// <summary> /// <summary>
/// Initialize the UI based on the currently selected theme /// Initialize the UI based on the currently selected theme
/// </summary> /// </summary>
/// <param name="xaml">XAML data representing the theme, null for default</param> /// <param name="xaml">XAML data representing the theme, null for default</param>
public PlayerView(string xaml) /// <param name="playerViewModel">Existing PlayerViewModel to load in instead of creating a new one</param>
public PlayerView(string xaml, PlayerViewModel playerViewModel)
{ {
if(playerViewModel != null)
DataContext = playerViewModel;
else
DataContext = new PlayerViewModel(); DataContext = new PlayerViewModel();
PlayerViewModel.PropertyChanged += PlayerViewModelStateChanged; PlayerViewModel.PropertyChanged += PlayerViewModelStateChanged;
LoadTheme(xaml); LoadTheme(xaml);