Skip to content

Commit

Permalink
WatchList.WPF - Add "ViewModel" in Window "MergeDatabaseWindow". (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan-Kudri authored Nov 20, 2024
1 parent 6427df7 commit 2d9415f
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace WatchList.WPF.Commands
{
public class RelayCommand : ICommand
public class RelayCommandApp : ICommand
{
private readonly Action<object?>? _execute;
private readonly Predicate<object?>? _canExecute;
Expand All @@ -13,10 +13,10 @@ public event EventHandler? CanExecuteChanged
remove => CommandManager.RequerySuggested -= value;
}

public RelayCommand(Action<object?> execute)
public RelayCommandApp(Action<object?> execute)
: this(execute, null) => _execute = execute;

public RelayCommand(Action<object?> execute, Predicate<object?>? canExecute)
public RelayCommandApp(Action<object?> execute, Predicate<object?>? canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
Expand Down
6 changes: 5 additions & 1 deletion WatchList.WPF/Data/ViewModelLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ public CinemaWindowViewModel CinemaWindowViewModel
public CinemaPageViewModel CinemaPageViewModel
=> _provider!.GetRequiredService<CinemaPageViewModel>();

public MergeDatabaseViewModel MergeDatabaseViewModel
=> _provider!.GetRequiredService<MergeDatabaseViewModel>();

public static IServiceCollection AddViewModels(ServiceCollection serviceCollection)
=> serviceCollection
.AddTransient<CinemaWindowViewModel>()
.AddTransient<CinemaPageViewModel>();
.AddTransient<CinemaPageViewModel>()
.AddTransient<MergeDatabaseViewModel>();

public static void Init(ServiceProvider provider) => _provider = provider;
}
Expand Down
2 changes: 1 addition & 1 deletion WatchList.WPF/Extension/AppServiceDIExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static IServiceCollection AppServiceContainer(this IServiceCollection ser
.AddScoped<FilterItemModel>()
.AddSingleton<PageService>()
.AddScoped<ViewModelLocator>()
.AddScoped<ModelLoadDataDB>()
.AddScoped<FiileLoaderDB>()
.AddLogging()
.AddSerilog();

Expand Down
Original file line number Diff line number Diff line change
@@ -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<WatchItemRepository> _logger;

public ModelLoadDataDB(IMessageBox messageBox, DownloadDataService downloadDataService, ILogger<WatchItemRepository> logger)
public FiileLoaderDB(DownloadDataService downloadDataService, ILogger<WatchItemRepository> 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
Expand All @@ -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);
}
}
}
28 changes: 12 additions & 16 deletions WatchList.WPF/ViewModel/CinemaPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -27,8 +27,6 @@ public class CinemaPageViewModel : BindingBaseModel
private readonly FilterItemModel _filterItem;
private readonly ItemSearchRequest _searchRequests;

private readonly ModelLoadDataDB _modelLoadDataDB;

private ObservableCollection<WatchItem> _watchItems = new ObservableCollection<WatchItem>();

private PagedList<WatchItem> _pagedList;
Expand All @@ -51,8 +49,7 @@ public CinemaPageViewModel(IMessageBox messageBox,
WatchItemService watchItemService,
SortWatchItemModel sortField,
FilterItemModel filterItem,
PageService pageService,
ModelLoadDataDB modelLoadDataDB)
PageService pageService)
{
_messageBox = messageBox;
_logger = logger;
Expand All @@ -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();
}
Expand All @@ -88,18 +84,18 @@ private set

public List<CinemaModel> 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());

/// <summary>
/// Load data in table.
Expand Down
168 changes: 168 additions & 0 deletions WatchList.WPF/ViewModel/MergeDatabaseViewModel.cs
Original file line number Diff line number Diff line change
@@ -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<Window>(MoveLoadDB);
CloseWindowCommand = new RelayCommand<Window>(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<TypeLoadingCinema> TypeLoadingCinemas => _modelTypeCinemaUpload.Items;
public IEnumerable<Grade> GradeLoadingCinemas => _modelDownloadMoreGrade.Items;

public RelayCommand<Window> MergeDateFromDB { get; private set; }
public RelayCommand<Window> 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<DuplicateLoadingRules> GetDuplicateLoadingRules()
{
if (!IsConsiderDuplicate)
{
return new List<DuplicateLoadingRules>();
}

var listConsiderDuplicates = new List<DuplicateLoadingRules>();

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));
}
}
}
Loading

0 comments on commit 2d9415f

Please sign in to comment.