Common -> Models

This commit is contained in:
Matt Nadareski
2021-07-12 15:40:56 -07:00
parent 1e9b41f997
commit b11ccc48db
14 changed files with 150 additions and 152 deletions

View File

@@ -65,6 +65,8 @@ namespace RedBookPlayer.GUI
Instance.Width = ((PlayerView)Instance.ContentControl.Content).Width; Instance.Width = ((PlayerView)Instance.ContentControl.Content).Width;
Instance.Height = ((PlayerView)Instance.ContentControl.Content).Height; Instance.Height = ((PlayerView)Instance.ContentControl.Content).Height;
pvm.InitializeDigits();
} }
/// <summary> /// <summary>
@@ -82,6 +84,8 @@ namespace RedBookPlayer.GUI
ContentControl.Content = new PlayerView(); ContentControl.Content = new PlayerView();
((PlayerView)ContentControl.Content).PlayerViewModel.InitializeDigits();
CanResize = false; CanResize = false;
KeyDown += OnKeyDown; KeyDown += OnKeyDown;

View File

@@ -1,12 +1,5 @@
using System;
using System.ComponentModel;
using System.IO;
using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Threading;
using RedBookPlayer.GUI.ViewModels; using RedBookPlayer.GUI.ViewModels;
namespace RedBookPlayer.GUI namespace RedBookPlayer.GUI
@@ -18,11 +11,6 @@ namespace RedBookPlayer.GUI
/// </summary> /// </summary>
public PlayerViewModel PlayerViewModel => DataContext as PlayerViewModel; public PlayerViewModel PlayerViewModel => DataContext as PlayerViewModel;
/// <summary>
/// Set of images representing the digits for the UI
/// </summary>
private Image[] _digits;
/// <summary> /// <summary>
/// Initialize the UI based on the default theme /// Initialize the UI based on the default theme
/// </summary> /// </summary>
@@ -42,15 +30,12 @@ namespace RedBookPlayer.GUI
/// <param name="playerViewModel">Existing PlayerViewModel to load in instead of creating a new one</param> /// <param name="playerViewModel">Existing PlayerViewModel to load in instead of creating a new one</param>
public PlayerView(string xaml, PlayerViewModel playerViewModel) public PlayerView(string xaml, PlayerViewModel playerViewModel)
{ {
LoadTheme(xaml);
if(playerViewModel != null) if(playerViewModel != null)
DataContext = playerViewModel; DataContext = playerViewModel;
else else
DataContext = new PlayerViewModel(); DataContext = new PlayerViewModel();
PlayerViewModel.PropertyChanged += PlayerViewModelStateChanged;
LoadTheme(xaml);
InitializeDigits();
} }
#region Helpers #region Helpers
@@ -64,69 +49,6 @@ namespace RedBookPlayer.GUI
PlayerViewModel.SetLoadHiddenTracks(App.Settings.PlayHiddenTracks); PlayerViewModel.SetLoadHiddenTracks(App.Settings.PlayHiddenTracks);
} }
/// <summary>
/// Load the png image for a given character based on the theme
/// </summary>
/// <param name="character">Character to load the image for</param>
/// <returns>Bitmap representing the loaded image</returns>
private Bitmap GetBitmap(char character)
{
try
{
if(App.Settings.SelectedTheme == "default")
{
IAssetLoader assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
return new Bitmap(assets.Open(new Uri($"avares://RedBookPlayer/Assets/{character}.png")));
}
else
{
string themeDirectory = $"{Directory.GetCurrentDirectory()}/themes/{App.Settings.SelectedTheme}";
using FileStream stream = File.Open($"{themeDirectory}/{character}.png", FileMode.Open);
return new Bitmap(stream);
}
}
catch
{
return null;
}
}
/// <summary>
/// Initialize the displayed digits array
/// </summary>
private void InitializeDigits()
{
_digits = new Image[]
{
this.FindControl<Image>("TrackDigit1"),
this.FindControl<Image>("TrackDigit2"),
this.FindControl<Image>("IndexDigit1"),
this.FindControl<Image>("IndexDigit2"),
this.FindControl<Image>("TimeDigit1"),
this.FindControl<Image>("TimeDigit2"),
this.FindControl<Image>("TimeDigit3"),
this.FindControl<Image>("TimeDigit4"),
this.FindControl<Image>("TimeDigit5"),
this.FindControl<Image>("TimeDigit6"),
this.FindControl<Image>("TotalTracksDigit1"),
this.FindControl<Image>("TotalTracksDigit2"),
this.FindControl<Image>("TotalIndexesDigit1"),
this.FindControl<Image>("TotalIndexesDigit2"),
this.FindControl<Image>("TotalTimeDigit1"),
this.FindControl<Image>("TotalTimeDigit2"),
this.FindControl<Image>("TotalTimeDigit3"),
this.FindControl<Image>("TotalTimeDigit4"),
this.FindControl<Image>("TotalTimeDigit5"),
this.FindControl<Image>("TotalTimeDigit6"),
};
}
/// <summary> /// <summary>
/// Load the theme from a XAML, if possible /// Load the theme from a XAML, if possible
/// </summary> /// </summary>
@@ -146,23 +68,6 @@ namespace RedBookPlayer.GUI
} }
} }
/// <summary>
/// Update the UI from the view-model
/// </summary>
private void PlayerViewModelStateChanged(object sender, PropertyChangedEventArgs e)
{
Dispatcher.UIThread.InvokeAsync(() =>
{
string digitString = PlayerViewModel?.GenerateDigitString() ?? string.Empty.PadLeft(20, '-');
for(int i = 0; i < _digits.Length; i++)
{
Bitmap digitImage = GetBitmap(digitString[i]);
if(_digits[i] != null && digitImage != null)
_digits[i].Source = digitImage;
}
});
}
#endregion #endregion
} }
} }

View File

@@ -23,7 +23,7 @@
<PackageReference Include="Avalonia.ReactiveUI" Version="0.9.12" /> <PackageReference Include="Avalonia.ReactiveUI" Version="0.9.12" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\RedBookPlayer.Common\RedBookPlayer.Common.csproj" /> <ProjectReference Include="..\RedBookPlayer.Models\RedBookPlayer.Models.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<AvaloniaResource Include="Assets\*" /> <AvaloniaResource Include="Assets\*" />

View File

@@ -1,12 +1,17 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.IO;
using System.Linq; using System.Linq;
using System.Reactive; using System.Reactive;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Threading; using Avalonia.Threading;
using ReactiveUI; using ReactiveUI;
using RedBookPlayer.Common.Hardware; using RedBookPlayer.Models.Hardware;
namespace RedBookPlayer.GUI.ViewModels namespace RedBookPlayer.GUI.ViewModels
{ {
@@ -17,6 +22,11 @@ namespace RedBookPlayer.GUI.ViewModels
/// </summary> /// </summary>
private Player _player; private Player _player;
/// <summary>
/// Set of images representing the digits for the UI
/// </summary>
private Image[] _digits;
#region Player Passthrough #region Player Passthrough
#region OpticalDisc Passthrough #region OpticalDisc Passthrough
@@ -433,11 +443,88 @@ namespace RedBookPlayer.GUI.ViewModels
#region Helpers #region Helpers
/// <summary>
/// Load a disc image from a selection box
/// </summary>
public async void ExecuteLoad()
{
string path = await GetPath();
if(path == null)
return;
await LoadImage(path);
}
/// <summary>
/// Initialize the displayed digits array
/// </summary>
public void InitializeDigits()
{
PlayerView playerView = MainWindow.Instance.ContentControl.Content as PlayerView;
_digits = new Image[]
{
playerView.FindControl<Image>("TrackDigit1"),
playerView.FindControl<Image>("TrackDigit2"),
playerView.FindControl<Image>("IndexDigit1"),
playerView.FindControl<Image>("IndexDigit2"),
playerView.FindControl<Image>("TimeDigit1"),
playerView.FindControl<Image>("TimeDigit2"),
playerView.FindControl<Image>("TimeDigit3"),
playerView.FindControl<Image>("TimeDigit4"),
playerView.FindControl<Image>("TimeDigit5"),
playerView.FindControl<Image>("TimeDigit6"),
playerView.FindControl<Image>("TotalTracksDigit1"),
playerView.FindControl<Image>("TotalTracksDigit2"),
playerView.FindControl<Image>("TotalIndexesDigit1"),
playerView.FindControl<Image>("TotalIndexesDigit2"),
playerView.FindControl<Image>("TotalTimeDigit1"),
playerView.FindControl<Image>("TotalTimeDigit2"),
playerView.FindControl<Image>("TotalTimeDigit3"),
playerView.FindControl<Image>("TotalTimeDigit4"),
playerView.FindControl<Image>("TotalTimeDigit5"),
playerView.FindControl<Image>("TotalTimeDigit6"),
};
}
/// <summary>
/// Load an image from the path
/// </summary>
/// <param name="path">Path to the image to load</param>
public async Task<bool> LoadImage(string path)
{
return await Dispatcher.UIThread.InvokeAsync(() =>
{
Init(path, App.Settings.GenerateMissingTOC, App.Settings.PlayHiddenTracks, App.Settings.PlayDataTracks, App.Settings.AutoPlay, App.Settings.Volume);
if(Initialized)
MainWindow.Instance.Title = "RedBookPlayer - " + path.Split('/').Last().Split('\\').Last();
return Initialized;
});
}
/// <summary>
/// Set the value for loading data tracks [CompactDisc only]
/// </summary>
/// <param name="load">True to enable loading data tracks, false otherwise</param>
public void SetLoadDataTracks(bool load) => _player?.SetLoadDataTracks(load);
/// <summary>
/// Set the value for loading hidden tracks [CompactDisc only]
/// </summary>
/// <param name="load">True to enable loading hidden tracks, false otherwise</param>
public void SetLoadHiddenTracks(bool load) => _player?.SetLoadHiddenTracks(load);
/// <summary> /// <summary>
/// Generate the digit string to be interpreted by the frontend /// Generate the digit string to be interpreted by the frontend
/// </summary> /// </summary>
/// <returns>String representing the digits for the frontend</returns> /// <returns>String representing the digits for the frontend</returns>
public string GenerateDigitString() private string GenerateDigitString()
{ {
// If the disc isn't initialized, return all '-' characters // If the disc isn't initialized, return all '-' characters
if(Initialized != true) if(Initialized != true)
@@ -473,44 +560,32 @@ namespace RedBookPlayer.GUI.ViewModels
} }
/// <summary> /// <summary>
/// Load a disc image from a selection box /// Load the png image for a given character based on the theme
/// </summary> /// </summary>
public async void ExecuteLoad() /// <param name="character">Character to load the image for</param>
/// <returns>Bitmap representing the loaded image</returns>
private Bitmap GetBitmap(char character)
{ {
string path = await GetPath(); try
if(path == null) {
return; if(App.Settings.SelectedTheme == "default")
{
IAssetLoader assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
await LoadImage(path); return new Bitmap(assets.Open(new Uri($"avares://RedBookPlayer/Assets/{character}.png")));
} }
else
/// <summary>
/// Load an image from the path
/// </summary>
/// <param name="path">Path to the image to load</param>
public async Task<bool> LoadImage(string path)
{ {
return await Dispatcher.UIThread.InvokeAsync(() => string themeDirectory = $"{Directory.GetCurrentDirectory()}/themes/{App.Settings.SelectedTheme}";
{ using FileStream stream = File.Open($"{themeDirectory}/{character}.png", FileMode.Open);
Init(path, App.Settings.GenerateMissingTOC, App.Settings.PlayHiddenTracks, App.Settings.PlayDataTracks, App.Settings.AutoPlay, App.Settings.Volume); return new Bitmap(stream);
if(Initialized) }
MainWindow.Instance.Title = "RedBookPlayer - " + path.Split('/').Last().Split('\\').Last(); }
catch
return Initialized; {
}); return null;
}
} }
/// <summary>
/// Set the value for loading data tracks [CompactDisc only]
/// </summary>
/// <param name="load">True to enable loading data tracks, false otherwise</param>
public void SetLoadDataTracks(bool load) => _player?.SetLoadDataTracks(load);
/// <summary>
/// Set the value for loading hidden tracks [CompactDisc only]
/// </summary>
/// <param name="load">True to enable loading hidden tracks, false otherwise</param>
public void SetLoadHiddenTracks(bool load) => _player?.SetLoadHiddenTracks(load);
/// <summary> /// <summary>
/// Get current sector time, accounting for offsets /// Get current sector time, accounting for offsets
@@ -532,6 +607,8 @@ namespace RedBookPlayer.GUI.ViewModels
/// </summary> /// </summary>
/// <returns>User-selected path, if possible</returns> /// <returns>User-selected path, if possible</returns>
private async Task<string> GetPath() private async Task<string> GetPath()
{
return await Dispatcher.UIThread.InvokeAsync(async () =>
{ {
var dialog = new OpenFileDialog { AllowMultiple = false }; var dialog = new OpenFileDialog { AllowMultiple = false };
List<string> knownExtensions = new Aaru.DiscImages.AaruFormat().KnownExtensions.ToList(); List<string> knownExtensions = new Aaru.DiscImages.AaruFormat().KnownExtensions.ToList();
@@ -542,6 +619,7 @@ namespace RedBookPlayer.GUI.ViewModels
}); });
return (await dialog.ShowAsync(MainWindow.Instance))?.FirstOrDefault(); return (await dialog.ShowAsync(MainWindow.Instance))?.FirstOrDefault();
});
} }
/// <summary> /// <summary>
@@ -567,6 +645,17 @@ namespace RedBookPlayer.GUI.ViewModels
Playing = _player.Playing; Playing = _player.Playing;
ApplyDeEmphasis = _player.ApplyDeEmphasis; ApplyDeEmphasis = _player.ApplyDeEmphasis;
Volume = _player.Volume; Volume = _player.Volume;
Dispatcher.UIThread.InvokeAsync(() =>
{
string digitString = GenerateDigitString() ?? string.Empty.PadLeft(20, '-');
for(int i = 0; i < _digits.Length; i++)
{
Bitmap digitImage = GetBitmap(digitString[i]);
if(_digits[i] != null && digitImage != null)
_digits[i].Source = digitImage;
}
});
} }
#endregion #endregion

View File

@@ -9,7 +9,7 @@ using Aaru.Helpers;
using ReactiveUI; using ReactiveUI;
using static Aaru.Decoders.CD.FullTOC; using static Aaru.Decoders.CD.FullTOC;
namespace RedBookPlayer.Common.Discs namespace RedBookPlayer.Models.Discs
{ {
public class CompactDisc : OpticalDiscBase, IReactiveObject public class CompactDisc : OpticalDiscBase, IReactiveObject
{ {

View File

@@ -2,7 +2,7 @@ using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces; using Aaru.CommonTypes.Interfaces;
using ReactiveUI; using ReactiveUI;
namespace RedBookPlayer.Common.Discs namespace RedBookPlayer.Models.Discs
{ {
public abstract class OpticalDiscBase : ReactiveObject public abstract class OpticalDiscBase : ReactiveObject
{ {

View File

@@ -3,9 +3,9 @@ using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Metadata; using Aaru.CommonTypes.Metadata;
using Aaru.DiscImages; using Aaru.DiscImages;
using Aaru.Filters; using Aaru.Filters;
using RedBookPlayer.Common.Discs; using RedBookPlayer.Models.Discs;
namespace RedBookPlayer.Common.Factories namespace RedBookPlayer.Models.Factories
{ {
public static class OpticalDiscFactory public static class OpticalDiscFactory
{ {

View File

@@ -1,7 +1,7 @@
using System; using System;
using NWaves.Filters.BiQuad; using NWaves.Filters.BiQuad;
namespace RedBookPlayer.Common.Hardware namespace RedBookPlayer.Models.Hardware
{ {
/// <summary> /// <summary>
/// Filter for applying de-emphasis to audio /// Filter for applying de-emphasis to audio

View File

@@ -2,10 +2,10 @@ using System;
using System.ComponentModel; using System.ComponentModel;
using Aaru.CommonTypes.Enums; using Aaru.CommonTypes.Enums;
using ReactiveUI; using ReactiveUI;
using RedBookPlayer.Common.Discs; using RedBookPlayer.Models.Discs;
using RedBookPlayer.Common.Factories; using RedBookPlayer.Models.Factories;
namespace RedBookPlayer.Common.Hardware namespace RedBookPlayer.Models.Hardware
{ {
public class Player : ReactiveObject public class Player : ReactiveObject
{ {

View File

@@ -2,7 +2,7 @@ using System;
using CSCore; using CSCore;
using WaveFormat = CSCore.WaveFormat; using WaveFormat = CSCore.WaveFormat;
namespace RedBookPlayer.Common.Hardware namespace RedBookPlayer.Models.Hardware
{ {
public class PlayerSource : IWaveSource public class PlayerSource : IWaveSource
{ {

View File

@@ -5,9 +5,9 @@ using CSCore.SoundOut;
using NWaves.Audio; using NWaves.Audio;
using NWaves.Filters.BiQuad; using NWaves.Filters.BiQuad;
using ReactiveUI; using ReactiveUI;
using RedBookPlayer.Common.Discs; using RedBookPlayer.Models.Discs;
namespace RedBookPlayer.Common.Hardware namespace RedBookPlayer.Models.Hardware
{ {
public class SoundOutput : ReactiveObject public class SoundOutput : ReactiveObject
{ {

View File

@@ -40,7 +40,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md README.md = README.md
EndProjectSection EndProjectSection
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RedBookPlayer.Common", "RedBookPlayer.Common\RedBookPlayer.Common.csproj", "{462A3B8E-A5D4-4539-8469-1647B47AB2A8}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RedBookPlayer.Models", "RedBookPlayer.Models\RedBookPlayer.Models.csproj", "{462A3B8E-A5D4-4539-8469-1647B47AB2A8}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution