From 1e9b41f9973d1f3a76907641553282171a65c636 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 12 Jul 2021 12:38:33 -0700 Subject: [PATCH] Move image loading to view model --- RedBookPlayer.GUI/MainWindow.xaml.cs | 4 +- RedBookPlayer.GUI/PlayerView.xaml | 2 +- RedBookPlayer.GUI/PlayerView.xaml.cs | 50 ----------------- .../ViewModels/PlayerViewModel.cs | 56 +++++++++++++++++++ 4 files changed, 59 insertions(+), 53 deletions(-) diff --git a/RedBookPlayer.GUI/MainWindow.xaml.cs b/RedBookPlayer.GUI/MainWindow.xaml.cs index 2172c1a..08fd2b1 100644 --- a/RedBookPlayer.GUI/MainWindow.xaml.cs +++ b/RedBookPlayer.GUI/MainWindow.xaml.cs @@ -111,7 +111,7 @@ namespace RedBookPlayer.GUI IEnumerable fileNames = e.Data.GetFileNames(); foreach(string filename in fileNames) { - bool loaded = await playerView.LoadImage(filename); + bool loaded = await playerView?.PlayerViewModel?.LoadImage(filename); if(loaded) break; } @@ -132,7 +132,7 @@ namespace RedBookPlayer.GUI // Load image else if (e.Key == App.Settings.LoadImageKey) { - playerView?.LoadButton_Click(this, null); + playerView?.PlayerViewModel?.ExecuteLoad(); } // Toggle playback diff --git a/RedBookPlayer.GUI/PlayerView.xaml b/RedBookPlayer.GUI/PlayerView.xaml index e531106..26d230d 100644 --- a/RedBookPlayer.GUI/PlayerView.xaml +++ b/RedBookPlayer.GUI/PlayerView.xaml @@ -3,7 +3,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="RedBookPlayer.GUI.PlayerView" Width="900" Height="400"> - + diff --git a/RedBookPlayer.GUI/PlayerView.xaml.cs b/RedBookPlayer.GUI/PlayerView.xaml.cs index a1b763b..a4596fd 100644 --- a/RedBookPlayer.GUI/PlayerView.xaml.cs +++ b/RedBookPlayer.GUI/PlayerView.xaml.cs @@ -1,12 +1,8 @@ using System; -using System.Collections.Generic; using System.ComponentModel; using System.IO; -using System.Linq; -using System.Threading.Tasks; using Avalonia; using Avalonia.Controls; -using Avalonia.Interactivity; using Avalonia.Markup.Xaml; using Avalonia.Media.Imaging; using Avalonia.Platform; @@ -59,22 +55,6 @@ namespace RedBookPlayer.GUI #region Helpers - /// - /// Load an image from the path - /// - /// Path to the image to load - public async Task 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; - }); - } - /// /// Update the view model with new settings /// @@ -112,23 +92,6 @@ namespace RedBookPlayer.GUI } } - /// - /// Generate a path selection dialog box - /// - /// User-selected path, if possible - private async Task GetPath() - { - var dialog = new OpenFileDialog { AllowMultiple = false }; - List 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(); - } - /// /// Initialize the displayed digits array /// @@ -201,18 +164,5 @@ namespace RedBookPlayer.GUI } #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 } } \ No newline at end of file diff --git a/RedBookPlayer.GUI/ViewModels/PlayerViewModel.cs b/RedBookPlayer.GUI/ViewModels/PlayerViewModel.cs index a5cad4a..df8269b 100644 --- a/RedBookPlayer.GUI/ViewModels/PlayerViewModel.cs +++ b/RedBookPlayer.GUI/ViewModels/PlayerViewModel.cs @@ -1,6 +1,10 @@ +using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reactive; +using System.Threading.Tasks; +using Avalonia.Controls; +using Avalonia.Threading; using ReactiveUI; using RedBookPlayer.Common.Hardware; @@ -180,6 +184,11 @@ namespace RedBookPlayer.GUI.ViewModels #region Commands + /// + /// Command for loading a disc + /// + public ReactiveCommand LoadCommand { get; } + #region Playback /// @@ -279,6 +288,8 @@ namespace RedBookPlayer.GUI.ViewModels /// public PlayerViewModel() { + LoadCommand = ReactiveCommand.Create(ExecuteLoad); + PlayCommand = ReactiveCommand.Create(ExecutePlay); PauseCommand = ReactiveCommand.Create(ExecutePause); 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))); } + /// + /// Load a disc image from a selection box + /// + public async void ExecuteLoad() + { + string path = await GetPath(); + if(path == null) + return; + + await LoadImage(path); + } + + /// + /// Load an image from the path + /// + /// Path to the image to load + public async Task 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; + }); + } + /// /// Set the value for loading data tracks [CompactDisc only] /// @@ -488,6 +527,23 @@ namespace RedBookPlayer.GUI.ViewModels return sectorTime; } + /// + /// Generate a path selection dialog box + /// + /// User-selected path, if possible + private async Task GetPath() + { + var dialog = new OpenFileDialog { AllowMultiple = false }; + List 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(); + } + /// /// Update the view-model from the Player ///