2016-08-13 07:30:41 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using Android.Graphics;
|
|
|
|
|
using Android.Text;
|
|
|
|
|
using Android.Text.Method;
|
|
|
|
|
using Android.Views.InputMethods;
|
|
|
|
|
using Android.Widget;
|
|
|
|
|
using Bit.Android.Controls;
|
|
|
|
|
using Bit.App.Controls;
|
|
|
|
|
using Bit.App.Enums;
|
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using Xamarin.Forms.Platform.Android;
|
2016-08-18 06:57:14 +03:00
|
|
|
|
using Android.Content;
|
|
|
|
|
using AView = Android.Views.View;
|
|
|
|
|
using AListView = Android.Widget.ListView;
|
|
|
|
|
using Android.Views;
|
|
|
|
|
using Android.Util;
|
2016-08-13 07:30:41 +03:00
|
|
|
|
|
|
|
|
|
[assembly: ExportRenderer(typeof(ExtendedTableView), typeof(ExtendedTableViewRenderer))]
|
|
|
|
|
namespace Bit.Android.Controls
|
|
|
|
|
{
|
|
|
|
|
public class ExtendedTableViewRenderer : TableViewRenderer
|
|
|
|
|
{
|
2016-08-19 02:09:01 +03:00
|
|
|
|
protected override void OnElementChanged(ElementChangedEventArgs<TableView> e)
|
|
|
|
|
{
|
|
|
|
|
base.OnElementChanged(e);
|
|
|
|
|
Control.Divider = null;
|
|
|
|
|
Control.DividerHeight = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-18 06:57:14 +03:00
|
|
|
|
protected override TableViewModelRenderer GetModelRenderer(AListView listView, TableView view)
|
|
|
|
|
{
|
|
|
|
|
return new CustomTableViewModelRenderer(Context, listView, view);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-13 07:30:41 +03:00
|
|
|
|
public override SizeRequest GetDesiredSize(int widthConstraint, int heightConstraint)
|
|
|
|
|
{
|
|
|
|
|
var baseSize = base.GetDesiredSize(widthConstraint, heightConstraint);
|
|
|
|
|
var height = ComputeHeight(Control, Convert.ToInt32(baseSize.Request.Width));
|
2016-08-18 06:57:14 +03:00
|
|
|
|
return new SizeRequest(new Xamarin.Forms.Size(baseSize.Request.Width, height));
|
2016-08-13 07:30:41 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-18 06:57:14 +03:00
|
|
|
|
private int ComputeHeight(AListView listView, int width)
|
2016-08-13 07:30:41 +03:00
|
|
|
|
{
|
|
|
|
|
var adapter = listView.Adapter;
|
|
|
|
|
var totalHeight = listView.PaddingTop + listView.PaddingBottom;
|
|
|
|
|
var desiredWidth = MeasureSpec.MakeMeasureSpec(width, global::Android.Views.MeasureSpecMode.AtMost);
|
|
|
|
|
for(var i = 0; i < adapter.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var view = adapter.GetView(i, null, listView);
|
|
|
|
|
view.LayoutParameters = new LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent);
|
|
|
|
|
view.Measure(desiredWidth, MeasureSpec.MakeMeasureSpec(0, global::Android.Views.MeasureSpecMode.Unspecified));
|
|
|
|
|
totalHeight += view.MeasuredHeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return totalHeight + (listView.DividerHeight * (adapter.Count - 1));
|
|
|
|
|
}
|
2016-08-18 06:57:14 +03:00
|
|
|
|
|
|
|
|
|
private class CustomTableViewModelRenderer : TableViewModelRenderer
|
|
|
|
|
{
|
2016-08-19 02:09:01 +03:00
|
|
|
|
private readonly ExtendedTableView _view;
|
2016-08-18 06:57:14 +03:00
|
|
|
|
private readonly AListView _listView;
|
2016-08-19 02:58:25 +03:00
|
|
|
|
private bool _removedHeader;
|
2016-08-18 06:57:14 +03:00
|
|
|
|
|
|
|
|
|
public CustomTableViewModelRenderer(Context context, AListView listView, TableView view)
|
|
|
|
|
: base(context, listView, view)
|
|
|
|
|
{
|
2016-08-19 02:09:01 +03:00
|
|
|
|
_view = view as ExtendedTableView;
|
2016-08-18 06:57:14 +03:00
|
|
|
|
_listView = listView;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ITableViewController Controller => _view;
|
|
|
|
|
|
|
|
|
|
public override AView GetView(int position, AView convertView, ViewGroup parent)
|
|
|
|
|
{
|
|
|
|
|
var baseView = base.GetView(position, convertView, parent);
|
2016-08-19 02:09:01 +03:00
|
|
|
|
var layout = baseView as LinearLayout;
|
|
|
|
|
|
2016-08-18 06:57:14 +03:00
|
|
|
|
bool isHeader, nextIsHeader;
|
|
|
|
|
GetCellPosition(position, out isHeader, out nextIsHeader);
|
2016-08-19 02:09:01 +03:00
|
|
|
|
|
2016-08-18 06:57:14 +03:00
|
|
|
|
if(isHeader)
|
|
|
|
|
{
|
|
|
|
|
baseView.SetBackgroundColor(Xamarin.Forms.Color.Transparent.ToAndroid());
|
2016-08-19 02:09:01 +03:00
|
|
|
|
|
|
|
|
|
var textCell = layout?.GetChildAt(0) as BaseCellView;
|
|
|
|
|
if(textCell != null)
|
|
|
|
|
{
|
2016-08-19 02:58:25 +03:00
|
|
|
|
if(!_removedHeader && position == 0 && _view.NoHeader)
|
|
|
|
|
{
|
|
|
|
|
textCell.Visibility = ViewStates.Gone;
|
|
|
|
|
_removedHeader = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
textCell.MainText = textCell.MainText?.ToUpperInvariant();
|
|
|
|
|
textCell.SetMainTextColor(Xamarin.Forms.Color.FromHex("777777"));
|
|
|
|
|
}
|
2016-08-19 02:09:01 +03:00
|
|
|
|
}
|
2016-08-18 06:57:14 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-08-19 02:09:01 +03:00
|
|
|
|
baseView.SetBackgroundColor(_view.SeparatorColor.ToAndroid());
|
2016-08-18 06:57:14 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return baseView;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GetCellPosition(int position, out bool isHeader, out bool nextIsHeader)
|
|
|
|
|
{
|
|
|
|
|
isHeader = false;
|
|
|
|
|
nextIsHeader = false;
|
|
|
|
|
|
|
|
|
|
var model = Controller.Model;
|
|
|
|
|
var sectionCount = model.GetSectionCount();
|
|
|
|
|
|
|
|
|
|
for(var sectionIndex = 0; sectionIndex < sectionCount; sectionIndex++)
|
|
|
|
|
{
|
|
|
|
|
var size = model.GetRowCount(sectionIndex) + 1;
|
|
|
|
|
if(position == 0)
|
|
|
|
|
{
|
|
|
|
|
isHeader = true;
|
|
|
|
|
nextIsHeader = size == 0 && sectionIndex < sectionCount - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(position < size)
|
|
|
|
|
{
|
|
|
|
|
nextIsHeader = position == size - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
position -= size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-13 07:30:41 +03:00
|
|
|
|
}
|
|
|
|
|
}
|