From fe5c32241d8a976f0770dacb998380ab1cf70ce4 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Tue, 9 Jul 2019 16:56:19 +0300 Subject: Implemented MS color catalogs. --- .../Tango.MachineStudio.Catalogs.csproj | 1 - .../ViewModels/CatalogVM.cs | 24 -- .../ViewModels/MainViewVM.cs | 290 ++++++++++++++++++++- .../Views/CatalogView.xaml | 208 ++++++++++++++- .../Views/CatalogView.xaml.cs | 8 +- .../Views/CatalogsView.xaml | 20 +- .../Views/CatalogsView.xaml.cs | 4 +- .../Views/MainView.xaml | 2 +- .../Views/MainView.xaml.cs | 2 + 9 files changed, 513 insertions(+), 46 deletions(-) delete mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/ViewModels/CatalogVM.cs (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs') diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Tango.MachineStudio.Catalogs.csproj b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Tango.MachineStudio.Catalogs.csproj index 1d29166be..e90e00b41 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Tango.MachineStudio.Catalogs.csproj +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Tango.MachineStudio.Catalogs.csproj @@ -69,7 +69,6 @@ - CatalogView.xaml diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/ViewModels/CatalogVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/ViewModels/CatalogVM.cs deleted file mode 100644 index 7d9c79ada..000000000 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/ViewModels/CatalogVM.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.BL.Entities; -using Tango.Core; - -namespace Tango.MachineStudio.Catalogs.ViewModels -{ - public class CatalogVM : ExtendedObject - { - public ColorSpace ColorSpace { get; set; } - - public List SupportedMedia { get; set; } - - public int Colors { get; set; } - - public CatalogVM() - { - SupportedMedia = new List(); - } - } -} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/ViewModels/MainViewVM.cs index 6b1c82e0f..a10e944f9 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/ViewModels/MainViewVM.cs @@ -1,17 +1,301 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tango.BL; +using Tango.BL.Entities; using Tango.MachineStudio.Common; +using System.Data.Entity; +using Tango.MachineStudio.Catalogs.Contracts; +using Tango.MachineStudio.Common.Notifications; +using Tango.Core.Commands; +using Tango.BL.Builders; namespace Tango.MachineStudio.Catalogs.ViewModels { - public class MainViewVM : StudioViewModel + public class MainViewVM : StudioViewModel { - public override void OnApplicationReady() + private ObservablesContext _catalogsContext; + private ObservablesContext _activeCatalogContext; + private INotificationProvider _notification; + + #region Properties + + private ObservableCollection _catalogs; + /// + /// Gets or sets the catalogs. + /// + public ObservableCollection Catalogs + { + get { return _catalogs; } + set { _catalogs = value; RaisePropertyChangedAuto(); } + } + + private ColorCatalog _selectedCatalog; + /// + /// Gets or sets the selected catalog. + /// + public ColorCatalog SelectedCatalog + { + get { return _selectedCatalog; } + set { _selectedCatalog = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } + } + + private ColorCatalog _activeCatalog; + /// + /// Gets or sets the active catalog. + /// + public ColorCatalog ActiveCatalog + { + get { return _activeCatalog; } + set { _activeCatalog = value; RaisePropertyChangedAuto(); } + } + + private ColorCatalogsGroup _selectedGroup; + /// + /// Gets or sets the selected group. + /// + public ColorCatalogsGroup SelectedGroup + { + get { return _selectedGroup; } + set { _selectedGroup = value; RaisePropertyChangedAuto(); } + } + + private ColorCatalogsItem _selectedItem; + /// + /// Gets or sets the selected item. + /// + public ColorCatalogsItem SelectedItem + { + get { return _selectedItem; } + set { _selectedItem = value; RaisePropertyChangedAuto(); } + } + + private List _rmls; + /// + /// Gets or sets the RMLS. + /// + public List RMLS + { + get { return _rmls; } + set { _rmls = value; RaisePropertyChangedAuto(); } + } + + #endregion + + #region Commands + + /// + /// Gets or sets the new catalog command. + /// + public RelayCommand NewCatalogCommand { get; set; } + + /// + /// Gets or sets the delete catalog command. + /// + public RelayCommand DeleteCatalogCommand { get; set; } + + /// + /// Gets or sets the edit catalog command. + /// + public RelayCommand EditCatalogCommand { get; set; } + + /// + /// Gets or sets the back to catalogs command. + /// + public RelayCommand BackToCatalogsCommand { get; set; } + + /// + /// Gets or sets the save active catalog command. + /// + public RelayCommand SaveActiveCatalogCommand { get; set; } + + #endregion + + #region Constructors + + /// + /// Initializes a new instance of the class. + /// + public MainViewVM() + { + Catalogs = new ObservableCollection(); + _catalogsContext = ObservablesContext.CreateDefault(); + + EditCatalogCommand = new RelayCommand(EditSelectedCatalog, () => SelectedCatalog != null); + NewCatalogCommand = new RelayCommand(CreateNewCatalog); + DeleteCatalogCommand = new RelayCommand(DeleteSelectedCatalog, () => SelectedCatalog != null); + BackToCatalogsCommand = new RelayCommand(BackToCatalogs); + SaveActiveCatalogCommand = new RelayCommand(SaveActiveCatalog); + } + + /// + /// Initializes a new instance of the class. + /// + /// The notification provider. + public MainViewVM(INotificationProvider notificationProvider) : this() + { + _notification = notificationProvider; + } + + #endregion + + #region Edit / Delete / Create Catalog + + public async Task LoadCatalogs() + { + using (_notification.PushTaskItem("Loading color catalogs...")) + { + IsFree = false; + Catalogs = (await _catalogsContext.ColorCatalogs.Include(x => x.ColorCatalogsGroups.Select(g => g.ColorCatalogsItems)).ToListAsync()).ToSynchronizedObservableCollection(); + SelectedCatalog = null; + IsFree = true; + } + } + + public async void EditSelectedCatalog() + { + if (SelectedCatalog != null) + { + using (_notification.PushTaskItem("Loading selected catalog...")) + { + try + { + IsFree = false; + + if (_activeCatalogContext != null) + { + _activeCatalogContext.Dispose(); + } + + _activeCatalogContext = ObservablesContext.CreateDefault(); + + ActiveCatalog = await new ColorCatalogBuilder(_activeCatalogContext).Set(SelectedCatalog.Guid).WithGroups().WithItems().WithRecipes().BuildAsync(); + SelectedGroup = ActiveCatalog.ColorCatalogsGroups.FirstOrDefault(); + RMLS = await _activeCatalogContext.Rmls.ToListAsync(); + + if (SelectedGroup != null) + { + SelectedItem = SelectedGroup.ColorCatalogsItems.FirstOrDefault(); + } + + View.NavigateTo(CatalogsNavigationView.CatalogView); + } + catch (Exception ex) + { + _notification.ShowError($"An error occurred while trying to load the selected catalog.\n{ex.Message}"); + LogManager.Log(ex, $"Error loading selected catalog '{SelectedCatalog.Name}'."); + } + finally + { + IsFree = true; + } + } + } + } + + private async void DeleteSelectedCatalog() + { + if (SelectedCatalog != null) + { + if (_notification.ShowQuestion("Are you sure you want to delete the selected catalog ?")) + { + using (_notification.PushTaskItem("Deleting selected catalog...")) + { + try + { + IsFree = false; + await SelectedCatalog.DeleteCascadeAsync(_catalogsContext); + SelectedCatalog = null; + } + catch (Exception ex) + { + _notification.ShowError($"An error occurred while trying to delete the selected catalog.\n{ex.Message}"); + LogManager.Log(ex, $"Error deleting catalog {SelectedCatalog.Name}."); + } + finally + { + IsFree = true; + } + } + } + } + } + + private async void CreateNewCatalog() + { + String name = _notification.ShowTextInput("Please enter a catalog name", "Catalog name"); + if (!String.IsNullOrWhiteSpace(name)) + { + using (_notification.PushTaskItem("Generating catalog...")) + { + try + { + IsFree = false; + ColorCatalog newCatalog = new ColorCatalog(); + newCatalog.Name = name; + newCatalog.Description = name; + newCatalog.Company = "Twine"; + _catalogsContext.ColorCatalogs.Add(newCatalog); + await _catalogsContext.SaveChangesAsync(); + SelectedCatalog = newCatalog; + EditSelectedCatalog(); + } + catch (Exception ex) + { + _notification.ShowError($"An error occurred while trying to create the catalog.\n{ex.Message}"); + LogManager.Log(ex, $"Error creating new catalog."); + } + finally + { + IsFree = true; + } + } + } + } + + private async void SaveActiveCatalog() + { + if (ActiveCatalog != null) + { + using (_notification.PushTaskItem("Updating catalog...")) + { + try + { + IsFree = false; + await _activeCatalogContext.SaveChangesAsync(); + await LoadCatalogs(); + _notification.ShowInfo("Catalog updated successfully."); + } + catch (Exception ex) + { + _notification.ShowError($"An error occurred while trying to update the catalog.\n{ex.Message}"); + LogManager.Log(ex, $"Error updating catalog {ActiveCatalog.Name}."); + } + finally + { + IsFree = true; + } + } + } + } + + private void BackToCatalogs() + { + View.NavigateTo(CatalogsNavigationView.CatalogsView); + } + + #endregion + + #region Application Ready + + public async override void OnApplicationReady() { - + await LoadCatalogs(); } + + #endregion } } diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Views/CatalogView.xaml b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Views/CatalogView.xaml index 17198b866..8dc6b5d02 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Views/CatalogView.xaml +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Views/CatalogView.xaml @@ -1,14 +1,214 @@ - - - + d:DesignHeight="1080" d:DesignWidth="1920" Background="Transparent" Foreground="#232323" d:DataContext="{d:DesignInstance Type=vm:MainViewVM, IsDesignTimeCreatable=False}" DataContext="{x:Static global:ViewModelLocator.MainViewVM}"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + PROPERTIES + + + + + + + + + + + + + + + + + + + + + + + + DESIGN CONTENT + + + + + + + + + + + + + + + + + + + + + + CATALOG GROUPS + + + + + + + + + + + + + + + + + + + + + + + + + GROUP COLORS + () + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + COLOR MEDIA RECIPES + () + + + + + + + + + + + + + + + + + + diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Views/CatalogView.xaml.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Views/CatalogView.xaml.cs index 02418b677..ad44d4c18 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Views/CatalogView.xaml.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Views/CatalogView.xaml.cs @@ -12,17 +12,21 @@ using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using Tango.MachineStudio.Catalogs.ViewModels; namespace Tango.MachineStudio.Catalogs.Views { /// /// Interaction logic for CatalogsView.xaml /// - public partial class CatalogsView : UserControl + public partial class CatalogView : UserControl { - public CatalogsView() + private MainViewVM _vm; + + public CatalogView() { InitializeComponent(); + Loaded += (_, __) => _vm = DataContext as MainViewVM; } } } diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Views/CatalogsView.xaml b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Views/CatalogsView.xaml index e324fb8d7..dca9c0e96 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Views/CatalogsView.xaml +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Catalogs/Views/CatalogsView.xaml @@ -1,4 +1,4 @@ - - + - - -