using System; using System.ComponentModel; using System.Threading.Tasks; using System.Windows.Threading; using DTS.Common.Events; using DTS.Common.Interactivity; using Prism.Commands; using Prism.Events; using Prism.Regions; using Unity; // ReSharper disable once CheckNamespace namespace DTS.Common.Base { public abstract class BaseViewModel : BasePropertyChanged, IBaseViewModel where TModel : class { protected IEventAggregator Aggregator { get; private set; } protected IUnityContainer Container { get; private set; } public TModel Model { get; set; } protected BaseViewModel() { } protected BaseViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer) { Aggregator = eventAggregator; Container = unityContainer; CreateCommands(); } protected virtual void CreateCommands() { ConfirmationRequest = new InteractionRequest(); CloseCommand = new DelegateCommand(CloseMethod); } #region Commands /// /// The interaction request to display the confirmation. /// public InteractionRequest ConfirmationRequest { get; private set; } /// /// The delegate command to close object. /// public DelegateCommand CloseCommand { get; private set; } /// /// This is an execute method for the CloseCommand. /// /// The parameter to pass to the CloseCommand. protected virtual void CloseMethod(object parameter) { CleanupAtClose(); } private void CleanupAtClose() { Cleanup(); } #endregion Commands #region Bases Methods /// /// Executes after the viewmodel activated. /// public virtual void Activated() { } /// /// Publishes the ShowStatus during the viewmodel initialization. /// public virtual void Initialize() { Aggregator.GetEvent() .Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading)); } /// /// Publishes the ShowStatus during the viewmodel initialization. /// /// The parameter to be used initialize viewmodel. public virtual void Initialize(object parameter) { Aggregator.GetEvent() .Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading)); } public virtual void Initialize(object parameter, object model) { } /// /// Publishes the ShowStatus during the viewmodel initialization. /// public virtual async Task InitializeAsync() { await Dispatcher.CurrentDispatcher.InvokeAsync(() => { Aggregator.GetEvent() .Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading)); }); } /// /// Publishes the ShowStatus during the viewmodel initialization. /// /// The parameter to be used initialize viewmodel. public virtual async Task InitializeAsync(object parameter) { await Dispatcher.CurrentDispatcher.InvokeAsync(() => { Aggregator.GetEvent() .Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading)); }); } #endregion Bases Methods public new event PropertyChangedEventHandler PropertyChanged; public bool IsMenuIncluded { get; set; } public bool IsNavigationIncluded { get; set; } public int Percentage { get; set; } public string IsBusyMessage { get; set; } public bool IsBusy { get; set; } public bool IsDirty { get; set; } /// /// Sets the Model to null. /// public virtual void Cleanup() { Model = default(TModel); } public Task CleanupAsync() { return Task.CompletedTask; } } }