File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11using System . Windows ;
22using System . Windows . Threading ;
3+ using Desktop . Tasks ;
34using Desktop . Tasks . Extensions ;
45using Microsoft . Extensions . Configuration ;
56using Microsoft . Extensions . DependencyInjection ;
@@ -21,6 +22,8 @@ protected override void OnStartup(StartupEventArgs e)
2122 var services = new ServiceCollection ( ) ;
2223
2324 services . AddSingleton < MainWindow > ( ) ;
25+ services . AddSingleton < TaskCreation > ( ) ;
26+ services . AddTransient < TaskCreationViewModel > ( ) ;
2427 services . AddBackendTaskRepository ( Config ) ;
2528
2629 var serviceProvider = services . BuildServiceProvider ( ) ;
Original file line number Diff line number Diff line change @@ -7,17 +7,21 @@ namespace Desktop;
77public partial class MainWindow : Window
88{
99 private readonly ITaskRepository taskRepository ;
10+ private readonly TaskCreationViewModel taskCreationViewModel ;
1011
11- public MainWindow ( ITaskRepository taskRepository )
12+ public MainWindow (
13+ ITaskRepository taskRepository ,
14+ TaskCreationViewModel taskCreationViewModel )
1215 {
1316 this . taskRepository = taskRepository ;
17+ this . taskCreationViewModel = taskCreationViewModel ;
1418 InitializeComponent ( ) ;
1519 NavigateToTaskList ( ) ;
1620 }
1721
1822 private void NavigateToTaskList ( )
1923 {
20- var tasksList = new TasksList ( taskRepository ) ;
24+ var tasksList = new TasksList ( taskRepository , taskCreationViewModel ) ;
2125 Content . Content = tasksList ;
2226 }
2327
Original file line number Diff line number Diff line change @@ -12,11 +12,15 @@ namespace Desktop.Project;
1212public partial class TasksList : UserControl
1313{
1414 private readonly ITaskRepository taskRepository ;
15+ private readonly TaskCreationViewModel taskCreationViewModel ;
1516 private readonly ObservableCollection < Task > tasks = [ ] ;
1617
17- public TasksList ( ITaskRepository taskRepository )
18+ public TasksList (
19+ ITaskRepository taskRepository ,
20+ TaskCreationViewModel taskCreationViewModel )
1821 {
1922 this . taskRepository = taskRepository ;
23+ this . taskCreationViewModel = taskCreationViewModel ;
2024
2125 InitializeComponent ( ) ;
2226 Tasks . ItemsSource = tasks ;
@@ -29,14 +33,13 @@ public TasksList(ITaskRepository taskRepository)
2933
3034 private void Add ( object sender , RoutedEventArgs e )
3135 {
32- var window = new TaskCreation ( ) ;
33- window . ShowDialog ( ) ;
36+ var taskCreationWindow = new TaskCreation ( taskCreationViewModel ) ;
37+ taskCreationWindow . ShowDialog ( ) ;
3438
35- if ( window . CreatedTask is null )
39+ if ( taskCreationWindow . CreatedTask is null )
3640 return ;
3741
38- tasks . Add ( window . CreatedTask ) ;
39- taskRepository . Save ( window . CreatedTask ) ;
42+ tasks . Add ( taskCreationWindow . CreatedTask ) ;
4043 }
4144
4245 private void Edit ( object sender , MouseButtonEventArgs e )
Original file line number Diff line number Diff line change 1+ using System . Globalization ;
2+ using System . Windows . Data ;
3+ using Desktop . Common ;
4+
5+ namespace Desktop . Tasks . Converters ;
6+
7+ public class SaveCommandConverter : IMultiValueConverter
8+ {
9+ public object Convert (
10+ object [ ] values ,
11+ Type targetType ,
12+ object parameter ,
13+ CultureInfo culture )
14+ {
15+ return ( values [ 0 ] as ICloseable , values [ 1 ] as string , values [ 2 ] as string ) ;
16+ }
17+
18+ public object [ ] ConvertBack (
19+ object value ,
20+ Type [ ] targetTypes ,
21+ object parameter ,
22+ CultureInfo culture )
23+ {
24+ throw new NotSupportedException ( ) ;
25+ }
26+ }
Original file line number Diff line number Diff line change 33 xmlns : x =" http://schemas.microsoft.com/winfx/2006/xaml"
44 xmlns : d =" http://schemas.microsoft.com/expression/blend/2008"
55 xmlns : mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"
6- xmlns : local =" clr-namespace:Desktop.Tasks"
6+ xmlns : converters =" clr-namespace:Desktop.Tasks.Converters "
77 mc : Ignorable =" d"
88 x : Name =" TaskCreationWindow"
99 Title =" Task creation" Height =" 225" Width =" 400"
1010 WindowStartupLocation =" CenterScreen" >
11- <Window .DataContext >
12- <local : TaskCreationViewModel />
13- </Window .DataContext >
11+ <Window .Resources >
12+ <converters : SaveCommandConverter x : Key = " SaveCommandConverter " />
13+ </Window .Resources >
1414 <Grid FocusManager.FocusedElement=" {Binding ElementName=Name}" >
1515 <Grid .RowDefinitions>
1616 <RowDefinition Height =" Auto" />
2626 VerticalAlignment =" Stretch" />
2727 <Button AutomationProperties.AutomationId=" SaveTask" Grid.Row=" 4" Content =" Add" HorizontalAlignment =" Right"
2828 IsDefault =" True"
29- Click =" SaveTask"
30- Command =" {Binding SaveTask }"
31- CommandParameter =" {Binding ElementName=TaskCreationWindow}" />
29+ Command =" {Binding SaveTask }" >
30+ <Button .CommandParameter>
31+ <MultiBinding Converter =" {StaticResource SaveCommandConverter}" >
32+ <Binding ElementName =" TaskCreationWindow" />
33+ <Binding ElementName =" Name" Path =" Text" />
34+ <Binding ElementName =" Description" Path =" Text" />
35+ </MultiBinding >
36+ </Button .CommandParameter>
37+ </Button >
3238 </Grid >
3339</Window >
Original file line number Diff line number Diff line change @@ -8,15 +8,14 @@ namespace Desktop.Tasks;
88
99public partial class TaskCreation : Window , ICloseable
1010{
11- public TaskCreation ( )
11+ private readonly TaskCreationViewModel viewModel ;
12+
13+ public TaskCreation ( TaskCreationViewModel viewModel )
1214 {
1315 InitializeComponent ( ) ;
16+ DataContext = viewModel ;
17+ this . viewModel = viewModel ;
1418 }
1519
16- public Task ? CreatedTask { get ; private set ; }
17-
18- private void SaveTask ( object sender , RoutedEventArgs e )
19- {
20- CreatedTask = new Task ( name : Name . Text , description : Description . Text ) ;
21- }
20+ public Task ? CreatedTask => viewModel . CreatedTask ;
2221}
Original file line number Diff line number Diff line change 1- using System . Windows . Input ;
2- using CommunityToolkit . Mvvm . Input ;
1+ using CommunityToolkit . Mvvm . Input ;
32using Desktop . Common ;
3+ using Task = Desktop . Domain . Task ;
44
55namespace Desktop . Tasks ;
66
77public class TaskCreationViewModel : ViewModelBase
88{
9- public ICommand SaveTask { get ; } =
10- new RelayCommand < ICloseable > ( closeable => { closeable . Close ( ) ; } ) ;
9+ public TaskCreationViewModel ( ITaskRepository taskRepository )
10+ {
11+ SaveTask =
12+ new RelayCommand < ( ICloseable , string , string ) > ( ( args ) =>
13+ {
14+ var ( closeable , taskName , taskDescription ) = args ;
15+
16+ CreatedTask = new Task ( taskName , taskDescription ) ;
17+ taskRepository . Save ( CreatedTask ) ;
18+ closeable ! . Close ( ) ;
19+ } ) ;
20+ }
21+
22+ public IRelayCommand SaveTask { get ; }
23+ public Task ? CreatedTask { get ; private set ; }
1124}
You can’t perform that action at this time.
0 commit comments