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 : 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(); 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 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 #region Methods #region PropertyChanged /// ///Occurs when a property value changes. /// 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 /// /// Sets the Model to null. /// public virtual void Cleanup() { Model = default(TModel); } public Task CleanupAsync() { return Task.CompletedTask; } } }