2019-07-01 21:12:40 +02:00
|
|
|
|
using Bit.iOS.Core.Controllers;
|
|
|
|
|
using Bit.iOS.Core.Utilities;
|
|
|
|
|
using System;
|
2021-10-26 23:46:11 +02:00
|
|
|
|
using System.Drawing;
|
2019-06-27 21:48:25 +02:00
|
|
|
|
using UIKit;
|
|
|
|
|
|
|
|
|
|
namespace Bit.iOS.Core.Views
|
|
|
|
|
{
|
2019-07-01 21:12:40 +02:00
|
|
|
|
public class FormEntryTableViewCell : ExtendedUITableViewCell, ISelectable
|
2019-06-27 21:48:25 +02:00
|
|
|
|
{
|
2021-10-26 23:46:11 +02:00
|
|
|
|
public UILabel Label { get; set; }
|
|
|
|
|
public UITextField TextField { get; set; }
|
|
|
|
|
public UITextView TextView { get; set; }
|
|
|
|
|
public UIButton Button { get; set; }
|
|
|
|
|
public event EventHandler ValueChanged;
|
|
|
|
|
|
|
|
|
|
|
2019-06-27 21:48:25 +02:00
|
|
|
|
public FormEntryTableViewCell(
|
|
|
|
|
string labelName = null,
|
|
|
|
|
bool useTextView = false,
|
|
|
|
|
nfloat? height = null,
|
2021-10-26 23:46:11 +02:00
|
|
|
|
bool useButton = false,
|
2021-01-25 21:23:50 +01:00
|
|
|
|
bool useLabelAsPlaceholder = false,
|
|
|
|
|
float leadingConstant = 15f)
|
2019-06-27 21:48:25 +02:00
|
|
|
|
: base(UITableViewCellStyle.Default, nameof(FormEntryTableViewCell))
|
|
|
|
|
{
|
|
|
|
|
var descriptor = UIFontDescriptor.PreferredBody;
|
|
|
|
|
var pointSize = descriptor.PointSize;
|
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (labelName != null && !useLabelAsPlaceholder)
|
2019-06-27 21:48:25 +02:00
|
|
|
|
{
|
|
|
|
|
Label = new UILabel
|
|
|
|
|
{
|
|
|
|
|
Text = labelName,
|
|
|
|
|
TranslatesAutoresizingMaskIntoConstraints = false,
|
|
|
|
|
Font = UIFont.FromDescriptor(descriptor, 0.8f * pointSize),
|
2019-07-01 21:12:40 +02:00
|
|
|
|
TextColor = ThemeHelpers.MutedColor
|
2019-06-27 21:48:25 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ContentView.Add(Label);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (useTextView)
|
2019-06-27 21:48:25 +02:00
|
|
|
|
{
|
|
|
|
|
TextView = new UITextView
|
|
|
|
|
{
|
|
|
|
|
TranslatesAutoresizingMaskIntoConstraints = false,
|
2019-07-01 21:12:40 +02:00
|
|
|
|
Font = UIFont.FromDescriptor(descriptor, pointSize),
|
|
|
|
|
TextColor = ThemeHelpers.TextColor,
|
|
|
|
|
TintColor = ThemeHelpers.TextColor,
|
|
|
|
|
BackgroundColor = ThemeHelpers.BackgroundColor
|
2019-06-27 21:48:25 +02:00
|
|
|
|
};
|
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!ThemeHelpers.LightTheme)
|
2019-08-29 02:27:15 +02:00
|
|
|
|
{
|
|
|
|
|
TextView.KeyboardAppearance = UIKeyboardAppearance.Dark;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-27 21:48:25 +02:00
|
|
|
|
ContentView.Add(TextView);
|
|
|
|
|
ContentView.AddConstraints(new NSLayoutConstraint[] {
|
2021-01-25 21:23:50 +01:00
|
|
|
|
NSLayoutConstraint.Create(TextView, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Leading, 1f, leadingConstant),
|
2019-06-27 21:48:25 +02:00
|
|
|
|
NSLayoutConstraint.Create(ContentView, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, TextView, NSLayoutAttribute.Trailing, 1f, 15f),
|
|
|
|
|
NSLayoutConstraint.Create(ContentView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, TextView, NSLayoutAttribute.Bottom, 1f, 10f)
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (labelName != null && !useLabelAsPlaceholder)
|
2019-06-27 21:48:25 +02:00
|
|
|
|
{
|
|
|
|
|
ContentView.AddConstraint(
|
|
|
|
|
NSLayoutConstraint.Create(TextView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, Label, NSLayoutAttribute.Bottom, 1f, 10f));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ContentView.AddConstraint(
|
|
|
|
|
NSLayoutConstraint.Create(TextView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Top, 1f, 10f));
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (height.HasValue)
|
2019-06-27 21:48:25 +02:00
|
|
|
|
{
|
|
|
|
|
ContentView.AddConstraint(
|
|
|
|
|
NSLayoutConstraint.Create(TextView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1f, height.Value));
|
|
|
|
|
}
|
2021-01-25 21:23:50 +01:00
|
|
|
|
|
|
|
|
|
TextView.Changed += (object sender, EventArgs e) =>
|
|
|
|
|
{
|
|
|
|
|
ValueChanged?.Invoke(sender, e);
|
|
|
|
|
};
|
2019-06-27 21:48:25 +02:00
|
|
|
|
}
|
2021-01-25 21:23:50 +01:00
|
|
|
|
|
2019-06-27 21:48:25 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
TextField = new UITextField
|
|
|
|
|
{
|
|
|
|
|
TranslatesAutoresizingMaskIntoConstraints = false,
|
|
|
|
|
BorderStyle = UITextBorderStyle.None,
|
|
|
|
|
Font = UIFont.FromDescriptor(descriptor, pointSize),
|
2019-07-01 21:12:40 +02:00
|
|
|
|
ClearButtonMode = UITextFieldViewMode.WhileEditing,
|
|
|
|
|
TextColor = ThemeHelpers.TextColor,
|
|
|
|
|
TintColor = ThemeHelpers.TextColor,
|
|
|
|
|
BackgroundColor = ThemeHelpers.BackgroundColor
|
2019-06-27 21:48:25 +02:00
|
|
|
|
};
|
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!ThemeHelpers.LightTheme)
|
2019-08-29 02:27:15 +02:00
|
|
|
|
{
|
|
|
|
|
TextField.KeyboardAppearance = UIKeyboardAppearance.Dark;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (useLabelAsPlaceholder)
|
2019-06-27 21:48:25 +02:00
|
|
|
|
{
|
|
|
|
|
TextField.Placeholder = labelName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ContentView.Add(TextField);
|
|
|
|
|
ContentView.AddConstraints(new NSLayoutConstraint[] {
|
2021-01-25 21:23:50 +01:00
|
|
|
|
NSLayoutConstraint.Create(TextField, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Leading, 1f, leadingConstant),
|
2021-10-26 23:46:11 +02:00
|
|
|
|
NSLayoutConstraint.Create(ContentView, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, TextField, NSLayoutAttribute.Trailing, 1f, useButton ? 55f : 15f),
|
2019-06-27 21:48:25 +02:00
|
|
|
|
NSLayoutConstraint.Create(ContentView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, TextField, NSLayoutAttribute.Bottom, 1f, 10f)
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (labelName != null && !useLabelAsPlaceholder)
|
2019-06-27 21:48:25 +02:00
|
|
|
|
{
|
|
|
|
|
ContentView.AddConstraint(
|
|
|
|
|
NSLayoutConstraint.Create(TextField, NSLayoutAttribute.Top, NSLayoutRelation.Equal, Label, NSLayoutAttribute.Bottom, 1f, 10f));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ContentView.AddConstraint(
|
|
|
|
|
NSLayoutConstraint.Create(TextField, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Top, 1f, 10f));
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (height.HasValue)
|
2019-06-27 21:48:25 +02:00
|
|
|
|
{
|
|
|
|
|
ContentView.AddConstraint(
|
|
|
|
|
NSLayoutConstraint.Create(TextField, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1f, height.Value));
|
|
|
|
|
}
|
2021-01-25 21:23:50 +01:00
|
|
|
|
|
|
|
|
|
TextField.AddTarget((object sender, EventArgs e) =>
|
|
|
|
|
{
|
|
|
|
|
ValueChanged?.Invoke(sender, e);
|
|
|
|
|
}, UIControlEvent.EditingChanged);
|
2019-06-27 21:48:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (labelName != null && !useLabelAsPlaceholder)
|
2019-06-27 21:48:25 +02:00
|
|
|
|
{
|
|
|
|
|
ContentView.AddConstraints(new NSLayoutConstraint[] {
|
2021-01-25 21:23:50 +01:00
|
|
|
|
NSLayoutConstraint.Create(Label, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Leading, 1f, leadingConstant),
|
2019-06-27 21:48:25 +02:00
|
|
|
|
NSLayoutConstraint.Create(Label, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Top, 1f, 10f),
|
|
|
|
|
NSLayoutConstraint.Create(ContentView, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, Label, NSLayoutAttribute.Trailing, 1f, 15f)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 23:46:11 +02:00
|
|
|
|
if (useButton)
|
|
|
|
|
{
|
|
|
|
|
Button = new UIButton(UIButtonType.System);
|
|
|
|
|
Button.Frame = ContentView.Bounds;
|
|
|
|
|
Button.TranslatesAutoresizingMaskIntoConstraints = false;
|
|
|
|
|
Button.SetTitleColor(ThemeHelpers.PrimaryColor, UIControlState.Normal);
|
|
|
|
|
|
|
|
|
|
ContentView.Add(Button);
|
|
|
|
|
|
|
|
|
|
ContentView.BottomAnchor.ConstraintEqualTo(Button.BottomAnchor, 10f).Active = true;
|
|
|
|
|
ContentView.TrailingAnchor.ConstraintEqualTo(Button.TrailingAnchor, 10f).Active = true;
|
|
|
|
|
Button.LeadingAnchor.ConstraintEqualTo(TextField.TrailingAnchor, 10f).Active = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-27 21:48:25 +02:00
|
|
|
|
|
|
|
|
|
public void Select()
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (TextView != null)
|
2019-06-27 21:48:25 +02:00
|
|
|
|
{
|
|
|
|
|
TextView.BecomeFirstResponder();
|
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
else if (TextField != null)
|
2019-06-27 21:48:25 +02:00
|
|
|
|
{
|
|
|
|
|
TextField.BecomeFirstResponder();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|