Remove unnecessaray parameter of TizenDeviceInfo
[platform/upstream/xamarin-forms.git] / Xamarin.Forms.Pages / DataView.cs
1 using System.Collections.Generic;
2 using System.Linq;
3
4 namespace Xamarin.Forms.Pages
5 {
6         public class DataView : ContentView, IDataSourceProvider
7         {
8                 public static readonly BindableProperty DataProperty = BindableProperty.Create(nameof(Data), typeof(IEnumerable<IDataItem>), typeof(DataView), default(IEnumerable<IDataItem>));
9
10                 public static readonly BindableProperty DataSourceProperty = BindableProperty.Create(nameof(DataSource), typeof(IDataSource), typeof(DataView), null, propertyChanged: OnDataSourceChanged);
11
12                 public static readonly BindableProperty DefaultItemTemplateProperty = BindableProperty.Create(nameof(DefaultItemTemplate), typeof(DataTemplate), typeof(DataView), default(DataTemplate));
13
14                 readonly HashSet<string> _maskedKeys = new HashSet<string>();
15
16                 public DataView()
17                 {
18                         SetBinding(DataProperty, new Binding("DataSource.Data", source: this));
19                 }
20
21                 public IEnumerable<IDataItem> Data
22                 {
23                         get { return (IEnumerable<IDataItem>)GetValue(DataProperty); }
24                         set { SetValue(DataProperty, value); }
25                 }
26
27                 public DataTemplate DefaultItemTemplate
28                 {
29                         get { return (DataTemplate)GetValue(DefaultItemTemplateProperty); }
30                         set { SetValue(DefaultItemTemplateProperty, value); }
31                 }
32
33                 public IDataSource DataSource
34                 {
35                         get { return (IDataSource)GetValue(DataSourceProperty); }
36                         set { SetValue(DataSourceProperty, value); }
37                 }
38
39                 void IDataSourceProvider.MaskKey(string key)
40                 {
41                         _maskedKeys.Add(key);
42                         IDataSource dataSource = DataSource;
43                         if (dataSource != null && !dataSource.MaskedKeys.Contains(key))
44                         {
45                                 dataSource.MaskKey(key);
46                         }
47                 }
48
49                 void IDataSourceProvider.UnmaskKey(string key)
50                 {
51                         _maskedKeys.Remove(key);
52                         DataSource?.UnmaskKey(key);
53                 }
54
55                 static void OnDataSourceChanged(BindableObject bindable, object oldValue, object newValue)
56                 {
57                         var dataView = (DataView)bindable;
58                         var dataSource = (IDataSource)newValue;
59                         var oldSource = (IDataSource)oldValue;
60
61                         if (oldSource != null)
62                         {
63                                 foreach (string key in dataView._maskedKeys)
64                                         oldSource.UnmaskKey(key);
65                         }
66
67                         if (dataSource != null)
68                         {
69                                 foreach (string key in dataView._maskedKeys)
70                                 {
71                                         dataSource.MaskKey(key);
72                                 }
73                         }
74                 }
75         }
76 }