bitwarden-android/src/App/Controls/FormEntryCell.cs

181 lines
5.2 KiB
C#
Raw Normal View History

using Bit.App.Abstractions;
using System;
using Xamarin.Forms;
using XLabs.Ioc;
namespace Bit.App.Controls
{
public class FormEntryCell : ExtendedViewCell, IDisposable
{
private VisualElement _nextElement;
private TapGestureRecognizer _tgr;
public FormEntryCell(
string labelText,
Keyboard entryKeyboard = null,
bool isPassword = false,
VisualElement nextElement = null,
bool useLabelAsPlaceholder = false,
string imageSource = null,
Thickness? containerPadding = null,
bool useButton = false)
{
_nextElement = nextElement;
2016-06-28 03:56:59 +03:00
if(!useLabelAsPlaceholder)
{
2016-06-28 03:56:59 +03:00
Label = new Label
{
Text = labelText,
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
Style = (Style)Application.Current.Resources["text-muted"],
HorizontalOptions = LayoutOptions.FillAndExpand
2016-06-28 03:56:59 +03:00
};
}
Entry = new ExtendedEntry
{
Keyboard = entryKeyboard,
2016-05-17 06:54:24 +03:00
HasBorder = false,
IsPassword = isPassword,
AllowClear = true,
2016-08-23 01:59:15 +03:00
HorizontalOptions = LayoutOptions.FillAndExpand,
WidthRequest = 1,
2016-08-23 01:59:15 +03:00
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Entry))
};
2016-06-28 03:56:59 +03:00
if(useLabelAsPlaceholder)
{
Entry.Placeholder = labelText;
}
if(nextElement != null)
{
Entry.ReturnType = Enums.ReturnType.Next;
}
var imageStackLayout = new StackLayout
{
Padding = containerPadding ?? new Thickness(15, 10),
Orientation = StackOrientation.Horizontal,
Spacing = 10,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
if(imageSource != null)
{
_tgr = new TapGestureRecognizer();
var theImage = new Image
{
Source = imageSource,
HorizontalOptions = LayoutOptions.Start,
VerticalOptions = LayoutOptions.Center
};
theImage.GestureRecognizers.Add(_tgr);
imageStackLayout.Children.Add(theImage);
}
var formStackLayout = new StackLayout
{
HorizontalOptions = LayoutOptions.FillAndExpand,
2016-08-14 07:54:07 +03:00
VerticalOptions = LayoutOptions.CenterAndExpand
};
2016-08-14 07:54:07 +03:00
if(Device.OS == TargetPlatform.Android)
{
var deviceInfo = Resolver.Resolve<IDeviceInfoService>();
if(useLabelAsPlaceholder)
{
if(deviceInfo.Version < 21)
{
Entry.Margin = new Thickness(-9, 1, -9, 0);
}
else if(deviceInfo.Version == 21)
{
Entry.Margin = new Thickness(0, 4, 0, -4);
}
}
else
{
Entry.AdjustMarginsForDevice();
}
if(containerPadding == null)
{
imageStackLayout.AdjustPaddingForDevice();
}
2016-08-14 07:54:07 +03:00
}
2016-06-28 03:56:59 +03:00
if(!useLabelAsPlaceholder)
{
formStackLayout.Children.Add(Label);
2016-06-28 03:56:59 +03:00
}
formStackLayout.Children.Add(Entry);
imageStackLayout.Children.Add(formStackLayout);
if(useButton)
{
Button = new ExtendedButton();
imageStackLayout.Children.Add(Button);
if(Device.OS == TargetPlatform.Android)
{
Button.Padding = new Thickness(0);
Button.BackgroundColor = Color.Transparent;
}
}
View = imageStackLayout;
}
public Label Label { get; private set; }
public ExtendedEntry Entry { get; private set; }
public ExtendedButton Button { get; private set; }
public void InitEvents()
{
if(_nextElement != null)
{
Entry.Completed += Entry_Completed;
}
if(_tgr != null)
{
_tgr.Tapped += Tgr_Tapped;
}
Tapped += FormEntryCell_Tapped;
}
private void Tgr_Tapped(object sender, EventArgs e)
{
Entry.Focus();
}
2016-07-05 05:35:49 +03:00
private void FormEntryCell_Tapped(object sender, EventArgs e)
{
Entry.Focus();
}
private void Entry_Completed(object sender, EventArgs e)
{
_nextElement?.Focus();
}
public void Dispose()
{
if(_tgr != null)
{
_tgr.Tapped -= Tgr_Tapped;
}
Tapped -= FormEntryCell_Tapped;
Entry.Completed -= Entry_Completed;
}
}
}