using ElmSharp; using System.Collections.Generic; using EColor = ElmSharp.Color; namespace Xamarin.Forms.Platform.Tizen { public abstract class CellRenderer : IRegisterable { const string _heightProperty = "Height"; readonly Dictionary> _realizedNativeViews = new Dictionary>(); Native.ListView.ItemContext _currentItem; protected CellRenderer(string style) { Class = new GenItemClass(style) { GetTextHandler = GetText, GetContentHandler = GetContent, DeleteHandler = ItemDeleted, ReusableContentHandler = ReusableContent, }; } 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 virtual EvasObject OnReusableContent(Cell cell, string part, EvasObject old) { return null; } 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.ToNative(); nativeSpan.FontAttributes = span.FontAttributes; nativeSpan.BackgroundColor = 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) { Dictionary realizedView = null; _realizedNativeViews.TryGetValue(cell, out realizedView); realizedView?.Clear(); OnUnrealizedCell(cell); } internal Native.ListView.ItemContext GetCurrentItem() { return _currentItem; } string GetText(object data, string part) { _currentItem = data as Native.ListView.ItemContext; var span = OnGetText(_currentItem.Cell, part); return span != null ? ToNative(span).GetMarkupText() : null; } EvasObject GetContent(object data, string part) { _currentItem = data as Native.ListView.ItemContext; var cell = _currentItem.Cell; EvasObject nativeView = OnGetContent(cell, part); UpdateRealizedView(cell, part, nativeView); return nativeView; } EvasObject ReusableContent(object data, string part, EvasObject old) { _currentItem = data as Native.ListView.ItemContext; var cell = _currentItem.Cell; EvasObject nativeView = OnReusableContent(cell, part, old); UpdateRealizedView(cell, part, nativeView); return nativeView; } void UpdateRealizedView(Cell cell, string part, EvasObject nativeView) { 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; } } void ItemDeleted(object data) { _currentItem = data as Native.ListView.ItemContext; var cell = _currentItem.Cell; _realizedNativeViews.Remove(cell); OnDeleted(cell); } } }