Skip to content

Commit

Permalink
WatchList.WPF - Implemented the ability to add an item.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan-Kudri committed Dec 7, 2024
1 parent 3d7ad13 commit 85ed645
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 137 deletions.
3 changes: 2 additions & 1 deletion WatchList.WPF/Extension/AppServiceDIExtension.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using DevExpress.Mvvm.POCO;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using WatchList.Core.Repository;
using WatchList.Core.Service;
using WatchList.Core.Service.Component;
using WatchList.Core.Service.DataLoading;
using WatchList.Migrations.SQLite;
using WatchList.WPF.Data;
using WatchList.WPF.Models;
using WatchList.WPF.Models.Filter;
using WatchList.WPF.Models.ModelDataLoad;
using WatchList.WPF.Models.Sorter;
Expand All @@ -22,6 +22,7 @@ public static IServiceCollection AppServiceContainer(this IServiceCollection ser
.AddScoped(e => e.GetRequiredService<DbContextFactoryMigrator>().Create())
.AddScoped<WatchItemRepository>()
.AddScoped<IMessageBox, MessageWindow>()
.AddScoped<WatchItemCreator>()
.AddScoped<WatchItemService>()
.AddScoped<DownloadDataService>()
.AddScoped<SortWatchItemModel>()
Expand Down
110 changes: 0 additions & 110 deletions WatchList.WPF/Models/CinemaModel.cs

This file was deleted.

30 changes: 30 additions & 0 deletions WatchList.WPF/Models/WatchItemCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using WatchList.Core.Model.ItemCinema;
using WatchList.Core.Model.ItemCinema.Components;

namespace WatchList.WPF.Models
{
public class WatchItemCreator
{
public WatchItemCreator()
{
}

public WatchItem CreateNonPlanned(
string title,
int sequel,
StatusCinema status,
TypeCinema type,
DateTime? date = null,
int? grade = null,
Guid? id = null)
=> new WatchItem(title, sequel, status, type, id, date, grade);

public WatchItem CreatePlanned(
string title,
int sequel,
StatusCinema status,
TypeCinema type,
Guid? id)
=> new WatchItem(title, sequel, status, type, id, null, null);
}
}
17 changes: 14 additions & 3 deletions WatchList.WPF/ViewModel/CinemaPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public ObservableCollection<WatchItem> WatchItems

public string PageDisplayText => $"Page {CurPage} of {_pagedList.PageCount}";

public List<CinemaModel> PageWatchItems { get; set; }
public List<WatchItemCreator> PageWatchItems { get; set; }

public RelayCommandApp MoveToPreviousPage
=> new RelayCommandApp(async _ => await LoadDataAsyncPage(--Page.Number), _ => _pagedList.HasPreviousPage);
Expand All @@ -83,12 +83,23 @@ public RelayCommandApp MoveToNextPage
public RelayCommandApp MoveToLastPage
=> new RelayCommandApp(async _ => await LoadDataAsyncPage(_pagedList.PageCount), _ => _pagedList.HasNextPage);

public RelayCommandApp MoveAddItem
=> new RelayCommandApp(_ => new AddCinemaWindow().Show());
public RelayCommandApp AddItemCommand
=> new RelayCommandApp(async async => await MoveAddItem());

public RelayCommandApp MoveAddDataDB
=> new RelayCommandApp(_ => new MergeDatabaseWindow().Show());

private async Task MoveAddItem()
{
var addWindow = new AddCinemaWindow();
if (addWindow.ShowDialog() != true)
{
return;
}

await LoadDataAsync();
}

/// <summary>
/// Load data in table.
/// </summary>
Expand Down
27 changes: 17 additions & 10 deletions WatchList.WPF/ViewModel/ItemsView/AddCinemaViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class AddCinemaViewModel : BindableBase
{
private readonly IMessageBox _messageBox;
private readonly WatchItemRepository _watchItemRepository;
private readonly WatchItemCreator _watchItemCreator;

private bool _isWatch;

Expand All @@ -28,19 +29,21 @@ public class AddCinemaViewModel : BindableBase
private DateTime? _date;
private int? _grade;

public AddCinemaViewModel(IMessageBox messageBox, WatchItemRepository watchItemRepository)
: this(messageBox, watchItemRepository, null)
public AddCinemaViewModel(IMessageBox messageBox, WatchItemRepository watchItemRepository, WatchItemCreator watchItemCreator)
: this(messageBox, watchItemRepository, watchItemCreator, null)
{
}

public AddCinemaViewModel(IMessageBox messageBox, WatchItemRepository watchItemRepository, WatchItem? watchItem = null)
public AddCinemaViewModel(IMessageBox messageBox, WatchItemRepository watchItemRepository, WatchItemCreator watchItemCreator, WatchItem? watchItem = null)
{
_messageBox = messageBox;
_watchItemRepository = watchItemRepository;
_watchItemCreator = watchItemCreator;
SetValueCinema(watchItem);
MaxDateWatched = DateTime.Now;
MinDateWatched = new DateTime(1945, 1, 1);
LabelSequelType = SelectedTypeCinema.TypeSequel;
AddItemCommand = new RelayCommand<Window>(MoveAddCinema);
SetDefoultValueCommand = new RelayCommand(SetDefaultValues);
CloseWindowCommand = new RelayCommand<Window>(CloseWindow);
}
Expand Down Expand Up @@ -77,6 +80,7 @@ public bool IsWatch
public IEnumerable<StatusCinema> ListStatus => StatusCinema.List;
public IEnumerable<TypeCinema> ListType => TypeCinema.List;

public RelayCommand<Window> AddItemCommand { get; private set; }
public RelayCommand SetDefoultValueCommand { get; private set; }
public RelayCommand<Window> CloseWindowCommand { get; private set; }

Expand Down Expand Up @@ -134,13 +138,12 @@ private async void MoveAddCinema(Window currentWindowAdd)
if (!ValidateFields(out var errorMessage))
{
await _messageBox.ShowWarning(errorMessage);
return;
}
else
{
var cinemaModel = new CinemaModel(SetTitle, SetSequel, SetDateTime, SetGrade, SelectedStatusCinema, SelectedTypeCinema, SetId);
_watchItemRepository.Add(cinemaModel.ToWatchItem());
currentWindowAdd.Close();
}

currentWindowAdd.DialogResult = true;
_watchItemRepository.Add(GetCinema());
currentWindowAdd.Close();
}

private void CloseWindow(Window window) => window?.Close();
Expand All @@ -151,7 +154,6 @@ private void SetDefaultValues()
SetSequel = 1;
SelectedTypeCinema = TypeCinema.Movie;
SelectedStatusCinema = StatusCinema.Planned;
_isWatch = false;
}

private bool ValidateFields(out string errorMessage)
Expand Down Expand Up @@ -191,5 +193,10 @@ private void SetValueCinema(WatchItem? watchItem)
SelectedStatusCinema = watchItem.Status;
SelectedTypeCinema = watchItem.Type;
}

public WatchItem GetCinema()
=> SelectedStatusCinema == StatusCinema.Planned
? _watchItemCreator.CreatePlanned(SetTitle, SetSequel, SelectedStatusCinema, SelectedTypeCinema, SetId)
: _watchItemCreator.CreateNonPlanned(SetTitle, SetSequel, SelectedStatusCinema, SelectedTypeCinema, SetDateTime, SetGrade, SetId);
}
}
2 changes: 1 addition & 1 deletion WatchList.WPF/Views/CinemaPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
AutomationProperties.Name="Btn_Add_Item"
Background="AliceBlue"
Focusable="False"
Command="{Binding MoveAddItem}"/>
Command="{Binding AddItemCommand}"/>
<Button
Content="Edit"
HorizontalAlignment="Stretch"
Expand Down
26 changes: 14 additions & 12 deletions WatchList.WPF/Views/CinemaView/AddCinemaWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,24 @@
Grid.Row="3"
Grid.ColumnSpan="8">
<Button Width="90" Height="30"
Content="Add"
Focusable="False"
Margin="5 5 5 5">
Content="Add"
Focusable="False"
Margin="5 5 5 5"
Command="{Binding AddItemCommand}"
CommandParameter="{Binding ElementName=AddItem}">
</Button>
<Button Width="90" Height="30"
Content="Clear"
Margin="5 5 5 5"
Focusable="False"
Command="{Binding SetDefoultValueCommand}">
Content="Clear"
Margin="5 5 5 5"
Focusable="False"
Command="{Binding SetDefoultValueCommand}">
</Button>
<Button Width="90" Height="30"
Content="Close"
Margin="5 5 5 5"
Focusable="False"
Command="{Binding CloseWindowCommand}"
CommandParameter="{Binding ElementName=AddItem}">
Content="Close"
Margin="5 5 5 5"
Focusable="False"
Command="{Binding CloseWindowCommand}"
CommandParameter="{Binding ElementName=AddItem}">
</Button>
</StackPanel>

Expand Down

0 comments on commit 85ed645

Please sign in to comment.