Skip to content
34 changes: 34 additions & 0 deletions Float.Core/UX/Coordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public abstract class Coordinator : ICoordinator
/// </summary>
Page managedPage;

/// <summary>
/// The event args that are pending a finish.
/// </summary>
EventArgs waitingToFinishEventArgs;
Comment thread
EBusch marked this conversation as resolved.
Outdated
Comment thread
EBusch marked this conversation as resolved.
Outdated

/// <summary>
/// Occurs when this coordinator is started.
/// </summary>
Expand Down Expand Up @@ -93,6 +98,35 @@ public virtual void Start()
isStarted = true;
}

/// <inheritdoc/>
public Task<bool> RequestFinish(EventArgs args)
{
return Task.FromResult(HandleFinishRequested(this, args));
}

/// <summary>
/// Handles when a finish is requested.
/// </summary>
/// <param name="coordinator">The coordinator that has requested this coordinator to finish.</param>
/// <param name="eventArgs">The event args.</param>
/// <returns>A value indicating whether this finished.</returns>
public virtual bool HandleFinishRequested(ICoordinator coordinator, EventArgs eventArgs)
{
if (this == coordinator)
{
if (managedPage == null)
{
Finish(eventArgs);
return true;
}

waitingToFinishEventArgs = eventArgs;
NavigationContext.Reset(false);
}

return false;
}

/// <summary>
/// Returning a page here will allow the coordinator to automatically
/// manage itself based on the state of the UI.
Expand Down
39 changes: 39 additions & 0 deletions Float.Core/UX/CoordinatorParent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Float.Core.Extensions;

namespace Float.Core.UX
Expand All @@ -15,6 +16,11 @@ public abstract class CoordinatorParent : Coordinator, ICoordinatorParent
/// </summary>
readonly List<ICoordinator> childCoordinators = new ();

/// <summary>
/// The event args that have been used as we are waiting to finish all the children before closing the parent.
/// </summary>
EventArgs waitingToFinishEventArgs = null;

Comment thread
EBusch marked this conversation as resolved.
Outdated
/// <summary>
/// Gets a value indicating whether this <see cref="CoordinatorParent"/> has children.
/// </summary>
Expand Down Expand Up @@ -117,6 +123,33 @@ public override string ToString()
return $"[{GetType()}, Children: {string.Join(",", childCoordinators)}]";
}

/// <summary>
/// Handles the finish request.
/// </summary>
/// <param name="coordinator">The calling coordinator.</param>
/// <param name="eventArgs">The event args.</param>
/// <returns>A value indicating whether this finished.</returns>
Comment thread
EBusch marked this conversation as resolved.
Outdated
public override bool HandleFinishRequested(ICoordinator coordinator, EventArgs eventArgs)
{
var didFinish = true;

waitingToFinishEventArgs = eventArgs;

foreach (var eachChild in ChildCoordinators)
{
if (eachChild is Coordinator childCoordinator)
{
var finished = childCoordinator.HandleFinishRequested(childCoordinator, eventArgs);
Comment thread
EBusch marked this conversation as resolved.
Outdated
if (!finished)
{
didFinish = false;
}
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EBusch We're probably missing a call to the base implementation here. When you add that, you may have an issue with Finish getting called twice:

Coordinator.HandleNavigation may call it and so will CoordinatorParent.HandleChildFinish -- it is not currently clear to me the best way to address that.

return didFinish;
}

/// <inheritdoc />
protected override void Finish(EventArgs args)
{
Expand Down Expand Up @@ -149,6 +182,12 @@ protected virtual void HandleChildFinish(object sender, EventArgs args)
{
RemoveChild(child);
}

if (!HasChildren && waitingToFinishEventArgs != null)
{
Finish(waitingToFinishEventArgs);
waitingToFinishEventArgs = null;
}
}
}
}
8 changes: 8 additions & 0 deletions Float.Core/UX/ICoordinator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;

namespace Float.Core.UX
{
Expand All @@ -23,5 +24,12 @@ public interface ICoordinator
/// </summary>
/// <param name="navigationContext">The navigation context for this coordinator.</param>
void Start(INavigationContext navigationContext);

/// <summary>
/// Requests that a coordinator will finish.
/// </summary>
/// <param name="eventArgs">The event args.</param>
/// <returns>A task, with a boolean indicating whether this did finish.</returns>
Comment thread
EBusch marked this conversation as resolved.
Task<bool> RequestFinish(EventArgs eventArgs);
}
}
3 changes: 3 additions & 0 deletions Float.Core/UX/ICoordinatorParent.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.Threading.Tasks;

Comment thread
EBusch marked this conversation as resolved.
Outdated
namespace Float.Core.UX
{
/// <summary>
Expand Down