using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core.Commands; using Tango.BL.Entities; using Tango.MachineStudio.Common.Notifications; using Tango.SharedUI; using Tango.BL; namespace Tango.MachineStudio.HardwareDesigner.ViewModels { public class MainViewVM : ViewModel { private INotificationProvider _notification; private bool _isNew; private ObservablesEntitiesAdapter _adapter; public ObservablesEntitiesAdapter Adapter { get { return _adapter; } set { _adapter = value; RaisePropertyChangedAuto(); } } private HardwareVersion _selectedVersion; public HardwareVersion SelectedVersion { get { return _selectedVersion; } set { _selectedVersion = value; RaisePropertyChangedAuto(); OnSelectedVersionChanged(); } } private HardwareVersion _currentVersion; public HardwareVersion CurrentVersion { get { return _currentVersion; } set { _currentVersion = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private IObservableEntity _selectedHardwareObject; public IObservableEntity SelectedHardwareObject { get { return _selectedHardwareObject; } set { _selectedHardwareObject = null; RaisePropertyChangedAuto(); _selectedHardwareObject = value; RaisePropertyChangedAuto(); } } public RelayCommand SaveCommand { get; set; } public RelayCommand DeleteCommand { get; set; } public RelayCommand NewCommand { get; set; } public MainViewVM(INotificationProvider notification) { _notification = notification; Adapter = ObservablesEntitiesAdapter.Instance; SaveCommand = new RelayCommand(Save, () => CurrentVersion != null); NewCommand = new RelayCommand(New); DeleteCommand = new RelayCommand(Delete, () => !_isNew && CurrentVersion != null); } private void OnSelectedVersionChanged() { if (SelectedVersion != null) { _isNew = false; CurrentVersion = SelectedVersion.Clone(); } InvalidateRelayCommands(); } public void OnRemoveDancer(HardwareDancer dancer) { CurrentVersion.HardwareDancers.Remove(dancer); } public void OnRemoveMotor(HardwareMotor motor) { CurrentVersion.HardwareMotors.Remove(motor); } public void OnRemovePidControl(HardwarePidControl pidControl) { CurrentVersion.HardwarePidControls.Remove(pidControl); } public void OnRemoveWinder(HardwareWinder hardwareWinder) { CurrentVersion.HardwareWinders.Remove(hardwareWinder); } public void OnMotorDrop(HardwareMotorType motorType) { if (CheckCurrentVersionNull()) return; if (!CurrentVersion.HardwareMotors.ToList().Exists(x => x.HardwareMotorType == motorType)) { CurrentVersion.HardwareMotors.Add(new HardwareMotor() { HardwareMotorType = motorType }); } } public void OnDropDancer(HardwareDancerType dancerType) { if (CheckCurrentVersionNull()) return; if (!CurrentVersion.HardwareDancers.ToList().Exists(x => x.HardwareDancerType == dancerType)) { CurrentVersion.HardwareDancers.Add(new HardwareDancer() { HardwareDancerType = dancerType }); } } public void OnDropPidControl(HardwarePidControlType pidControlType) { if (CheckCurrentVersionNull()) return; if (!CurrentVersion.HardwarePidControls.ToList().Exists(x => x.HardwarePidControlType == pidControlType)) { CurrentVersion.HardwarePidControls.Add(new HardwarePidControl() { HardwarePidControlType = pidControlType }); } } public void OnDropWinder(HardwareWinderType hardwareWinderType) { if (CheckCurrentVersionNull()) return; if (!CurrentVersion.HardwareWinders.ToList().Exists(x => x.HardwareWinderType == hardwareWinderType)) { CurrentVersion.HardwareWinders.Add(new HardwareWinder() { HardwareWinderType = hardwareWinderType }); } } private bool CheckCurrentVersionNull() { if (CurrentVersion == null) { _notification.ShowInfo("Please select a hardware version before attempting to insert any components."); return true; } return false; } private void New() { String name = _notification.ShowTextInput("Enter hardware version name", "Name"); if (!String.IsNullOrWhiteSpace(name)) { SelectedVersion = null; CurrentVersion = new HardwareVersion(); CurrentVersion.Version = Adapter.HardwareVersions.Max(x => x.Version) + 1; CurrentVersion.Name = name; _isNew = true; InvalidateRelayCommands(); } } private async void Save() { if (CurrentVersion != null) { using (_notification.PushTaskItem("Saving hardware version...")) { HardwareVersion realVersion = null; if (_isNew) { realVersion = CurrentVersion; } else { realVersion = Adapter.HardwareVersions.SingleOrDefault(x => x.Guid == SelectedVersion.Guid); realVersion.Version = CurrentVersion.Version; realVersion.Name = CurrentVersion.Name; realVersion.HardwareDancers.ToList().ForEach(x => x.DefferedDelete(Adapter.Context)); realVersion.HardwareMotors.ToList().ForEach(x => x.DefferedDelete(Adapter.Context)); realVersion.HardwarePidControls.ToList().ForEach(x => x.DefferedDelete(Adapter.Context)); realVersion.HardwareWinders.ToList().ForEach(x => x.DefferedDelete(Adapter.Context)); realVersion.HardwareDancers.Clear(); realVersion.HardwareMotors.Clear(); realVersion.HardwarePidControls.Clear(); realVersion.HardwareWinders.Clear(); foreach (var item in CurrentVersion.HardwareDancers.ToList().Select(x => x.Clone())) { item.HardwareVersionGuid = realVersion.Guid; realVersion.HardwareDancers.Add(item); } foreach (var item in CurrentVersion.HardwareMotors.ToList().Select(x => x.Clone())) { item.HardwareVersionGuid = realVersion.Guid; realVersion.HardwareMotors.Add(item); } foreach (var item in CurrentVersion.HardwarePidControls.ToList().Select(x => x.Clone())) { item.HardwareVersionGuid = realVersion.Guid; realVersion.HardwarePidControls.Add(item); } foreach (var item in CurrentVersion.HardwareWinders.ToList().Select(x => x.Clone())) { item.HardwareVersionGuid = realVersion.Guid; realVersion.HardwareWinders.Add(item); } } if (_isNew) { Adapter.Context.HardwareVersions.Add(realVersion); } await realVersion.SaveAsync(Adapter.Context); SelectedVersion = Adapter.HardwareVersions.SingleOrDefault(x => x.Guid == realVersion.Guid); } } } private void Delete() { if (_notification.ShowQuestion("Are you sure you want to delete this hardware version?")) { using (_notification.PushTaskItem("Deleting hardware version...")) { SelectedVersion.DeleteAsync(Adapter.Context); SelectedVersion = null; CurrentVersion = null; } } } } }