mirror of
https://github.com/bitwarden/android.git
synced 2024-12-24 18:08:26 +03:00
update repeaterview to handle observables
This commit is contained in:
parent
cbda58f770
commit
8d0dc2d230
1 changed files with 40 additions and 9 deletions
|
@ -1,4 +1,5 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Specialized;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace Bit.App.Controls
|
||||
|
@ -10,7 +11,7 @@ namespace Bit.App.Controls
|
|||
|
||||
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
|
||||
nameof(ItemsSource), typeof(ICollection), typeof(RepeaterView), null, BindingMode.OneWay,
|
||||
propertyChanged: ItemsChanged);
|
||||
propertyChanged: ItemsSourceChanging);
|
||||
|
||||
public RepeaterView()
|
||||
{
|
||||
|
@ -29,6 +30,27 @@ namespace Bit.App.Controls
|
|||
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;
|
||||
|
@ -46,19 +68,28 @@ namespace Bit.App.Controls
|
|||
return view;
|
||||
}
|
||||
|
||||
private static void ItemsChanged(BindableObject bindable, object oldValue, object newValue)
|
||||
private void Populate()
|
||||
{
|
||||
if(bindable is RepeaterView control)
|
||||
if(ItemsSource != null)
|
||||
{
|
||||
control.Children.Clear();
|
||||
if(newValue is ICollection items)
|
||||
Children.Clear();
|
||||
foreach(var item in ItemsSource)
|
||||
{
|
||||
foreach(var item in items)
|
||||
{
|
||||
control.Children.Add(control.ViewFor(item));
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue