From bf4dbf384a99f62df3144690e6aa0307b652d39f Mon Sep 17 00:00:00 2001 From: laurencee Date: Wed, 3 Aug 2016 21:14:49 +1000 Subject: [PATCH] Refactor refresh timer event hooking to occur during activation/deactivation of the livestream list screen. Attempt to only process errors from refreshes when the screen is active. --- GlobalAssemblyInfo.cs | 4 +- Livestream.Monitor/Core/Extensions.cs | 7 ++- .../ViewModels/LivestreamListViewModel.cs | 43 +++++++++++-------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/GlobalAssemblyInfo.cs b/GlobalAssemblyInfo.cs index e30d306..627fb4e 100644 --- a/GlobalAssemblyInfo.cs +++ b/GlobalAssemblyInfo.cs @@ -22,5 +22,5 @@ // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.5.1.0")] -[assembly: AssemblyFileVersion("2.5.1.0")] \ No newline at end of file +[assembly: AssemblyVersion("2.5.2.0")] +[assembly: AssemblyFileVersion("2.5.2.0")] \ No newline at end of file diff --git a/Livestream.Monitor/Core/Extensions.cs b/Livestream.Monitor/Core/Extensions.cs index 0e6111a..3430aa6 100644 --- a/Livestream.Monitor/Core/Extensions.cs +++ b/Livestream.Monitor/Core/Extensions.cs @@ -8,7 +8,6 @@ using Caliburn.Micro; using Livestream.Monitor.Model; using Livestream.Monitor.Model.ApiClients; -using Livestream.Monitor.Model.Monitoring; using MahApps.Metro.Controls; using MahApps.Metro.Controls.Dialogs; @@ -189,6 +188,12 @@ public static string ExtractErrorMessage(this Exception exception, int skipExcep { if (exception == null) return null; + var aggregateException = exception as AggregateException; + if (aggregateException != null) + { + exception = aggregateException.Flatten(); + } + // Avoiding large recursion... just in case int count = 0; diff --git a/Livestream.Monitor/ViewModels/LivestreamListViewModel.cs b/Livestream.Monitor/ViewModels/LivestreamListViewModel.cs index 43e76f4..7ee9b5f 100644 --- a/Livestream.Monitor/ViewModels/LivestreamListViewModel.cs +++ b/Livestream.Monitor/ViewModels/LivestreamListViewModel.cs @@ -23,7 +23,8 @@ public class LivestreamListViewModel : Screen, IHandleWithTask await RefreshLivestreams(); } public bool Loading @@ -85,33 +85,30 @@ public async Task RefreshLivestreams() try { await StreamsModel.RefreshLivestreams(); - } - catch (AggregateException ex) - { - if (!displayingException) - { - Execute.OnUIThread(async () => - { - displayingException = true; - await this.ShowMessageAsync("Error refreshing livestreams", ex.Flatten().ExtractErrorMessage()); - displayingException = false; - }); - } + refreshErrorCount = 0; + + // only reactive the timer if we're still on this screen + if (IsActive) refreshTimer.Start(); } catch (Exception ex) { - if (!displayingException) + if (!IsActive) return; + refreshErrorCount++; + + // keep trying to refresh until we hit too many consecutive errors + if (refreshErrorCount >= 3) { Execute.OnUIThread(async () => { - displayingException = true; await this.ShowMessageAsync("Error refreshing livestreams", ex.ExtractErrorMessage()); - displayingException = false; + refreshTimer.Start(); }); } + else + { + refreshTimer.Start(); + } } - - refreshTimer.Start(); } /// Loads the selected stream through livestreamer and displays a messagebox with the loading process details @@ -190,6 +187,8 @@ protected override async void OnActivate() Loading = true; try { + refreshErrorCount = 0; + refreshTimer.Tick += RefreshTimerOnTick; StreamsModel.LivestreamsRefreshComplete += OnLivestreamsRefreshComplete; FilterModel.PropertyChanged += OnFilterModelOnPropertyChanged; ViewSource.Source = StreamsModel.Livestreams; @@ -221,6 +220,7 @@ protected override async void OnActivate() protected override void OnDeactivate(bool close) { + refreshTimer.Tick -= RefreshTimerOnTick; refreshTimer.Stop(); StreamsModel.LivestreamsRefreshComplete -= OnLivestreamsRefreshComplete; FilterModel.PropertyChanged -= OnFilterModelOnPropertyChanged; @@ -236,6 +236,11 @@ protected override void OnDeactivate(bool close) base.OnDeactivate(close); } + private async void RefreshTimerOnTick(object sender, EventArgs args) + { + await RefreshLivestreams(); + } + private void LivestreamsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.NewItems != null)