From 8a0b8f6abe3d143b43131a330e0ee39c2547ce8f Mon Sep 17 00:00:00 2001 From: Victoria Plitt Date: Sun, 4 Jul 2021 15:25:08 +0300 Subject: After Virus --- .../ViewModels/MainViewVM.cs | 555 ++++++++++++--------- 1 file changed, 324 insertions(+), 231 deletions(-) (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/MainViewVM.cs') diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/MainViewVM.cs index 3fde7abbe..09befb26f 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/MainViewVM.cs @@ -25,6 +25,8 @@ using Tango.BL.DTO; using Tango.BL.Enumerations; using Tango.MachineStudio.ThreadExtensions.Contracts; using Tango.MachineStudio.ThreadExtensions.Views; +using Tango.MachineStudio.ThreadExtensions.Models; +using Tango.Settings; namespace Tango.MachineStudio.ThreadExtensions.ViewModels { @@ -33,34 +35,55 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels private INotificationProvider _notification; private IAuthenticationProvider _authentication; private IActionLogManager _actionLogManager; - private RmlsExtensionDTO _rmlExtensionBeforeSave; - + private ObservablesContext _rmlExtentions_context; private ObservablesContext _active_context; + private List _allUsers; + #region properties - private ObservableCollection _rmlsExtension; - public ObservableCollection RmlsExtensions - { - get { return _rmlsExtension; } - set { _rmlsExtension = value; + //private ObservableCollection _rmlsExtension; + //public ObservableCollection RmlsExtensions + //{ + // get { return _rmlsExtension; } + // set { _rmlsExtension = value; + // RaisePropertyChangedAuto(); } + //} + + private List _rmlExtensions; + public List RmlExtensions + { + get { return _rmlExtensions; } + set { _rmlExtensions = value; RaisePropertyChangedAuto(); } } - - private RmlsExtension _activeRMLExtention; - public RmlsExtension ActiveRMLExtention + private RmlsExtension _ActiveRMLExtension; + public RmlsExtension ActiveRMLExtension { - get { return _activeRMLExtention; } - set { _activeRMLExtention = value; + get { return _ActiveRMLExtension; } + set { _ActiveRMLExtension = value; RaisePropertyChangedAuto(); } } - private RmlsExtension _selectedRMLExtension; - public RmlsExtension SelectedRMLExtension + private Rml _activeRML; + public Rml ActiveRML + { + get { return _activeRML; } + set + { + _activeRML = value; + RaisePropertyChangedAuto(); + } + } + + private RmlExtensionModel _selectedRMLExtension; + public RmlExtensionModel SelectedRMLExtension { get { return _selectedRMLExtension; } - set { _selectedRMLExtension = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } + set { _selectedRMLExtension = value; + RaisePropertyChangedAuto(); + InvalidateRelayCommands(); } } private ICollectionView _rmlExtCollectionView; @@ -91,20 +114,26 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels set { _brands = value; RaisePropertyChangedAuto(); } } - private ObservableCollection _yarnColor; - public ObservableCollection YarnColor + private ObservableCollection _yarnWhiteShade; + public ObservableCollection YarnWhiteShade { - get { return _yarnColor; } - set { _yarnColor = value; RaisePropertyChangedAuto(); } + get { return _yarnWhiteShade; } + set { _yarnWhiteShade = value; RaisePropertyChangedAuto(); } } - - private ObservableCollection _enduse; - public ObservableCollection EndUse + private ObservableCollection _enduse; + public ObservableCollection EndUse { get { return _enduse; } set { _enduse = value; RaisePropertyChangedAuto(); } } - + + private ObservableCollection _units; + public ObservableCollection Units + { + get { return _units; } + set { _units = value; RaisePropertyChangedAuto(); } + } + private ObservableCollection _family; public ObservableCollection Family { @@ -112,8 +141,8 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels set { _family = value; RaisePropertyChangedAuto(); } } - private ObservableCollection _geometry; - public ObservableCollection Geometry + private ObservableCollection _geometry; + public ObservableCollection Geometry { get { return _geometry; } set { _geometry = value; RaisePropertyChangedAuto(); } @@ -133,20 +162,30 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels set { _group = value; RaisePropertyChangedAuto(); } } - private ObservableCollection _manufacturer; - public ObservableCollection Manufacturer + //private ObservableCollection _manufacturer; + //public ObservableCollection Manufacturer + //{ + // get { return _manufacturer; } + // set { _manufacturer = value; RaisePropertyChangedAuto(); } + //} + private List _manufacturers; + + public List Manufacturers { - get { return _manufacturer; } - set { _manufacturer = value; RaisePropertyChangedAuto(); } + get { return _manufacturers; } + set { _manufacturers = value; + RaisePropertyChangedAuto(); + } } - private ObservableCollection _materials; - public ObservableCollection Materials + + private ObservableCollection _materials; + public ObservableCollection Materials { get { return _materials; } set { _materials = value; RaisePropertyChangedAuto(); } } - + private ObservableCollection _subFamilies; public ObservableCollection SubFamilies { @@ -185,9 +224,9 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels set { _Filter = value; RaisePropertyChangedAuto(); OnFilterChanged(); } } - private void OnFilterChanged() + private async void OnFilterChanged() { - RmlExtCollectionView.Refresh(); + await LoadRmlExtentions(); } private ColorParametersVewVM _colorParametersVewVM; @@ -196,97 +235,69 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels get { return _colorParametersVewVM; } set { _colorParametersVewVM = value; RaisePropertyChangedAuto(); } } - #endregion - #region commands - - public RelayCommand SaveCommand { get; set; } - - public RelayCommand ManageRmlExtensionCommand { get; set; } - - public RelayCommand AddRmlExtCommand { get; set; } - - public RelayCommand RemoveRmlExtensionCommand { get; set; } - - public RelayCommand CloneRmlExtensionCommand { get; set; } - - public RelayCommand BackToThreadExtensionViewsCommand { get; set; } - - - private void BackToThreadExtensionViews(object obj) + private TestResultsViewVM _testResultsViewVM; + public TestResultsViewVM TestResultsViewVM { - View.NavigateTo(ThreadExtensionNavigationView.ThreadExtensionsView); - LoadRmlExtentions(); + get { return _testResultsViewVM; } + set { _testResultsViewVM = value; RaisePropertyChangedAuto(); } } - private async void CloneSelectedRmlExtension(object obj) + protected Machine _selectedMachine; + /// + /// Gets or sets the selected machine. + /// + public Machine SelectedMachine { - using (_notification.PushTaskItem("Cloning thread...")) + get { return _selectedMachine; } + set { - try - { - IsFree = false; - - using (var context = ObservablesContext.CreateDefault()) - { - RmlsExtension rml_extention = await new RmlExtensionsBuilder(_rmlExtentions_context).Set(SelectedRMLExtension.Guid).WithUser().BuildAsync(); - - RmlsExtension cloned = new RmlsExtension(); - rml_extention.MapPropertiesTo(cloned, MappingFlags.NoReferenceTypes); - - cloned.Guid = Guid.NewGuid().ToString(); - cloned.ID = 0; - - - context.RmlsExtensions.Add(cloned); - await context.SaveChangesAsync(); - - //_actionLogManager.InsertLog(BL.Enumerations.ActionLogType.RmlCreated, _authentication.CurrentUser, cloned.Name, cloned, "RML cloned from Machine Studio."); - } - - LoadRmlExtentions(); - } - catch (Exception ex) + if (value != null && _selectedMachine != value) { - LogManager.Log(ex, "Error cloning thread."); - _notification.ShowError($"An error occurred while trying to clone the selected thread\n{ex.Message}"); - } - finally - { - IsFree = true; + _selectedMachine = value; + SelectedMachineChanged(); + RaisePropertyChangedAuto(); + InvalidateRelayCommands(); } } } - private async void RemoveRmlExtension(object obj) + private ObservableCollection _machines; + /// + /// Gets or sets the Machines. + /// + public ObservableCollection Machines { - if (_notification.ShowQuestion(" Are you sure you want to delete the selected RML Extension?")) + get { - using (_notification.PushTaskItem("Removing RML Extension...")) - { - try - { - IsFree = false; - - await SelectedRMLExtension.DeleteCascadeAsync(_rmlExtentions_context); - //_actionLogManager.InsertLog(BL.Enumerations.ActionLogType.RmlDeleted, _authentication.CurrentUser, SelectedRML.Name, SelectedRML, "RML deleted from Machine Studio."); + return _machines; + } - LoadRmlExtentions(); - } - catch (Exception ex) - { - LogManager.Log(ex, $"Error removing selected RML {SelectedRMLExtension?.Name}."); - _notification.ShowError($"An error occurred while trying to remove the selected RML Extension.\n{ex.FlattenMessage()}"); - LoadRmlExtentions(); - } - finally - { - IsFree = true; - } - } + set + { + _machines = value; RaisePropertyChanged(nameof(Machines)); } + } + #endregion + + #region commands + + public RelayCommand SaveCommand { get; set; } + + public RelayCommand ManageRmlExtensionCommand { get; set; } + + public RelayCommand BackToThreadExtensionViewsCommand { get; set; } + + private async void BackToThreadExtensionViews(object obj) + { + View.NavigateTo(RMLExtensionNavigationView.RMLExtensionsView); + await LoadRmlExtentions(); + } + + #endregion + public MainViewVM(INotificationProvider notificationProvider, IAuthenticationProvider authentication, IActionLogManager actionLogManager) { @@ -296,21 +307,20 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels BackToThreadExtensionViewsCommand = new RelayCommand(BackToThreadExtensionViews, () => IsFree); SaveCommand = new RelayCommand(Save, () => IsFree); - ManageRmlExtensionCommand = new RelayCommand(() => LoadActiveRMLExtention(SelectedRMLExtension.Guid), () => SelectedRMLExtension != null); - RemoveRmlExtensionCommand = new RelayCommand(RemoveRmlExtension, () => SelectedRMLExtension != null); - CloneRmlExtensionCommand = new RelayCommand(CloneSelectedRmlExtension, () => SelectedRMLExtension != null); - AddRmlExtCommand = new RelayCommand(AddNewRmlExtention); + ManageRmlExtensionCommand = new RelayCommand(() => LoadActiveRMLExtension(SelectedRMLExtension.Guid), () => SelectedRMLExtension != null); } public override void OnApplicationReady() { - LoadRmlExtentions(); } + #region Loading - private async void LoadRmlExtentions() + private async Task LoadRmlExtentions() { + var filter = Filter.ToStringOrEmpty().ToLower(); + try { IsFree = false; @@ -320,19 +330,36 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels if (_rmlExtentions_context != null) _rmlExtentions_context.Dispose(); _rmlExtentions_context = ObservablesContext.CreateDefault(); - RmlsExtensions = await new RMLExtentionsCollectionBuilder(_rmlExtentions_context).SetAll().WithUser().WithYarnProperties().BuildAsync(); - - - RmlExtCollectionView = CollectionViewSource.GetDefaultView(RmlsExtensions); - RmlExtCollectionView.SortDescriptions.Add(new SortDescription(nameof(Rml.LastUpdated), ListSortDirection.Descending)); - - RmlExtCollectionView.Filter = (rml) => - { - RmlsExtension r = rml as RmlsExtension; - return String.IsNullOrWhiteSpace(Filter) - || r.Name.ToLower().Contains(Filter.ToLower()); - }; + Brands = _rmlExtentions_context.YarnBrands.ToObservableCollection(); + _allUsers = await _rmlExtentions_context.Users.Include(x => x.Contact).ToListAsync(); + var q = (from c in _rmlExtentions_context.Rmls.Where(x => x.Name.ToLower().Contains(filter)) + join p in _rmlExtentions_context.RmlsExtensions on c.Guid equals p.RmlsGuid into ps + from p in ps.DefaultIfEmpty() + select new { RML = c, RMLExtesion = p }).Distinct().ToList().DistinctBy(x => x.RML.Guid) + .Select(x => new RmlExtensionModel() + { + RMLGuid = x.RML.Guid, + Guid = x.RMLExtesion == null? null : x.RMLExtesion.Guid, + Name = x.RML.Name, + Manufacturer = x.RML.Manufacturer, + Brand = x.RMLExtesion == null ? "" : (Brands.Where( y => y.Guid == x.RMLExtesion.YarnBrandGuid).Select( z => z.Name).FirstOrDefault()), + LinearDensity = (int) x.RML.FiberSize, + CreatedBy = x.RMLExtesion == null ? "" : _allUsers.SingleOrDefault(y => y.Guid == x.RMLExtesion.UsersGuid).Contact.FullName, + Created = x.RMLExtesion == null ? DateTime.Now : x.RMLExtesion.Created, + LastUpdated = x.RMLExtesion == null ? DateTime.Now : x.RMLExtesion.LastUpdated, + Status = x.RMLExtesion == null ? RMLExtensionStatus.New : x.RMLExtesion.RMLStatus + }).ToList(); + RmlExtensions = q; + RmlExtCollectionView = CollectionViewSource.GetDefaultView(RmlExtensions); + RmlExtCollectionView.SortDescriptions.Add(new SortDescription(nameof(Rml.LastUpdated), ListSortDirection.Ascending)); + + //RmlExtCollectionView.Filter = (rml) => + //{ + // RmlExtensionModel r = rml as RmlExtensionModel; + // return String.IsNullOrWhiteSpace(Filter) + // || r.Name.ToLower().Contains(Filter.ToLower()); + //}; } } catch (Exception ex) @@ -348,84 +375,68 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels private void LoadRmlProperties() { Applications = _active_context.YarnApplications.ToObservableCollection(); - Brands = _active_context.YarnBrand.ToObservableCollection(); - YarnColor = _active_context.YarnColor.ToObservableCollection(); - EndUse = _active_context.YarnEndUse.ToObservableCollection(); - Family = _active_context.YarnFamily.ToObservableCollection(); - Geometry = _active_context.YarnGeometry.ToObservableCollection(); - GlossLevel = _active_context.YarnGlossLevel.ToObservableCollection(); - Group = _active_context.YarnGroup.ToObservableCollection(); - Manufacturer = _active_context.YarnManufacturer.ToObservableCollection(); - - Materials = _active_context.YarnMaterials.ToObservableCollection(); - SubFamilies = _active_context.YarnSubFamily.ToObservableCollection(); - Texturing = _active_context.YarnTexturing.ToObservableCollection(); - YarnTypes = _active_context.YarnType.ToObservableCollection(); - IndustrySector = _active_context.YarnIndustrysector.ToObservableCollection(); - } - - private async void AddNewRmlExtention(object obj) - { - using (_notification.PushTaskItem("Creating new RML Extension...")) + Brands = _active_context.YarnBrands.ToObservableCollection(); + YarnWhiteShade = _active_context.YarnWhiteShades.ToObservableCollection(); + EndUse = _active_context.MediaPurposes.ToObservableCollection(); + Family = _active_context.YarnFamilies.ToObservableCollection(); + Geometry = _active_context.FiberShapes.ToObservableCollection(); + GlossLevel = _active_context.YarnGlossLevels.ToObservableCollection(); + Group = _active_context.YarnGroups.ToObservableCollection(); + //Manufacturer = _active_context.YarnManufacturers.ToObservableCollection(); + Manufacturers = _active_context.YarnManufacturers.Select(x => x.Name).ToList(); + Units = _active_context.LinearMassDensityUnits.ToObservableCollection(); + Materials = _active_context.MediaMaterials.ToObservableCollection(); + SubFamilies = _active_context.YarnSubFamilies.ToObservableCollection(); + Texturing = _active_context.YarnTexturings.ToObservableCollection(); + YarnTypes = _active_context.YarnTypes.ToObservableCollection(); + IndustrySector = _active_context.YarnIndustrysectors.ToObservableCollection(); + Machines = ObservablesStaticCollections.Instance.Machines.Select(x => new MachineModel() { - IsFree = false; - - if (_active_context != null) - { - _active_context.Dispose(); - } - - _active_context = ObservablesContext.CreateDefault(); - - LoadRmlProperties(); - - RmlsExtension rml_extention = new RmlsExtension(); - rml_extention.Created = DateTime.UtcNow; - rml_extention.UserGuid = _authentication.CurrentUser.Guid; - rml_extention.YarnManufacturer = Manufacturer.FirstOrDefault(); - rml_extention.YarnBrand = Brands.FirstOrDefault(); - rml_extention.Country = null; - rml_extention.YarnEndUse = EndUse.FirstOrDefault(); - rml_extention.YarnApplications = Applications.FirstOrDefault(); - rml_extention.YarnIndustrysector = IndustrySector.FirstOrDefault(); - - rml_extention.YarnMaterial = Materials.FirstOrDefault(); - rml_extention.YarnType = YarnTypes.FirstOrDefault(); - rml_extention.YarnSubFamily = SubFamilies.FirstOrDefault(); - rml_extention.YarnFamily = Family.FirstOrDefault(); - rml_extention.YarnGroup = Group.FirstOrDefault(); - rml_extention.YarnTexturing = Texturing.FirstOrDefault(); - rml_extention.YarnGeometry = Geometry.FirstOrDefault(); - rml_extention.YarnColor = YarnColor.FirstOrDefault(); - rml_extention.YarnGlossLevel = GlossLevel.FirstOrDefault(); - rml_extention.LinearDensity = 0; - rml_extention.YarnUnit = YarnUnits.DTEX; - rml_extention.YarnPlies = Plies.P1; - rml_extention.FilamentCount = 0; - rml_extention.TwistTpm = 0; - rml_extention.YarnTwistDirections = TwistDirections.S; - rml_extention.MinElongation = 0.0; - rml_extention.MaxElongation = 100.0; - rml_extention.MinMaxForceN = 0.0; - rml_extention.MaxMaxForceN = 100.0; - rml_extention.MinElasticity = 0.0; - rml_extention.MaxElasticity = 100.0; - rml_extention.MinTenacity = 0.0; - rml_extention.MaxTenacity = 100.0; - rml_extention.Finishing = "Lubrication"; - - _active_context.RmlsExtensions.Add(rml_extention); - await _active_context.SaveChangesAsync(); - - //_actionLogManager.InsertLog(BL.Enumerations.ActionLogType.RmlCreated, _authentication.CurrentUser, rml.Name, rml, "Rml created using Machine Studio."); - ColorParametersVewVM = new ColorParametersVewVM(_notification, _actionLogManager); - LoadActiveRMLExtention(rml_extention.Guid); + Guid = x.Guid, + Name = x.Name, + SerialNumber = x.SerialNumber + }).ToObservableCollection(); - IsFree = true; - } } - private async void LoadActiveRMLExtention(String guid) + private RmlsExtension GetNewRMLsExtension( string RML_Guid) + { + RmlsExtension rml_extention = new RmlsExtension(); + rml_extention.Created = DateTime.UtcNow; + rml_extention.RmlsGuid = RML_Guid; + rml_extention.UsersGuid = _authentication.CurrentUser.Guid; + //rml_extention.YarnManufacturer = Manufacturer.FirstOrDefault(); + rml_extention.YarnBrand = Brands.FirstOrDefault(); + rml_extention.Country = null; + //rml_extention.YarnEndUse = EndUse.FirstOrDefault(); + rml_extention.YarnApplication = Applications.FirstOrDefault(); + rml_extention.YarnIndustrysector = IndustrySector.FirstOrDefault(); + + //rml_extention.YarnMaterial = Materials.FirstOrDefault(); + rml_extention.YarnType = YarnTypes.FirstOrDefault(); + rml_extention.YarnSubFamily = SubFamilies.FirstOrDefault(); + rml_extention.YarnFamily = Family.FirstOrDefault(); + rml_extention.YarnGroup = Group.FirstOrDefault(); + rml_extention.YarnTexturing = Texturing.FirstOrDefault(); + rml_extention.YarnWhiteShade = YarnWhiteShade.FirstOrDefault(); + rml_extention.YarnGlossLevel = GlossLevel.FirstOrDefault(); + rml_extention.TwistTpm = 0; + rml_extention.YarnTwistDirections = TwistDirections.Unknown; + rml_extention.MinElongation = 0.0; + rml_extention.MaxElongation = 100.0; + rml_extention.MinMaxForceN = 0.0; + rml_extention.MaxMaxForceN = 100.0; + rml_extention.MinElasticity = 0.0; + rml_extention.MaxElasticity = 100.0; + rml_extention.MinTenacity = 0.0; + rml_extention.MaxTenacity = 100.0; + rml_extention.Finishing = "Lubrication"; + rml_extention.RMLStatus = RMLExtensionStatus.New; + + return rml_extention; + } + + private async void LoadActiveRMLExtension(String guid) { using (_notification.PushTaskItem("Loading RML Extension...")) { @@ -439,17 +450,60 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels } _active_context = ObservablesContext.CreateDefault(); - LoadRmlProperties(); - ActiveRMLExtention = await new RmlExtensionsBuilder(_active_context) - .Set(guid) - .WithUser() - .BuildAsync(); + if (guid == null) + { + RmlsExtension rml_extention = GetNewRMLsExtension(SelectedRMLExtension.RMLGuid); + _active_context.RmlsExtensions.Add(rml_extention); + await _active_context.SaveChangesAsync(); + guid = rml_extention.Guid; + } + + ActiveRMLExtension = await new RmlExtensionsBuilder(_active_context) + .Set(guid) + .WithUser() + .BuildAsync(); + + ActiveRML = new RmlBuilder(_active_context) + .Set(SelectedRMLExtension.RMLGuid) + .Build(); + + var machinesWithTest = _active_context.RmlExtensionTestResults.Select(x => x.MachineGuid).ToList(); + var machinesWithTest2 = + Join(_active_context.ColorProcessParameters.Select(y => y.MachineGuid), st1 => st1, st2=> st2, (st1, st2)=>).ToList(); + Machines = ObservablesStaticCollections.Instance.Machines.Select(x => new MachineModel() + { + Guid = x.Guid, + Name = x.Name, + SerialNumber = x.SerialNumber + }).ToObservableCollection(); + var settings = SettingsManager.Default.GetOrCreate(); + SelectedMachine = ObservablesStaticCollections.Instance.Machines.SingleOrDefault(x => x.SerialNumber == settings.LastVirtualMachineSerialNumber); ColorParametersVewVM = new ColorParametersVewVM(_notification, _actionLogManager); - ColorParametersVewVM.LoadColorParameters(guid); - View.NavigateTo(ThreadExtensionNavigationView.ThreadExtentionView); + ColorParametersVewVM.RMLExtemtionGUID = guid; + ColorParametersVewVM.SelectedMachineGUID = SelectedMachine.Guid; + + TestResultsViewVM = new TestResultsViewVM(_notification, _actionLogManager); + TestResultsViewVM.RMLExtemtionGUID = guid; + TestResultsViewVM.ThreadName = ActiveRML.Manufacturer; + + var machineGuidsWithTests = await _active_context.RmlExtensionTestResults.Where(x => x.RmlsExtensionsGuid == guid).Select(y => y.MachineGuid).ToListAsync(); + if(machineGuidsWithTests.Count > 0) + { + + } + + if (ActiveRMLExtension.RMLStatus == RMLExtensionStatus.New) + { + ColorParametersVewVM.SaveColorParameters -= UpdateStatus; + ColorParametersVewVM.SaveColorParameters += UpdateStatus; + TestResultsViewVM.SaveTestResults -= UpdateStatus; + TestResultsViewVM.SaveTestResults += UpdateStatus; + } + + View.NavigateTo(RMLExtensionNavigationView.RMLExtentionView); InvalidateRelayCommands(); @@ -466,22 +520,18 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels } } } - - private async void RefreshView(String guid) + + private async void RefreshView(String guid, String rmlGuid) { try { IsFree = false; - - //if (_active_context != null) - //{ - // _active_context.Dispose(); - //} - - //_active_context = ObservablesContext.CreateDefault(); - + LoadRmlProperties(); - ActiveRMLExtention = await new RmlExtensionsBuilder(_active_context) + ActiveRML = ActiveRML = await new RmlBuilder(_active_context) + .Set(rmlGuid) + .BuildAsync(); + ActiveRMLExtension = await new RmlExtensionsBuilder(_active_context) .Set(guid) .WithUser() .BuildAsync(); @@ -493,7 +543,7 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels catch (Exception ex) { //LogManager.Log($"Error loading RML '{ActiveRML.Name}'..."); - _notification.ShowError($"Error refresh the selected thread.\n{ex.FlattenMessage()}"); + _notification.ShowError($"Error refresh after save the selected thread.\n{ex.FlattenMessage()}"); } finally { @@ -503,34 +553,75 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels #endregion - private async void Save() + #region event handlers + + private async void UpdateStatus(object sender, EventArgs e) { IsFree = false; try { - using (_notification.PushTaskItem("Saving RML Extension...")) - { + ColorParametersVewVM.SaveColorParameters -= UpdateStatus; + TestResultsViewVM.SaveTestResults -= UpdateStatus; + ActiveRMLExtension.RMLStatus = RMLExtensionStatus.InProgress; + ActiveRMLExtension.LastUpdated = DateTime.UtcNow; - ActiveRMLExtention.LastUpdated = DateTime.UtcNow; + await _active_context.SaveChangesAsync(); + } + catch (Exception ex) + { + LogManager.Log(ex, $"Error saving Status of RML Extension"); + _notification.ShowError($"An error occurred while trying to save status of the current RML Extension.\n{ex.FlattenMessage()}"); + } + finally + { + IsFree = true; + } + } - ColorParametersVewVM.Save(); + private void SelectedMachineChanged() + { + if(ColorParametersVewVM != null) + { + ColorParametersVewVM.SelectedMachineGUID = SelectedMachine.Guid; + } + } - var rmlExtensionAfter = RmlsExtensionDTO.FromObservable(ActiveRMLExtention); + #endregion - await _active_context.SaveChangesAsync(); + #region Save - //ColorParametersVewVM.LoadColorParameters(ActiveRMLExtention.Guid); - // _actionLogManager.InsertLog(BL.Enumerations.ActionLogType.RmlSaved, _authentication.CurrentUser, _rmlBeforeSave.Name, _rmlBeforeSave, rmlAfter, "RML saved using Machine Studio."); + /// + /// Saves this instance. + /// + private async void Save() + { + IsFree = false; + + try + { + using (_notification.PushTaskItem("Saving RML Extension...")) + { + if(ActiveRMLExtension.RMLStatus == RMLExtensionStatus.New) + { + ActiveRMLExtension.RMLStatus = RMLExtensionStatus.InProgress; + ColorParametersVewVM.SaveColorParameters -= UpdateStatus; + TestResultsViewVM.SaveTestResults -= UpdateStatus; + } - _rmlExtensionBeforeSave = rmlExtensionAfter; + ActiveRMLExtension.LastUpdated = DateTime.UtcNow; + ActiveRML.LastUpdated = DateTime.UtcNow; + + await _active_context.SaveChangesAsync(); - RefreshView(SelectedRMLExtension.Guid); + // _actionLogManager.InsertLog(BL.Enumerations.ActionLogType.RmlSaved, _authentication.CurrentUser, _rmlBeforeSave.Name, _rmlBeforeSave, rmlAfter, "RML saved using Machine Studio."); + + RefreshView(ActiveRMLExtension.Guid, ActiveRML.Guid); } } catch (Exception ex) { - LogManager.Log(ex, $"Error saving RML Extension {ActiveRMLExtention.Name}"); + LogManager.Log(ex, $"Error saving RML Extension of RML{ActiveRML.Name}"); _notification.ShowError($"An error occurred while trying to save the current RML Extension.\n{ex.FlattenMessage()}"); } finally @@ -538,5 +629,7 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels IsFree = true; } } + + #endregion } } -- cgit v1.3.1