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