Optimizations for dynamic text sizes.

This commit is contained in:
Kyle Spearrin 2016-06-28 23:44:47 -04:00
parent 734d2632dc
commit 2e9410846e
12 changed files with 206 additions and 30 deletions

View file

@ -43,6 +43,20 @@ namespace Bit.Android.Controls
return View;
}
protected override void OnCellPropertyChanged(object sender, PropertyChangedEventArgs args)
{
base.OnCellPropertyChanged(sender, args);
var cell = (ExtendedTextCell)Cell;
if(args.PropertyName == ExtendedTextCell.BackgroundColorProperty.PropertyName)
{
View.SetBackgroundColor(cell.BackgroundColor.ToAndroid());
}
// TODO: other properties
}
private class DisclosureImage : ImageView
{
private ExtendedTextCell _cell;
@ -66,19 +80,5 @@ namespace Bit.Android.Controls
return true;
}
}
protected override void OnCellPropertyChanged(object sender, PropertyChangedEventArgs args)
{
base.OnCellPropertyChanged(sender, args);
var cell = (ExtendedTextCell)Cell;
if(args.PropertyName == ExtendedTextCell.BackgroundColorProperty.PropertyName)
{
View.SetBackgroundColor(cell.BackgroundColor.ToAndroid());
}
// TODO: other properties
}
}
}

View file

@ -6,6 +6,7 @@ using Bit.App.Controls;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using AView = Android.Views.View;
using Android.Widget;
[assembly: ExportRenderer(typeof(ExtendedViewCell), typeof(ExtendedViewCellRenderer))]
namespace Bit.Android.Controls
@ -16,12 +17,28 @@ namespace Bit.Android.Controls
protected override AView GetCellCore(Cell item, AView convertView, ViewGroup parent, Context context)
{
var View = base.GetCellCore(item, convertView, parent, context);
var View = (BaseCellView)base.GetCellCore(item, convertView, parent, context);
var extendedCell = (ExtendedViewCell)item;
if(View != null)
{
View.SetBackgroundColor(extendedCell.BackgroundColor.ToAndroid());
if(extendedCell.ShowDisclousure)
{
var resourceId = Resource.Drawable.ion_chevron_right;
if(!string.IsNullOrWhiteSpace(extendedCell.DisclousureImage))
{
var fileName = System.IO.Path.GetFileNameWithoutExtension(extendedCell.DisclousureImage);
resourceId = context.Resources.GetIdentifier(fileName, "drawable", context.PackageName);
}
var image = new DisclosureImage(context, extendedCell);
image.SetImageResource(resourceId);
image.SetPadding(10, 10, 30, 10);
View.SetAccessoryView(image);
}
}
return View;
@ -31,12 +48,36 @@ namespace Bit.Android.Controls
{
base.OnCellPropertyChanged(sender, args);
var cell = (ExtendedTextCell)Cell;
var cell = (ExtendedViewCell)Cell;
if(args.PropertyName == ExtendedTextCell.BackgroundColorProperty.PropertyName)
if(args.PropertyName == ExtendedViewCell.BackgroundColorProperty.PropertyName)
{
View.SetBackgroundColor(cell.BackgroundColor.ToAndroid());
}
}
private class DisclosureImage : ImageView
{
private ExtendedViewCell _cell;
public DisclosureImage(Context context, ExtendedViewCell cell) : base(context)
{
_cell = cell;
}
public override bool OnTouchEvent(MotionEvent e)
{
switch(e.Action)
{
case MotionEventActions.Up:
_cell.OnDisclousureTapped();
break;
default:
break;
}
return true;
}
}
}
}

View file

@ -62,6 +62,7 @@
<Compile Include="Controls\ExtendedViewCell.cs" />
<Compile Include="Controls\FormEditorCell.cs" />
<Compile Include="Controls\ExtendedTextCell.cs" />
<Compile Include="Controls\LabeledDetailCell.cs" />
<Compile Include="Controls\LabeledValueCell.cs" />
<Compile Include="Controls\FormPickerCell.cs" />
<Compile Include="Controls\FormEntryCell.cs" />

View file

@ -1,4 +1,5 @@
using Xamarin.Forms;
using System;
using Xamarin.Forms;
namespace Bit.App.Controls
{
@ -7,10 +8,35 @@ namespace Bit.App.Controls
public static readonly BindableProperty BackgroundColorProperty =
BindableProperty.Create(nameof(BackgroundColor), typeof(Color), typeof(ExtendedTextCell), Color.White);
public static readonly BindableProperty ShowDisclousureProperty =
BindableProperty.Create(nameof(DisclousureImage), typeof(bool), typeof(ExtendedTextCell), false);
public static readonly BindableProperty DisclousureImageProperty =
BindableProperty.Create(nameof(DisclousureImage), typeof(string), typeof(ExtendedTextCell), string.Empty);
public Color BackgroundColor
{
get { return (Color)GetValue(BackgroundColorProperty); }
set { SetValue(BackgroundColorProperty, value); }
}
public bool ShowDisclousure
{
get { return (bool)GetValue(ShowDisclousureProperty); }
set { SetValue(ShowDisclousureProperty, value); }
}
public string DisclousureImage
{
get { return (string)GetValue(DisclousureImageProperty); }
set { SetValue(DisclousureImageProperty, value); }
}
public event EventHandler DisclousureTapped;
public void OnDisclousureTapped()
{
DisclousureTapped?.Invoke(this, EventArgs.Empty);
}
}
}

View file

@ -0,0 +1,38 @@
using Xamarin.Forms;
namespace Bit.App.Controls
{
public class LabeledDetailCell : ExtendedViewCell
{
public LabeledDetailCell()
{
Label = new Label
{
VerticalOptions = LayoutOptions.CenterAndExpand,
LineBreakMode = LineBreakMode.TailTruncation
};
Detail = new Label
{
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
LineBreakMode = LineBreakMode.TailTruncation,
VerticalOptions = LayoutOptions.End,
Style = (Style)Application.Current.Resources["text-muted"],
};
var stackLayout = new StackLayout
{
Padding = new Thickness(15, 5),
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
Children = { Label, Detail },
Spacing = 0
};
View = stackLayout;
}
public Label Label { get; private set; }
public Label Detail { get; private set; }
}
}

View file

@ -87,6 +87,7 @@ namespace Bit.App.Pages
{
EnableScrolling = true,
Intent = TableIntent.Menu,
HasUnevenRows = true,
Root = new TableRoot
{
new TableSection("Security")
@ -108,10 +109,11 @@ namespace Bit.App.Pages
}
};
var scrollView = new ScrollView
if( Device.OS == TargetPlatform.iOS )
{
Content = table
};
table.RowHeight = -1;
table.EstimatedRowHeight = 44;
}
Title = AppResources.Settings;
Content = table;

View file

@ -179,7 +179,7 @@ namespace Bit.App.Pages
}
}
private class VaultListViewCell : ExtendedTextCell
private class VaultListViewCell : LabeledDetailCell
{
private VaultListSitesPage _page;
@ -196,9 +196,9 @@ namespace Bit.App.Pages
moreAction.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));
moreAction.Clicked += MoreAction_Clicked;
SetBinding(CommandParameterProperty, new Binding("."));
this.SetBinding<VaultListPageModel.Site>(TextProperty, s => s.Name);
this.SetBinding<VaultListPageModel.Site>(DetailProperty, s => s.Username);
//SetBinding(CommandParameterProperty, new Binding("."));
Label.SetBinding<VaultListPageModel.Site>(Label.TextProperty, s => s.Name);
Detail.SetBinding<VaultListPageModel.Site>(Label.TextProperty, s => s.Username);
ContextActions.Add(deleteAction);
ContextActions.Add(moreAction);
@ -218,8 +218,8 @@ namespace Bit.App.Pages
private void VaultListViewCell_DisclousureTapped(object sender, EventArgs e)
{
var cell = sender as VaultListViewCell;
var site = cell.CommandParameter as VaultListPageModel.Site;
_page.MoreClickedAsync(site);
//var site = cell.CommandParameter as VaultListPageModel.Site;
// _page.MoreClickedAsync(site);
}
}

View file

@ -0,0 +1,40 @@
using System;
using Bit.iOS.Controls;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(Button), typeof(CustomButtonRenderer))]
namespace Bit.iOS.Controls
{
public class CustomButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
var view = e.NewElement as Button;
if(Control != null && view != null)
{
var descriptor = UIFontDescriptor.PreferredBody;
var pointSize = descriptor.PointSize;
var size = view.FontSize;
if(size == Device.GetNamedSize(NamedSize.Large, typeof(Button)))
{
pointSize *= 1.4f;
}
else if(size == Device.GetNamedSize(NamedSize.Small, typeof(Button)))
{
pointSize *= .8f;
}
else if(size == Device.GetNamedSize(NamedSize.Micro, typeof(Button)))
{
pointSize *= .6f;
}
Control.Font = UIFont.FromDescriptor(descriptor, pointSize);
}
}
}
}

View file

@ -22,7 +22,7 @@ namespace Bit.iOS.Controls
var size = view.FontSize;
if(size == Device.GetNamedSize(NamedSize.Large, typeof(Label)))
{
pointSize *= 1.7f;
pointSize *= 1.4f;
}
else if(size == Device.GetNamedSize(NamedSize.Small, typeof(Label)))
{
@ -33,7 +33,7 @@ namespace Bit.iOS.Controls
pointSize *= .6f;
}
UIFont.FromDescriptor(descriptor, pointSize);
Control.Font = UIFont.FromDescriptor(descriptor, pointSize);
}
}
}

View file

@ -21,7 +21,8 @@ namespace Bit.iOS.Controls
var view = e.NewElement as ExtendedEntry;
if(view != null)
{
UIFont.FromDescriptor(UIFontDescriptor.PreferredBody, 1.0f);
var descriptor = UIFontDescriptor.PreferredBody;
Control.Font = UIFont.FromDescriptor( descriptor, descriptor.PointSize );
SetBorder(view);
SetMaxLength(view);

View file

@ -1,5 +1,6 @@
using Bit.App.Controls;
using Bit.iOS.Controls;
using CoreGraphics;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
@ -17,6 +18,31 @@ namespace Bit.iOS.Controls
if(cell != null)
{
cell.BackgroundColor = extendedCell.BackgroundColor.ToUIColor();
if(extendedCell.ShowDisclousure)
{
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
if(!string.IsNullOrEmpty(extendedCell.DisclousureImage))
{
var detailDisclosureButton = UIButton.FromType(UIButtonType.Custom);
detailDisclosureButton.SetImage(UIImage.FromBundle(extendedCell.DisclousureImage), UIControlState.Normal);
try
{
detailDisclosureButton.SetImage(UIImage.FromBundle(extendedCell.DisclousureImage + "_selected"), UIControlState.Selected);
}
catch
{
detailDisclosureButton.SetImage(UIImage.FromBundle(extendedCell.DisclousureImage), UIControlState.Selected);
}
detailDisclosureButton.Frame = new CGRect(0f, 0f, 50f, 40f);
detailDisclosureButton.TouchUpInside += (sender, e) =>
{
extendedCell.OnDisclousureTapped();
};
cell.AccessoryView = detailDisclosureButton;
}
}
}
return cell;

View file

@ -101,6 +101,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="Controls\ContentPageRenderer.cs" />
<Compile Include="Controls\CustomButtonRenderer.cs" />
<Compile Include="Controls\CustomLabelRenderer.cs" />
<Compile Include="Controls\ExtendedSwitchCellRenderer.cs" />
<Compile Include="Controls\ListViewRenderer.cs" />