Implementing Recent Files WIP

Code refactoring
Entry coloring
This commit is contained in:
bg45
2017-09-29 17:23:35 -04:00
committed by BONNEVILLE Geoffroy
parent 817f25e8a8
commit 1ca3f29da0
12 changed files with 197 additions and 34 deletions

View File

@@ -29,21 +29,21 @@ namespace ModernKeePass
new MainMenuItem {Title = "Open", PageType = typeof(OpenDatabasePage), Destination = MenuFrame, Parameter = Frame, SymbolIcon = Symbol.Page2}, new MainMenuItem {Title = "Open", PageType = typeof(OpenDatabasePage), Destination = MenuFrame, Parameter = Frame, SymbolIcon = Symbol.Page2},
new MainMenuItem {Title = "New" /*, PageType = typeof(NewDatabasePage)*/, Destination = MenuFrame, SymbolIcon = Symbol.Add}, new MainMenuItem {Title = "New" /*, PageType = typeof(NewDatabasePage)*/, Destination = MenuFrame, SymbolIcon = Symbol.Add},
new MainMenuItem {Title = "Save" , PageType = typeof(SaveDatabasePage), Destination = MenuFrame, Parameter = Frame, SymbolIcon = Symbol.Save}, new MainMenuItem {Title = "Save" , PageType = typeof(SaveDatabasePage), Destination = MenuFrame, Parameter = Frame, SymbolIcon = Symbol.Save},
new MainMenuItem {Title = "Recent" /*, PageType = typeof(RecentDatabasesPage)*/, Destination = MenuFrame, SymbolIcon = Symbol.Copy} new MainMenuItem {Title = "Recent" , PageType = typeof(RecentDatabasesPage), Destination = MenuFrame, Parameter = Frame, SymbolIcon = Symbol.Copy}
}; };
var app = (App)Application.Current; var app = (App)Application.Current;
if (app.Database != null && app.Database.IsOpen) if (app.Database != null && app.Database.IsOpen)
mainMenuItems.Add(new MainMenuItem { Title = app.Database.Name, PageType = typeof(GroupDetailPage), Destination = Frame, Parameter = app.Database.RootGroup, Group = 1, SymbolIcon = Symbol.ProtectedDocument}); mainMenuItems.Add(new MainMenuItem { Title = app.Database.Name, PageType = typeof(GroupDetailPage), Destination = Frame, Parameter = app.Database.RootGroup, Group = 1, SymbolIcon = Symbol.ProtectedDocument});
var result = from item in mainMenuItems group item by item.Group into grp orderby grp.Key select grp;
MenuItemsSource.Source = result; var mainVm = DataContext as MainVm;
mainVm.MainMenuItems = from item in mainMenuItems group item by item.Group into grp orderby grp.Key select grp;
mainVm.NotifyPropertyChanged("MainMenuItems");
} }
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e) private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ {
var listView = sender as ListView; var mainMenuItem = e.AddedItems[0] as MainMenuItem;
if (listView == null) return; mainMenuItem?.Destination.Navigate(mainMenuItem.PageType, mainMenuItem.Parameter);
var selectedItem = listView.SelectedItem as MainMenuItem;
selectedItem?.Destination.Navigate(selectedItem.PageType, selectedItem.Parameter);
} }
} }
} }

View File

@@ -0,0 +1,8 @@
namespace ModernKeePass.Models
{
public class RecentItem
{
public string Token { get; set; }
public string Name { get; set; }
}
}

View File

@@ -123,6 +123,7 @@
</Compile> </Compile>
<Compile Include="Mappings\PwIconToSegoeMapping.cs" /> <Compile Include="Mappings\PwIconToSegoeMapping.cs" />
<Compile Include="Models\MainMenuItem.cs" /> <Compile Include="Models\MainMenuItem.cs" />
<Compile Include="Models\RecentItem.cs" />
<Compile Include="Pages\EntryDetailPage.xaml.cs"> <Compile Include="Pages\EntryDetailPage.xaml.cs">
<DependentUpon>EntryDetailPage.xaml</DependentUpon> <DependentUpon>EntryDetailPage.xaml</DependentUpon>
</Compile> </Compile>
@@ -132,6 +133,9 @@
<Compile Include="Pages\OpenDatabasePage.xaml.cs"> <Compile Include="Pages\OpenDatabasePage.xaml.cs">
<DependentUpon>OpenDatabasePage.xaml</DependentUpon> <DependentUpon>OpenDatabasePage.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Pages\RecentDatabasesPage.xaml.cs">
<DependentUpon>RecentDatabasesPage.xaml</DependentUpon>
</Compile>
<Compile Include="Pages\SaveDatabasePage.xaml.cs"> <Compile Include="Pages\SaveDatabasePage.xaml.cs">
<DependentUpon>SaveDatabasePage.xaml</DependentUpon> <DependentUpon>SaveDatabasePage.xaml</DependentUpon>
</Compile> </Compile>
@@ -140,6 +144,7 @@
<Compile Include="ViewModels\EntryVm.cs" /> <Compile Include="ViewModels\EntryVm.cs" />
<Compile Include="ViewModels\GroupVm.cs" /> <Compile Include="ViewModels\GroupVm.cs" />
<Compile Include="ViewModels\MainVm.cs" /> <Compile Include="ViewModels\MainVm.cs" />
<Compile Include="ViewModels\RecentVm.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<AppxManifest Include="Package.appxmanifest"> <AppxManifest Include="Package.appxmanifest">
@@ -173,6 +178,10 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</Page> </Page>
<Page Include="Pages\RecentDatabasesPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Pages\SaveDatabasePage.xaml"> <Page Include="Pages\SaveDatabasePage.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
@@ -183,7 +192,7 @@
<HintPath>..\packages\Portable.BouncyCastle.1.8.1.3\lib\netstandard1.0\BouncyCastle.Crypto.dll</HintPath> <HintPath>..\packages\Portable.BouncyCastle.1.8.1.3\lib\netstandard1.0\BouncyCastle.Crypto.dll</HintPath>
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>
<Reference Include="ModernKeePassLib, Version=2.28.1.27501, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="ModernKeePassLib, Version=2.28.1.30896, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\ModernKeePassLib.2.28.3000\lib\netstandard1.2\ModernKeePassLib.dll</HintPath> <HintPath>..\packages\ModernKeePassLib.2.28.3000\lib\netstandard1.2\ModernKeePassLib.dll</HintPath>
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>

View File

@@ -18,7 +18,6 @@
</Page.DataContext> </Page.DataContext>
<Grid> <Grid>
<Grid.Resources> <Grid.Resources>
<CollectionViewSource <CollectionViewSource
x:Name="groupsViewSource" x:Name="groupsViewSource"
@@ -36,7 +35,6 @@
<RowDefinition Height="140"/> <RowDefinition Height="140"/>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<!-- Horizontal scrolling grid --> <!-- Horizontal scrolling grid -->
<GridView <GridView
x:Name="groupsGridView" x:Name="groupsGridView"
@@ -80,9 +78,9 @@
IsSynchronizedWithCurrentItem="False" > IsSynchronizedWithCurrentItem="False" >
<ListView.ItemTemplate> <ListView.ItemTemplate>
<DataTemplate> <DataTemplate>
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal" Background="{Binding BackgroundColor}">
<SymbolIcon Symbol="{Binding IconSymbol}" Margin="0,5,0,0"/> <SymbolIcon Symbol="{Binding IconSymbol}" Margin="0,5,0,0"/>
<TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="30,0,0,0"/> <TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="30,0,0,10" Foreground="{Binding ForegroundColor}"/>
</StackPanel> </StackPanel>
</DataTemplate> </DataTemplate>
</ListView.ItemTemplate> </ListView.ItemTemplate>
@@ -91,6 +89,12 @@
<TextBlock Text="Entries" FontSize="20" Margin="0,20,0,20" /> <TextBlock Text="Entries" FontSize="20" Margin="0,20,0,20" />
</DataTemplate> </DataTemplate>
</ListView.HeaderTemplate> </ListView.HeaderTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
</ListView> </ListView>
</GridView.Header> </GridView.Header>
<GridView.ItemContainerStyle> <GridView.ItemContainerStyle>

View File

@@ -7,6 +7,8 @@ using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Navigation; using Windows.UI.Xaml.Navigation;
using ModernKeePass.Common; using ModernKeePass.Common;
using ModernKeePass.ViewModels; using ModernKeePass.ViewModels;
using Windows.Storage.AccessCache;
using Windows.Storage;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
@@ -22,7 +24,6 @@ namespace ModernKeePass.Pages
public OpenDatabasePage() public OpenDatabasePage()
{ {
InitializeComponent(); InitializeComponent();
DataContext = new DatabaseVm();
} }
protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatedTo(NavigationEventArgs e)
@@ -42,9 +43,28 @@ namespace ModernKeePass.Pages
picker.FileTypeFilter.Add(".kdbx"); picker.FileTypeFilter.Add(".kdbx");
var file = await picker.PickSingleFileAsync(); var file = await picker.PickSingleFileAsync();
if (file == null) return;
// Application now has read/write access to the picked file // Application now has read/write access to the picked file
if (file == null) return;
// Initialize KDBX database
((App)Application.Current).Database = new DatabaseHelper(file); ((App)Application.Current).Database = new DatabaseHelper(file);
AddToRecentFiles(file);
ShowPassword(file);
}
private void AddToRecentFiles(StorageFile file)
{
var mru = StorageApplicationPermissions.MostRecentlyUsedList;
var mruToken = mru.Add(file, file.DisplayName);
/*var localSettings = ApplicationData.Current.LocalSettings;
if (!localSettings.Containers.ContainsKey("Recent"))
localSettings.CreateContainer("Recent", ApplicationDataCreateDisposition.Always);
localSettings.Containers["Recent"].Values[file.DisplayName] = mruToken;*/
}
private void ShowPassword(StorageFile file)
{
var databaseVm = DataContext as DatabaseVm; var databaseVm = DataContext as DatabaseVm;
if (databaseVm == null) return; if (databaseVm == null) return;
databaseVm.SelectedVisibility = Visibility.Visible; databaseVm.SelectedVisibility = Visibility.Visible;

View File

@@ -0,0 +1,36 @@
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ModernKeePass.Pages"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="using:ModernKeePass.ViewModels"
x:Class="ModernKeePass.Pages.RecentDatabasesPage"
mc:Ignorable="d">
<Page.DataContext>
<viewModels:RecentVm/>
</Page.DataContext>
<Page.Resources>
<CollectionViewSource
x:Name="RecentItemsSource"
Source="{Binding RecentItems}" />
</Page.Resources>
<ListView
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
ItemsSource="{Binding Source={StaticResource RecentItemsSource}}"
x:Name="RecentListView"
SelectionChanged="RecentListView_SelectionChanged"
IsSynchronizedWithCurrentItem="False">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Width="350" Padding="5" />
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Page>

View File

@@ -0,0 +1,55 @@
using System;
using System.Collections.ObjectModel;
using System.Linq;
using ModernKeePass.Common;
using ModernKeePass.Models;
using ModernKeePass.ViewModels;
using Windows.Storage;
using Windows.Storage.AccessCache;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
// Pour en savoir plus sur le modèle d'élément Page vierge, consultez la page http://go.microsoft.com/fwlink/?LinkId=234238
namespace ModernKeePass.Pages
{
/// <summary>
/// Une page vide peut être utilisée seule ou constituer une page de destination au sein d'un frame.
/// </summary>
public sealed partial class RecentDatabasesPage : Page
{
private Frame _mainFrame;
public RecentDatabasesPage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
_mainFrame = e.Parameter as Frame;
//RecentListView.SelectedIndex = -1;
var mru = StorageApplicationPermissions.MostRecentlyUsedList;
var recentVm = DataContext as RecentVm;
recentVm.RecentItems = new ObservableCollection<RecentItem>(
from entry in mru.Entries
select new RecentItem() { Name = entry.Metadata, Token = entry.Token });
recentVm.NotifyPropertyChanged("RecentItems");
}
private async void RecentListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//if (RecentListView == null || e.RemovedItems.Count > 0) return;
var recentItem = e.AddedItems[0] as RecentItem;
var mru = StorageApplicationPermissions.MostRecentlyUsedList;
var file = await mru.GetFileAsync(recentItem.Token) as StorageFile;
var app = (App)Application.Current;
app.Database = new DatabaseHelper(file);
app.Database.Open("test");
_mainFrame.Navigate(typeof(GroupDetailPage), app.Database.RootGroup);
}
}
}

View File

@@ -11,17 +11,8 @@
<viewModels:DatabaseVm/> <viewModels:DatabaseVm/>
</Page.DataContext> </Page.DataContext>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.ChildrenTransitions> <HyperlinkButton x:Name="SaveButton" Content="Save and close" Click="SaveButton_OnClick" VerticalAlignment="Top" IsEnabled="{Binding IsOpen}" />
<TransitionCollection> <HyperlinkButton x:Name="SaveAsButton" Content="Save as..." VerticalAlignment="Top" IsEnabled="False" />
<EntranceThemeTransition/> </StackPanel>
</TransitionCollection>
</Grid.ChildrenTransitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<HyperlinkButton Grid.Row="0" x:Name="SaveButton" Content="Save and close" Click="SaveButton_OnClick" VerticalAlignment="Top" IsEnabled="{Binding IsOpen}" />
<HyperlinkButton Grid.Row="1" x:Name="SaveAsButton" Content="Save as..." VerticalAlignment="Top" IsEnabled="False" />
</Grid>
</Page> </Page>

View File

@@ -16,7 +16,6 @@ namespace ModernKeePass.Pages
public SaveDatabasePage() public SaveDatabasePage()
{ {
InitializeComponent(); InitializeComponent();
DataContext = new DatabaseVm();
} }
protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatedTo(NavigationEventArgs e)

View File

@@ -1,12 +1,15 @@
using System.ComponentModel; using System.ComponentModel;
using System.Drawing;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
using ModernKeePass.Mappings; using ModernKeePass.Mappings;
using ModernKeePassLib; using ModernKeePassLib;
using ModernKeePassLib.Security; using ModernKeePassLib.Security;
using Windows.UI.Xaml.Media;
using Windows.UI;
namespace ModernKeePass.ViewModels namespace ModernKeePass.ViewModels
{ {
public class EntryVm : INotifyPropertyChanged public class EntryVm
{ {
public string Title public string Title
{ {
@@ -34,6 +37,10 @@ namespace ModernKeePass.ViewModels
set { SetEntryValue(PwDefs.NotesField, value); } set { SetEntryValue(PwDefs.NotesField, value); }
} }
public SolidColorBrush BackgroundColor => CreateFromColor(_pwEntry.BackgroundColor, Colors.Transparent);
public SolidColorBrush ForegroundColor => CreateFromColor(_pwEntry.ForegroundColor, Colors.White);
public Symbol IconSymbol public Symbol IconSymbol
{ {
get get
@@ -42,9 +49,7 @@ namespace ModernKeePass.ViewModels
return result == Symbol.More ? Symbol.Permissions : result; return result == Symbol.More ? Symbol.Permissions : result;
} }
} }
public event PropertyChangedEventHandler PropertyChanged;
private readonly PwEntry _pwEntry; private readonly PwEntry _pwEntry;
public EntryVm() { } public EntryVm() { }
@@ -62,5 +67,15 @@ namespace ModernKeePass.ViewModels
{ {
_pwEntry.Strings.Set(key, new ProtectedString(true, newValue)); _pwEntry.Strings.Set(key, new ProtectedString(true, newValue));
} }
private SolidColorBrush CreateFromColor(System.Drawing.Color color, Windows.UI.Color defaultValue)
{
if (color == System.Drawing.Color.Empty) return new SolidColorBrush(defaultValue);
return new SolidColorBrush(Windows.UI.Color.FromArgb(
color.A,
color.R,
color.G,
color.B));
}
} }
} }

View File

@@ -1,10 +1,18 @@
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using ModernKeePass.Models; using ModernKeePass.Models;
using System.ComponentModel;
using System.Linq;
namespace ModernKeePass.ViewModels namespace ModernKeePass.ViewModels
{ {
public class MainVm public class MainVm : INotifyPropertyChanged
{ {
public ObservableCollection<MainMenuItem> MainMenuItems { get; set; } public IOrderedEnumerable<IGrouping<int, MainMenuItem>> MainMenuItems { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
} }
} }

View File

@@ -0,0 +1,18 @@
using ModernKeePass.Models;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace ModernKeePass.ViewModels
{
public class RecentVm : INotifyPropertyChanged
{
public ObservableCollection<RecentItem> RecentItems { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}