From 2d9415f36f81485a938adc25386ec518c2b9957e Mon Sep 17 00:00:00 2001 From: Sergey <53861218+Stan-Kudri@users.noreply.github.com> Date: Thu, 21 Nov 2024 00:30:19 +0300 Subject: [PATCH] WatchList.WPF - Add "ViewModel" in Window "MergeDatabaseWindow". (#95) --- .../{RelayCommand.cs => RelayCommandApp.cs} | 6 +- WatchList.WPF/Data/ViewModelLocator.cs | 6 +- .../Extension/AppServiceDIExtension.cs | 2 +- .../{ModelLoadDataDB.cs => FiileLoaderDB.cs} | 22 +-- .../ViewModel/CinemaPageViewModel.cs | 28 ++- .../ViewModel/MergeDatabaseViewModel.cs | 168 ++++++++++++++++++ WatchList.WPF/Views/MergeDatabaseWindow.xaml | 132 ++++++++++++-- .../Views/MergeDatabaseWindow.xaml.cs | 111 +----------- WatchList.WPF/Views/MessageWindow.xaml.cs | 2 +- WatchList.WPF/WatchList.WPF.csproj | 1 + 10 files changed, 313 insertions(+), 165 deletions(-) rename WatchList.WPF/Commands/{RelayCommand.cs => RelayCommandApp.cs} (80%) rename WatchList.WPF/Models/ModelDataLoad/{ModelLoadDataDB.cs => FiileLoaderDB.cs} (61%) create mode 100644 WatchList.WPF/ViewModel/MergeDatabaseViewModel.cs diff --git a/WatchList.WPF/Commands/RelayCommand.cs b/WatchList.WPF/Commands/RelayCommandApp.cs similarity index 80% rename from WatchList.WPF/Commands/RelayCommand.cs rename to WatchList.WPF/Commands/RelayCommandApp.cs index feea8db..155586a 100644 --- a/WatchList.WPF/Commands/RelayCommand.cs +++ b/WatchList.WPF/Commands/RelayCommandApp.cs @@ -2,7 +2,7 @@ namespace WatchList.WPF.Commands { - public class RelayCommand : ICommand + public class RelayCommandApp : ICommand { private readonly Action? _execute; private readonly Predicate? _canExecute; @@ -13,10 +13,10 @@ public event EventHandler? CanExecuteChanged remove => CommandManager.RequerySuggested -= value; } - public RelayCommand(Action execute) + public RelayCommandApp(Action execute) : this(execute, null) => _execute = execute; - public RelayCommand(Action execute, Predicate? canExecute) + public RelayCommandApp(Action execute, Predicate? canExecute) { _execute = execute ?? throw new ArgumentNullException(nameof(execute)); _canExecute = canExecute; diff --git a/WatchList.WPF/Data/ViewModelLocator.cs b/WatchList.WPF/Data/ViewModelLocator.cs index 625ac96..7bd9225 100644 --- a/WatchList.WPF/Data/ViewModelLocator.cs +++ b/WatchList.WPF/Data/ViewModelLocator.cs @@ -13,10 +13,14 @@ public CinemaWindowViewModel CinemaWindowViewModel public CinemaPageViewModel CinemaPageViewModel => _provider!.GetRequiredService(); + public MergeDatabaseViewModel MergeDatabaseViewModel + => _provider!.GetRequiredService(); + public static IServiceCollection AddViewModels(ServiceCollection serviceCollection) => serviceCollection .AddTransient() - .AddTransient(); + .AddTransient() + .AddTransient(); public static void Init(ServiceProvider provider) => _provider = provider; } diff --git a/WatchList.WPF/Extension/AppServiceDIExtension.cs b/WatchList.WPF/Extension/AppServiceDIExtension.cs index d05762f..af9a9a9 100644 --- a/WatchList.WPF/Extension/AppServiceDIExtension.cs +++ b/WatchList.WPF/Extension/AppServiceDIExtension.cs @@ -26,7 +26,7 @@ public static IServiceCollection AppServiceContainer(this IServiceCollection ser .AddScoped() .AddSingleton() .AddScoped() - .AddScoped() + .AddScoped() .AddLogging() .AddSerilog(); diff --git a/WatchList.WPF/Models/ModelDataLoad/ModelLoadDataDB.cs b/WatchList.WPF/Models/ModelDataLoad/FiileLoaderDB.cs similarity index 61% rename from WatchList.WPF/Models/ModelDataLoad/ModelLoadDataDB.cs rename to WatchList.WPF/Models/ModelDataLoad/FiileLoaderDB.cs index 48fffcb..20e1696 100644 --- a/WatchList.WPF/Models/ModelDataLoad/ModelLoadDataDB.cs +++ b/WatchList.WPF/Models/ModelDataLoad/FiileLoaderDB.cs @@ -1,35 +1,24 @@ using Microsoft.Extensions.Logging; +using WatchList.Core.Model.Load; using WatchList.Core.Repository; -using WatchList.Core.Service.Component; using WatchList.Core.Service.DataLoading; using WatchList.Migrations.SQLite; -using WatchList.WPF.Views; namespace WatchList.WPF.Models.ModelDataLoad { - public class ModelLoadDataDB + public class FiileLoaderDB { - private readonly IMessageBox _messageBox; private readonly DownloadDataService _downloadDataService; private readonly ILogger _logger; - public ModelLoadDataDB(IMessageBox messageBox, DownloadDataService downloadDataService, ILogger logger) + public FiileLoaderDB(DownloadDataService downloadDataService, ILogger logger) { - _messageBox = messageBox; _downloadDataService = downloadDataService; _logger = logger; } - public async void CanLoadDataFromDB() + public async void DownloadDataToDB(ILoadRulesConfig loadRulesConfig) { - var dataLoadingWindow = new MergeDatabaseWindow(_messageBox); - var showResult = dataLoadingWindow.ShowDialog() ?? false; - - if (!showResult) - { - return; - } - using OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.DefaultExt = ".db"; // Required file extension fileDialog.Filter = "Text documents (.db)|*.db"; // Optional file extensions @@ -43,8 +32,7 @@ public async void CanLoadDataFromDB() _logger.LogInformation($"Add item from the selected file <{0}>", pathFile); var dbContext = new DbContextFactoryMigrator(pathFile).Create(); - var loadRuleConfig = dataLoadingWindow.GetLoadRuleConfig(); - await _downloadDataService.DownloadDataByDB(dbContext, loadRuleConfig); + await _downloadDataService.DownloadDataByDB(dbContext, loadRulesConfig); } } } diff --git a/WatchList.WPF/ViewModel/CinemaPageViewModel.cs b/WatchList.WPF/ViewModel/CinemaPageViewModel.cs index 575a575..f3fc3d7 100644 --- a/WatchList.WPF/ViewModel/CinemaPageViewModel.cs +++ b/WatchList.WPF/ViewModel/CinemaPageViewModel.cs @@ -11,8 +11,8 @@ using WatchList.WPF.Extension; using WatchList.WPF.Models; using WatchList.WPF.Models.Filter; -using WatchList.WPF.Models.ModelDataLoad; using WatchList.WPF.Models.Sorter; +using WatchList.WPF.Views; namespace WatchList.WPF.ViewModel { @@ -27,8 +27,6 @@ public class CinemaPageViewModel : BindingBaseModel private readonly FilterItemModel _filterItem; private readonly ItemSearchRequest _searchRequests; - private readonly ModelLoadDataDB _modelLoadDataDB; - private ObservableCollection _watchItems = new ObservableCollection(); private PagedList _pagedList; @@ -51,8 +49,7 @@ public CinemaPageViewModel(IMessageBox messageBox, WatchItemService watchItemService, SortWatchItemModel sortField, FilterItemModel filterItem, - PageService pageService, - ModelLoadDataDB modelLoadDataDB) + PageService pageService) { _messageBox = messageBox; _logger = logger; @@ -62,7 +59,6 @@ public CinemaPageViewModel(IMessageBox messageBox, _pageService = pageService; _searchRequests = new ItemSearchRequest(_filterItem, _sortField.GetSortItem(), Page.GetPage(), _isAscending); _pagedList = _itemService.GetPage(_searchRequests); - _modelLoadDataDB = modelLoadDataDB; CurPage = Page.Number; LoadDataAsync(); } @@ -88,18 +84,18 @@ private set public List PageWatchItems { get; set; } - public RelayCommand MoveToPreviousPage - => new RelayCommand(async _ => await LoadDataAsyncPage(--Page.Number), _ => _pagedList.HasPreviousPage); - public RelayCommand MoveToFirstPage - => new RelayCommand(async _ => await LoadDataAsyncPage(1), _ => _pagedList.HasPreviousPage); + public RelayCommandApp MoveToPreviousPage + => new RelayCommandApp(async _ => await LoadDataAsyncPage(--Page.Number), _ => _pagedList.HasPreviousPage); + public RelayCommandApp MoveToFirstPage + => new RelayCommandApp(async _ => await LoadDataAsyncPage(1), _ => _pagedList.HasPreviousPage); - public RelayCommand MoveToNextPage - => new RelayCommand(async _ => await LoadDataAsyncPage(++Page.Number), _ => _pagedList.HasNextPage); - public RelayCommand MoveToLastPage - => new RelayCommand(async _ => await LoadDataAsyncPage(_pagedList.PageCount), _ => _pagedList.HasNextPage); + public RelayCommandApp MoveToNextPage + => new RelayCommandApp(async _ => await LoadDataAsyncPage(++Page.Number), _ => _pagedList.HasNextPage); + public RelayCommandApp MoveToLastPage + => new RelayCommandApp(async _ => await LoadDataAsyncPage(_pagedList.PageCount), _ => _pagedList.HasNextPage); - public RelayCommand MoveAddDataDB - => new RelayCommand(_ => _modelLoadDataDB.CanLoadDataFromDB()); + public RelayCommandApp MoveAddDataDB + => new RelayCommandApp(_ => new MergeDatabaseWindow().Show()); /// /// Load data in table. diff --git a/WatchList.WPF/ViewModel/MergeDatabaseViewModel.cs b/WatchList.WPF/ViewModel/MergeDatabaseViewModel.cs new file mode 100644 index 0000000..ab3611d --- /dev/null +++ b/WatchList.WPF/ViewModel/MergeDatabaseViewModel.cs @@ -0,0 +1,168 @@ +using System.ComponentModel; +using System.Runtime.CompilerServices; +using System.Windows; +using CommunityToolkit.Mvvm.Input; +using WatchList.Core.Model.ItemCinema.Components; +using WatchList.Core.Model.Load; +using WatchList.Core.Model.Load.Components; +using WatchList.Core.Model.Load.ItemActions; +using WatchList.Core.Service.Component; +using WatchList.WPF.Commands; +using WatchList.WPF.Models.ModelDataLoad; +using WatchList.WPF.Models.ModelDataLoad.LoadModel; + +namespace WatchList.WPF.ViewModel +{ + public class MergeDatabaseViewModel : INotifyPropertyChanged + { + private readonly FiileLoaderDB _fiileLoader; + private readonly IMessageBox _messageBox; + + private readonly ModelTypeCinemaUpload _modelTypeCinemaUpload = new ModelTypeCinemaUpload(); + private readonly ModelDownloadMoreGrade _modelDownloadMoreGrade = new ModelDownloadMoreGrade(); + + private bool _isExistGrade; + + private bool _isConsiderDuplicate; + private bool _isUpdateDuplicateItem; + private bool _isCaseSensitive; + + public MergeDatabaseViewModel(IMessageBox messageBox, FiileLoaderDB fiileLoader) + { + _messageBox = messageBox; + _fiileLoader = fiileLoader; + SelectGradeLoadCinema = Grade.AnyGrade; + SelectTypeLoadCinema = new TypeLoadingCinema(); + MergeDateFromDB = new RelayCommand(MoveLoadDB); + CloseWindowCommand = new RelayCommand(CloseWindow); + SetDefoultValueWindow = new RelayCommandApp(_ => SetupDefaultValues()); + } + + public TypeLoadingCinema SelectTypeLoadCinema + { + get => _modelTypeCinemaUpload.SelectedValue; + set + { + _modelTypeCinemaUpload.SelectedValue = value; + OnPropertyChanged(nameof(SelectTypeLoadCinema)); + } + } + public Grade SelectGradeLoadCinema + { + get => _modelDownloadMoreGrade.Value; + set + { + _modelDownloadMoreGrade.Value = value; + OnPropertyChanged(nameof(SelectGradeLoadCinema)); + } + } + + public bool IsExistGrade + { + get => _isExistGrade; + set + { + _isExistGrade = value; + OnPropertyChanged(nameof(IsExistGrade)); + } + } + + public bool IsConsiderDuplicate + { + get => _isConsiderDuplicate; + set + { + _isConsiderDuplicate = value; + OnPropertyChanged(nameof(IsConsiderDuplicate)); + } + } + + public bool IsUpdateDuplicateItem + { + get => _isUpdateDuplicateItem; + set + { + _isUpdateDuplicateItem = value; + OnPropertyChanged(nameof(IsUpdateDuplicateItem)); + } + } + + public bool IsCaseSensitive + { + get => _isCaseSensitive; + set + { + _isCaseSensitive = value; + OnPropertyChanged(nameof(IsCaseSensitive)); + } + } + + public IEnumerable TypeLoadingCinemas => _modelTypeCinemaUpload.Items; + public IEnumerable GradeLoadingCinemas => _modelDownloadMoreGrade.Items; + + public RelayCommand MergeDateFromDB { get; private set; } + public RelayCommand CloseWindowCommand { get; private set; } + public RelayCommandApp SetDefoultValueWindow { get; private set; } + + private async void MoveLoadDB(Window window) + { + if (await _messageBox.ShowQuestion("Add data from a file using the following algorithm?")) + { + var loadRuleConfig = GetLoadRuleConfig(); + _fiileLoader.DownloadDataToDB(loadRuleConfig); + } + + window?.Close(); + } + + private void SetupDefaultValues() + { + SelectTypeLoadCinema = new TypeLoadingCinema(TypeCinema.Movie); + SelectGradeLoadCinema = Grade.AnyGrade; + IsExistGrade = IsConsiderDuplicate = + IsCaseSensitive = IsUpdateDuplicateItem = false; + } + + private ILoadRulesConfig GetLoadRuleConfig() + { + if (!IsConsiderDuplicate) + { + return new LoadRulesConfigModel(IsExistGrade, new ActionDuplicateItems(), SelectTypeLoadCinema.Value, SelectGradeLoadCinema); + } + + var listDuplicateLoadRule = GetDuplicateLoadingRules(); + return new LoadRulesConfigModel(IsExistGrade, new ActionDuplicateItems(IsConsiderDuplicate, listDuplicateLoadRule), SelectTypeLoadCinema.Value, SelectGradeLoadCinema); + } + + private List GetDuplicateLoadingRules() + { + if (!IsConsiderDuplicate) + { + return new List(); + } + + var listConsiderDuplicates = new List(); + + if (IsUpdateDuplicateItem) + { + listConsiderDuplicates.Add(DuplicateLoadingRules.UpdateDuplicate); + } + + if (IsCaseSensitive) + { + listConsiderDuplicates.Add(DuplicateLoadingRules.CaseSensitive); + } + + return listConsiderDuplicates; + } + + private void CloseWindow(Window window) => window?.Close(); + + public event PropertyChangedEventHandler PropertyChanged; + public void OnPropertyChanged([CallerMemberName] string prop = "") + { + if (PropertyChanged != null) + PropertyChanged(this, new PropertyChangedEventArgs(prop)); + } + } +} diff --git a/WatchList.WPF/Views/MergeDatabaseWindow.xaml b/WatchList.WPF/Views/MergeDatabaseWindow.xaml index 93a4b5b..6eb925c 100644 --- a/WatchList.WPF/Views/MergeDatabaseWindow.xaml +++ b/WatchList.WPF/Views/MergeDatabaseWindow.xaml @@ -1,4 +1,5 @@ - - + mc:Ignorable="d" Height="240" Width="420" + ResizeMode="NoResize" + d:DesignHeight="240" d:DesignWidth="420" + Title="Loading data" Foreground="Black" + DataContext="{Binding MergeDatabaseViewModel, Source={StaticResource ViewModelLocator}}"> @@ -33,16 +35,112 @@ - - - - - - - + diff --git a/WatchList.WPF/Views/MergeDatabaseWindow.xaml.cs b/WatchList.WPF/Views/MergeDatabaseWindow.xaml.cs index f78221a..39821fd 100644 --- a/WatchList.WPF/Views/MergeDatabaseWindow.xaml.cs +++ b/WatchList.WPF/Views/MergeDatabaseWindow.xaml.cs @@ -1,120 +1,13 @@ -using System.Windows; -using WatchList.Core.Model.Load; -using WatchList.Core.Model.Load.Components; -using WatchList.Core.Model.Load.ItemActions; -using WatchList.Core.Service.Component; -using WatchList.WPF.Models.ModelDataLoad.LoadModel; - namespace WatchList.WPF.Views { /// /// Interaction logic for MergeDatabasePage.xaml /// - public partial class MergeDatabaseWindow : Window + public partial class MergeDatabaseWindow { - private readonly IMessageBox _messageBox; - private readonly ModelTypeCinemaUpload _modelTypeCinemaUpload = new ModelTypeCinemaUpload(); - private readonly ModelDownloadMoreGrade _modelDownloadMoreGrade = new ModelDownloadMoreGrade(); - - public MergeDatabaseWindow(IMessageBox messageBox) + public MergeDatabaseWindow() { InitializeComponent(); - _messageBox = messageBox; - } - - private TypeLoadingCinema SelectTypeCinema - => ComboBoxType.SelectedValue != null - ? (TypeLoadingCinema)ComboBoxType.SelectedValue - : throw new ArgumentNullException("Wrong combo box format"); - - private Grade SelectGrade - => ComboBoxMoreGrade.SelectedValue != null - ? (Grade)ComboBoxMoreGrade.SelectedValue - : throw new ArgumentNullException("Wrong combo box format"); - - public ILoadRulesConfig GetLoadRuleConfig() - { - var isDeleteGrade = CheckBoxExistGrade.IsChecked ?? false; - var considerDuplicates = CheckBoxConsiderDuplicates.IsChecked ?? false; - - if (!considerDuplicates) - { - return new LoadRulesConfigModel(isDeleteGrade, new ActionDuplicateItems(), SelectTypeCinema.Value, SelectGrade); - } - - var listDuplicateLoadRule = GetListDuplicateLoadingRules(); - return new LoadRulesConfigModel(isDeleteGrade, new ActionDuplicateItems(considerDuplicates, listDuplicateLoadRule), SelectTypeCinema.Value, SelectGrade); - } - - private async void BtnOK_Click(object sender, RoutedEventArgs e) - { - if (await _messageBox.ShowQuestion("Add data from a file using the following algorithm?")) - { - DialogResult = true; - } - else - { - return; - } - } - - private void BtnClear_Click(object sender, RoutedEventArgs e) - => SetupDefaultValues(); - - private void BtnClose_Click(object sender, RoutedEventArgs e) - => Close(); - - private void LoadingDBWindow_Loaded(object sender, RoutedEventArgs e) - { - ComboBoxType.ItemsSource = _modelTypeCinemaUpload.Items; - ComboBoxMoreGrade.ItemsSource = _modelDownloadMoreGrade.Items; - ComboBoxType.SelectedItem = _modelTypeCinemaUpload.SelectedValue; - ComboBoxMoreGrade.SelectedItem = Grade.AnyGrade; - CheckBoxExistGrade.IsChecked = false; - CheckBoxConsiderDuplicates.IsChecked = false; - } - - private void CheckBoxConsiderDuplicates_Checked(object sender, RoutedEventArgs e) - { - CheckBoxUpdateDuplicateItem.IsEnabled = CheckBoxCaseSensitive.IsEnabled = true; - } - - private void CheckBoxConsiderDuplicates_Unchecked(object sender, RoutedEventArgs e) - { - CheckBoxUpdateDuplicateItem.IsEnabled = CheckBoxCaseSensitive.IsEnabled = false; - } - - private void SetupDefaultValues() - { - ComboBoxType.SelectedItem = _modelTypeCinemaUpload.SelectedValue; - ComboBoxMoreGrade.SelectedItem = Grade.AnyGrade; - CheckBoxExistGrade.IsChecked = - CheckBoxConsiderDuplicates.IsChecked = - CheckBoxCaseSensitive.IsChecked = - CheckBoxUpdateDuplicateItem.IsChecked = - false; - } - - private List GetListDuplicateLoadingRules() - { - if (CheckBoxConsiderDuplicates.IsChecked == null || CheckBoxConsiderDuplicates.IsChecked == false) - { - return new List(); - } - - var listConsiderDuplicates = new List(); - - if (CheckBoxUpdateDuplicateItem.IsChecked == true) - { - listConsiderDuplicates.Add(DuplicateLoadingRules.UpdateDuplicate); - } - - if (CheckBoxCaseSensitive.IsChecked == true) - { - listConsiderDuplicates.Add(DuplicateLoadingRules.CaseSensitive); - } - - return listConsiderDuplicates; } } } diff --git a/WatchList.WPF/Views/MessageWindow.xaml.cs b/WatchList.WPF/Views/MessageWindow.xaml.cs index 8190250..81133e6 100644 --- a/WatchList.WPF/Views/MessageWindow.xaml.cs +++ b/WatchList.WPF/Views/MessageWindow.xaml.cs @@ -17,7 +17,7 @@ public MessageWindow() public Task ShowDataReplaceQuestion(string titleItem) { - throw new NotImplementedException(); + return Task.FromResult(DialogReplaceItemQuestion.AllYes); } public Task ShowError(string message) diff --git a/WatchList.WPF/WatchList.WPF.csproj b/WatchList.WPF/WatchList.WPF.csproj index 40442ba..88234f4 100644 --- a/WatchList.WPF/WatchList.WPF.csproj +++ b/WatchList.WPF/WatchList.WPF.csproj @@ -11,6 +11,7 @@ +