Skip to content
Merged
69 changes: 55 additions & 14 deletions Float.TinCan.ActivityLibrary/ActivityLaunchCoordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Float.Core.UI;
using Float.Core.UX;
using Float.FileDownloader;
Expand Down Expand Up @@ -29,6 +30,7 @@ public abstract class ActivityLaunchCoordinator : Coordinator

string startLocation;
DownloadStatus downloadStatus;
bool isCreatingRunner;

/// <summary>
/// Initializes a new instance of the <see cref="ActivityLaunchCoordinator"/> class.
Expand Down Expand Up @@ -76,6 +78,14 @@ protected ActivityLaunchCoordinator(IActivity activity, IRemoteFileProvider remo
/// <value>The managed html activity runner page.</value>
protected BaseContentPage ManagedHtmlActivityRunnerPage { get; set; }

/// <summary>
/// Gets a value indicating whether this is creating a runner, which may inform the implementing instance it would not want to allow finishing of the coordinator.
/// </summary>
/// <value>
/// A value indicating whether this is creating a runner, which may inform the implementing instance it would not want to allow finishing of the coordinator.
/// </value>
protected bool IsCreatingRunner => isCreatingRunner;

/// <inheritdoc />
public override void Start()
{
Expand All @@ -101,7 +111,7 @@ public override void Start()
}
else
{
Device.BeginInvokeOnMainThread(() =>
Device.BeginInvokeOnMainThread(async () =>
{
try
{
Expand All @@ -118,8 +128,7 @@ public override void Start()

downloadStatus.DownloadsCompleted += HandleDownloadCompleted;
downloadStatus.DownloadsCancelled += HandleDownloadCancelled;

NavigationContext.PresentPage(CreateDownloadStatusPage(downloadStatus));
await ShowDownloadStatus(CreateDownloadStatusPage(downloadStatus));
});
}
}
Expand All @@ -129,7 +138,31 @@ public override void Start()
/// </summary>
/// <returns>The download status page.</returns>
/// <param name="downloadStatus">Download status.</param>
protected abstract BaseContentPage CreateDownloadStatusPage(DownloadStatus downloadStatus);
protected abstract ContentPage CreateDownloadStatusPage(DownloadStatus downloadStatus);

/// <summary>
/// Shows the download status page.
/// </summary>
/// <param name="downloadStatusPage">The download status page.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
protected virtual async Task ShowDownloadStatus(ContentPage downloadStatusPage)
{
if (downloadStatusPage is null)
{
throw new ArgumentNullException(nameof(downloadStatusPage));
}

await NavigationContext.PresentPageAsync(downloadStatusPage);
}

/// <summary>
/// Dismisses the download status page.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
protected virtual async Task DismissDownloadStatus()
{
await NavigationContext.PopPageAsync();
}

/// <summary>
/// Creates the activity complete page.
Expand All @@ -153,6 +186,11 @@ public override void Start()
/// <inheritdoc />
protected override void Finish(EventArgs args)
{
if (isCreatingRunner)
{
return;
}

base.Finish(args);
try
{
Expand Down Expand Up @@ -205,10 +243,10 @@ protected virtual void OnActivityFinished(object sender, EventArgs args)
/// </summary>
protected void ShowCompletionScreen()
{
Device.BeginInvokeOnMainThread(() =>
Device.BeginInvokeOnMainThread(async () =>
{
var completionPage = CreateActivityCompletePage(AvailablePostAssessments != null && AvailablePostAssessments.Any());
NavigationContext.PresentPage(completionPage);
await NavigationContext.PresentPageAsync(completionPage);
});
}

Expand Down Expand Up @@ -318,19 +356,21 @@ protected virtual void HandleAssessmentFailed(object sender, EventArgs args)
/// </summary>
/// <param name="sender">The sending object.</param>
/// <param name="args">Arguments related to the event.</param>
protected virtual void HandleDownloadCompleted(object sender, EventArgs args)
protected virtual async void HandleDownloadCompleted(object sender, EventArgs args)
{
if (downloadStatus != null && downloadStatus.State != DownloadStatus.DownloadState.Error)
{
downloadStatus.DownloadsCompleted -= HandleDownloadCompleted;
downloadStatus.DownloadsCancelled -= HandleDownloadCancelled;

NavigationContext.DismissPage();
startLocation = Activity.MetaData.StartLocation;
isCreatingRunner = true;
await DismissDownloadStatus();

CreateRunnerAndHandleErrors();

downloadStatus = null;
isCreatingRunner = false;
}
else
{
Expand All @@ -343,15 +383,16 @@ protected virtual void HandleDownloadCompleted(object sender, EventArgs args)
/// </summary>
/// <param name="sender">The sending object.</param>
/// <param name="args">Arguments related to the event.</param>
protected virtual void HandleDownloadCancelled(object sender, EventArgs args)
protected virtual async void HandleDownloadCancelled(object sender, EventArgs args)
{
if (downloadStatus != null)
{
downloadStatus.DownloadsCompleted -= HandleDownloadCompleted;
downloadStatus.DownloadsCancelled -= HandleDownloadCancelled;
}

NavigationContext.DismissPage();
await DismissDownloadStatus();

downloadStatus = null;
}

Expand All @@ -373,11 +414,11 @@ protected virtual void HandleDownloadError(object sender, EventArgs args)

void CreateRunnerAndHandleErrors()
{
Device.BeginInvokeOnMainThread(() =>
Device.BeginInvokeOnMainThread(async () =>
{
try
{
CreateRunner();
await CreateRunner();
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception e)
Expand All @@ -392,7 +433,7 @@ void CreateRunnerAndHandleErrors()
/// <summary>
/// Creates the runner. Note that this method expects to be called on the main thread.
/// </summary>
void CreateRunner()
async Task CreateRunner()
{
// because this method is always invoked on the main thread, it's possible this coordinator was finished on a different thread first
// this can cause all sorts of problems because we create a runner and never dispose it
Expand Down Expand Up @@ -449,7 +490,7 @@ void CreateRunner()
throw new InvalidOperationException($"No navigation context for activity {Activity.Name}");
}

NavigationContext.PushPage(ManagedHtmlActivityRunnerPage);
await NavigationContext.PushPageAsync(ManagedHtmlActivityRunnerPage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
<PackageReference Include="Float.FileDownloader" Version="1.0.0.5" />
<PackageReference Include="Float.TinCan" Version="1.0.3.29" />
<PackageReference Include="Float.TinCan.LocalLRSServer" Version="1.0.0.24" />
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2478+927-sha.9f26f70d6-azdo.6239473" />
<PackageReference Include="Float.Core" Version="1.0.0.30" />
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2515" />
<PackageReference Include="Float.Core" Version="1.0.0.39" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down