Skip to content

Commit

Permalink
Make an Element for each cell type
Browse files Browse the repository at this point in the history
  • Loading branch information
praeclarum committed Apr 26, 2018
1 parent 010982b commit 4f9f46c
Show file tree
Hide file tree
Showing 18 changed files with 443 additions and 459 deletions.
44 changes: 44 additions & 0 deletions Ooui.Forms/Cells/CellElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.ComponentModel;
using Xamarin.Forms;

namespace Ooui.Forms.Cells
{
public class CellElement : Div, INativeElementView
{
Cell cell;

public Cell Cell {
get => cell;
set {
if (cell == value)
return;

if (cell != null)
UnbindCell ();

cell = value;

if (cell != null)
BindCell ();
}
}

public virtual Xamarin.Forms.Element Element => Cell;

public CellElement ()
{
Style.Width = "100%";
}

protected virtual void UnbindCell ()
{
Device.BeginInvokeOnMainThread (Cell.SendDisappearing);
}

protected virtual void BindCell ()
{
Device.BeginInvokeOnMainThread (cell.SendAppearing);
}
}
}
46 changes: 22 additions & 24 deletions Ooui.Forms/Cells/CellRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,59 @@ namespace Ooui.Forms.Cells
{
public class CellRenderer : IRegisterable
{
private EventHandler _onForceUpdateSizeRequested;
EventHandler _onForceUpdateSizeRequested;

static readonly BindableProperty RealCellProperty =
BindableProperty.CreateAttached("RealCell", typeof(Div),
typeof(Cell), null);
BindableProperty.CreateAttached ("RealCell", typeof (CellElement), typeof (Cell), null);

public virtual CellView GetCell(Cell item, CellView reusableView, List listView)
public virtual CellElement GetCellElement (Cell cell, CellElement reusableElement, List listView)
{
var nativeCell = reusableView ?? GetCellInstance (item);
var cellElement = reusableElement ?? CreateCellElement (cell);

nativeCell.Cell = item;
cellElement.Cell = cell;

WireUpForceUpdateSizeRequested(item, nativeCell);
UpdateBackground(nativeCell, item);
WireUpForceUpdateSizeRequested (cell, cellElement);
UpdateBackground (cellElement, cell);

return nativeCell;
return cellElement;
}

internal static CellView GetRealCell(BindableObject cell)
protected static CellElement GetRealCell (BindableObject cell)
{
return (CellView)cell.GetValue(RealCellProperty);
return (CellElement)cell.GetValue (RealCellProperty);
}

internal static void SetRealCell(BindableObject cell, CellView renderer)
protected static void SetRealCell (BindableObject cell, CellElement renderer)
{
cell.SetValue(RealCellProperty, renderer);
cell.SetValue (RealCellProperty, renderer);
}

protected virtual CellView GetCellInstance(Cell item)
protected virtual CellElement CreateCellElement (Cell cell)
{
return new CellView();
return new CellElement ();
}

protected virtual void OnForceUpdateSizeRequest(Cell cell, CellView nativeCell)
protected virtual void OnForceUpdateSizeRequest (Cell cell, CellElement cellElement)
{
nativeCell.Style.Height = (int)cell.RenderHeight;
cellElement.Style.Height = (int)cell.RenderHeight;
}

protected void UpdateBackground(CellView tableViewCell, Cell cell)
protected void UpdateBackground (CellElement cellElement, Cell cell)
{
var backgroundColor = Xamarin.Forms.Color.Default;

if (backgroundColor == Xamarin.Forms.Color.Default && cell.RealParent is VisualElement element)
backgroundColor = element.BackgroundColor;
tableViewCell.Style.BackgroundColor = backgroundColor.ToOouiColor (Xamarin.Forms.Color.White);

cellElement.Style.BackgroundColor = backgroundColor.ToOouiColor (Xamarin.Forms.Color.White);
}

protected void WireUpForceUpdateSizeRequested(Cell cell, CellView nativeCell)
protected void WireUpForceUpdateSizeRequested (Cell cell, CellElement cellElement)
{
cell.ForceUpdateSizeRequested -= _onForceUpdateSizeRequested;

_onForceUpdateSizeRequested = (sender, e) =>
{
OnForceUpdateSizeRequest(cell, nativeCell);
_onForceUpdateSizeRequested = (sender, e) => {
OnForceUpdateSizeRequest (cell, cellElement);
};

cell.ForceUpdateSizeRequested += _onForceUpdateSizeRequested;
Expand Down
84 changes: 0 additions & 84 deletions Ooui.Forms/Cells/CellView.cs

This file was deleted.

81 changes: 81 additions & 0 deletions Ooui.Forms/Cells/EntryCellElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.ComponentModel;
using Ooui.Forms.Extensions;
using Xamarin.Forms;

namespace Ooui.Forms.Cells
{
public class EntryCellElement : CellElement
{
public Label TextLabel { get; } = new Label ();
public TextInput TextInput { get; } = new TextInput ();

public EntryCellElement ()
{
AppendChild (TextLabel);
AppendChild (TextInput);

TextInput.Change += TextInput_Change;
}

protected override void BindCell ()
{
Cell.PropertyChanged += Cell_PropertyChanged;

if (Cell is EntryCell cell) {
UpdateLabel (cell);
UpdateText (cell);
UpdatePlaceholder (cell);
UpdateLabelColor (cell);
}

base.BindCell ();
}

protected override void UnbindCell ()
{
Cell.PropertyChanged -= Cell_PropertyChanged;
base.UnbindCell ();
}

void Cell_PropertyChanged (object sender, PropertyChangedEventArgs e)
{
var entryCell = (EntryCell)sender;

if (e.PropertyName == EntryCell.LabelProperty.PropertyName)
UpdateLabel (entryCell);
else if (e.PropertyName == EntryCell.TextProperty.PropertyName)
UpdateText (entryCell);
else if (e.PropertyName == EntryCell.PlaceholderProperty.PropertyName)
UpdatePlaceholder (entryCell);
else if (e.PropertyName == EntryCell.LabelColorProperty.PropertyName)
UpdateLabelColor (entryCell);
}

void UpdateLabel (EntryCell entryCell)
{
TextLabel.Text = entryCell.Label ?? string.Empty;
}

void UpdateLabelColor (EntryCell entryCell)
{
TextLabel.Style.Color = entryCell.LabelColor.ToOouiColor (OouiTheme.TextColor);
}

void UpdatePlaceholder (EntryCell entryCell)
{
TextInput.Placeholder = entryCell.Placeholder ?? string.Empty;
}

void UpdateText (EntryCell entryCell)
{
TextInput.Value = entryCell.Text ?? string.Empty;
}

void TextInput_Change (object sender, EventArgs e)
{
if (Cell is EntryCell cell)
cell.Text = TextInput.Text;
}
}
}
Loading

0 comments on commit 4f9f46c

Please sign in to comment.