2018-01-05 12:10:57 +00:00
|
|
|
|
using Ooui.Forms.Extensions;
|
|
|
|
|
using System.Collections.Generic;
|
2018-03-22 19:16:11 +00:00
|
|
|
|
using System.Collections.Specialized;
|
2018-01-05 12:10:57 +00:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
2018-11-12 19:09:51 +00:00
|
|
|
|
using System.Timers;
|
2018-01-05 12:10:57 +00:00
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using Xamarin.Forms.Internals;
|
2018-04-26 02:55:26 +00:00
|
|
|
|
using Ooui.Forms.Cells;
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
|
|
|
|
namespace Ooui.Forms.Renderers
|
|
|
|
|
{
|
|
|
|
|
public class ListViewRenderer : ViewRenderer<ListView, List>
|
|
|
|
|
{
|
2018-11-09 16:52:22 +00:00
|
|
|
|
const int DefaultRowHeight = 44;
|
2018-01-05 12:10:57 +00:00
|
|
|
|
private bool _disposed;
|
2018-11-09 16:52:22 +00:00
|
|
|
|
IVisualElementRenderer _prototype;
|
2018-11-12 19:09:51 +00:00
|
|
|
|
Timer _timer;
|
2018-11-09 16:52:22 +00:00
|
|
|
|
|
|
|
|
|
int _rowHeight;
|
|
|
|
|
|
|
|
|
|
public int RowHeight
|
|
|
|
|
{
|
|
|
|
|
get => _rowHeight;
|
|
|
|
|
set => _rowHeight = value;
|
|
|
|
|
}
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
public ListViewRenderer ()
|
2018-01-05 12:10:57 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ITemplatedItemsView<Cell> TemplatedItemsView => Element;
|
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
protected override void OnElementChanged (ElementChangedEventArgs<ListView> e)
|
2018-01-05 12:10:57 +00:00
|
|
|
|
{
|
2018-04-26 02:55:26 +00:00
|
|
|
|
if (e.OldElement != null) {
|
2018-03-22 19:16:11 +00:00
|
|
|
|
var templatedItems = TemplatedItemsView.TemplatedItems;
|
|
|
|
|
templatedItems.CollectionChanged -= OnCollectionChanged;
|
2018-04-16 05:55:23 +00:00
|
|
|
|
e.OldElement.ScrollToRequested -= ListView_ScrollToRequested;
|
2018-03-22 19:16:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
if (e.NewElement != null) {
|
|
|
|
|
if (Control == null) {
|
|
|
|
|
var list = new List ();
|
|
|
|
|
list.Style.Overflow = "scroll";
|
|
|
|
|
list.Style.Padding = "0";
|
2018-11-09 16:52:22 +00:00
|
|
|
|
// Make the list element positioned so child elements will
|
|
|
|
|
// be positioned relative to it. This will allow the list
|
|
|
|
|
// to scroll properly.
|
|
|
|
|
list.Style.Position = "relative";
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
SetNativeControl (list);
|
2018-01-05 12:10:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 19:16:11 +00:00
|
|
|
|
var templatedItems = TemplatedItemsView.TemplatedItems;
|
|
|
|
|
templatedItems.CollectionChanged += OnCollectionChanged;
|
2018-04-16 05:55:23 +00:00
|
|
|
|
e.NewElement.ScrollToRequested += ListView_ScrollToRequested;
|
2018-03-22 19:16:11 +00:00
|
|
|
|
|
2018-11-09 16:52:22 +00:00
|
|
|
|
UpdateRowHeight();
|
|
|
|
|
|
2018-03-22 19:16:11 +00:00
|
|
|
|
UpdateItems ();
|
2018-04-26 02:55:26 +00:00
|
|
|
|
UpdateBackgroundColor ();
|
2018-01-05 12:10:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
base.OnElementChanged (e);
|
2018-01-05 12:10:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e)
|
2018-01-05 12:10:57 +00:00
|
|
|
|
{
|
2018-04-26 02:55:26 +00:00
|
|
|
|
base.OnElementPropertyChanged (sender, e);
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
if (e.PropertyName == ItemsView<Cell>.ItemsSourceProperty.PropertyName)
|
2018-11-12 19:09:51 +00:00
|
|
|
|
UpdateItems();
|
|
|
|
|
else if (e.PropertyName == Xamarin.Forms.ListView.RowHeightProperty.PropertyName)
|
|
|
|
|
{
|
|
|
|
|
UpdateRowHeight();
|
|
|
|
|
UpdateItems();
|
|
|
|
|
}
|
|
|
|
|
else if (e.PropertyName == VisualElement.WidthProperty.PropertyName)
|
|
|
|
|
{
|
|
|
|
|
if (_timer != null)
|
|
|
|
|
{
|
|
|
|
|
_timer.Stop();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_timer = new Timer();
|
|
|
|
|
_timer.Interval = 250;
|
|
|
|
|
_timer.Elapsed += delegate {
|
|
|
|
|
UpdateItems();
|
|
|
|
|
};
|
|
|
|
|
_timer.Enabled = true;
|
|
|
|
|
_timer.AutoReset = false;
|
|
|
|
|
}
|
|
|
|
|
_timer.Start();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-05 12:10:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
protected override void Dispose (bool disposing)
|
2018-01-05 12:10:57 +00:00
|
|
|
|
{
|
2018-04-26 02:55:26 +00:00
|
|
|
|
UnsubscribeCellClicks ();
|
2018-02-15 03:10:58 +00:00
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
base.Dispose (disposing);
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
if (disposing && !_disposed) {
|
2018-03-22 19:16:11 +00:00
|
|
|
|
|
2018-11-09 17:51:55 +00:00
|
|
|
|
ClearPrototype();
|
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
if (Element != null) {
|
2018-03-22 19:16:11 +00:00
|
|
|
|
var templatedItems = TemplatedItemsView.TemplatedItems;
|
|
|
|
|
templatedItems.CollectionChanged -= OnCollectionChanged;
|
2018-04-16 05:55:23 +00:00
|
|
|
|
Element.ScrollToRequested -= ListView_ScrollToRequested;
|
2018-03-22 19:16:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-05 12:10:57 +00:00
|
|
|
|
_disposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 17:51:55 +00:00
|
|
|
|
void ClearPrototype()
|
|
|
|
|
{
|
|
|
|
|
if (_prototype != null)
|
|
|
|
|
{
|
|
|
|
|
var element = _prototype.Element;
|
|
|
|
|
element?.ClearValue(Platform.RendererProperty);
|
|
|
|
|
_prototype?.Dispose();
|
|
|
|
|
_prototype = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 19:16:11 +00:00
|
|
|
|
private void OnCollectionChanged (object sender, NotifyCollectionChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
UpdateItems ();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
private void UnsubscribeCellClicks ()
|
|
|
|
|
{
|
|
|
|
|
if (Control == null)
|
|
|
|
|
return;
|
|
|
|
|
foreach (var c in Control.Children) {
|
|
|
|
|
if (c is Element e)
|
|
|
|
|
e.Click -= ListItem_Click;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-15 03:10:58 +00:00
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
private void UpdateItems ()
|
2018-01-05 12:10:57 +00:00
|
|
|
|
{
|
2018-04-26 02:55:26 +00:00
|
|
|
|
if (Control == null)
|
|
|
|
|
return;
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
var listItems = Control.Children.OfType<ListItem> ().ToList ();
|
2018-03-22 19:16:11 +00:00
|
|
|
|
|
2018-01-05 12:10:57 +00:00
|
|
|
|
var items = TemplatedItemsView.TemplatedItems;
|
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
if (listItems.Count > items.Count) {
|
|
|
|
|
for (var i = items.Count; i < listItems.Count; i++) {
|
|
|
|
|
listItems[i].Click -= ListItem_Click;
|
|
|
|
|
Control.RemoveChild (listItems[i]);
|
|
|
|
|
}
|
|
|
|
|
listItems.RemoveRange (items.Count, listItems.Count - items.Count);
|
2018-01-05 12:10:57 +00:00
|
|
|
|
}
|
2018-04-26 02:55:26 +00:00
|
|
|
|
if (listItems.Count < items.Count) {
|
|
|
|
|
for (var i = listItems.Count; i < items.Count; i++) {
|
|
|
|
|
var li = new ListItem ();
|
|
|
|
|
li.Style["list-style-type"] = "none";
|
|
|
|
|
li.Click += ListItem_Click;
|
|
|
|
|
Control.AppendChild (li);
|
|
|
|
|
listItems.Add (li);
|
|
|
|
|
}
|
2018-01-05 12:10:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
bool grouping = Element.IsGroupingEnabled;
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
if (grouping) {
|
|
|
|
|
// Not Implemented
|
2018-01-05 12:10:57 +00:00
|
|
|
|
}
|
2018-04-26 02:55:26 +00:00
|
|
|
|
else {
|
|
|
|
|
var i = 0;
|
2018-11-09 16:52:22 +00:00
|
|
|
|
double offset = 0;
|
2018-04-26 02:55:26 +00:00
|
|
|
|
foreach (var item in items) {
|
|
|
|
|
var li = listItems[i];
|
2018-11-09 16:52:22 +00:00
|
|
|
|
var nativeCell = items[i];
|
2018-04-26 02:55:26 +00:00
|
|
|
|
var children = li.Children;
|
2018-04-26 23:55:23 +00:00
|
|
|
|
var rv = children.Count > 0 ? children[0] as CellElement : null;
|
2018-04-26 02:55:26 +00:00
|
|
|
|
var cell = GetCell (item, rv);
|
2018-11-09 16:52:22 +00:00
|
|
|
|
var height = CalculateHeightForCell(nativeCell);
|
|
|
|
|
li.Style.Height = height;
|
|
|
|
|
var viewCell = (ViewCell)cell.Cell;
|
|
|
|
|
if (viewCell != null && viewCell.View != null)
|
|
|
|
|
{
|
|
|
|
|
var rect = new Rectangle(0, offset, Element.Width, height);
|
|
|
|
|
Layout.LayoutChildIntoBoundingRegion(viewCell.View, rect);
|
|
|
|
|
}
|
|
|
|
|
offset += height;
|
2018-04-26 02:55:26 +00:00
|
|
|
|
if (rv == null) {
|
|
|
|
|
li.AppendChild (cell);
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
2018-01-05 12:10:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
void ListItem_Click (object sender, TargetEventArgs e)
|
2018-02-11 02:12:33 +00:00
|
|
|
|
{
|
2018-04-26 02:55:26 +00:00
|
|
|
|
if (Control == null)
|
|
|
|
|
return;
|
2018-02-11 02:12:33 +00:00
|
|
|
|
var it = (ListItem)sender;
|
2018-04-26 02:55:26 +00:00
|
|
|
|
var ndx = Control.Children.IndexOf (it);
|
|
|
|
|
Element.NotifyRowTapped (ndx, null);
|
2018-02-11 02:12:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-16 05:55:23 +00:00
|
|
|
|
void ListView_ScrollToRequested (object sender, ScrollToRequestedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (Control == null)
|
|
|
|
|
return;
|
2018-04-26 02:55:26 +00:00
|
|
|
|
|
2018-04-16 05:55:23 +00:00
|
|
|
|
var oe = (ITemplatedItemsListScrollToRequestedEventArgs)e;
|
|
|
|
|
var item = oe.Item;
|
|
|
|
|
var group = oe.Group;
|
|
|
|
|
switch (e.Position) {
|
|
|
|
|
case ScrollToPosition.Start:
|
|
|
|
|
Control.Send (Ooui.Message.Set (Control.Id, "scrollTop", 0));
|
|
|
|
|
break;
|
|
|
|
|
case ScrollToPosition.End:
|
|
|
|
|
Control.Send (Ooui.Message.Set (Control.Id, "scrollTop", new Ooui.Message.PropertyReference { TargetId = Control.Id, Key = "scrollHeight" }));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 16:52:22 +00:00
|
|
|
|
void UpdateRowHeight()
|
|
|
|
|
{
|
|
|
|
|
var rowHeight = Element.RowHeight;
|
|
|
|
|
if (Element.HasUnevenRows && rowHeight == -1)
|
|
|
|
|
RowHeight = -1;
|
|
|
|
|
else
|
|
|
|
|
RowHeight = rowHeight <= 0 ? DefaultRowHeight : rowHeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal double CalculateHeightForCell(Cell cell)
|
|
|
|
|
{
|
|
|
|
|
if (!Element.HasUnevenRows)
|
|
|
|
|
{
|
|
|
|
|
return RowHeight;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var viewCell = cell as ViewCell;
|
|
|
|
|
if (viewCell != null && viewCell.View != null)
|
|
|
|
|
{
|
|
|
|
|
var target = viewCell.View;
|
|
|
|
|
if (_prototype == null)
|
|
|
|
|
_prototype = Platform.CreateRenderer(target);
|
|
|
|
|
else
|
|
|
|
|
_prototype.SetElement(target);
|
|
|
|
|
|
|
|
|
|
Platform.SetRenderer(target, _prototype);
|
|
|
|
|
|
|
|
|
|
var req = target.Measure(Element.Width, double.PositiveInfinity, MeasureFlags.IncludeMargins);
|
|
|
|
|
|
|
|
|
|
target.ClearValue(Platform.RendererProperty);
|
|
|
|
|
foreach (Xamarin.Forms.Element descendant in target.Descendants())
|
|
|
|
|
{
|
|
|
|
|
IVisualElementRenderer renderer = Platform.GetRenderer(descendant as VisualElement);
|
|
|
|
|
|
|
|
|
|
// Clear renderer from descendent; this will not happen in Dispose as normal because we need to
|
|
|
|
|
// unhook the Element from the renderer before disposing it.
|
|
|
|
|
descendant.ClearValue(Platform.RendererProperty);
|
|
|
|
|
renderer?.Dispose();
|
|
|
|
|
renderer = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var height = req.Request.Height;
|
|
|
|
|
return height > 1 ? height : DefaultRowHeight;
|
|
|
|
|
}
|
|
|
|
|
var renderHeight = cell.RenderHeight;
|
|
|
|
|
return renderHeight > 0 ? renderHeight : DefaultRowHeight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
void UpdateBackgroundColor ()
|
2018-01-05 12:10:57 +00:00
|
|
|
|
{
|
2018-04-26 02:55:26 +00:00
|
|
|
|
if (Control == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var backgroundColor = Element.BackgroundColor.ToOouiColor (Xamarin.Forms.Color.White);
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
2018-04-26 02:55:26 +00:00
|
|
|
|
Control.Style.BackgroundColor = backgroundColor;
|
2018-01-05 12:10:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 23:55:23 +00:00
|
|
|
|
CellElement GetCell (Cell cell, CellElement reusableView)
|
2018-01-05 12:10:57 +00:00
|
|
|
|
{
|
2018-04-26 02:55:26 +00:00
|
|
|
|
var renderer = (Cells.CellRenderer)Registrar.Registered.GetHandlerForObject<IRegisterable> (cell);
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
2018-04-26 23:55:23 +00:00
|
|
|
|
var realCell = renderer.GetCellElement (cell, reusableView, Control);
|
2018-01-05 12:10:57 +00:00
|
|
|
|
|
|
|
|
|
return realCell;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|