1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-06-26 10:36:21 +02:00
bitwarden-mobile/src/App/Controls/RepeaterView.cs
Federico Maccaroni b7048de2a1
[EC-528] Refactor Custom Fields into separate components (#1662)
* Refactored CustomFields to stop using RepeaterView and use BindableLayout and divided the different types on different files and added a factory to create them

* Fix formatting
2022-09-09 15:58:11 -03:00

98 lines
3.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Specialized;
using Xamarin.Forms;
namespace Bit.App.Controls
{
[Obsolete]
public class RepeaterView : StackLayout
{
public static readonly BindableProperty ItemTemplateProperty = BindableProperty.Create(
nameof(ItemTemplate), typeof(DataTemplate), typeof(RepeaterView), default(DataTemplate));
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
nameof(ItemsSource), typeof(ICollection), typeof(RepeaterView), null, BindingMode.OneWay,
propertyChanged: ItemsSourceChanging);
public RepeaterView()
{
Spacing = 0;
}
public ICollection ItemsSource
{
get => GetValue(ItemsSourceProperty) as ICollection;
set => SetValue(ItemsSourceProperty, value);
}
public DataTemplate ItemTemplate
{
get => GetValue(ItemTemplateProperty) as DataTemplate;
set => SetValue(ItemTemplateProperty, value);
}
private void OnCollectionChanged(object sender,
NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
{
Populate();
}
protected override void OnPropertyChanged(string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if (propertyName == ItemTemplateProperty.PropertyName || propertyName == ItemsSourceProperty.PropertyName)
{
Populate();
}
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
Populate();
}
protected virtual View ViewFor(object item)
{
View view = null;
var template = ItemTemplate;
if (template != null)
{
if (template is DataTemplateSelector selector)
{
template = selector.SelectTemplate(item, this);
}
var content = template.CreateContent();
view = content is View ? content as View : ((ViewCell)content).View;
view.BindingContext = item;
}
return view;
}
private void Populate()
{
if (ItemsSource != null)
{
Children.Clear();
foreach (var item in ItemsSource)
{
Children.Add(ViewFor(item));
}
}
}
private static void ItemsSourceChanging(BindableObject bindable, object oldValue, object newValue)
{
if (oldValue != null && oldValue is INotifyCollectionChanged ov)
{
ov.CollectionChanged -= (bindable as RepeaterView).OnCollectionChanged;
}
if (newValue != null && newValue is INotifyCollectionChanged nv)
{
nv.CollectionChanged += (bindable as RepeaterView).OnCollectionChanged;
}
}
}
}