Add currently opened database to main menu

Save and close reloads the page
Design enhancements
Some refactoring
This commit is contained in:
2017-09-28 17:51:30 +02:00
committed by BONNEVILLE Geoffroy
parent caaf34918e
commit 1582060466
6 changed files with 74 additions and 31 deletions

View File

@@ -11,16 +11,18 @@ namespace ModernKeePass.Common
{ {
public class DatabaseHelper public class DatabaseHelper
{ {
private PwDatabase _pwDatabase = new PwDatabase(); private readonly PwDatabase _pwDatabase = new PwDatabase();
private StorageFile databaseFile; private readonly StorageFile _databaseFile;
public GroupVm RootGroup { get; set; } public GroupVm RootGroup { get; set; }
public bool IsOpen { get; set; }
public string Name { get; set; } public bool IsOpen => _pwDatabase.IsOpen;
public string Name => _databaseFile.Name;
public DatabaseHelper(StorageFile databaseFile) public DatabaseHelper(StorageFile databaseFile)
{ {
this.databaseFile = databaseFile; this._databaseFile = databaseFile;
} }
public string Open(string password) public string Open(string password)
{ {
@@ -28,14 +30,10 @@ namespace ModernKeePass.Common
try try
{ {
key.AddUserKey(new KcpPassword(password)); key.AddUserKey(new KcpPassword(password));
_pwDatabase.Open(IOConnectionInfo.FromFile(databaseFile), key, new NullStatusLogger()); _pwDatabase.Open(IOConnectionInfo.FromFile(_databaseFile), key, new NullStatusLogger());
//_pwDatabase.Open(IOConnectionInfo.FromPath(databaseFile.Path), key, new NullStatusLogger()); //_pwDatabase.Open(IOConnectionInfo.FromPath(databaseFile.Path), key, new NullStatusLogger());
IsOpen = _pwDatabase.IsOpen;
if (IsOpen) if (IsOpen) RootGroup = new GroupVm(_pwDatabase.RootGroup);
{
Name = databaseFile.DisplayName;
RootGroup = new GroupVm(_pwDatabase.RootGroup);
}
} }
catch (ArgumentNullException) catch (ArgumentNullException)
{ {

View File

@@ -14,7 +14,7 @@
<Page.Resources> <Page.Resources>
<CollectionViewSource <CollectionViewSource
x:Name="MenuItemsSource" x:Name="MenuItemsSource"
Source="{Binding MainMenuItems}" /> Source="{Binding MainMenuItems}" IsSourceGrouped="True" />
</Page.Resources> </Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" > <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
@@ -40,6 +40,17 @@
<Setter Property="Padding" Value="20,5,0,0" /> <Setter Property="Padding" Value="20,5,0,0" />
</Style> </Style>
</ListView.ItemContainerStyle> </ListView.ItemContainerStyle>
<ListView.GroupStyle>
<GroupStyle HidesIfEmpty="True">
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Background="DarkGray" Margin="20,0,0,0">
<Border Height="1" Width="240" HorizontalAlignment="Stretch"/>
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</controls:ListViewWithDisable> </controls:ListViewWithDisable>
<Frame Grid.Column="2" Name="MenuFrame" Width="auto" Margin="0,60,0,0" /> <Frame Grid.Column="2" Name="MenuFrame" Width="auto" Margin="0,60,0,0" />
</Grid> </Grid>

View File

@@ -1,5 +1,8 @@
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using ModernKeePass.Models; using ModernKeePass.Models;
using ModernKeePass.Pages; using ModernKeePass.Pages;
using ModernKeePass.ViewModels; using ModernKeePass.ViewModels;
@@ -16,26 +19,35 @@ namespace ModernKeePass
public MainPage() public MainPage()
{ {
InitializeComponent(); InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var mainMenuItems = new ObservableCollection<MainMenuItem> var mainMenuItems = new ObservableCollection<MainMenuItem>
{ {
new MainMenuItem {Title = "File", PageType = typeof(OpenDatabasePage)}, new MainMenuItem {Title = "Open", PageType = typeof(OpenDatabasePage), Destination = MenuFrame, Parameter = Frame},
new MainMenuItem {Title = "New" /*, PageType = typeof(NewDatabasePage)*/}, new MainMenuItem {Title = "New" /*, PageType = typeof(NewDatabasePage)*/, Destination = MenuFrame},
new MainMenuItem {Title = "Save" , PageType = typeof(SaveDatabasePage)}, new MainMenuItem {Title = "Save" , PageType = typeof(SaveDatabasePage), Destination = MenuFrame, Parameter = Frame},
new MainMenuItem {Title = "Recent files - Coming soon" /*, PageType = typeof(RecentDatabasesPage)*/}, new MainMenuItem {Title = "Recent" /*, PageType = typeof(RecentDatabasesPage)*/, Destination = MenuFrame}
new MainMenuItem {Title = "Url files - Coming soon" /*, PageType = typeof(OpenUrlPage)*/}
}; };
DataContext = new MainVm {MainMenuItems = mainMenuItems}; var app = (App)Application.Current;
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});
var result = from item in mainMenuItems group item by item.Group into grp orderby grp.Key select grp;
MenuItemsSource.Source = result;
//DataContext = new MainVm {MainMenuItems = mainMenuItems};
MenuListView.SelectedIndex = -1; MenuListView.SelectedIndex = -1;
} }
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e) private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ {
if (Frame == null) return;
var listView = sender as ListView; var listView = sender as ListView;
if (listView == null) return; if (listView == null) return;
if (listView.SelectedIndex == -1) return; if (listView.SelectedIndex == -1) return;
var selectedItem = listView.SelectedItem as MainMenuItem; var selectedItem = listView.SelectedItem as MainMenuItem;
if (selectedItem != null) MenuFrame.Navigate(selectedItem.PageType, Frame); selectedItem?.Destination.Navigate(selectedItem.PageType, selectedItem.Parameter);
} }
} }
} }

View File

@@ -1,12 +1,23 @@
using System; using System;
using Windows.UI.Xaml.Controls;
using ModernKeePass.Interfaces; using ModernKeePass.Interfaces;
namespace ModernKeePass.Models namespace ModernKeePass.Models
{ {
public class MainMenuItem: IIsEnabled public class MainMenuItem: IIsEnabled
{ {
public string Title { get; set; } private string _title;
public string Title
{
get { return IsEnabled ? _title : _title + " - Coming soon"; }
set { _title = value; }
}
public Type PageType { get; set; } public Type PageType { get; set; }
public object Parameter { get; set; }
public Frame Destination { get; set; }
public int Group { get; set; } = 0;
public bool IsEnabled => PageType != null; public bool IsEnabled => PageType != null;
public override string ToString() public override string ToString()

View File

@@ -25,13 +25,15 @@
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" /> <RowDefinition Height="*" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<HyperlinkButton Grid.Row="0" Grid.ColumnSpan="2" Content="Browse files..." Click="ButtonBase_OnClick" /> <HyperlinkButton Grid.Row="0" Grid.ColumnSpan="2" Content="Browse files..." Click="ButtonBase_OnClick" />
<TextBlock Grid.Column="1" Grid.Row="0" HorizontalAlignment="Left" Visibility="{Binding SelectedVisibility}" TextWrapping="Wrap" Text="{Binding Name}" VerticalAlignment="Center" Height="auto" Width="auto" FontSize="16" Margin="10,7,0,6" /> <HyperlinkButton Grid.Row="1" Grid.ColumnSpan="2" Content="From Url..." IsEnabled="False" />
<TextBlock Grid.Column="0" Grid.Row="2" HorizontalAlignment="Left" Visibility="{Binding SelectedVisibility}" TextWrapping="Wrap" Text="Password" VerticalAlignment="Center" Height="auto" Width="auto" FontSize="16" Margin="10,7,0,6" /> <TextBlock Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" Visibility="{Binding SelectedVisibility}" TextWrapping="Wrap" Text="{Binding Name}" VerticalAlignment="Center" Height="auto" Width="auto" FontSize="16" Margin="10,7,0,6" />
<PasswordBox Grid.Column="1" Grid.Row="2" x:Name="PasswordBox" VerticalAlignment="Top" HorizontalAlignment=" Right" Visibility="{Binding SelectedVisibility}" Width="500" IsPasswordRevealButtonEnabled="True" Margin="0,0,10,0"/> <TextBlock Grid.Row="3" Grid.Column="0" HorizontalAlignment="Left" Visibility="{Binding SelectedVisibility}" TextWrapping="Wrap" Text="Password" VerticalAlignment="Center" Height="auto" Width="auto" FontSize="16" Margin="10,7,0,6" />
<TextBlock Grid.Column="1" Grid.Row="3" x:Name="StatusTextBlock" Visibility="{Binding SelectedVisibility}" Height="auto" Width="auto" Foreground="#FF9E1B1B" FontSize="16" /> <PasswordBox Grid.Row="3" Grid.Column="1" x:Name="PasswordBox" Visibility="{Binding SelectedVisibility}" Width="500" IsPasswordRevealButtonEnabled="True" Margin="0,0,10,0"/>
<Button Grid.Column="1" Grid.Row="4" Content="OK" Visibility="{Binding SelectedVisibility}" HorizontalAlignment="Right" VerticalAlignment="Top" Click="OpenButton_OnClick" Margin="0,0,7,0" Width="auto"/> <TextBlock Grid.Row="4" Grid.Column="1" x:Name="StatusTextBlock" Visibility="{Binding SelectedVisibility}" Height="auto" Width="auto" Foreground="#FF9E1B1B" FontSize="16" />
<Button Grid.Row="5" Grid.Column="1" Content="OK" Visibility="{Binding SelectedVisibility}" HorizontalAlignment="Right" VerticalAlignment="Top" Click="OpenButton_OnClick" Margin="0,0,7,0" Width="auto"/>
</Grid> </Grid>
</Page> </Page>

View File

@@ -12,21 +12,22 @@ namespace ModernKeePass.Pages
/// </summary> /// </summary>
public sealed partial class SaveDatabasePage : Page public sealed partial class SaveDatabasePage : Page
{ {
private Frame _mainFrame;
public SaveDatabasePage() public SaveDatabasePage()
{ {
this.InitializeComponent(); InitializeComponent();
DataContext = new DatabaseVm(); DataContext = new DatabaseVm();
} }
protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatedTo(NavigationEventArgs e)
{ {
base.OnNavigatedTo(e); base.OnNavigatedTo(e);
_mainFrame = e.Parameter as Frame;
var app = (App)Application.Current; var app = (App)Application.Current;
if (app.Database == null) return; if (app.Database == null) return;
var databaseVm = DataContext as DatabaseVm; var databaseVm = DataContext as DatabaseVm;
if (databaseVm == null) return; if (databaseVm == null) return;
databaseVm.IsOpen = app.Database.IsOpen; UpdateDatabaseStatus(app, databaseVm);
databaseVm.NotifyPropertyChanged("IsOpen");
} }
private void SaveButton_OnClick(object sender, RoutedEventArgs e) private void SaveButton_OnClick(object sender, RoutedEventArgs e)
@@ -34,6 +35,14 @@ namespace ModernKeePass.Pages
var app = (App) Application.Current; var app = (App) Application.Current;
app.Database.Save(); app.Database.Save();
app.Database.Close(); app.Database.Close();
UpdateDatabaseStatus(app, DataContext as DatabaseVm);
_mainFrame.Navigate(typeof(MainPage));
}
private void UpdateDatabaseStatus(App app, DatabaseVm databaseVm)
{
databaseVm.IsOpen = app.Database.IsOpen;
databaseVm.NotifyPropertyChanged("IsOpen");
} }
} }
} }