using System.Threading;
using System.Threading.Tasks;
namespace Artemis.UI.Shared.Routing;
///
/// Represents a view model to which routing can take place and which in turn can host another view model.
///
/// The type of view model the screen can host.
public abstract class RoutableScreen : ActivatableViewModelBase, IRoutableScreen where TScreen : class
{
private TScreen? _screen;
private bool _recycleScreen = true;
///
/// Gets the currently active child screen.
///
public TScreen? Screen
{
get => _screen;
private set => RaiseAndSetIfChanged(ref _screen, value);
}
///
public bool RecycleScreen
{
get => _recycleScreen;
protected set => RaiseAndSetIfChanged(ref _recycleScreen, value);
}
///
/// Called before navigating to this screen.
///
/// Navigation arguments containing information about the navigation action.
public virtual Task BeforeNavigating(NavigationArguments args)
{
return Task.CompletedTask;
}
///
/// Called while navigating to this screen.
///
/// Navigation arguments containing information about the navigation action.
/// A cancellation token that can be used by other objects or threads to receive notice of cancellation.
public virtual Task OnNavigating(NavigationArguments args, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
///
/// Called before navigating away from this screen.
///
/// Navigation arguments containing information about the navigation action.
public virtual Task OnClosing(NavigationArguments args)
{
return Task.CompletedTask;
}
#region Overrides of RoutableScreen
object? IRoutableScreen.InternalScreen => Screen;
void IRoutableScreen.InternalChangeScreen(object? screen)
{
Screen = screen as TScreen;
}
async Task IRoutableScreen.InternalOnNavigating(NavigationArguments args, CancellationToken cancellationToken)
{
await OnNavigating(args, cancellationToken);
}
async Task IRoutableScreen.InternalOnClosing(NavigationArguments args)
{
await OnClosing(args);
}
#endregion
}