Exemplo n.º 1
0
        /// <summary>
        /// Registers commands for UI elements
        /// </summary>
        private void RegisterCommands()
        {
            // Handle add category
            AddCategoryCommand = new Command(async(p) =>
            {
                // Prompt for name
                await DialogHelper.ShowClosableDialog <NamePromptDialog>(async(d) =>
                {
                    try
                    {
                        //create new category
                        var category = new ApiCategory()
                        {
                            Name  = d.Value,
                            Items = new ApiItem[] { }
                        };

                        // Send the category to the api
                        var resp = await KryptPadApi.SaveCategoryAsync(category);

                        // Set the id of the newly created category
                        category.Id = resp.Id;

                        // Add the category to the list
                        Categories.Add(category);

                        // Add view to the ItemsView object
                        ItemsView.Source = Categories;

                        // Refresh
                        OnPropertyChanged(nameof(ItemsView));

                        // Hide empty message
                        EmptyMessageVisibility = Visibility.Collapsed;
                    }
                    catch (WebException ex)
                    {
                        // Something went wrong in the api
                        await DialogHelper.ShowMessageDialogAsync(ex.Message);
                    }
                    catch (Exception ex)
                    {
                        // Failed
                        await DialogHelper.ShowGenericErrorDialogAsync(ex);
                    }
                }, "Add Category");
            });

            // Handle add new item
            AddItemCommand = new Command(AddItemCommandHandler);

            // Handle item click
            ItemClickCommand = new Command((p) =>
            {
                var item     = p as ApiItem;
                var category = (from c in Categories
                                where c.Items.Contains(item)
                                select c).FirstOrDefault();

                // Navigate to edit
                NavigationHelper.Navigate(typeof(NewItemPage),
                                          new EditItemPageParams()
                {
                    Category = category,
                    Item     = item
                });
            });

            // Handle change passphrase command
            ChangePassphraseCommand = new Command(async(p) =>
            {
                var dialog = new ChangePassphraseDialog();

                await dialog.ShowAsync();
            });

            // Handle rename command
            RenameProfileCommand = new Command(async(p) =>
            {
                // Prompt for name
                await DialogHelper.GetValueAsync(async(d) =>
                {
                    try
                    {
                        // Set new name
                        var profile  = KryptPadApi.CurrentProfile;
                        profile.Name = d.Value;

                        // Send the category to the api
                        var resp = await KryptPadApi.SaveProfileAsync(profile);
                    }
                    catch (WebException ex)
                    {
                        // Something went wrong in the api
                        await DialogHelper.ShowMessageDialogAsync(ex.Message);
                    }
                    catch (Exception ex)
                    {
                        // Failed
                        await DialogHelper.ShowGenericErrorDialogAsync(ex);
                    }
                }, "RENAME PROFILE", KryptPadApi.CurrentProfile.Name);
            });

            // Handle delete command
            DeleteProfileCommand = new Command(async(p) =>
            {
                var res = await DialogHelper.Confirm(
                    "All of your data in this profile will be deleted permanently. THIS ACTION CANNOT BE UNDONE. Are you sure you want to delete this entire profile?",
                    "WARNING - CONFIRM DELETE",
                    async(ap) =>
                {
                    try
                    {
                        // Delete the selected profile
                        await KryptPadApi.DeleteProfileAsync(KryptPadApi.CurrentProfile);

                        // Navigate back to the profiles list
                        NavigationHelper.Navigate(typeof(SelectProfilePage), null);
                    }
                    catch (WebException ex)
                    {
                        // Something went wrong in the api
                        await DialogHelper.ShowMessageDialogAsync(ex.Message);
                    }
                    catch (Exception ex)
                    {
                        // Failed
                        await DialogHelper.ShowGenericErrorDialogAsync(ex);
                    }
                }
                    );
            });

            // Download profile handler
            DownloadProfileCommand = new Command(async(prop) =>
            {
                try
                {
                    // Prompt for a place to save the file
                    var sfd = new FileSavePicker()
                    {
                        SuggestedFileName = KryptPadApi.CurrentProfile.Name,
                    };

                    //sfd.FileTypeChoices.Add("KryptPad Document Format", new List<string>(new[] { ".kdf" }));
                    sfd.FileTypeChoices.Add("KryptPad Document Format", new[] { ".kdf" });

                    // Show the picker
                    var file = await sfd.PickSaveFileAsync();

                    if (file != null)
                    {
                        // Get profile
                        var profileData = await KryptPadApi.DownloadCurrentProfileAsync();

                        // Save profile to file
                        using (var fs = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
                            using (var sw = new StreamWriter(fs.AsStreamForWrite()))
                            {
                                // Write the data
                                sw.Write(profileData);
                            }

                        await DialogHelper.ShowMessageDialogAsync("Profile downloaded successfully");
                    }
                }
                catch (WebException ex)
                {
                    // Something went wrong in the api
                    await DialogHelper.ShowMessageDialogAsync(ex.Message);
                }
                catch (Exception ex)
                {
                    // Failed
                    await DialogHelper.ShowGenericErrorDialogAsync(ex);
                }
            });

            // Handle category rename
            RenameCategoryCommand = new Command(RenameCategoryCommandHandler);

            // Handle category delete
            DeleteCategoryCommand = new Command(DeleteCategoryCommandHandler);

            // Handle selection mode
            SelectModeCommand = new Command(SelectModeCommandHandler);

            // Handle the move command
            MoveItemsCommand = new Command(MoveItemsCommandHandler, CanMoveItems);

            // Handle setting favorites
            SetFavoriteCommand = new Command(SetFavoriteCommandHandler);
        }