/// <summary> /// Adds a field to the collection and listens to changes to properties /// </summary> /// <param name="field"></param> private void AddFieldToCollection(FieldModel field) { // Listen to changes to the Value property field.PropertyChanged += (sender, e) => { // If it is the value property, call update on our field if (e.PropertyName == nameof(field.Value)) { UpdateField((FieldModel)sender); } }; // Add field model to the list Fields.Add(field); }
/// <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) { // Failed await DialogHelper.ShowConnectionErrorMessageDialog(); } // Set main window busy state //(Window.Current.Content as MainPage).SetIsBusy(false); }
/// <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) { // Failed await DialogHelper.ShowConnectionErrorMessageDialog(); } }); }); // 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) { // Failed await DialogHelper.ShowConnectionErrorMessageDialog(); } }); }); // 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); }