1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-26 13:13:28 +00:00
This commit is contained in:
Kyle Spearrin
2019-04-04 23:00:02 -04:00
parent 61e95e03c8
commit 8006bcf10c
8 changed files with 369 additions and 513 deletions

View File

@@ -12,7 +12,7 @@ using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using AView = Android.Views.View;
namespace Bit.Droid.Renderers
namespace Bit.Droid.Renderers.BoxedView
{
[Preserve(AllMembers = true)]
public class BoxedViewRecyclerAdapter : RecyclerView.Adapter, AView.IOnClickListener
@@ -20,8 +20,6 @@ namespace Bit.Droid.Renderers
private const int ViewTypeHeader = 0;
private const int ViewTypeFooter = 1;
private float MinRowHeight => _context.ToPixels(44);
private Dictionary<Type, int> _viewTypes;
private List<CellCache> _cellCaches;
@@ -42,28 +40,21 @@ namespace Bit.Droid.Renderers
private AView _preSelectedCell = null;
Context _context;
BoxedView _boxedView;
App.Controls.BoxedView.BoxedView _boxedView;
RecyclerView _recyclerView;
List<ViewHolder> _viewHolders = new List<ViewHolder>();
public BoxedViewRecyclerAdapter(Context context, BoxedView boxedView, RecyclerView recyclerView)
public BoxedViewRecyclerAdapter(Context context, App.Controls.BoxedView.BoxedView boxedView,
RecyclerView recyclerView)
{
_context = context;
_boxedView = boxedView;
_recyclerView = recyclerView;
_boxedView.ModelChanged += _boxedView_ModelChanged;
_boxedView.ModelChanged += BoxedView_ModelChanged;
}
void _boxedView_ModelChanged(object sender, EventArgs e)
{
if(_recyclerView != null)
{
_cellCaches = null;
NotifyDataSetChanged();
}
}
private float MinRowHeight => _context.ToPixels(44);
public override int ItemCount => CellCaches.Count;
@@ -101,28 +92,24 @@ namespace Bit.Droid.Renderers
{
var position = _recyclerView.GetChildAdapterPosition(view);
//TODO: It is desirable that the forms side has Selected property and reflects it.
// But do it at a later as iOS side doesn't have that process.
// TODO: It is desirable that the forms side has Selected property and reflects it.
// But do it at a later as iOS side doesn't have that process.
DeselectRow();
var cell = view.FindViewById<LinearLayout>(Resource.Id.ContentCellBody).GetChildAt(0) as BaseCellView;
if(cell == null || !CellCaches[position].Cell.IsEnabled)
{
//if FormsCell IsEnable is false, does nothing.
// If FormsCell IsEnable is false, does nothing.
return;
}
_boxedView.Model.RowSelected(CellCaches[position].Cell);
cell.RowSelected(this, position);
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
var cellInfo = CellCaches[position];
switch(holder.ItemViewType)
{
case ViewTypeHeader:
@@ -175,7 +162,7 @@ namespace Bit.Droid.Renderers
{
if(disposing)
{
_boxedView.ModelChanged -= _boxedView_ModelChanged;
_boxedView.ModelChanged -= BoxedView_ModelChanged;
_cellCaches?.Clear();
_cellCaches = null;
_boxedView = null;
@@ -190,12 +177,20 @@ namespace Bit.Droid.Renderers
base.Dispose(disposing);
}
private void BoxedView_ModelChanged(object sender, EventArgs e)
{
if(_recyclerView != null)
{
_cellCaches = null;
NotifyDataSetChanged();
}
}
private void BindHeaderView(HeaderViewHolder holder, TextCell formsCell)
{
var view = holder.ItemView;
//judging cell height
// Judging cell height
int cellHeight = (int)_context.ToPixels(44);
var individualHeight = formsCell.Height;
@@ -211,7 +206,6 @@ namespace Bit.Droid.Renderers
view.SetMinimumHeight(cellHeight);
view.LayoutParameters.Height = cellHeight;
//textview setting
holder.TextView.SetPadding(
(int)view.Context.ToPixels(_boxedView.HeaderPadding.Left),
(int)view.Context.ToPixels(_boxedView.HeaderPadding.Top),
@@ -231,7 +225,7 @@ namespace Bit.Droid.Renderers
holder.TextView.SetTextColor(_boxedView.HeaderTextColor.ToAndroid());
}
//border setting
// Border setting
if(_boxedView.ShowSectionTopBottomBorder)
{
holder.Border.SetBackgroundColor(_boxedView.SeparatorColor.ToAndroid());
@@ -241,7 +235,7 @@ namespace Bit.Droid.Renderers
holder.Border.SetBackgroundColor(Android.Graphics.Color.Transparent);
}
//update text
// Update text
holder.TextView.Text = formsCell.Text;
}
@@ -249,7 +243,7 @@ namespace Bit.Droid.Renderers
{
var view = holder.ItemView;
//footer visible setting
// Footer visible setting
if(string.IsNullOrEmpty(formsCell.Text))
{
//if text is empty, hidden (height 0)
@@ -262,7 +256,6 @@ namespace Bit.Droid.Renderers
view.Visibility = ViewStates.Visible;
}
//textview setting
holder.TextView.SetPadding(
(int)view.Context.ToPixels(_boxedView.FooterPadding.Left),
(int)view.Context.ToPixels(_boxedView.FooterPadding.Top),
@@ -276,7 +269,7 @@ namespace Bit.Droid.Renderers
holder.TextView.SetTextColor(_boxedView.FooterTextColor.ToAndroid());
}
//update text
// Update text
holder.TextView.Text = formsCell.Text;
}
@@ -305,31 +298,31 @@ namespace Bit.Droid.Renderers
var minHeight = (int)Math.Max(_context.ToPixels(_boxedView.RowHeight), MinRowHeight);
//it is neccesary to set both
// It is necessary to set both
layout.SetMinimumHeight(minHeight);
nativeCell.SetMinimumHeight(minHeight);
if(!_boxedView.HasUnevenRows)
{
// if not Uneven, set the larger one of RowHeight and MinRowHeight.
// If not Uneven, set the larger one of RowHeight and MinRowHeight.
layout.LayoutParameters.Height = minHeight;
}
else if(formsCell.Height > -1)
{
// if the cell itself was specified height, set it.
// If the cell itself was specified height, set it.
layout.SetMinimumHeight((int)_context.ToPixels(formsCell.Height));
layout.LayoutParameters.Height = (int)_context.ToPixels(formsCell.Height);
}
else if(formsCell is ViewCell viewCell)
{
// if used a viewcell, calculate the size and layout it.
// If used a viewcell, calculate the size and layout it.
var size = viewCell.View.Measure(_boxedView.Width, double.PositiveInfinity);
viewCell.View.Layout(new Rectangle(0, 0, size.Request.Width, size.Request.Height));
layout.LayoutParameters.Height = (int)_context.ToPixels(size.Request.Height);
}
else
{
layout.LayoutParameters.Height = -2; //wrap_content
layout.LayoutParameters.Height = -2; // wrap_content
}
if(!CellCaches[position].IsLastCell || _boxedView.ShowSectionTopBottomBorder)
@@ -446,7 +439,7 @@ namespace Bit.Droid.Renderers
[Preserve(AllMembers = true)]
internal class HeaderViewHolder : ViewHolder
{
public HeaderViewHolder(AView view, BoxedView boxedView)
public HeaderViewHolder(AView view, App.Controls.BoxedView.BoxedView boxedView)
: base(view)
{
TextView = view.FindViewById<TextView>(Resource.Id.HeaderCellText);
@@ -474,7 +467,7 @@ namespace Bit.Droid.Renderers
{
public TextView TextView { get; private set; }
public FooterViewHolder(AView view, BoxedView boxedView)
public FooterViewHolder(AView view, App.Controls.BoxedView.BoxedView boxedView)
: base(view)
{
TextView = view.FindViewById<TextView>(Resource.Id.FooterCellText);

View File

@@ -4,17 +4,17 @@ using Android.Support.V7.Widget;
using Android.Support.V7.Widget.Helper;
using Android.Views;
using Bit.App.Controls.BoxedView;
using Bit.Droid.Renderers;
using Bit.Droid.Renderers.BoxedView;
using System;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(BoxedView), typeof(BoxedViewRenderer))]
namespace Bit.Droid.Renderers
namespace Bit.Droid.Renderers.BoxedView
{
[Preserve(AllMembers = true)]
public class BoxedViewRenderer : ViewRenderer<BoxedView, RecyclerView>
public class BoxedViewRenderer : ViewRenderer<App.Controls.BoxedView.BoxedView, RecyclerView>
{
private Page _parentPage;
private LinearLayoutManager _layoutManager;
@@ -28,7 +28,7 @@ namespace Bit.Droid.Renderers
AutoPackage = false;
}
protected override void OnElementChanged(ElementChangedEventArgs<BoxedView> e)
protected override void OnElementChanged(ElementChangedEventArgs<App.Controls.BoxedView.BoxedView> e)
{
base.OnElementChanged(e);
if(e.NewElement != null)
@@ -48,7 +48,8 @@ namespace Bit.Droid.Renderers
_adapter = new BoxedViewRecyclerAdapter(Context, e.NewElement, recyclerView);
Control.SetAdapter(_adapter);
_simpleCallback = new BoxedViewSimpleCallback(e.NewElement, ItemTouchHelper.Up | ItemTouchHelper.Down, 0);
_simpleCallback = new BoxedViewSimpleCallback(
e.NewElement, ItemTouchHelper.Up | ItemTouchHelper.Down, 0);
_itemTouchhelper = new ItemTouchHelper(_simpleCallback);
_itemTouchhelper.AttachToRecyclerView(Control);
@@ -100,11 +101,11 @@ namespace Bit.Droid.Renderers
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if(e.PropertyName == BoxedView.SeparatorColorProperty.PropertyName)
if(e.PropertyName == App.Controls.BoxedView.BoxedView.SeparatorColorProperty.PropertyName)
{
_adapter.NotifyDataSetChanged();
}
else if(e.PropertyName == BoxedView.BackgroundColorProperty.PropertyName)
else if(e.PropertyName == App.Controls.BoxedView.BoxedView.BackgroundColorProperty.PropertyName)
{
UpdateBackgroundColor();
}
@@ -112,11 +113,11 @@ namespace Bit.Droid.Renderers
{
UpdateRowHeight();
}
else if(e.PropertyName == BoxedView.SelectedColorProperty.PropertyName)
else if(e.PropertyName == App.Controls.BoxedView.BoxedView.SelectedColorProperty.PropertyName)
{
//_adapter.NotifyDataSetChanged();
}
else if(e.PropertyName == BoxedView.ShowSectionTopBottomBorderProperty.PropertyName)
else if(e.PropertyName == App.Controls.BoxedView.BoxedView.ShowSectionTopBottomBorderProperty.PropertyName)
{
_adapter.NotifyDataSetChanged();
}
@@ -124,11 +125,11 @@ namespace Bit.Droid.Renderers
{
_adapter.NotifyDataSetChanged();
}
else if(e.PropertyName == BoxedView.ScrollToTopProperty.PropertyName)
else if(e.PropertyName == App.Controls.BoxedView.BoxedView.ScrollToTopProperty.PropertyName)
{
UpdateScrollToTop();
}
else if(e.PropertyName == BoxedView.ScrollToBottomProperty.PropertyName)
else if(e.PropertyName == App.Controls.BoxedView.BoxedView.ScrollToBottomProperty.PropertyName)
{
UpdateScrollToBottom();
}

View File

@@ -1,24 +1,24 @@
using Android.Runtime;
using Android.Support.V7.Widget;
using Android.Support.V7.Widget.Helper;
using Bit.App.Controls.BoxedView;
using System;
namespace Bit.Droid.Renderers
namespace Bit.Droid.Renderers.BoxedView
{
[Preserve(AllMembers = true)]
public class BoxedViewSimpleCallback : ItemTouchHelper.SimpleCallback
{
private BoxedView _boxedView;
private App.Controls.BoxedView.BoxedView _boxedView;
private int _offset = 0;
public BoxedViewSimpleCallback(BoxedView boxedView, int dragDirs, int swipeDirs)
public BoxedViewSimpleCallback(App.Controls.BoxedView.BoxedView boxedView, int dragDirs, int swipeDirs)
: base(dragDirs, swipeDirs)
{
_boxedView = boxedView;
}
public override bool OnMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target)
public override bool OnMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target)
{
if(!(viewHolder is ContentViewHolder fromContentHolder))
{

View File

@@ -8,26 +8,11 @@ using System.Linq.Expressions;
using System.Reflection;
using Xamarin.Forms.Platform.Android;
namespace Bit.Droid.Renderers
namespace Bit.Droid.Renderers.BoxedView
{
[Preserve(AllMembers = true)]
public class BaseCellRenderer<TNativeCell> : CellRenderer where TNativeCell : BaseCellView
{
internal static class InstanceCreator<T1, T2, TInstance>
{
public static Func<T1, T2, TInstance> Create { get; } = CreateInstance();
private static Func<T1, T2, TInstance> CreateInstance()
{
var argsTypes = new[] { typeof(T1), typeof(T2) };
var constructor = typeof(TInstance).GetConstructor(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
Type.DefaultBinder, argsTypes, null);
var args = argsTypes.Select(Expression.Parameter).ToArray();
return Expression.Lambda<Func<T1, T2, TInstance>>(Expression.New(constructor, args), args).Compile();
}
}
protected override View GetCellCore(Xamarin.Forms.Cell item, View convertView, ViewGroup parent,
Context context)
{
@@ -47,7 +32,7 @@ namespace Bit.Droid.Renderers
{
var formsCell = nativeCell.Cell as BaseCell;
formsCell.PropertyChanged += nativeCell.CellPropertyChanged;
if(formsCell.Parent is BoxedView parentElement)
if(formsCell.Parent is App.Controls.BoxedView.BoxedView parentElement)
{
parentElement.PropertyChanged += nativeCell.ParentPropertyChanged;
var section = parentElement.Model.GetSection(BoxedModel.GetPath(formsCell).Item1);
@@ -63,7 +48,7 @@ namespace Bit.Droid.Renderers
{
var formsCell = nativeCell.Cell as BaseCell;
formsCell.PropertyChanged -= nativeCell.CellPropertyChanged;
if(formsCell.Parent is BoxedView parentElement)
if(formsCell.Parent is App.Controls.BoxedView.BoxedView parentElement)
{
parentElement.PropertyChanged -= nativeCell.ParentPropertyChanged;
if(formsCell.Section != null)
@@ -72,5 +57,20 @@ namespace Bit.Droid.Renderers
}
}
}
internal static class InstanceCreator<T1, T2, TInstance>
{
public static Func<T1, T2, TInstance> Create { get; } = CreateInstance();
private static Func<T1, T2, TInstance> CreateInstance()
{
var argsTypes = new[] { typeof(T1), typeof(T2) };
var constructor = typeof(TInstance).GetConstructor(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
Type.DefaultBinder, argsTypes, null);
var args = argsTypes.Select(Expression.Parameter).ToArray();
return Expression.Lambda<Func<T1, T2, TInstance>>(Expression.New(constructor, args), args).Compile();
}
}
}
}

View File

@@ -12,7 +12,7 @@ using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using ARelativeLayout = Android.Widget.RelativeLayout;
namespace Bit.Droid.Renderers
namespace Bit.Droid.Renderers.BoxedView
{
[Preserve(AllMembers = true)]
public class BaseCellView : ARelativeLayout, INativeElementView
@@ -37,7 +37,7 @@ namespace Bit.Droid.Renderers
public Cell Cell { get; set; }
public Element Element => Cell;
protected BaseCell CellBase => Cell as BaseCell;
public BoxedView CellParent => Cell.Parent as BoxedView;
public App.Controls.BoxedView.BoxedView CellParent => Cell.Parent as App.Controls.BoxedView.BoxedView;
public TextView TitleLabel { get; set; }
public TextView DescriptionLabel { get; set; }
public LinearLayout ContentStack { get; set; }
@@ -122,27 +122,27 @@ namespace Bit.Droid.Renderers
return;
}
if(e.PropertyName == BoxedView.CellTitleColorProperty.PropertyName)
if(e.PropertyName == App.Controls.BoxedView.BoxedView.CellTitleColorProperty.PropertyName)
{
UpdateTitleColor();
}
else if(e.PropertyName == BoxedView.CellTitleFontSizeProperty.PropertyName)
else if(e.PropertyName == App.Controls.BoxedView.BoxedView.CellTitleFontSizeProperty.PropertyName)
{
UpdateWithForceLayout(UpdateTitleFontSize);
}
else if(e.PropertyName == BoxedView.CellDescriptionColorProperty.PropertyName)
else if(e.PropertyName == App.Controls.BoxedView.BoxedView.CellDescriptionColorProperty.PropertyName)
{
UpdateDescriptionColor();
}
else if(e.PropertyName == BoxedView.CellDescriptionFontSizeProperty.PropertyName)
else if(e.PropertyName == App.Controls.BoxedView.BoxedView.CellDescriptionFontSizeProperty.PropertyName)
{
UpdateWithForceLayout(UpdateDescriptionFontSize);
}
else if(e.PropertyName == BoxedView.CellBackgroundColorProperty.PropertyName)
else if(e.PropertyName == App.Controls.BoxedView.BoxedView.CellBackgroundColorProperty.PropertyName)
{
UpdateBackgroundColor();
}
else if(e.PropertyName == BoxedView.SelectedColorProperty.PropertyName)
else if(e.PropertyName == App.Controls.BoxedView.BoxedView.SelectedColorProperty.PropertyName)
{
UpdateWithForceLayout(UpdateSelectedColor);
}

View File

@@ -4,13 +4,13 @@ using Android.Text;
using Android.Views;
using Android.Widget;
using Bit.App.Controls.BoxedView;
using Bit.Droid.Renderers;
using Bit.Droid.Renderers.BoxedView;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(LabelCell), typeof(LabelCellRenderer))]
namespace Bit.Droid.Renderers
namespace Bit.Droid.Renderers.BoxedView
{
[Preserve(AllMembers = true)]
public class LabelCellRenderer : BaseCellRenderer<LabelCellView>
@@ -59,11 +59,11 @@ namespace Bit.Droid.Renderers
public override void ParentPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.ParentPropertyChanged(sender, e);
if(e.PropertyName == BoxedView.CellValueTextColorProperty.PropertyName)
if(e.PropertyName == App.Controls.BoxedView.BoxedView.CellValueTextColorProperty.PropertyName)
{
UpdateValueTextColor();
}
else if(e.PropertyName == BoxedView.CellValueTextFontSizeProperty.PropertyName)
else if(e.PropertyName == App.Controls.BoxedView.BoxedView.CellValueTextFontSizeProperty.PropertyName)
{
UpdateValueTextFontSize();
}