mirror of
https://github.com/bitwarden/android.git
synced 2025-03-07 23:16:00 +03:00
Setup picker tableview cell to mimic xamarin forms picker
This commit is contained in:
parent
822a14e56c
commit
4723e6a101
2 changed files with 125 additions and 17 deletions
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using CoreGraphics;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using UIKit;
|
||||
|
@ -7,14 +8,35 @@ namespace Bit.iOS.Core.Views
|
|||
{
|
||||
public class PickerTableViewCell : UITableViewCell
|
||||
{
|
||||
public PickerTableViewCell(string labelName)
|
||||
private List<string> _items = new List<string>();
|
||||
private int _selectedIndex = -1;
|
||||
|
||||
public PickerTableViewCell(
|
||||
string labelName,
|
||||
nfloat? height = null)
|
||||
: base(UITableViewCellStyle.Default, nameof(PickerTableViewCell))
|
||||
{
|
||||
TextLabel.Text = labelName;
|
||||
var descriptor = UIFontDescriptor.PreferredBody;
|
||||
var pointSize = descriptor.PointSize;
|
||||
|
||||
var entry = new UITextField { BorderStyle = UITextBorderStyle.RoundedRect };
|
||||
entry.Started += Entry_Started;
|
||||
entry.Ended += Entry_Ended;
|
||||
Label = new UILabel
|
||||
{
|
||||
Text = labelName,
|
||||
TranslatesAutoresizingMaskIntoConstraints = false,
|
||||
Font = UIFont.FromDescriptor(descriptor, 0.8f * pointSize),
|
||||
TextColor = new UIColor(red: 0.47f, green: 0.47f, blue: 0.47f, alpha: 1.0f)
|
||||
};
|
||||
|
||||
ContentView.Add(Label);
|
||||
|
||||
TextField = new NoCaretField
|
||||
{
|
||||
BorderStyle = UITextBorderStyle.None,
|
||||
TranslatesAutoresizingMaskIntoConstraints = false,
|
||||
Font = UIFont.FromDescriptor(descriptor, pointSize)
|
||||
};
|
||||
TextField.Started += Entry_Started;
|
||||
TextField.Ended += Entry_Ended;
|
||||
|
||||
var width = (float)UIScreen.MainScreen.Bounds.Width;
|
||||
var toolbar = new UIToolbar(new RectangleF(0, 0, width, 44))
|
||||
|
@ -28,21 +50,89 @@ namespace Bit.iOS.Core.Views
|
|||
var s = (PickerSource)Picker.Model;
|
||||
if(s.SelectedIndex == -1 && Items != null && Items.Count > 0)
|
||||
{
|
||||
|
||||
UpdatePickerSelectedIndex(0);
|
||||
}
|
||||
entry.ResignFirstResponder();
|
||||
TextField.Text = s.SelectedItem;
|
||||
TextField.ResignFirstResponder();
|
||||
});
|
||||
|
||||
toolbar.SetItems(new[] { spacer, doneButton }, false);
|
||||
|
||||
entry.InputView = Picker;
|
||||
entry.InputAccessoryView = toolbar;
|
||||
TextField.InputView = Picker;
|
||||
TextField.InputAccessoryView = toolbar;
|
||||
|
||||
ContentView.Add(TextField);
|
||||
|
||||
ContentView.AddConstraints(new NSLayoutConstraint[] {
|
||||
NSLayoutConstraint.Create(TextField, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Leading, 1f, 15f),
|
||||
NSLayoutConstraint.Create(ContentView, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, TextField, NSLayoutAttribute.Trailing, 1f, 15f),
|
||||
NSLayoutConstraint.Create(ContentView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, TextField, NSLayoutAttribute.Bottom, 1f, 10f),
|
||||
NSLayoutConstraint.Create(TextField, NSLayoutAttribute.Top, NSLayoutRelation.Equal, Label, NSLayoutAttribute.Bottom, 1f, 10f),
|
||||
NSLayoutConstraint.Create(Label, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Leading, 1f, 15f),
|
||||
NSLayoutConstraint.Create(Label, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Top, 1f, 10f),
|
||||
NSLayoutConstraint.Create(ContentView, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, Label, NSLayoutAttribute.Trailing, 1f, 15f)
|
||||
});
|
||||
|
||||
if(height.HasValue)
|
||||
{
|
||||
ContentView.AddConstraint(
|
||||
NSLayoutConstraint.Create(TextField, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1f, height.Value));
|
||||
}
|
||||
|
||||
Picker.Model = new PickerSource(this);
|
||||
}
|
||||
|
||||
public UITextField TextField { get; set; }
|
||||
public UILabel Label { get; set; }
|
||||
public UIPickerView Picker { get; set; } = new UIPickerView();
|
||||
public int MyProperty { get; set; }
|
||||
public List<string> Items { get; set; } = new List<string>();
|
||||
public int SelectedIndex { get; set; }
|
||||
|
||||
public List<string> Items
|
||||
{
|
||||
get { return _items; }
|
||||
set
|
||||
{
|
||||
_items = value;
|
||||
UpdatePicker();
|
||||
}
|
||||
}
|
||||
|
||||
public int SelectedIndex
|
||||
{
|
||||
get { return _selectedIndex; }
|
||||
set
|
||||
{
|
||||
_selectedIndex = value;
|
||||
UpdatePicker();
|
||||
}
|
||||
}
|
||||
|
||||
public string SelectedItem => TextField.Text;
|
||||
|
||||
private void UpdatePicker()
|
||||
{
|
||||
TextField.Text = SelectedIndex == -1 || Items == null ? "" : Items[SelectedIndex];
|
||||
Picker.ReloadAllComponents();
|
||||
if(Items == null || Items.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UpdatePickerSelectedIndex(SelectedIndex);
|
||||
}
|
||||
|
||||
private void UpdatePickerFromModel(PickerSource s)
|
||||
{
|
||||
TextField.Text = s.SelectedItem;
|
||||
_selectedIndex = s.SelectedIndex;
|
||||
}
|
||||
|
||||
private void UpdatePickerSelectedIndex(int formsIndex)
|
||||
{
|
||||
var source = (PickerSource)Picker.Model;
|
||||
source.SelectedIndex = formsIndex;
|
||||
source.SelectedItem = formsIndex >= 0 ? Items[formsIndex] : null;
|
||||
Picker.Select(Math.Max(formsIndex, 0), 0, true);
|
||||
}
|
||||
|
||||
private void Entry_Ended(object sender, EventArgs e)
|
||||
{
|
||||
|
@ -54,9 +144,20 @@ namespace Bit.iOS.Core.Views
|
|||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private class NoCaretField : UITextField
|
||||
{
|
||||
public NoCaretField() : base(default(CGRect))
|
||||
{ }
|
||||
|
||||
public override CGRect GetCaretRectForPosition(UITextPosition position)
|
||||
{
|
||||
return default(CGRect);
|
||||
}
|
||||
}
|
||||
|
||||
private class PickerSource : UIPickerViewModel
|
||||
{
|
||||
readonly PickerTableViewCell _cell;
|
||||
private readonly PickerTableViewCell _cell;
|
||||
|
||||
public PickerSource(PickerTableViewCell cell)
|
||||
{
|
||||
|
@ -64,7 +165,6 @@ namespace Bit.iOS.Core.Views
|
|||
}
|
||||
|
||||
public int SelectedIndex { get; internal set; }
|
||||
|
||||
public string SelectedItem { get; internal set; }
|
||||
|
||||
public override nint GetComponentCount(UIPickerView picker)
|
||||
|
@ -95,7 +195,7 @@ namespace Bit.iOS.Core.Views
|
|||
SelectedIndex = (int)row;
|
||||
}
|
||||
|
||||
//_renderer.UpdatePickerFromModel(this);
|
||||
_cell.UpdatePickerFromModel(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ namespace Bit.iOS.Extension
|
|||
public UITableViewCell GeneratePasswordCell { get; set; } = new UITableViewCell(UITableViewCellStyle.Subtitle, "GeneratePasswordCell");
|
||||
public SwitchTableViewCell FavoriteCell { get; set; } = new SwitchTableViewCell("Favorite");
|
||||
public FormEntryTableViewCell NotesCell { get; set; } = new FormEntryTableViewCell(useTextView: true, height: 90);
|
||||
public PickerTableViewCell FolderCell { get; set; } = new PickerTableViewCell(AppResources.Folder);
|
||||
|
||||
public override void ViewWillAppear(bool animated)
|
||||
{
|
||||
|
@ -87,6 +88,9 @@ namespace Bit.iOS.Extension
|
|||
GeneratePasswordCell.TextLabel.Text = "Generate Password";
|
||||
GeneratePasswordCell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
|
||||
|
||||
FolderCell.Items = new List<string> { "Folder 1", "Folder 2" };
|
||||
FolderCell.SelectedIndex = 1;
|
||||
|
||||
TableView.RowHeight = UITableView.AutomaticDimension;
|
||||
TableView.EstimatedRowHeight = 70;
|
||||
TableView.Source = new TableSource(this);
|
||||
|
@ -195,7 +199,11 @@ namespace Bit.iOS.Extension
|
|||
}
|
||||
else if(indexPath.Section == 1)
|
||||
{
|
||||
if(indexPath.Row == 1)
|
||||
if(indexPath.Row == 0)
|
||||
{
|
||||
return _controller.FolderCell;
|
||||
}
|
||||
else if(indexPath.Row == 1)
|
||||
{
|
||||
return _controller.FavoriteCell;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue