mirror of
https://github.com/bitwarden/android.git
synced 2024-12-25 18:38:27 +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;
|
||||||
|
using System.Collections.Specialized;
|
||||||
using Xamarin.Forms;
|
using Xamarin.Forms;
|
||||||
|
|
||||||
namespace Bit.App.Controls
|
namespace Bit.App.Controls
|
||||||
|
@ -10,7 +11,7 @@ namespace Bit.App.Controls
|
||||||
|
|
||||||
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
|
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
|
||||||
nameof(ItemsSource), typeof(ICollection), typeof(RepeaterView), null, BindingMode.OneWay,
|
nameof(ItemsSource), typeof(ICollection), typeof(RepeaterView), null, BindingMode.OneWay,
|
||||||
propertyChanged: ItemsChanged);
|
propertyChanged: ItemsSourceChanging);
|
||||||
|
|
||||||
public RepeaterView()
|
public RepeaterView()
|
||||||
{
|
{
|
||||||
|
@ -29,6 +30,27 @@ namespace Bit.App.Controls
|
||||||
set => SetValue(ItemTemplateProperty, value);
|
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)
|
protected virtual View ViewFor(object item)
|
||||||
{
|
{
|
||||||
View view = null;
|
View view = null;
|
||||||
|
@ -46,18 +68,27 @@ namespace Bit.App.Controls
|
||||||
return view;
|
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();
|
Children.Clear();
|
||||||
if(newValue is ICollection items)
|
foreach(var item in ItemsSource)
|
||||||
{
|
{
|
||||||
foreach(var item in items)
|
Children.Add(ViewFor(item));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ItemsSourceChanging(BindableObject bindable, object oldValue, object newValue)
|
||||||
{
|
{
|
||||||
control.Children.Add(control.ViewFor(item));
|
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