Files
modernkeepass/ModernKeePass/Pages/GroupDetailPage.xaml.cs

157 lines
6.1 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using Windows.Storage.Streams;
using Windows.UI.Popups;
using ModernKeePass.Common;
2017-09-11 18:25:00 +02:00
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using ModernKeePass.ViewModels;
2017-09-11 18:25:00 +02:00
// The Group Detail Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234229
2017-09-11 18:25:00 +02:00
namespace ModernKeePass.Pages
{
/// <summary>
/// A page that displays an overview of a single group, including a preview of the items
/// within the group.
2017-09-11 18:25:00 +02:00
/// </summary>
2017-10-12 17:45:37 +02:00
public sealed partial class GroupDetailPage
2017-09-11 18:25:00 +02:00
{
/// <summary>
/// NavigationHelper is used on each page to aid in navigation and
/// process lifetime management
/// </summary>
public NavigationHelper NavigationHelper { get; }
public GroupVm Model => (GroupVm)DataContext;
public GroupDetailPage()
2017-09-11 18:25:00 +02:00
{
2017-09-15 11:47:43 +02:00
InitializeComponent();
NavigationHelper = new NavigationHelper(this);
NavigationHelper.LoadState += navigationHelper_LoadState;
2017-09-11 18:25:00 +02:00
}
2017-09-11 18:25:00 +02:00
/// <summary>
/// Populates the page with content passed during navigation. Any saved state is also
/// provided when recreating a page from a prior session.
/// </summary>
/// <param name="sender">
/// The source of the event; typically <see cref="Common.NavigationHelper"/>
/// </param>
/// <param name="e">Event data that provides both the navigation parameter passed to
/// <see cref="Frame.Navigate(Type, object)"/> when this page was initially requested and
2017-09-11 18:25:00 +02:00
/// a dictionary of state preserved by this page during an earlier
/// session. The state will be null the first time a page is visited.</param>
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e) {}
2017-09-11 18:25:00 +02:00
#region NavigationHelper registration
/// The methods provided in this section are simply used to allow
/// NavigationHelper to respond to the page's navigation methods.
///
/// Page specific logic should be placed in event handlers for the
/// <see cref="Common.NavigationHelper.LoadState"/>
/// and <see cref="Common.NavigationHelper.SaveState"/>.
/// The navigation parameter is available in the LoadState method
/// in addition to page state preserved during an earlier session.
2017-09-15 11:47:43 +02:00
///
2017-09-11 18:25:00 +02:00
protected override void OnNavigatedTo(NavigationEventArgs e)
{
NavigationHelper.OnNavigatedTo(e);
if (!(e.Parameter is GroupVm)) return;
DataContext = (GroupVm) e.Parameter;
2017-09-11 18:25:00 +02:00
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
NavigationHelper.OnNavigatedFrom(e);
2017-09-11 18:25:00 +02:00
}
#endregion
#region Event Handlers
private void groups_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (LeftListView.SelectedIndex == 0)
{
var currentGroup = DataContext as GroupVm;
currentGroup?.CreateNewGroup();
LeftListView.SelectedIndex = -1;
// TODO: Navigate to new group?
return;
}
var selectedItem = LeftListView.SelectedItem as GroupVm;
if (selectedItem == null) return;
Frame.Navigate(typeof(GroupDetailPage), selectedItem);
}
private void entries_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (GridView.SelectedIndex)
{
case -1:
return;
case 0:
Model.CreateNewEntry();
GridView.SelectedIndex = -1;
// TODO: Navigate to new entry?
return;
}
Frame.Navigate(typeof(EntryDetailPage), GridView.SelectedItem as EntryVm);
}
private async void DeleteButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
// Create the message dialog and set its content
var messageDialog = new MessageDialog("Are you sure you want to delete the whole group and all its entries?");
// Add commands and set their callbacks; both buttons use the same callback function instead of inline event handlers
messageDialog.Commands.Add(new UICommand("Delete", delete =>
{
Model.RemoveGroup();
if (Frame.CanGoBack) Frame.GoBack();
}));
messageDialog.Commands.Add(new UICommand("Cancel"));
// Set the command that will be invoked by default
messageDialog.DefaultCommandIndex = 1;
// Set the command to be invoked when escape is pressed
messageDialog.CancelCommandIndex = 1;
// Show the message dialog
await messageDialog.ShowAsync();
}
private void SemanticZoom_ViewChangeStarted(object sender, SemanticZoomViewChangedEventArgs e)
{
if (e.IsSourceZoomedInView == false)
{
e.DestinationItem.Item = e.SourceItem.Item;
}
}
private void SearchBox_OnSuggestionsRequested(SearchBox sender, SearchBoxSuggestionsRequestedEventArgs args)
{
var imageUri = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx://Assets/Logo.scale-80.png"));
var results = Model.Entries.Skip(1).Where(e => e.Title.IndexOf(args.QueryText, StringComparison.OrdinalIgnoreCase) >= 0).Take(5);
foreach (var result in results)
{
args.Request.SearchSuggestionCollection.AppendResultSuggestion(result.Title, result.ParentGroup.Name, result.Id, imageUri, string.Empty);
}
}
private void SearchBox_OnResultSuggestionChosen(SearchBox sender, SearchBoxResultSuggestionChosenEventArgs args)
{
var entry = Model.Entries.Skip(1).FirstOrDefault(e => e.Id == args.Tag);
Frame.Navigate(typeof(EntryDetailPage), entry);
}
#endregion
2017-09-11 18:25:00 +02:00
}
}