Skip to content

Commit

Permalink
Refactor refresh timer event hooking to occur during activation/deact…
Browse files Browse the repository at this point in the history
…ivation of the livestream list screen.

Attempt to only process errors from refreshes when the screen is active.
  • Loading branch information
laurencee committed Aug 3, 2016
1 parent 5b9819e commit bf4dbf3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
4 changes: 2 additions & 2 deletions GlobalAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
[assembly: AssemblyVersion("2.5.2.0")]
[assembly: AssemblyFileVersion("2.5.2.0")]
7 changes: 6 additions & 1 deletion Livestream.Monitor/Core/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
43 changes: 24 additions & 19 deletions Livestream.Monitor/ViewModels/LivestreamListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class LivestreamListViewModel : Screen, IHandleWithTask<ExceptionDispatch
private readonly INavigationService navigationService;
private readonly DispatcherTimer refreshTimer;

private bool loading, displayingException;
private bool loading;
private int refreshErrorCount = 0;
private LivestreamsLayoutMode layoutModeMode = LivestreamsLayoutMode.Grid;

public LivestreamListViewModel()
Expand All @@ -50,7 +51,6 @@ public LivestreamListViewModel(
this.StreamsModel = monitorStreamsModel;
FilterModel = filterModel;
refreshTimer = new DispatcherTimer { Interval = Constants.RefreshPollingTime };
refreshTimer.Tick += async (sender, args) => await RefreshLivestreams();
}

public bool Loading
Expand Down Expand Up @@ -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();
}

/// <summary> Loads the selected stream through livestreamer and displays a messagebox with the loading process details </summary>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down

0 comments on commit bf4dbf3

Please sign in to comment.