2018-01-05 12:10:57 +00:00
|
|
|
|
using Ooui.Forms.Extensions;
|
|
|
|
|
using System;
|
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Ooui.Forms.Cells
|
|
|
|
|
{
|
|
|
|
|
public class CellRenderer : IRegisterable
|
|
|
|
|
{
|
2018-04-26 23:55:23 +00:00
|
|
|
|
EventHandler _onForceUpdateSizeRequested;
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
|
|
|
|
static readonly BindableProperty RealCellProperty =
|
2018-04-26 23:55:23 +00:00
|
|
|
|
BindableProperty.CreateAttached ("RealCell", typeof (CellElement), typeof (Cell), null);
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
2018-04-26 23:55:23 +00:00
|
|
|
|
public virtual CellElement GetCellElement (Cell cell, CellElement reusableElement, List listView)
|
2018-01-05 12:10:57 +00:00
|
|
|
|
{
|
2018-04-26 23:55:23 +00:00
|
|
|
|
var cellElement = reusableElement ?? CreateCellElement (cell);
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
2018-04-26 23:55:23 +00:00
|
|
|
|
cellElement.Cell = cell;
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
2018-04-26 23:55:23 +00:00
|
|
|
|
WireUpForceUpdateSizeRequested (cell, cellElement);
|
|
|
|
|
UpdateBackground (cellElement, cell);
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
2018-04-26 23:55:23 +00:00
|
|
|
|
return cellElement;
|
2018-01-05 12:10:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 23:55:23 +00:00
|
|
|
|
protected static CellElement GetRealCell (BindableObject cell)
|
2018-01-05 12:10:57 +00:00
|
|
|
|
{
|
2018-04-26 23:55:23 +00:00
|
|
|
|
return (CellElement)cell.GetValue (RealCellProperty);
|
2018-01-05 12:10:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 23:55:23 +00:00
|
|
|
|
protected static void SetRealCell (BindableObject cell, CellElement renderer)
|
2018-01-05 12:10:57 +00:00
|
|
|
|
{
|
2018-04-26 23:55:23 +00:00
|
|
|
|
cell.SetValue (RealCellProperty, renderer);
|
2018-01-05 12:10:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 23:55:23 +00:00
|
|
|
|
protected virtual CellElement CreateCellElement (Cell cell)
|
2018-01-05 12:10:57 +00:00
|
|
|
|
{
|
2018-04-26 23:55:23 +00:00
|
|
|
|
return new CellElement ();
|
2018-01-05 12:10:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 23:55:23 +00:00
|
|
|
|
protected virtual void OnForceUpdateSizeRequest (Cell cell, CellElement cellElement)
|
2018-01-05 12:10:57 +00:00
|
|
|
|
{
|
2018-04-26 23:55:23 +00:00
|
|
|
|
cellElement.Style.Height = (int)cell.RenderHeight;
|
2018-01-05 12:10:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 23:55:23 +00:00
|
|
|
|
protected void UpdateBackground (CellElement cellElement, Cell cell)
|
2018-01-05 12:10:57 +00:00
|
|
|
|
{
|
2018-04-26 02:55:26 +00:00
|
|
|
|
var backgroundColor = Xamarin.Forms.Color.Default;
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
if (backgroundColor == Xamarin.Forms.Color.Default && cell.RealParent is VisualElement element)
|
|
|
|
|
backgroundColor = element.BackgroundColor;
|
2018-04-26 23:55:23 +00:00
|
|
|
|
|
|
|
|
|
cellElement.Style.BackgroundColor = backgroundColor.ToOouiColor (Xamarin.Forms.Color.White);
|
2018-01-05 12:10:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 23:55:23 +00:00
|
|
|
|
protected void WireUpForceUpdateSizeRequested (Cell cell, CellElement cellElement)
|
2018-01-05 12:10:57 +00:00
|
|
|
|
{
|
|
|
|
|
cell.ForceUpdateSizeRequested -= _onForceUpdateSizeRequested;
|
|
|
|
|
|
2018-04-26 23:55:23 +00:00
|
|
|
|
_onForceUpdateSizeRequested = (sender, e) => {
|
|
|
|
|
OnForceUpdateSizeRequest (cell, cellElement);
|
2018-01-05 12:10:57 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
cell.ForceUpdateSizeRequested += _onForceUpdateSizeRequested;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|