using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.SharedUI;
namespace Tango.SharedUI
{
/// <summary>
/// Represents a dialog view model base class.
/// </summary>
/// <seealso cref="Tango.SharedUI.ViewModel" />
public abstract class DialogViewVM : ViewModel
{
public event Action Accepted;
public event Action Canceled;
/// <summary>
/// Initializes a new instance of the <see cref="DialogViewVM"/> class.
/// </summary>
public DialogViewVM()
{
CanClose = true;
CloseCommand = new RelayCommand(Cancel, (x) => CanClose);
OKCommand = new RelayCommand(Accept, (x) => CanOK());
}
private bool _canClose;
/// <summary>
/// Gets or sets a value indicating whether this dialog can be closed.
/// </summary>
public bool CanClose
{
get { return _canClose; }
set { _canClose = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
}
private bool _isVisible;
/// <summary>
/// Gets or sets a value indicating whether this instance is visible.
/// </summary>
public bool IsVisible
{
get { return _isVisible; }
set { _isVisible = value; RaisePropertyChangedAuto(); }
}
/// <summary>
/// Determines whether this instance can invoke the OK command.
/// </summary>
protected virtual bool CanOK()
{
return true;
}
/// <summary>
/// Gets a value indicating whether the dialog has been confirmed.
/// </summary>
public bool DialogResult { get; private set; }
/// <summary>
/// Gets or sets the close command.
/// </summary>
public RelayCommand CloseCommand { get; set; }
/// <summary>
/// Gets or sets the OK command.
/// </summary>
public RelayCommand OKCommand { get; set; }
/// <summary>
/// Called when the dialog has been shown.
/// </summary>
public virtual void OnShow()
{
IsVisible = true;
}
/// <summary>
/// Invokes the <see cref="Accepted"/> event.
/// </summary>
protected virtual void Accept()
{
IsVisible = false;
DialogResult = true;
Accepted?.Invoke();
}
/// <summary>
/// Invokes the <see cref="Canceled"/> event.
/// </summary>
protected virtual void Cancel()
{
IsVisible = false;
Canceled?.Invoke();
}
/// <summary>
/// Closes the dialog with the specified result.
/// </summary>
/// <param name="result">if set to <c>true</c> accepted.</param>
public void Close(bool result)
{
DialogResult = result;
if (result)
{
Accept();
}
else
{
Cancel();
}
}
}
}