From 65ab29f29f64ec5280cf8d237f99a427857f878b Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sun, 4 Jul 2021 23:43:55 -0700 Subject: [PATCH] Remove need for UI timer for updates --- RedBookPlayer/GUI/PlayerView.xaml.cs | 32 ++++++---------------------- RedBookPlayer/GUI/PlayerViewModel.cs | 2 +- RedBookPlayer/Hardware/Player.cs | 9 ++++++++ 3 files changed, 16 insertions(+), 27 deletions(-) diff --git a/RedBookPlayer/GUI/PlayerView.xaml.cs b/RedBookPlayer/GUI/PlayerView.xaml.cs index bdb1ecc..ec749cb 100644 --- a/RedBookPlayer/GUI/PlayerView.xaml.cs +++ b/RedBookPlayer/GUI/PlayerView.xaml.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -29,11 +30,6 @@ namespace RedBookPlayer.GUI /// private Image[] _digits; - /// - /// Timer for performing UI updates - /// - private Timer _updateTimer; - public PlayerView() => InitializeComponent(null); public PlayerView(string xaml) => InitializeComponent(xaml); @@ -111,6 +107,7 @@ namespace RedBookPlayer.GUI private void InitializeComponent(string xaml) { DataContext = new PlayerViewModel(); + PlayerViewModel.PropertyChanged += PlayerViewModelStateChanged; // Load the theme try @@ -126,23 +123,6 @@ namespace RedBookPlayer.GUI } InitializeDigits(); - - _updateTimer = new Timer(1000 / 60); - - _updateTimer.Elapsed += (sender, e) => - { - try - { - UpdateView(sender, e); - } - catch(Exception ex) - { - Console.WriteLine(ex); - } - }; - - _updateTimer.AutoReset = true; - _updateTimer.Start(); } /// @@ -181,16 +161,16 @@ namespace RedBookPlayer.GUI } /// - /// Update the UI with the most recent information from the Player + /// Update the UI from the view-model /// - private void UpdateView(object sender, ElapsedEventArgs e) + private void PlayerViewModelStateChanged(object sender, PropertyChangedEventArgs e) { Dispatcher.UIThread.InvokeAsync(() => { string digitString = PlayerViewModel.GenerateDigitString(); - for (int i = 0; i < _digits.Length; i++) + for(int i = 0; i < _digits.Length; i++) { - if (_digits[i] != null) + if(_digits[i] != null) _digits[i].Source = GetBitmap(digitString[i]); } }); diff --git a/RedBookPlayer/GUI/PlayerViewModel.cs b/RedBookPlayer/GUI/PlayerViewModel.cs index 8714d48..cee36af 100644 --- a/RedBookPlayer/GUI/PlayerViewModel.cs +++ b/RedBookPlayer/GUI/PlayerViewModel.cs @@ -305,7 +305,7 @@ namespace RedBookPlayer.GUI } /// - /// Update the UI from the internal player + /// Update the view-model from the Player /// private void PlayerStateChanged(object sender, PropertyChangedEventArgs e) { diff --git a/RedBookPlayer/Hardware/Player.cs b/RedBookPlayer/Hardware/Player.cs index 3c72fa6..0733e5a 100644 --- a/RedBookPlayer/Hardware/Player.cs +++ b/RedBookPlayer/Hardware/Player.cs @@ -211,6 +211,10 @@ namespace RedBookPlayer.Hardware return; } + // Add event handling for the optical disc + if(_opticalDisc != null) + _opticalDisc.PropertyChanged += OpticalDiscStateChanged; + // Initialize the sound output _soundOutput.Init(_opticalDisc, autoPlay, defaultVolume); if(_soundOutput == null || !_soundOutput.Initialized) @@ -409,6 +413,11 @@ namespace RedBookPlayer.Hardware /// public void SetDeEmphasis(bool apply) => _soundOutput?.SetDeEmphasis(apply); + /// + /// Update the player from the current OpticalDisc + /// + private void OpticalDiscStateChanged(object sender, PropertyChangedEventArgs e) => SetDiscInformation(); + /// /// Update the player from the current SoundOutput ///