Skip to content

Commit

Permalink
Adding New Views
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanfaciol committed Oct 8, 2023
1 parent 54a5d5e commit 94a8624
Show file tree
Hide file tree
Showing 31 changed files with 443 additions and 23 deletions.
53 changes: 43 additions & 10 deletions WPF.MVVM.Navigation/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ public App()
services.AddSingleton<NavigationStore>();
services.AddSingleton<AccountStore>();
services.AddSingleton<ModalNavigationStore>();
services.AddSingleton<PeopleStore>();

services.AddSingleton<INavigationService>(s => CreateHomeNavigationService(s));
services.AddSingleton<CloseModalNavigationService>();

services.AddTransient<HomeViewModel>(s => new HomeViewModel(CreateLoginNavigationService(s)));
services.AddTransient<AccountViewModel>(s => new AccountViewModel(s.GetRequiredService<AccountStore>(), CreateHomeNavigationService(s)));
services.AddTransient<LoginViewModel>(CreateLoginViewModel);
services.AddTransient<PeopleListingViewModel>(s => new PeopleListingViewModel(CreateCreatePersonNavigationService(s), s.GetRequiredService<PeopleStore>()));
services.AddTransient<CreatePersonViewModel>(s => new CreatePersonViewModel(s.GetRequiredService<CloseModalNavigationService>(), s.GetRequiredService<PeopleStore>()));
services.AddTransient<NavigationBarViewModel>(CreateNavigationBarViewModel);

services.AddSingleton<MainViewModel>();
Expand Down Expand Up @@ -61,38 +64,68 @@ private INavigationService CreateHomeNavigationService(IServiceProvider serviceP
NavigationStore navigationStore = serviceProvider.GetRequiredService<NavigationStore>();
HomeViewModel homeViewModel = serviceProvider.GetRequiredService<HomeViewModel>();

return new LayoutNavigationService<HomeViewModel>(navigationStore, () => homeViewModel, () => serviceProvider.GetRequiredService<NavigationBarViewModel>());
return new LayoutNavigationService<HomeViewModel>(
navigationStore,
() => homeViewModel,
() => serviceProvider.GetRequiredService<NavigationBarViewModel>());
}

private INavigationService CreateAccountNavigationService(IServiceProvider serviceProvider)
{
NavigationStore navigationStore = serviceProvider.GetRequiredService<NavigationStore>();
AccountStore accountStore = serviceProvider.GetRequiredService<AccountStore>();

return new LayoutNavigationService<AccountViewModel>(navigationStore, () => serviceProvider.GetRequiredService<AccountViewModel>(), () => serviceProvider.GetRequiredService<NavigationBarViewModel>());
return new LayoutNavigationService<AccountViewModel>(
navigationStore,
() => serviceProvider.GetRequiredService<AccountViewModel>(),
() => serviceProvider.GetRequiredService<NavigationBarViewModel>());
}

private INavigationService CreateLoginNavigationService(IServiceProvider serviceProvider)
{
ModalNavigationStore modalNavigationStore = serviceProvider.GetRequiredService<ModalNavigationStore>();

CompositeNavigationService compositeNavigationService = new CompositeNavigationService(new CloseModalNavigationService(modalNavigationStore), CreateAccountNavigationService(serviceProvider));

return new ModalNavigationService<LoginViewModel>(modalNavigationStore, () => serviceProvider.GetRequiredService<LoginViewModel>());
return new ModalNavigationService<LoginViewModel>(
serviceProvider.GetRequiredService<ModalNavigationStore>(),
() => serviceProvider.GetRequiredService<LoginViewModel>());
}

private NavigationBarViewModel CreateNavigationBarViewModel(IServiceProvider serviceProvider)
{
AccountStore accountStore = serviceProvider.GetRequiredService<AccountStore>();

return new NavigationBarViewModel(CreateHomeNavigationService(serviceProvider), CreateAccountNavigationService(serviceProvider), CreateLoginNavigationService(serviceProvider), accountStore);
return new NavigationBarViewModel(
CreateHomeNavigationService(serviceProvider),
CreateAccountNavigationService(serviceProvider),
CreateLoginNavigationService(serviceProvider),
accountStore,
CreatePeopleListingNavigationService(serviceProvider));
}

private LoginViewModel CreateLoginViewModel(IServiceProvider serviceProvider)
{
CompositeNavigationService compositeNavigationService = new CompositeNavigationService(serviceProvider.GetRequiredService<CloseModalNavigationService>(), CreateAccountNavigationService(serviceProvider));
CompositeNavigationService compositeNavigationService = new CompositeNavigationService(
serviceProvider.GetRequiredService<CloseModalNavigationService>(),
CreateAccountNavigationService(serviceProvider));

return new LoginViewModel(
serviceProvider.GetRequiredService<AccountStore>(),
compositeNavigationService);
}

return new LoginViewModel(serviceProvider.GetRequiredService<AccountStore>(), compositeNavigationService);
private INavigationService CreatePeopleListingNavigationService(IServiceProvider serviceProvider)
{
NavigationStore navigationStore = serviceProvider.GetRequiredService<NavigationStore>();
AccountStore accountStore = serviceProvider.GetRequiredService<AccountStore>();

return new LayoutNavigationService<PeopleListingViewModel>(serviceProvider.GetRequiredService<NavigationStore>(),
() => serviceProvider.GetRequiredService<PeopleListingViewModel>(),
() => serviceProvider.GetRequiredService<NavigationBarViewModel>());
}

private INavigationService CreateCreatePersonNavigationService(IServiceProvider serviceProvider)
{
return new ModalNavigationService<CreatePersonViewModel>(
serviceProvider.GetRequiredService<ModalNavigationStore>(),
() => serviceProvider.GetRequiredService<CreatePersonViewModel>());
}
#endregion
}
Expand Down
34 changes: 34 additions & 0 deletions WPF.MVVM.Navigation/Commands/CreatePersonCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using WPF.MVVM.Navigation.Services;
using WPF.MVVM.Navigation.Stores;
using WPF.MVVM.Navigation.ViewModels;

namespace WPF.MVVM.Navigation.Commands
{
public class CreatePersonCommand : CommandBase
{
#region Fields
private readonly CreatePersonViewModel _createPersonViewModel;
private INavigationService _navigationService;
private readonly PeopleStore _peopleStore;
#endregion

#region Constructor
public CreatePersonCommand(CreatePersonViewModel createPersonViewModel, INavigationService navigationService, PeopleStore peopleStore)
{
_createPersonViewModel = createPersonViewModel;
_navigationService = navigationService;
_peopleStore = peopleStore;
}
#endregion

#region CommandBase
public override void Execute(object? parameter)
{
string name = _createPersonViewModel.Name;
_peopleStore.Create(name);

_navigationService.Navigate();
}
#endregion
}
}
9 changes: 8 additions & 1 deletion WPF.MVVM.Navigation/Components/NavigationBar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>

<TextBlock
Expand All @@ -66,10 +67,16 @@
<Button
Grid.Column="3"
Margin="10,0,0,0"
Command="{Binding NavigatePeopleListingCommand}"
Content="People"
Visibility="{Binding IsLoggedIn, Converter={StaticResource BooleanToVisibilityConverter}}" />
<Button
Grid.Column="4"
Margin="10,0,0,0"
Command="{Binding NavigateLoginCommand}"
Content="Login" />
<Button
Grid.Column="4"
Grid.Column="5"
Margin="10,0,0,0"
Command="{Binding LogoutCommand}"
Content="Logout"
Expand Down
6 changes: 6 additions & 0 deletions WPF.MVVM.Navigation/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
<DataTemplate DataType="{x:Type viewmodels:LoginViewModel}">
<views:LoginView />
</DataTemplate>
<DataTemplate DataType="{x:Type viewmodels:PeopleListingViewModel}">
<views:PeopleListingView />
</DataTemplate>
<DataTemplate DataType="{x:Type viewmodels:CreatePersonViewModel}">
<views:CreatePersonView />
</DataTemplate>
<DataTemplate DataType="{x:Type viewmodels:LayoutViewModel}">
<components:Layout />
</DataTemplate>
Expand Down
18 changes: 18 additions & 0 deletions WPF.MVVM.Navigation/Stores/PeopleStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace WPF.MVVM.Navigation.Stores
{
public class PeopleStore
{
#region Events
public event Action<string> PersonCreated;
#endregion

#region Helper methods
public void Create(string name)
{
PersonCreated?.Invoke(name);
}
#endregion
}
}
31 changes: 31 additions & 0 deletions WPF.MVVM.Navigation/ViewModels/CreatePersonViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Windows.Input;
using WPF.MVVM.Navigation.Commands;
using WPF.MVVM.Navigation.Services;
using WPF.MVVM.Navigation.Stores;

namespace WPF.MVVM.Navigation.ViewModels
{
public class CreatePersonViewModel : ViewModelBase
{
#region Properties
private string _name;

public string Name
{
get { return _name; }
set { _name = value; }
}

public ICommand SubmitCommand { get; }
public ICommand CancelCommand { get; }
#endregion

#region Constructor
public CreatePersonViewModel(INavigationService cancelNavigationService, PeopleStore peopleStore)
{
SubmitCommand = new CreatePersonCommand(this, cancelNavigationService, peopleStore);
CancelCommand = new NavigateCommand(cancelNavigationService);
}
#endregion
}
}
9 changes: 8 additions & 1 deletion WPF.MVVM.Navigation/ViewModels/NavigationBarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,25 @@ public class NavigationBarViewModel : ViewModelBase
public ICommand NavigateHomeCommand { get; }
public ICommand NavigateAccountCommand { get; }
public ICommand NavigateLoginCommand { get; }
public ICommand NavigatePeopleListingCommand { get; }
public bool IsLoggedIn => _accountStore.IsLoggedIn;
public ICommand LogoutCommand { get; }
#endregion

#region Constructor
public NavigationBarViewModel(INavigationService homeNavigationService, INavigationService accountNavigationService, INavigationService loginNavigationService, AccountStore accountStore)
public NavigationBarViewModel(
INavigationService homeNavigationService,
INavigationService accountNavigationService,
INavigationService loginNavigationService,
AccountStore accountStore,
INavigationService peopleListingNavigationService)
{
_accountStore = accountStore;

NavigateHomeCommand = new NavigateCommand(homeNavigationService);
NavigateAccountCommand = new NavigateCommand(accountNavigationService);
NavigateLoginCommand = new NavigateCommand(loginNavigationService);
NavigatePeopleListingCommand = new NavigateCommand(peopleListingNavigationService);
LogoutCommand = new LogoutCommand(_accountStore);

_accountStore.CurrentAccountChanged += AccountStore_CurrentAccountChanged;
Expand Down
18 changes: 18 additions & 0 deletions WPF.MVVM.Navigation/ViewModels/PeopleListingItemViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using WPF.MVVM.Navigation.Stores;

namespace WPF.MVVM.Navigation.ViewModels
{
public class PeopleListingItemViewModel : ViewModelBase
{
#region Properties
public string Name { get; }
#endregion

#region Constructor
public PeopleListingItemViewModel(string name)
{
Name = name;
}
#endregion
}
}
46 changes: 46 additions & 0 deletions WPF.MVVM.Navigation/ViewModels/PeopleListingViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Xml.Linq;
using WPF.MVVM.Navigation.Commands;
using WPF.MVVM.Navigation.Services;
using WPF.MVVM.Navigation.Stores;

namespace WPF.MVVM.Navigation.ViewModels
{
public class PeopleListingViewModel : ViewModelBase
{
#region Fields
private readonly ObservableCollection<PeopleListingItemViewModel> _peopleListingItemViewModel;
private readonly PeopleStore _peopleStore;
#endregion

#region Properties
public IEnumerable<PeopleListingItemViewModel> PeopleListingItemViewModel => _peopleListingItemViewModel;
public ICommand CreatePersonCommand { get; }
#endregion

#region Constructor
public PeopleListingViewModel(INavigationService createPersonNavigationService, PeopleStore peopleStore)
{
_peopleListingItemViewModel = new ObservableCollection<PeopleListingItemViewModel>();
_peopleStore = peopleStore;

CreatePersonCommand = new NavigateCommand(createPersonNavigationService);

_peopleListingItemViewModel.Add(new PeopleListingItemViewModel("a"));
_peopleListingItemViewModel.Add(new PeopleListingItemViewModel("b"));
_peopleListingItemViewModel.Add(new PeopleListingItemViewModel("c"));

_peopleStore.PersonCreated += PeopleStore_PersonCreated;
}

#region PersonCreated Subscribed
private void PeopleStore_PersonCreated(string name)
{
_peopleListingItemViewModel.Add(new PeopleListingItemViewModel(name));
}
#endregion
#endregion
}
}
50 changes: 50 additions & 0 deletions WPF.MVVM.Navigation/Views/CreatePersonView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<UserControl
x:Class="WPF.MVVM.Navigation.Views.CreatePersonView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WPF.MVVM.Navigation.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>

<TextBlock
Grid.Row="0"
FontSize="28"
Text="Add Person" />

<Grid Grid.Row="1" Margin="0,20,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>

<TextBlock Grid.Row="0" Text="Name" />
<TextBox
Grid.Row="1"
Margin="0,5,0,0"
Text="{Binding Name}" />
</Grid>

<WrapPanel Grid.Row="2" Margin="0,20,0,0">
<Button
Width="100"
Height="30"
Command="{Binding SubmitCommand}"
Content="Submit" />
<Button
Width="150"
Height="30"
Margin="20,0,0,0"
Command="{Binding CancelCommand}"
Content="Cancel" />
</WrapPanel>
</Grid>
</UserControl>
28 changes: 28 additions & 0 deletions WPF.MVVM.Navigation/Views/CreatePersonView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPF.MVVM.Navigation.Views
{
/// <summary>
/// Interaction logic for CreatePersonView.xaml
/// </summary>
public partial class CreatePersonView : UserControl
{
public CreatePersonView()
{
InitializeComponent();
}
}
}
Loading

0 comments on commit 94a8624

Please sign in to comment.