/// <summary> /// Updates the field /// </summary> /// <param name="field"></param> private async void UpdateField(FieldModel field) { if (_isLoading) { return; } // Set main window busy state //(Window.Current.Content as MainPage).SetIsBusy(true); try { // Send the field to the API to be stored under the item await KryptPadApi.SaveFieldAsync(Category.Id, Item.Id, field.Field); } catch (WebException ex) { // Something went wrong in the api await DialogHelper.ShowMessageDialogAsync(ex.Message); } catch (Exception ex) { // Failed await DialogHelper.ShowGenericErrorDialogAsync(ex); } // Set main window busy state //(Window.Current.Content as MainPage).SetIsBusy(false); }
protected async void RenameFieldCommandHandler(object p) { // Get data context var field = p as FieldModel; await DialogHelper.GetValueAsync(async (d) => { try { // Update name field.Name = d.Value; // Call api to delete the field from the item await KryptPadApi.SaveFieldAsync(Category.Id, Item.Id, field.Field); } catch (WebException ex) { // Something went wrong in the api await DialogHelper.ShowMessageDialogAsync(ex.Message); } catch (Exception ex) { // Failed await DialogHelper.ShowGenericErrorDialogAsync(ex); } }); }
private async void AddItemCommandHandler(object p) { // Prompt to create the new item var dialog = new AddItemDialog(); // Show the dialog and wait for a response var result = await dialog.ShowAsync(); if (result == ContentDialogResult.Primary) { try { // Get the category var category = p as ApiCategory; // Create an item var item = new ApiItem() { Name = dialog.ItemName }; // Save the item to the api var r = await KryptPadApi.SaveItemAsync(category.Id, item); // Set the item item.Id = r.Id; // If a template was selected, create a couple of fields to start with if (dialog.SelectedItemTemplate != null) { var templateFields = dialog.SelectedItemTemplate.Fields; // A template was selected, add all the fields from the template foreach (var templateField in templateFields) { // Create field var field = new ApiField() { Name = templateField.Name, FieldType = templateField.FieldType }; // Send to api await KryptPadApi.SaveFieldAsync(category.Id, item.Id, field); } } // Navigate to item edit page NavigationHelper.Navigate(typeof(NewItemPage), new EditItemPageParams() { Category = category, Item = item }); } catch (WebException ex) { // Something went wrong in the api await DialogHelper.ShowMessageDialogAsync(ex.Message); } catch (Exception ex) { // Failed await DialogHelper.ShowGenericErrorDialogAsync(ex); } } }
/// <summary> /// Registers commands for UI elements /// </summary> private void RegisterCommands() { // Handle add new field AddFieldCommand = new Command(async(p) => { // Show the add field dialog var res = await DialogHelper.ShowClosableDialog <AddFieldDialog>(async(d) => { try { var m = (d.DataContext as AddFieldDialogViewModel); var field = new FieldModel(new ApiField() { Name = m.FieldName, FieldType = m.SelectedFieldType.Id }); // Send the field to the API to be stored under the item var resp = await KryptPadApi.SaveFieldAsync(Category.Id, Item.Id, field.Field); field.Id = resp.Id; // Add field to the list AddFieldToCollection(field); } catch (WebException ex) { // Something went wrong in the api await DialogHelper.ShowMessageDialogAsync(ex.Message); } catch (Exception ex) { // Failed await DialogHelper.ShowGenericErrorDialogAsync(ex); } }); }); // Handle item delete DeleteItemCommand = new Command(async(p) => { // Prompt user to delete the item var promptResp = await DialogHelper.Confirm( "This action cannot be undone. All data associated with this item will be deleted. Are you sure you want to delete this item?", async(c) => { try { // Delete the item await KryptPadApi.DeleteItemAsync(Category.Id, Item.Id); // Navigate back to items page NavigationHelper.Navigate(typeof(ItemsPage), null); } catch (WebException ex) { // Something went wrong in the api await DialogHelper.ShowMessageDialogAsync(ex.Message); } catch (Exception ex) { // Failed await DialogHelper.ShowGenericErrorDialogAsync(ex); } }); }); // Handle copy field value CopyFieldValueCommand = new Command(async(p) => { try { var field = p as FieldModel; // Check if field value is null if (!string.IsNullOrWhiteSpace(field.Value)) { // Create a data package var package = new DataPackage(); package.SetText(field.Value); // Set the value of the field to the clipboard Clipboard.SetContent(package); } } catch (Exception) { // Failed await DialogHelper.ShowMessageDialogAsync("Failed to copy text to clipboard."); } }); // Delete field DeleteFieldCommand = new Command(DeleteFieldCommandHandler); // Rename field RenameFieldCommand = new Command(RenameFieldCommandHandler); // Generate password GeneratePasswordCommand = new Command(GeneratePasswordCommandHandler); }