From 7eff98579401104a1d0a3512403abbcabb8a894d Mon Sep 17 00:00:00 2001 From: rvost Date: Tue, 11 Oct 2022 20:47:17 +0300 Subject: [PATCH] Add items import from classnames * Import items to types.xml from list of class names; * Import items to trader config from list of class names --- .../Models/IClassnameImportDialog.cs | 9 ++++ .../Services/IDialogFactory.cs | 1 + .../Stores/ItemTypesClassnameImportStore.cs | 45 +++++++++++++++++ .../ViewModels/ClassnamesImportViewModel.cs | 48 +++++++++++++++++++ .../ViewModels/ItemTypesViewModel.cs | 8 ++++ .../ViewModels/TraderCategoryViewModel.cs | 8 ++++ .../Models/WindowsClassnameImportDialog.cs | 18 +++++++ .../Resources/IconsDictionary.xaml | 25 ++++++++++ .../Services/WindowsDialogFactory.cs | 3 ++ .../Views/ClassnamesImportView.xaml | 31 ++++++++++++ .../Views/ClassnamesImportView.xaml.cs | 39 +++++++++++++++ .../Views/MainWindow.xaml | 6 +++ 12 files changed, 241 insertions(+) create mode 100644 src/DayzServerTools.Application/Models/IClassnameImportDialog.cs create mode 100644 src/DayzServerTools.Application/Stores/ItemTypesClassnameImportStore.cs create mode 100644 src/DayzServerTools.Application/ViewModels/ClassnamesImportViewModel.cs create mode 100644 src/DayzServerTools.Windows/Models/WindowsClassnameImportDialog.cs create mode 100644 src/DayzServerTools.Windows/Views/ClassnamesImportView.xaml create mode 100644 src/DayzServerTools.Windows/Views/ClassnamesImportView.xaml.cs diff --git a/src/DayzServerTools.Application/Models/IClassnameImportDialog.cs b/src/DayzServerTools.Application/Models/IClassnameImportDialog.cs new file mode 100644 index 0000000..1ba13b8 --- /dev/null +++ b/src/DayzServerTools.Application/Models/IClassnameImportDialog.cs @@ -0,0 +1,9 @@ +using DayzServerTools.Application.Stores; + +namespace DayzServerTools.Application.Models; + +public interface IClassnameImportDialog +{ + IClassnameImportStore Store { get; set; } + bool? ShowDialog(); +} diff --git a/src/DayzServerTools.Application/Services/IDialogFactory.cs b/src/DayzServerTools.Application/Services/IDialogFactory.cs index afb5801..61721d8 100644 --- a/src/DayzServerTools.Application/Services/IDialogFactory.cs +++ b/src/DayzServerTools.Application/Services/IDialogFactory.cs @@ -8,4 +8,5 @@ public interface IDialogFactory IFileDialog CreateOpenFileDialog(); IFileDialog CreateSaveFileDialog(); IExportDialog CreateExportDialog(); + IClassnameImportDialog CreateClassnameImportDialog(); } diff --git a/src/DayzServerTools.Application/Stores/ItemTypesClassnameImportStore.cs b/src/DayzServerTools.Application/Stores/ItemTypesClassnameImportStore.cs new file mode 100644 index 0000000..3f9a677 --- /dev/null +++ b/src/DayzServerTools.Application/Stores/ItemTypesClassnameImportStore.cs @@ -0,0 +1,45 @@ +using DayzServerTools.Application.Extensions; +using DayzServerTools.Application.ViewModels; +using DayzServerTools.Library.Trader; +using DayzServerTools.Library.Xml; + +namespace DayzServerTools.Application.Stores; + +public interface IClassnameImportStore +{ + void Accept(IEnumerable classnames); +} + +public class ItemTypesClassnameImportStore : IClassnameImportStore +{ + private readonly ItemTypesViewModel _viewModel; + + public ItemTypesClassnameImportStore(ItemTypesViewModel viewModel) + { + _viewModel = viewModel; + } + + public void Accept(IEnumerable classnames) + { + var itemTypes = classnames.Select(name => new ItemType() { Name = name }); + _viewModel.CopyItemTypes(itemTypes); + } +} + +public class TraderCategoryClassnameImportStore : IClassnameImportStore +{ + private readonly TraderCategoryViewModel _viewModel; + + public TraderCategoryClassnameImportStore(TraderCategoryViewModel viewModel) + { + _viewModel = viewModel; + } + + public void Accept(IEnumerable classnames) + { + var traderItems = classnames.Select(name => new TraderItem() { Name = name }); + var viewModels = traderItems.Select(item => new TraderItemViewModel(item)); + + _viewModel.Items.AddRange(viewModels); + } +} \ No newline at end of file diff --git a/src/DayzServerTools.Application/ViewModels/ClassnamesImportViewModel.cs b/src/DayzServerTools.Application/ViewModels/ClassnamesImportViewModel.cs new file mode 100644 index 0000000..2bcbeda --- /dev/null +++ b/src/DayzServerTools.Application/ViewModels/ClassnamesImportViewModel.cs @@ -0,0 +1,48 @@ +using System.Collections.ObjectModel; +using System.Text.RegularExpressions; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; + +using DayzServerTools.Application.Stores; + +namespace DayzServerTools.Application.ViewModels; + +public partial class ClassnamesImportViewModel : ObservableObject +{ + private readonly IClassnameImportStore _importStore; + [ObservableProperty] + private string rawInput = ""; + [ObservableProperty] + [NotifyCanExecuteChangedFor(nameof(ImportCommand))] + private IEnumerable classnames = new List(); + + public IRelayCommand> ImportCommand { get; } + public IRelayCommand ParseCommand { get; } + public event EventHandler CloseRequested; + + public ClassnamesImportViewModel(IClassnameImportStore importStore) + { + _importStore = importStore; + + ImportCommand = new RelayCommand>(Import, CanImport); + ParseCommand = new RelayCommand(Parse); + } + + protected bool CanImport(IEnumerable classnames) + => classnames?.Any() ?? false; + protected void Import(IEnumerable classnames) + { + _importStore.Accept(classnames); + CloseRequested?.Invoke(this, EventArgs.Empty); + } + + protected void Parse() + { + if (!string.IsNullOrWhiteSpace(rawInput)) + { + var regex = new Regex(@"\w+"); + Classnames = regex.Matches(RawInput).Select(m => m.Value).ToList(); + } + + } +} diff --git a/src/DayzServerTools.Application/ViewModels/ItemTypesViewModel.cs b/src/DayzServerTools.Application/ViewModels/ItemTypesViewModel.cs index 1ba9c59..aa6b02c 100644 --- a/src/DayzServerTools.Application/ViewModels/ItemTypesViewModel.cs +++ b/src/DayzServerTools.Application/ViewModels/ItemTypesViewModel.cs @@ -48,6 +48,7 @@ public partial class ItemTypesViewModel : ProjectFileViewModel, IDisp public IRelayCommand AdjustRestockCommand { get; } public IRelayCommand ExportToNewFileCommand { get; } public IRelayCommand ExportToTraderCommand { get; } + public IRelayCommand ClassnamesImportCommand { get; } public IRelayCommand SetCategoryCommand { get; } public IRelayCommand AddValueFlagCommand { get; } public IRelayCommand AddUsageFlagCommand { get; } @@ -66,6 +67,7 @@ public ItemTypesViewModel(IDialogFactory dialogFactory) : base(dialogFactory) AdjustRestockCommand = new RelayCommand(AdjustRestock, (param) => CanExecuteBatchCommand()); ExportToNewFileCommand = new RelayCommand(ExportToNewFile, CanExecuteExportCommand); ExportToTraderCommand = new RelayCommand(ExportToTrader, CanExecuteExportCommand); + ClassnamesImportCommand = new RelayCommand(ClassnamesImport); SetCategoryCommand = new RelayCommand(SetCategory, (param) => CanExecuteBatchCommand()); AddValueFlagCommand = new RelayCommand(AddValueFlag, (param) => CanExecuteBatchCommand()); AddUsageFlagCommand = new RelayCommand(AddUsageFlag, (param) => CanExecuteBatchCommand()); @@ -149,6 +151,12 @@ protected void ExportToTrader(object cmdParam) dialog.Store = new ItemTypesToTraderExportStore(items); dialog.ShowDialog(); } + protected void ClassnamesImport() + { + var dialog = _dialogFactory.CreateClassnameImportDialog(); + dialog.Store = new ItemTypesClassnameImportStore(this); + dialog.ShowDialog(); + } protected void SetCategory(VanillaFlag category) { if (category == null) diff --git a/src/DayzServerTools.Application/ViewModels/TraderCategoryViewModel.cs b/src/DayzServerTools.Application/ViewModels/TraderCategoryViewModel.cs index 19368d8..15ffbb4 100644 --- a/src/DayzServerTools.Application/ViewModels/TraderCategoryViewModel.cs +++ b/src/DayzServerTools.Application/ViewModels/TraderCategoryViewModel.cs @@ -32,6 +32,7 @@ public string Name } public ObservableCollection Items { get; } + public IRelayCommand ClassnamesImportCommand { get; } public IRelayCommand CopyItemsCommand { get; } public IRelayCommand MoveItemsCommand { get; } public IRelayCommand ProhibitSellingCommand { get; } @@ -46,6 +47,7 @@ public TraderCategoryViewModel(TraderCategory model) this.model = model; Items = new(model.TraderItems.Select(m => new TraderItemViewModel(m))); + ClassnamesImportCommand = new RelayCommand(ClassnamesImport); CopyItemsCommand = new RelayCommand(CopyItems, () => CanExecuteBatchCommand()); MoveItemsCommand = new RelayCommand(MoveItems, () => CanExecuteBatchCommand()); ProhibitBuyingCommand = new RelayCommand(ProhibitBuying, () => CanExecuteBatchCommand()); @@ -81,6 +83,12 @@ private void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEven public TraderCategoryViewModel() : this(new TraderCategory()) { } protected bool CanExecuteBatchCommand() => SelectedItems is not null; + protected void ClassnamesImport() + { + var dialog = _dialogFactory.CreateClassnameImportDialog(); + dialog.Store = new TraderCategoryClassnameImportStore(this); + dialog.ShowDialog(); + } protected void CopyItems() { if (SelectedItems is null) diff --git a/src/DayzServerTools.Windows/Models/WindowsClassnameImportDialog.cs b/src/DayzServerTools.Windows/Models/WindowsClassnameImportDialog.cs new file mode 100644 index 0000000..d0600f1 --- /dev/null +++ b/src/DayzServerTools.Windows/Models/WindowsClassnameImportDialog.cs @@ -0,0 +1,18 @@ +using DayzServerTools.Application.Models; +using DayzServerTools.Application.Stores; +using DayzServerTools.Application.ViewModels; +using DayzServerTools.Windows.Views; + +namespace DayzServerTools.Windows.Models; + +internal class WindowsClassnameImportDialog : IClassnameImportDialog +{ + public IClassnameImportStore Store { get; set; } + + public bool? ShowDialog() + { + var vm = new ClassnamesImportViewModel(Store); + var window = new ClassnamesImportView(vm); + return window.ShowDialog(); + } +} diff --git a/src/DayzServerTools.Windows/Resources/IconsDictionary.xaml b/src/DayzServerTools.Windows/Resources/IconsDictionary.xaml index f258e98..339d8e9 100644 --- a/src/DayzServerTools.Windows/Resources/IconsDictionary.xaml +++ b/src/DayzServerTools.Windows/Resources/IconsDictionary.xaml @@ -332,6 +332,31 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/DayzServerTools.Windows/Services/WindowsDialogFactory.cs b/src/DayzServerTools.Windows/Services/WindowsDialogFactory.cs index 9c8dbba..a50db4c 100644 --- a/src/DayzServerTools.Windows/Services/WindowsDialogFactory.cs +++ b/src/DayzServerTools.Windows/Services/WindowsDialogFactory.cs @@ -8,6 +8,9 @@ internal class WindowsDialogFactory : IDialogFactory { private string defaultFilter = "XML Files (*.xml)|*.xml"; + public IClassnameImportDialog CreateClassnameImportDialog() + => new WindowsClassnameImportDialog(); + public IExportDialog CreateExportDialog() => new WindowsExportDialog(); diff --git a/src/DayzServerTools.Windows/Views/ClassnamesImportView.xaml b/src/DayzServerTools.Windows/Views/ClassnamesImportView.xaml new file mode 100644 index 0000000..a446e16 --- /dev/null +++ b/src/DayzServerTools.Windows/Views/ClassnamesImportView.xaml @@ -0,0 +1,31 @@ + + + + + + + + + + + +