GroupDetailPage uses navigation service

This commit is contained in:
Geoffroy BONNEVILLE
2020-04-21 13:33:15 +02:00
parent c81f8bc835
commit ac66faa9e2
5 changed files with 70 additions and 46 deletions

View File

@@ -5,7 +5,7 @@ using System.Collections.Specialized;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Views;
using MediatR; using MediatR;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Database.Commands.SaveDatabase; using ModernKeePass.Application.Database.Commands.SaveDatabase;
@@ -25,9 +25,12 @@ using ModernKeePass.Application.Group.Commands.UpdateGroup;
using ModernKeePass.Application.Group.Models; using ModernKeePass.Application.Group.Models;
using ModernKeePass.Application.Group.Queries.GetGroup; using ModernKeePass.Application.Group.Queries.GetGroup;
using ModernKeePass.Application.Group.Queries.SearchEntries; using ModernKeePass.Application.Group.Queries.SearchEntries;
using ModernKeePass.Common;
using ModernKeePass.Domain.AOP; using ModernKeePass.Domain.AOP;
using ModernKeePass.Domain.Enums; using ModernKeePass.Domain.Enums;
using ModernKeePass.Interfaces; using ModernKeePass.Interfaces;
using ModernKeePass.Models;
using RelayCommand = GalaSoft.MvvmLight.Command.RelayCommand;
namespace ModernKeePass.ViewModels namespace ModernKeePass.ViewModels
{ {
@@ -77,14 +80,9 @@ namespace ModernKeePass.ViewModels
} }
} }
public bool IsRecycleOnDelete public bool IsRecycleOnDelete => Database.IsRecycleBinEnabled && !IsInRecycleBin;
{
get public bool IsInRecycleBin => _parent != null && _parent.Id == Database.RecycleBinId;
{
var database = Database;
return database.IsRecycleBinEnabled && _parent != null && _parent.Id != database.RecycleBinId;
}
}
public IEnumerable<GroupVm> BreadCrumb { get; } public IEnumerable<GroupVm> BreadCrumb { get; }
@@ -92,10 +90,13 @@ namespace ModernKeePass.ViewModels
public RelayCommand SortEntriesCommand { get; } public RelayCommand SortEntriesCommand { get; }
public RelayCommand SortGroupsCommand { get; } public RelayCommand SortGroupsCommand { get; }
public RelayCommand MoveCommand { get; } public RelayCommand MoveCommand { get; }
public RelayCommand CreateEntryCommand { get; }
public RelayCommand CreateGroupCommand { get; }
private DatabaseVm Database => _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult(); private DatabaseVm Database => _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult();
private readonly IMediator _mediator; private readonly IMediator _mediator;
private readonly INavigationService _navigation;
private readonly GroupVm _group; private readonly GroupVm _group;
private readonly GroupVm _parent; private readonly GroupVm _parent;
private bool _isEditMode; private bool _isEditMode;
@@ -103,12 +104,13 @@ namespace ModernKeePass.ViewModels
public GroupDetailVm() {} public GroupDetailVm() {}
internal GroupDetailVm(string groupId) : this(groupId, App.Services.GetRequiredService<IMediator>()) internal GroupDetailVm(string groupId) : this(groupId, App.Services.GetRequiredService<IMediator>(), App.Services.GetRequiredService<INavigationService>())
{ } { }
public GroupDetailVm(string groupId, IMediator mediator) public GroupDetailVm(string groupId, IMediator mediator, INavigationService navigation)
{ {
_mediator = mediator; _mediator = mediator;
_navigation = navigation;
_group = _mediator.Send(new GetGroupQuery { Id = groupId }).GetAwaiter().GetResult(); _group = _mediator.Send(new GetGroupQuery { Id = groupId }).GetAwaiter().GetResult();
if (!string.IsNullOrEmpty(_group.ParentGroupId)) if (!string.IsNullOrEmpty(_group.ParentGroupId))
{ {
@@ -120,20 +122,32 @@ namespace ModernKeePass.ViewModels
SortEntriesCommand = new RelayCommand(async () => await SortEntriesAsync(), () => IsEditMode); SortEntriesCommand = new RelayCommand(async () => await SortEntriesAsync(), () => IsEditMode);
SortGroupsCommand = new RelayCommand(async () => await SortGroupsAsync(), () => IsEditMode); SortGroupsCommand = new RelayCommand(async () => await SortGroupsAsync(), () => IsEditMode);
MoveCommand = new RelayCommand(async () => await Move(_parent), () => IsNotRoot); MoveCommand = new RelayCommand(async () => await Move(_parent), () => IsNotRoot);
CreateEntryCommand = new RelayCommand(async () => await AddNewEntry(), () => !IsInRecycleBin);
CreateGroupCommand = new RelayCommand(async () => await AddNewGroup(), () => !IsInRecycleBin);
Entries = new ObservableCollection<EntryVm>(_group.Entries); Entries = new ObservableCollection<EntryVm>(_group.Entries);
Entries.CollectionChanged += Entries_CollectionChanged; Entries.CollectionChanged += Entries_CollectionChanged;
Groups = new ObservableCollection<GroupVm>(_group.SubGroups); Groups = new ObservableCollection<GroupVm>(_group.SubGroups);
} }
public async Task<string> AddNewGroup(string name = "") public async Task AddNewGroup(string name = "")
{ {
return (await _mediator.Send(new CreateGroupCommand {Name = name, ParentGroup = _group})).Id; var group = await _mediator.Send(new CreateGroupCommand {Name = name, ParentGroup = _group});
_navigation.NavigateTo(Constants.Navigation.GroupPage, new NavigationItem
{
Id = group.Id,
IsNew = true
});
} }
public async Task<string> AddNewEntry() public async Task AddNewEntry()
{ {
return (await _mediator.Send(new CreateEntryCommand { ParentGroup = _group })).Id; var entry = await _mediator.Send(new CreateEntryCommand { ParentGroup = _group });
_navigation.NavigateTo(Constants.Navigation.EntryPage, new NavigationItem
{
Id = entry.Id,
IsNew = true
});
} }
public async Task MarkForDelete(string recycleBinTitle) public async Task MarkForDelete(string recycleBinTitle)

View File

@@ -48,7 +48,14 @@
<ColumnDefinition Width="{StaticResource ExpandedMenuGridLength}" x:Name="LeftListViewColumn" /> <ColumnDefinition Width="{StaticResource ExpandedMenuGridLength}" x:Name="LeftListViewColumn" />
<ColumnDefinition Width="*" /> <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<userControls:HamburgerMenuUserControl x:Uid="GroupsLeftListView" ItemsSource="{Binding Groups}" SelectionChanged="groups_SelectionChanged" ButtonClicked="CreateGroup_ButtonClick" ResizeTarget="{Binding ElementName=LeftListViewColumn}" IsOpen="True" IsButtonVisible="Visible" /> <userControls:HamburgerMenuUserControl
x:Uid="GroupsLeftListView"
ItemsSource="{Binding Groups}"
SelectionChanged="groups_SelectionChanged"
ActionButtonCommand="{Binding CreateGroupCommand}"
ResizeTarget="{Binding ElementName=LeftListViewColumn}"
IsOpen="True"
IsButtonVisible="Visible" />
<Grid Grid.Column="1"> <Grid Grid.Column="1">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" />
@@ -61,7 +68,7 @@
</Grid.RowDefinitions> </Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" x:Uid="ReorderEntriesLabel" Margin="10,10,0,0" Visibility="{Binding IsEditMode, Converter={StaticResource BooleanToVisibilityConverter}}" Style="{StaticResource BodyTextBlockStyle}" /> <TextBlock Grid.Column="0" Grid.Row="0" x:Uid="ReorderEntriesLabel" Margin="10,10,0,0" Visibility="{Binding IsEditMode, Converter={StaticResource BooleanToVisibilityConverter}}" Style="{StaticResource BodyTextBlockStyle}" />
<!--<TextBlock Grid.Column="1" Grid.Row="0" x:Uid="EntrySymbol" Margin="40,20,0,0" Visibility="{Binding IsEditMode, Converter={StaticResource BooleanToVisibilityConverter}}" Style="{StaticResource BodyTextBlockStyle}" />--> <!--<TextBlock Grid.Column="1" Grid.Row="0" x:Uid="EntrySymbol" Margin="40,20,0,0" Visibility="{Binding IsEditMode, Converter={StaticResource BooleanToVisibilityConverter}}" Style="{StaticResource BodyTextBlockStyle}" />-->
<HyperlinkButton Grid.Column="2" Grid.Row="0" VerticalAlignment="Top" Click="CreateEntry_ButtonClick" HorizontalAlignment="Right" Foreground="{StaticResource MainColor}" Style="{StaticResource MainColorHyperlinkButton}"> <HyperlinkButton Grid.Column="2" Grid.Row="0" VerticalAlignment="Top" Command="{Binding CreateEntryCommand}" HorizontalAlignment="Right" Foreground="{StaticResource MainColor}" Style="{StaticResource MainColorHyperlinkButton}">
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">
<SymbolIcon Symbol="Add"> <SymbolIcon Symbol="Add">
<ToolTipService.ToolTip> <ToolTipService.ToolTip>

View File

@@ -5,6 +5,7 @@ using Windows.Storage.Streams;
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; using Windows.UI.Xaml.Navigation;
using GalaSoft.MvvmLight.Views;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces; using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Application.Entry.Models; using ModernKeePass.Application.Entry.Models;
@@ -24,6 +25,7 @@ namespace ModernKeePass.Views
public sealed partial class GroupDetailPage public sealed partial class GroupDetailPage
{ {
private readonly IResourceProxy _resource; private readonly IResourceProxy _resource;
private readonly INavigationService _navigation;
/// <summary> /// <summary>
/// NavigationHelper is used on each page to aid in navigation and /// NavigationHelper is used on each page to aid in navigation and
@@ -32,11 +34,12 @@ namespace ModernKeePass.Views
public NavigationHelper NavigationHelper { get; } public NavigationHelper NavigationHelper { get; }
public GroupDetailVm Model => (GroupDetailVm)DataContext; public GroupDetailVm Model => (GroupDetailVm)DataContext;
public GroupDetailPage(): this (App.Services.GetRequiredService<IResourceProxy>()) { } public GroupDetailPage(): this (App.Services.GetRequiredService<IResourceProxy>(), App.Services.GetRequiredService<INavigationService>()) { }
public GroupDetailPage(IResourceProxy resource) public GroupDetailPage(IResourceProxy resource, INavigationService navigation)
{ {
InitializeComponent(); InitializeComponent();
_resource = resource; _resource = resource;
_navigation = navigation;
NavigationHelper = new NavigationHelper(this); NavigationHelper = new NavigationHelper(this);
} }
@@ -84,7 +87,7 @@ namespace ModernKeePass.Views
return; return;
default: default:
var group = listView?.SelectedItem as Application.Group.Models.GroupVm; var group = listView?.SelectedItem as Application.Group.Models.GroupVm;
Frame.Navigate(typeof(GroupDetailPage), new NavigationItem { Id = group?.Id }); _navigation.NavigateTo(Constants.Navigation.GroupPage, new NavigationItem { Id = group?.Id });
break; break;
} }
} }
@@ -97,7 +100,7 @@ namespace ModernKeePass.Views
return; return;
default: default:
var entry = GridView.SelectedItem as EntryVm; var entry = GridView.SelectedItem as EntryVm;
Frame.Navigate(typeof(EntryDetailPage), new NavigationItem { Id = entry?.Id }); _navigation.NavigateTo(Constants.Navigation.EntryPage, new NavigationItem { Id = entry?.Id });
break; break;
} }
} }
@@ -110,22 +113,6 @@ namespace ModernKeePass.Views
e.DestinationItem.Item = e.SourceItem.Item; e.DestinationItem.Item = e.SourceItem.Item;
} }
} }
private async void CreateEntry_ButtonClick(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(EntryDetailPage), new NavigationItem
{
Id = await Model.AddNewEntry(),
IsNew = true
});
}
private async void CreateGroup_ButtonClick(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(GroupDetailPage), new NavigationItem
{
Id = await Model.AddNewGroup(),
IsNew = true
});
}
private void GridView_DragItemsStarting(object sender, DragItemsStartingEventArgs e) private void GridView_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
{ {
@@ -145,7 +132,7 @@ namespace ModernKeePass.Views
private void SearchBox_OnResultSuggestionChosen(SearchBox sender, SearchBoxResultSuggestionChosenEventArgs args) private void SearchBox_OnResultSuggestionChosen(SearchBox sender, SearchBoxResultSuggestionChosenEventArgs args)
{ {
Frame.Navigate(typeof(EntryDetailPage), new NavigationItem { Id = args.Tag }); _navigation.NavigateTo(Constants.Navigation.EntryPage, new NavigationItem { Id = args.Tag });
} }
private void GroupDetailPage_OnSizeChanged(object sender, SizeChangedEventArgs e) private void GroupDetailPage_OnSizeChanged(object sender, SizeChangedEventArgs e)

View File

@@ -72,7 +72,15 @@
<DataTemplate> <DataTemplate>
<StackPanel Orientation="Vertical"> <StackPanel Orientation="Vertical">
<Border BorderBrush="White" BorderThickness="0,0,0,1" /> <Border BorderBrush="White" BorderThickness="0,0,0,1" />
<Button Padding="0" Height="{StaticResource MenuSize}" Margin="0" Visibility="{Binding IsButtonVisible, ElementName=UserControl}" Style="{StaticResource NoBorderButtonStyle}" Background="Transparent" BorderThickness="0" Width="{StaticResource ExpandedMenuSize}" HorizontalContentAlignment="Left" Click="ButtonBase_OnClick"> <Button Padding="0" Margin="0"
Height="{StaticResource MenuSize}"
Visibility="{Binding IsButtonVisible, ElementName=UserControl}"
Style="{StaticResource NoBorderButtonStyle}"
Background="Transparent"
BorderThickness="0"
Width="{StaticResource ExpandedMenuSize}"
HorizontalContentAlignment="Left"
Command="{Binding ActionButtonCommand, ElementName=UserControl}">
<StackPanel Orientation="Horizontal" Margin="17,0,5,0"> <StackPanel Orientation="Horizontal" Margin="17,0,5,0">
<SymbolIcon Symbol="Add"> <SymbolIcon Symbol="Add">
<ToolTipService.ToolTip> <ToolTipService.ToolTip>

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
using GalaSoft.MvvmLight.Command;
using ModernKeePass.Application.Common.Interfaces; using ModernKeePass.Application.Common.Interfaces;
// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236 // The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236
@@ -124,16 +125,23 @@ namespace ModernKeePass.Views.UserControls
typeof(HamburgerMenuUserControl), typeof(HamburgerMenuUserControl),
new PropertyMetadata(false, (o, args) => { })); new PropertyMetadata(false, (o, args) => { }));
public RelayCommand ActionButtonCommand
{
get { return (RelayCommand)GetValue(ActionButtonCommandProperty); }
set { SetValue(ActionButtonCommandProperty, value); }
}
public static readonly DependencyProperty ActionButtonCommandProperty =
DependencyProperty.Register(
nameof(ActionButtonCommand),
typeof(RelayCommand),
typeof(HamburgerMenuUserControl),
new PropertyMetadata(null, (o, args) => { }));
public event EventHandler<SelectionChangedEventArgs> SelectionChanged; public event EventHandler<SelectionChangedEventArgs> SelectionChanged;
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e) private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{ {
SelectionChanged?.Invoke(sender, e); SelectionChanged?.Invoke(sender, e);
} }
public event EventHandler<RoutedEventArgs> ButtonClicked;
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
ButtonClicked?.Invoke(sender, e);
}
} }
} }