BOB/BOB/ViewModels/StepsManagerViewModel.cs

127 lines
4.6 KiB
C#

using BOB.Models;
using Common.PubEvent;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Xml;
namespace BOB.ViewModels
{
public class StepsManagerViewModel:BindableBase
{
#region
public string Title
{
get => _globalVariables.Title;
set => SetProperty(ref _globalVariables.Title, value);
}
public StepModel SelectedStep
{
get => _globalVariables.SelectedStep;
set => SetProperty(ref _globalVariables.SelectedStep, value);
}
public ProgramModel Program
{
get => _globalVariables.Program;
set => SetProperty(ref _globalVariables.Program, value);
}
public bool IsAdmin
{
get => _globalVariables.IsAdmin;
set => SetProperty(ref _globalVariables.IsAdmin, value);
}
GlobalVariables _globalVariables { get; set; }
private List<StepModel> tmpCopyList = new List<StepModel>();
private IEventAggregator _eventAggregator;
#endregion
public ICommand EditStepCommand { get;set; }
public ICommand CopyStepCommand { get;set; }
public ICommand PasteStepCommand { get;set; }
public ICommand DeleteStepCommand { get;set; }
public StepsManagerViewModel(GlobalVariables _GlobalVariables,IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_globalVariables = _GlobalVariables;
EditStepCommand = new DelegateCommand(EditStep);
CopyStepCommand = new DelegateCommand(CopyStep);
PasteStepCommand = new DelegateCommand(PasteStep);
DeleteStepCommand = new DelegateCommand(DeleteStep);
Program.StepCollection.CollectionChanged += StepCollection_CollectionChanged;
}
#region
private void EditStep()
{
if (IsAdmin && SelectedStep != null&& _globalVariables.SelectedStep!=null)
{
_eventAggregator.GetEvent<EditSetpEvent>().Publish() ;
}
}
private void CopyStep()
{
if (IsAdmin && SelectedStep != null)
{
tmpCopyList.Clear();
tmpCopyList.Add(SelectedStep);
}
}
private void PasteStep()
{
if (IsAdmin && tmpCopyList.Any())
{
int insertIndex = Program.StepCollection.IndexOf(SelectedStep) + 1;
foreach (var item in tmpCopyList)
{
Program.StepCollection.Insert(insertIndex, new StepModel(item) { ID = Guid.NewGuid() });
insertIndex++;
}
}
}
private void DeleteStep()
{
if (IsAdmin && SelectedStep != null)
{
var tmpList = new List<StepModel> { SelectedStep };
_eventAggregator.GetEvent<DeletedStepEvent>().Publish(SelectedStep.ID);
foreach (var item in tmpList)
{
Program.StepCollection.Remove(item);
}
_globalVariables.SelectedStep = null;
}
}
#endregion
#region
private void StepCollection_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
// Add/Move/Remove 都会触发,这里判断具体情形
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add ||
e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Move||
e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
{
// 如果需要等 UI 更新完再处理,可也用 Dispatcher 延迟一小段时间
Application.Current?.Dispatcher.BeginInvoke(new Action(() =>
{
// after drop: 重新编号或其他处理
for (int i = 0; i < Program.StepCollection.Count; i++)
Program.StepCollection[i].Index = i + 1;
// 发布 Prism 事件(如果需要)
// _eventAggregator.GetEvent<StepMovedEvent>().Publish(...);
}), System.Windows.Threading.DispatcherPriority.Background);
}
}
#endregion
}
}