aad22fa45e1d6d5f1f3cb28e57552623bea7e741
[platform/core/csapi/xsf.git] / src / XSF / Xamarin.Forms.Core / SwipeItems.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Collections.ObjectModel;
5 using System.Collections.Specialized;
6 using System.Linq;
7
8 namespace Xamarin.Forms
9 {
10         public class SwipeItems : Element, IList<ISwipeItem>, INotifyCollectionChanged
11         {
12                 readonly ObservableCollection<ISwipeItem> _swipeItems;
13
14                 public SwipeItems(IEnumerable<ISwipeItem> swipeItems)
15                 {
16                         _swipeItems = new ObservableCollection<ISwipeItem>(swipeItems) ?? throw new ArgumentNullException(nameof(swipeItems));
17                         _swipeItems.CollectionChanged += OnSwipeItemsChanged;
18                 }
19
20                 public SwipeItems() : this(Enumerable.Empty<ISwipeItem>())
21                 {
22
23                 }
24
25                 public static readonly BindableProperty ModeProperty = BindableProperty.Create(nameof(Mode), typeof(SwipeMode), typeof(SwipeItems), SwipeMode.Reveal);
26                 public static readonly BindableProperty SwipeBehaviorOnInvokedProperty = BindableProperty.Create(nameof(SwipeBehaviorOnInvoked), typeof(SwipeBehaviorOnInvoked), typeof(SwipeItems), SwipeBehaviorOnInvoked.Auto);
27
28                 public SwipeMode Mode
29                 {
30                         get { return (SwipeMode)GetValue(ModeProperty); }
31                         set { SetValue(ModeProperty, value); }
32                 }
33
34                 public SwipeBehaviorOnInvoked SwipeBehaviorOnInvoked
35                 {
36                         get { return (SwipeBehaviorOnInvoked)GetValue(SwipeBehaviorOnInvokedProperty); }
37                         set { SetValue(SwipeBehaviorOnInvokedProperty, value); }
38                 }
39
40                 public event NotifyCollectionChangedEventHandler CollectionChanged
41                 {
42                         add { _swipeItems.CollectionChanged += value; }
43                         remove { _swipeItems.CollectionChanged -= value; }
44                 }
45
46                 public ISwipeItem this[int index]
47                 {
48                         get => _swipeItems.Count > index ? _swipeItems[index] : null;
49                         set => _swipeItems[index] = value;
50                 }
51
52                 public int Count => _swipeItems.Count;
53
54                 public bool IsReadOnly => false;
55
56                 public void Add(ISwipeItem item)
57                 {
58                         _swipeItems.Add(item);
59                 }
60
61                 public void Clear()
62                 {
63                         _swipeItems.Clear();
64                 }
65
66                 public bool Contains(ISwipeItem item)
67                 {
68                         return _swipeItems.Contains(item);
69                 }
70
71                 public void CopyTo(ISwipeItem[] array, int arrayIndex)
72                 {
73                         _swipeItems.CopyTo(array, arrayIndex);
74                 }
75
76                 public IEnumerator<ISwipeItem> GetEnumerator()
77                 {
78                         return _swipeItems.GetEnumerator();
79                 }
80
81                 public int IndexOf(ISwipeItem item)
82                 {
83                         return _swipeItems.IndexOf(item);
84                 }
85
86                 public void Insert(int index, ISwipeItem item)
87                 {
88                         _swipeItems.Insert(index, item);
89                 }
90
91                 public bool Remove(ISwipeItem item)
92                 {
93                         return _swipeItems.Remove(item);
94                 }
95
96                 public void RemoveAt(int index)
97                 {
98                         _swipeItems.RemoveAt(index);
99                 }
100
101                 protected override void OnBindingContextChanged()
102                 {
103                         base.OnBindingContextChanged();
104
105                         object bc = BindingContext;
106
107                         foreach (BindableObject item in _swipeItems)
108                                 SetInheritedBindingContext(item, bc);
109                 }
110
111                 void OnSwipeItemsChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
112                 {
113                         if (notifyCollectionChangedEventArgs.NewItems == null)
114                                 return;
115
116                         object bc = BindingContext;
117
118                         foreach (BindableObject item in notifyCollectionChangedEventArgs.NewItems)
119                                 SetInheritedBindingContext(item, bc);
120                 }
121
122                 IEnumerator IEnumerable.GetEnumerator()
123                 {
124                         return _swipeItems.GetEnumerator();
125                 }
126         }
127 }