using ElmSharp; using System.Collections.Generic; using EColor = ElmSharp.Color; namespace Xamarin.Forms.Platform.Tizen { public abstract class CellRenderer : IRegisterable { const string _heightProperty = "Height"; protected static readonly EColor s_defaultTextColor = EColor.Black; protected static readonly EColor s_defaultBackgroundColor = EColor.White; readonly Dictionary> _realizedNativeViews = new Dictionary>(); protected CellRenderer(string style) { Class = new GenItemClass(style) { GetTextHandler = GetText, GetContentHandler = GetContent, DeleteHandler = ItemDeleted, }; } public GenItemClass Class { get; private set; } protected virtual bool OnCellPropertyChanged(Cell cell, string property, Dictionary realizedView) { if (property == _heightProperty) { return true; } return false; } protected virtual Span OnGetText(Cell cell, string part) { return null; } protected virtual EvasObject OnGetContent(Cell cell, string part) { return null; } protected virtual void OnDeleted(Cell cell) { } protected virtual void OnUnrealizedCell(Cell cell) { } protected int FindCellContentHeight(Cell cell) { ViewCell viewCell = cell as ViewCell; if (viewCell != null) { var parentWidth = (cell.Parent as VisualElement).Width; var view = viewCell.View; return (int)view.Measure(parentWidth, double.PositiveInfinity).Request.Height; } else return -1; } static Native.Span ToNative(Span span) { var nativeSpan = new Native.Span(); nativeSpan.Text = span.Text; nativeSpan.ForegroundColor = span.ForegroundColor.IsDefault ? s_defaultTextColor : span.ForegroundColor.ToNative(); nativeSpan.FontAttributes = span.FontAttributes; nativeSpan.BackgroundColor = span.BackgroundColor.IsDefault ? s_defaultBackgroundColor : span.BackgroundColor.ToNative(); nativeSpan.FontSize = span.FontSize; nativeSpan.FontFamily = span.FontFamily; return nativeSpan; } internal void SendCellPropertyChanged(Cell cell, GenListItem item, string property) { Dictionary realizedView = null; _realizedNativeViews.TryGetValue(cell, out realizedView); // just to prevent null reference exception in OnCellPropertyChanged realizedView = realizedView ?? new Dictionary(); if (property == Cell.IsEnabledProperty.PropertyName) { item.IsEnabled = cell.IsEnabled; } // if true was returned, item was updated // if it's possible to update the cell property without Update(), return false else if (OnCellPropertyChanged(cell, property, realizedView)) { item.Update(); } } internal void SendUnrealizedCell(Cell cell) { _realizedNativeViews.Remove(cell); OnUnrealizedCell(cell); } string GetText(object data, string part) { var span = OnGetText((data as Native.ListView.ItemContext).Cell, part); return span != null ? ToNative(span).GetDecoratedText() : null; } EvasObject GetContent(object data, string part) { var cell = (data as Native.ListView.ItemContext).Cell; EvasObject nativeView = OnGetContent(cell, part); if (part != null && nativeView != null) { Dictionary realizedView = null; _realizedNativeViews.TryGetValue(cell, out realizedView); if (realizedView == null) { realizedView = new Dictionary(); _realizedNativeViews[cell] = realizedView; } realizedView[part] = nativeView; nativeView.Deleted += (sender, e) => { realizedView.Remove(part); }; } return nativeView; } void ItemDeleted(object data) { var cell = (data as Native.ListView.ItemContext).Cell; _realizedNativeViews.Remove(cell); OnDeleted(cell); } } }