1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-31 23:53:25 +00:00

add/edit/delete custom fields. remove field page.

This commit is contained in:
Kyle Spearrin
2018-03-05 15:15:20 -05:00
parent c3f4d56d1e
commit 1f21a2ecc7
14 changed files with 459 additions and 304 deletions

View File

@@ -0,0 +1,9 @@
using Xamarin.Forms;
namespace Bit.App.Utilities
{
public static class Colors
{
public static Color Primary = Color.FromHex("3c8dbc");
}
}

View File

@@ -1,11 +1,14 @@
using Bit.App.Abstractions;
using Bit.App.Controls;
using Bit.App.Enums;
using Bit.App.Models;
using Bit.App.Models.Page;
using Bit.App.Pages;
using Bit.App.Resources;
using Plugin.Settings.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xamarin.Forms;
using XLabs.Ioc;
@@ -195,5 +198,180 @@ namespace Bit.App.Utilities
var addPage = new VaultAddCipherPage(selectedType, defaultFolderId: folderId);
await page.Navigation.PushForDeviceAsync(addPage);
}
public static async Task AddField(Page page, TableSection fieldsSection)
{
var type = await page.DisplayActionSheet(AppResources.SelectTypeField, AppResources.Cancel, null,
AppResources.FieldTypeText, AppResources.FieldTypeHidden, AppResources.FieldTypeBoolean);
FieldType fieldType;
if(type == AppResources.FieldTypeText)
{
fieldType = FieldType.Text;
}
else if(type == AppResources.FieldTypeHidden)
{
fieldType = FieldType.Hidden;
}
else if(type == AppResources.FieldTypeBoolean)
{
fieldType = FieldType.Boolean;
}
else
{
return;
}
var daService = Resolver.Resolve<IDeviceActionService>();
var label = await daService.DisplayPromptAync(AppResources.CustomFieldName);
if(label == null)
{
return;
}
var cell = MakeFieldCell(fieldType, label, string.Empty, fieldsSection);
if(cell != null)
{
fieldsSection.Insert(fieldsSection.Count - 1, cell);
if(cell is FormEntryCell feCell)
{
feCell.InitEvents();
}
}
}
public static Cell MakeFieldCell(FieldType type, string label, string value, TableSection fieldsSection)
{
Cell cell;
switch(type)
{
case FieldType.Text:
case FieldType.Hidden:
var hidden = type == FieldType.Hidden;
var textFieldCell = new FormEntryCell(label, isPassword: hidden, useButton: hidden);
textFieldCell.Entry.Text = value;
textFieldCell.Entry.DisableAutocapitalize = true;
textFieldCell.Entry.Autocorrect = false;
if(hidden)
{
textFieldCell.Entry.FontFamily = Helpers.OnPlatform(
iOS: "Menlo-Regular", Android: "monospace", Windows: "Courier");
textFieldCell.Button.Image = "eye.png";
textFieldCell.Button.Command = new Command(() =>
{
textFieldCell.Entry.InvokeToggleIsPassword();
textFieldCell.Button.Image =
"eye" + (!textFieldCell.Entry.IsPasswordFromToggled ? "_slash" : string.Empty) + ".png";
});
}
cell = textFieldCell;
break;
case FieldType.Boolean:
var switchFieldCell = new ExtendedSwitchCell
{
Text = label,
On = value == "true"
};
cell = switchFieldCell;
break;
default:
cell = null;
break;
}
if(cell != null)
{
var deleteAction = new MenuItem { Text = AppResources.Remove, IsDestructive = true };
deleteAction.Clicked += (sender, e) =>
{
if(fieldsSection.Contains(cell))
{
fieldsSection.Remove(cell);
}
if(cell is FormEntryCell feCell)
{
feCell.Dispose();
}
cell = null;
};
var editNameAction = new MenuItem { Text = AppResources.Edit };
editNameAction.Clicked += async (sender, e) =>
{
string existingLabel = null;
var feCell = cell as FormEntryCell;
var esCell = cell as ExtendedSwitchCell;
if(feCell != null)
{
existingLabel = feCell.Label.Text;
}
else if(esCell != null)
{
existingLabel = esCell.Text;
}
var daService = Resolver.Resolve<IDeviceActionService>();
var editLabel = await daService.DisplayPromptAync(AppResources.CustomFieldName,
null, existingLabel);
if(editLabel != null)
{
if(feCell != null)
{
feCell.Label.Text = editLabel;
}
else if(esCell != null)
{
esCell.Text = editLabel;
}
}
};
cell.ContextActions.Add(editNameAction);
cell.ContextActions.Add(deleteAction);
}
return cell;
}
public static void ProcessFieldsSectionForSave(TableSection fieldsSection, Cipher cipher)
{
if(fieldsSection != null && fieldsSection.Count > 0)
{
var fields = new List<Field>();
foreach(var cell in fieldsSection)
{
if(cell is FormEntryCell entryCell)
{
fields.Add(new Field
{
Name = string.IsNullOrWhiteSpace(entryCell.Label.Text) ? null :
entryCell.Label.Text.Encrypt(cipher.OrganizationId),
Value = string.IsNullOrWhiteSpace(entryCell.Entry.Text) ? null :
entryCell.Entry.Text.Encrypt(cipher.OrganizationId),
Type = entryCell.Entry.IsPassword ? FieldType.Hidden : FieldType.Text
});
}
else if(cell is ExtendedSwitchCell switchCell)
{
var value = switchCell.On ? "true" : "false";
fields.Add(new Field
{
Name = string.IsNullOrWhiteSpace(switchCell.Text) ? null :
switchCell.Text.Encrypt(cipher.OrganizationId),
Value = value.Encrypt(cipher.OrganizationId),
Type = FieldType.Boolean
});
}
}
cipher.Fields = fields;
}
if(!cipher.Fields?.Any() ?? true)
{
cipher.Fields = null;
}
}
}
}