Move image loading to view model

This commit is contained in:
Matt Nadareski
2021-07-12 12:38:33 -07:00
parent bc810034d0
commit 1e9b41f997
4 changed files with 59 additions and 53 deletions

View File

@@ -111,7 +111,7 @@ namespace RedBookPlayer.GUI
IEnumerable<string> fileNames = e.Data.GetFileNames(); IEnumerable<string> fileNames = e.Data.GetFileNames();
foreach(string filename in fileNames) foreach(string filename in fileNames)
{ {
bool loaded = await playerView.LoadImage(filename); bool loaded = await playerView?.PlayerViewModel?.LoadImage(filename);
if(loaded) if(loaded)
break; break;
} }
@@ -132,7 +132,7 @@ namespace RedBookPlayer.GUI
// Load image // Load image
else if (e.Key == App.Settings.LoadImageKey) else if (e.Key == App.Settings.LoadImageKey)
{ {
playerView?.LoadButton_Click(this, null); playerView?.PlayerViewModel?.ExecuteLoad();
} }
// Toggle playback // Toggle playback

View File

@@ -3,7 +3,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="RedBookPlayer.GUI.PlayerView" Width="900" Height="400"> x:Class="RedBookPlayer.GUI.PlayerView" Width="900" Height="400">
<StackPanel Margin="16" VerticalAlignment="Center"> <StackPanel Margin="16" VerticalAlignment="Center">
<Button Click="LoadButton_Click" Margin="32,0,32,16">Load</Button> <Button Command="{Binding LoadCommand}" Margin="32,0,32,16">Load</Button>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,0,0,16"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,0,0,16">
<Button Command="{Binding PlayCommand}" Width="100" Margin="0,0,16,0">Play</Button> <Button Command="{Binding PlayCommand}" Width="100" Margin="0,0,16,0">Play</Button>
<Button Command="{Binding PauseCommand}" Width="100" Margin="0,0,16,0">Pause</Button> <Button Command="{Binding PauseCommand}" Width="100" Margin="0,0,16,0">Pause</Button>

View File

@@ -1,12 +1,8 @@
using System; using System;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.IO; using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging; using Avalonia.Media.Imaging;
using Avalonia.Platform; using Avalonia.Platform;
@@ -59,22 +55,6 @@ namespace RedBookPlayer.GUI
#region Helpers #region Helpers
/// <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(() =>
{
PlayerViewModel.Init(path, App.Settings.GenerateMissingTOC, App.Settings.PlayHiddenTracks, App.Settings.PlayDataTracks, App.Settings.AutoPlay, App.Settings.Volume);
if (PlayerViewModel.Initialized)
MainWindow.Instance.Title = "RedBookPlayer - " + path.Split('/').Last().Split('\\').Last();
return PlayerViewModel.Initialized;
});
}
/// <summary> /// <summary>
/// Update the view model with new settings /// Update the view model with new settings
/// </summary> /// </summary>
@@ -112,23 +92,6 @@ namespace RedBookPlayer.GUI
} }
} }
/// <summary>
/// Generate a path selection dialog box
/// </summary>
/// <returns>User-selected path, if possible</returns>
private async Task<string> GetPath()
{
var dialog = new OpenFileDialog { AllowMultiple = false };
List<string> knownExtensions = new Aaru.DiscImages.AaruFormat().KnownExtensions.ToList();
dialog.Filters.Add(new FileDialogFilter()
{
Name = "Aaru Image Format (*" + string.Join(", *", knownExtensions) + ")",
Extensions = knownExtensions.ConvertAll(e => e.TrimStart('.'))
});
return (await dialog.ShowAsync((Window)Parent.Parent))?.FirstOrDefault();
}
/// <summary> /// <summary>
/// Initialize the displayed digits array /// Initialize the displayed digits array
/// </summary> /// </summary>
@@ -201,18 +164,5 @@ namespace RedBookPlayer.GUI
} }
#endregion #endregion
#region Event Handlers
public async void LoadButton_Click(object sender, RoutedEventArgs e)
{
string path = await GetPath();
if (path == null)
return;
await LoadImage(path);
}
#endregion
} }
} }

View File

@@ -1,6 +1,10 @@
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Reactive; using System.Reactive;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Threading;
using ReactiveUI; using ReactiveUI;
using RedBookPlayer.Common.Hardware; using RedBookPlayer.Common.Hardware;
@@ -180,6 +184,11 @@ namespace RedBookPlayer.GUI.ViewModels
#region Commands #region Commands
/// <summary>
/// Command for loading a disc
/// </summary>
public ReactiveCommand<Unit, Unit> LoadCommand { get; }
#region Playback #region Playback
/// <summary> /// <summary>
@@ -279,6 +288,8 @@ namespace RedBookPlayer.GUI.ViewModels
/// </summary> /// </summary>
public PlayerViewModel() public PlayerViewModel()
{ {
LoadCommand = ReactiveCommand.Create(ExecuteLoad);
PlayCommand = ReactiveCommand.Create(ExecutePlay); PlayCommand = ReactiveCommand.Create(ExecutePlay);
PauseCommand = ReactiveCommand.Create(ExecutePause); PauseCommand = ReactiveCommand.Create(ExecutePause);
TogglePlayPauseCommand = ReactiveCommand.Create(ExecuteTogglePlayPause); TogglePlayPauseCommand = ReactiveCommand.Create(ExecuteTogglePlayPause);
@@ -461,6 +472,34 @@ namespace RedBookPlayer.GUI.ViewModels
return string.Join("", numbers.Select(i => i.ToString().PadLeft(2, '0').Substring(0, 2))); return string.Join("", numbers.Select(i => i.ToString().PadLeft(2, '0').Substring(0, 2)));
} }
/// <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>
/// 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> /// <summary>
/// Set the value for loading data tracks [CompactDisc only] /// Set the value for loading data tracks [CompactDisc only]
/// </summary> /// </summary>
@@ -488,6 +527,23 @@ namespace RedBookPlayer.GUI.ViewModels
return sectorTime; return sectorTime;
} }
/// <summary>
/// Generate a path selection dialog box
/// </summary>
/// <returns>User-selected path, if possible</returns>
private async Task<string> GetPath()
{
var dialog = new OpenFileDialog { AllowMultiple = false };
List<string> knownExtensions = new Aaru.DiscImages.AaruFormat().KnownExtensions.ToList();
dialog.Filters.Add(new FileDialogFilter()
{
Name = "Aaru Image Format (*" + string.Join(", *", knownExtensions) + ")",
Extensions = knownExtensions.ConvertAll(e => e.TrimStart('.'))
});
return (await dialog.ShowAsync(MainWindow.Instance))?.FirstOrDefault();
}
/// <summary> /// <summary>
/// Update the view-model from the Player /// Update the view-model from the Player
/// </summary> /// </summary>