Files
DP44/Common/DTS.Common/RibbonControl/ViewModel/RibbonViewModel.cs
2026-04-17 14:55:32 -04:00

187 lines
6.0 KiB
C#

using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Threading;
using DTS.Common.Base;
using DTS.Common.Events;
using Prism.Commands;
using Prism.Events;
using Unity;
using Prism.Regions;
using DTS.Common.Interactivity;
namespace DTS.Common.RibbonControl
{
public class RibbonViewModel<TModel> : BasePropertyChanged, IRibbonViewModel
where TModel : class
{
protected IEventAggregator Aggregator { get; }
protected IUnityContainer Container { get; }
protected IRegionManager RegionManager { get; }
public TModel Model { get; set; }
public IRibbonView View { get; }
protected RibbonViewModel(IRibbonView view, IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer)
{
View = view;
Aggregator = eventAggregator;
Container = unityContainer;
RegionManager = regionManager;
CreateCommands();
}
protected virtual void CreateCommands()
{
ConfirmationRequest = new InteractionRequest<Confirmation>();
CloseCommand = new DelegateCommand<object>(CloseMethod);
}
#region Commands
/// <summary>
/// The interaction request to display the confirmation.
/// </summary>
public InteractionRequest<Confirmation> ConfirmationRequest { get; private set; }
/// <summary>
/// The delegate command to close object.
/// </summary>
public DelegateCommand<object> CloseCommand { get; private set; }
/// <summary>
/// This is an execute method for the <see cref="CloseCommand">CloseCommand</see>.
/// </summary>
/// <param name="parameter">The parameter to pass to the <see cref="CloseCommand">CloseCommand</see>.</param>
protected virtual void CloseMethod(object parameter)
{
CleanupAtClose();
}
private void CleanupAtClose()
{
Cleanup();
}
#endregion Commands
#region Bases Methods
/// <summary>
/// Executes after the viewmodel activated.
/// </summary>
public virtual void Activated()
{
}
/// <summary>
/// Publishes the <see cref="ShowStatus">ShowStatus</see> during the viewmodel initialization.
/// </summary>
public virtual void Initialize()
{
Aggregator.GetEvent<ShowStatus>()
.Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading));
}
/// <summary>
/// Publishes the <see cref="ShowStatus">ShowStatus</see> during the viewmodel initialization.
/// </summary>
/// <param name="parameter">The parameter to be used initialize viewmodel.</param>
public virtual void Initialize(object parameter)
{
Aggregator.GetEvent<ShowStatus>()
.Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading));
}
public void Initialize(object parameter, object model)
{
}
/// <summary>
/// Publishes the <see cref="ShowStatus">ShowStatus</see> during the viewmodel initialization.
/// </summary>
public virtual async Task InitializeAsync()
{
await Dispatcher.CurrentDispatcher.InvokeAsync(() =>
{
Aggregator.GetEvent<ShowStatus>()
.Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading));
});
}
/// <summary>
/// Publishes the <see cref="ShowStatus">ShowStatus</see> during the viewmodel initialization.
/// </summary>
/// <param name="parameter">The parameter to be used initialize viewmodel.</param>
public virtual async Task InitializeAsync(object parameter)
{
await Dispatcher.CurrentDispatcher.InvokeAsync(() =>
{
Aggregator.GetEvent<ShowStatus>()
.Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading));
});
}
#endregion Bases Methods
#region Methods
#region PropertyChanged
///<summary>
///Occurs when a property value changes.
///</summary>
public new event PropertyChangedEventHandler PropertyChanged;
private new void OnPropertyChanged(string propertyName)
{
var eventHandler = PropertyChanged;
if (eventHandler != null)
{
eventHandler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion PropertyChanged
#endregion Methods
#region Properties
private bool _isMenuIncluded = true;
public bool IsMenuIncluded
{
get => _isMenuIncluded;
set
{
_isMenuIncluded = value;
OnPropertyChanged("IsMenuIncluded");
}
}
private bool _isNavigationIncluded = false;
public bool IsNavigationIncluded
{
get => _isNavigationIncluded;
set
{
_isNavigationIncluded = value;
OnPropertyChanged("IsNavigationIncluded");
}
}
public int Percentage { get; set; }
public string IsBusyMessage { get; set; }
public bool IsBusy { get; set; }
public bool IsDirty { get; set; }
#endregion Properties
/// <summary>
/// Sets the Model to null.
/// </summary>
public virtual void Cleanup()
{
Model = default(TModel);
}
public Task CleanupAsync()
{
return Task.CompletedTask;
}
}
}