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
{
///
/// Represents a dialog view model base class.
///
///
public abstract class DialogViewVM : ViewModel
{
public event Action Accepted;
public event Action Canceled;
///
/// Initializes a new instance of the class.
///
public DialogViewVM()
{
CanClose = true;
CloseCommand = new RelayCommand(Cancel, (x) => CanClose);
OKCommand = new RelayCommand(Accept, (x) => CanOK());
}
private bool _canClose;
///
/// Gets or sets a value indicating whether this dialog can be closed.
///
public bool CanClose
{
get { return _canClose; }
set { _canClose = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
}
private bool _isVisible;
///
/// Gets or sets a value indicating whether this instance is visible.
///
public bool IsVisible
{
get { return _isVisible; }
set { _isVisible = value; RaisePropertyChangedAuto(); }
}
///
/// Determines whether this instance can invoke the OK command.
///
protected virtual bool CanOK()
{
return true;
}
///
/// Gets a value indicating whether the dialog has been confirmed.
///
public bool DialogResult { get; private set; }
///
/// Gets or sets the close command.
///
public RelayCommand CloseCommand { get; set; }
///
/// Gets or sets the OK command.
///
public RelayCommand OKCommand { get; set; }
///
/// Called when the dialog has been shown.
///
public virtual void OnShow()
{
IsVisible = true;
}
///
/// Invokes the event.
///
protected virtual void Accept()
{
IsVisible = false;
DialogResult = true;
Accepted?.Invoke();
}
///
/// Invokes the event.
///
protected virtual void Cancel()
{
IsVisible = false;
Canceled?.Invoke();
}
///
/// Closes the dialog with the specified result.
///
/// if set to true accepted.
public void Close(bool result)
{
DialogResult = result;
if (result)
{
Accept();
}
else
{
Cancel();
}
}
}
}