From 8c9f10dd8ee622e0ad19a5f747b3101f0cef7f75 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 5 Jul 2021 10:42:28 -0700 Subject: [PATCH] Consolidate view init code --- RedBookPlayer/GUI/PlayerView.xaml.cs | 225 ++++++++++++++------------- 1 file changed, 113 insertions(+), 112 deletions(-) diff --git a/RedBookPlayer/GUI/PlayerView.xaml.cs b/RedBookPlayer/GUI/PlayerView.xaml.cs index ac975c3..2319c97 100644 --- a/RedBookPlayer/GUI/PlayerView.xaml.cs +++ b/RedBookPlayer/GUI/PlayerView.xaml.cs @@ -26,9 +26,18 @@ namespace RedBookPlayer.GUI /// private Image[] _digits; - public PlayerView() => InitializeComponent(null); + /// + /// Initialize the UI based on the currently selected theme + /// + /// XAML data representing the theme, null for default + public PlayerView(string xaml = null) + { + DataContext = new PlayerViewModel(); + PlayerViewModel.PropertyChanged += PlayerViewModelStateChanged; - public PlayerView(string xaml) => InitializeComponent(xaml); + LoadTheme(xaml); + InitializeDigits(); + } #region Helpers @@ -55,120 +64,13 @@ namespace RedBookPlayer.GUI /// Path to the image to load public async Task LoadImage(string path) { - bool result = await Dispatcher.UIThread.InvokeAsync(() => + return await Dispatcher.UIThread.InvokeAsync(() => { PlayerViewModel.Init(path, App.Settings.AutoPlay, App.Settings.Volume); - return PlayerViewModel.Initialized; - }); - - if(result) - { - await Dispatcher.UIThread.InvokeAsync(() => - { + if (PlayerViewModel.Initialized) MainWindow.Instance.Title = "RedBookPlayer - " + path.Split('/').Last().Split('\\').Last(); - }); - } - return result; - } - - /// - /// Load the png image for a given character based on the theme - /// - /// Character to load the image for - /// Bitmap representing the loaded image - /// - /// TODO: Currently assumes that an image must always exist - /// - private Bitmap GetBitmap(char character) - { - if(App.Settings.SelectedTheme == "default") - { - IAssetLoader assets = AvaloniaLocator.Current.GetService(); - - 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); - } - } - - /// - /// Initialize the UI based on the currently selected theme - /// - /// XAML data representing the theme, null for default - private void InitializeComponent(string xaml) - { - DataContext = new PlayerViewModel(); - PlayerViewModel.PropertyChanged += PlayerViewModelStateChanged; - - // Load the theme - try - { - if(xaml != null) - new AvaloniaXamlLoader().Load(xaml, null, this); - else - AvaloniaXamlLoader.Load(this); - } - catch - { - AvaloniaXamlLoader.Load(this); - } - - InitializeDigits(); - } - - /// - /// Initialize the displayed digits array - /// - private void InitializeDigits() - { - _digits = new Image[] - { - this.FindControl("TrackDigit1"), - this.FindControl("TrackDigit2"), - - this.FindControl("IndexDigit1"), - this.FindControl("IndexDigit2"), - - this.FindControl("TimeDigit1"), - this.FindControl("TimeDigit2"), - this.FindControl("TimeDigit3"), - this.FindControl("TimeDigit4"), - this.FindControl("TimeDigit5"), - this.FindControl("TimeDigit6"), - - this.FindControl("TotalTracksDigit1"), - this.FindControl("TotalTracksDigit2"), - - this.FindControl("TotalIndexesDigit1"), - this.FindControl("TotalIndexesDigit2"), - - this.FindControl("TotalTimeDigit1"), - this.FindControl("TotalTimeDigit2"), - this.FindControl("TotalTimeDigit3"), - this.FindControl("TotalTimeDigit4"), - this.FindControl("TotalTimeDigit5"), - this.FindControl("TotalTimeDigit6"), - }; - } - - /// - /// Update the UI from the view-model - /// - private void PlayerViewModelStateChanged(object sender, PropertyChangedEventArgs e) - { - Dispatcher.UIThread.InvokeAsync(() => - { - string digitString = GenerateDigitString(); - for(int i = 0; i < _digits.Length; i++) - { - if(_digits[i] != null) - _digits[i].Source = GetBitmap(digitString[i]); - } + return PlayerViewModel.Initialized; }); } @@ -220,6 +122,105 @@ namespace RedBookPlayer.GUI return sectorTime; } + /// + /// Load the png image for a given character based on the theme + /// + /// Character to load the image for + /// Bitmap representing the loaded image + private Bitmap GetBitmap(char character) + { + try + { + if(App.Settings.SelectedTheme == "default") + { + IAssetLoader assets = AvaloniaLocator.Current.GetService(); + + 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; + } + } + + /// + /// Initialize the displayed digits array + /// + private void InitializeDigits() + { + _digits = new Image[] + { + this.FindControl("TrackDigit1"), + this.FindControl("TrackDigit2"), + + this.FindControl("IndexDigit1"), + this.FindControl("IndexDigit2"), + + this.FindControl("TimeDigit1"), + this.FindControl("TimeDigit2"), + this.FindControl("TimeDigit3"), + this.FindControl("TimeDigit4"), + this.FindControl("TimeDigit5"), + this.FindControl("TimeDigit6"), + + this.FindControl("TotalTracksDigit1"), + this.FindControl("TotalTracksDigit2"), + + this.FindControl("TotalIndexesDigit1"), + this.FindControl("TotalIndexesDigit2"), + + this.FindControl("TotalTimeDigit1"), + this.FindControl("TotalTimeDigit2"), + this.FindControl("TotalTimeDigit3"), + this.FindControl("TotalTimeDigit4"), + this.FindControl("TotalTimeDigit5"), + this.FindControl("TotalTimeDigit6"), + }; + } + + /// + /// Load the theme from a XAML, if possible + /// + /// XAML data representing the theme, null for default + private void LoadTheme(string xaml) + { + try + { + if(xaml != null) + new AvaloniaXamlLoader().Load(xaml, null, this); + else + AvaloniaXamlLoader.Load(this); + } + catch + { + AvaloniaXamlLoader.Load(this); + } + } + + /// + /// Update the UI from the view-model + /// + private void PlayerViewModelStateChanged(object sender, PropertyChangedEventArgs e) + { + Dispatcher.UIThread.InvokeAsync(() => + { + string digitString = GenerateDigitString(); + for(int i = 0; i < _digits.Length; i++) + { + Bitmap digitImage = GetBitmap(digitString[i]); + if(_digits[i] != null && digitImage != null) + _digits[i].Source = digitImage; + } + }); + } + #endregion #region Event Handlers