Removing Tizen.Xamarin.Forms.Extensions
[profile/tv/apps/dotnet/mediahub.git] / TVMediaHub / TVMediaHub.Tizen / Extensions / DropdownList.cs
1 using System;
2 using System.Collections;
3 using System.Collections.ObjectModel;
4 using System.Collections.Specialized;
5 using System.ComponentModel;
6 using System.Linq;
7 using Xamarin.Forms;
8
9 namespace Tizen.Xamarin.Forms.Extension
10 {
11     /// <summary>
12     /// The DropdownList is a button like widget that pops up a list of items(automatically choosing the direction to display) that have a string label.
13     /// It is a convenient widget to avoid the need to do all the piecing together yourself.
14     /// It is intended for a small number of items in the DropdownList menu (no more than 8), though, it is capable of many more.
15     /// </summary>
16     /// <example>
17     /// <code>
18     /// var dropdownList = new DropdownList();
19     /// dropdownList.ItemSelected += (s, e) =>
20     /// {
21     ///     Debug.WriteLine("e.Selected Item: " + e.SelectedItem);
22     /// };
23     ///
24     /// ObservableCollection<string> ItemsList = new ObservableCollection<string>()
25     /// {
26     ///     "item5", "item6", "item7", "item8"
27     /// };
28     ///
29     /// dropdownList.ItemsSource = ItemsList;
30     /// </code>
31     /// </example>
32     public class DropdownList : View
33     {
34         /// <summary>
35         /// BindableProperty. Identifies the SelectedItem bindable property.
36         /// </summary>
37         public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create("SelectedItem", typeof(object), typeof(DropdownList), default(object), BindingMode.TwoWay, propertyChanged: OnSelectedItemChanged);
38
39         [Obsolete("IsHorizontalProperty is obsolete as of version 2.3.5-r256. The orientation is always vertical.")]
40         public static readonly BindableProperty IsHorizontalProperty = BindableProperty.Create("IsHorizontal", typeof(bool), typeof(DropdownList), default(bool));
41
42         [EditorBrowsable(EditorBrowsableState.Never)]
43         public static readonly BindablePropertyKey IsExpandedPropertyKey = BindableProperty.CreateReadOnly("IsExpanded", typeof(bool), typeof(DropdownList), default(bool), propertyChanged: OnIsExpandedPropertyChanged);
44
45         /// <summary>
46         /// BindableProperty. Identifies the IsExpanded bindable property.
47         /// </summary>
48         public static readonly BindableProperty IsExpandedProperty = IsExpandedPropertyKey.BindableProperty;
49
50         /// <summary>
51         /// BindableProperty. Identifies the ItemsSource bindable property.
52         /// </summary>
53         public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(DropdownList), default(IEnumerable), propertyChanged: OnItemsSourceChanged);
54
55         /// <summary>
56         /// BindableProperty. Identifies the DisplayMemberPath bindable property.
57         /// </summary>
58         public static readonly BindableProperty DisplayMemberPathProperty = BindableProperty.Create("DisplayMemberPath", typeof(string), typeof(DropdownList), default(string));
59
60         /// <summary>
61         /// Occurs when an item in the DropdownList is selected.
62         /// SelectedItemChangedEventArgs will also have the selected item.
63         /// </summary>
64         public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;
65
66         /// <summary>
67         /// Occurs when the DropdownList is expanded.
68         /// </summary>
69         public event EventHandler Expanded;
70
71         /// <summary>
72         /// Occurs when the DropdownList is collapsed.
73         /// </summary>
74         public event EventHandler Collapsed;
75
76         [EditorBrowsable(EditorBrowsableState.Never)]
77         public event EventHandler<ExpandRequestArgs> ExpandChangeRequested;
78
79         /// <summary>
80         /// Raised when one or more items of ItemsSource is changed.
81         /// </summary>
82         public event NotifyCollectionChangedEventHandler CollectionChanged;
83
84         /// <summary>
85         /// Gets or sets the currently selected item from the DropdownList.ItemsSource.
86         /// </summary>
87         public object SelectedItem
88         {
89             get { return (object)GetValue(SelectedItemProperty); }
90             set { SetValue(SelectedItemProperty, value); }
91         }
92
93         [Obsolete("IsHorizontal is obsolete as of version 2.3.5-r256. The orientation is always vertical.")]
94         public bool IsHorizontal
95         {
96             get { return (bool)GetValue(IsHorizontalProperty); }
97             set { SetValue(IsHorizontalProperty, value); }
98         }
99
100         /// <summary>
101         /// Gets the flag of whether the dropdownlist is expanded.
102         /// </summary>
103         public bool IsExpanded
104         {
105             get { return (bool)GetValue(IsExpandedProperty); }
106         }
107
108         /// <summary>
109         /// Gets or sets the source of items to template and display.
110         /// </summary>
111         public IEnumerable ItemsSource
112         {
113             get { return (IEnumerable)GetValue(ItemsSourceProperty); }
114             set { SetValue(ItemsSourceProperty, value); }
115         }
116
117         /// <summary>
118         /// Gets or sets a member path to a value on the ItemsSource.
119         /// </summary>
120         public string DisplayMemberPath
121         {
122             get { return (string)GetValue(DisplayMemberPathProperty); }
123             set { SetValue(DisplayMemberPathProperty, value); }
124         }
125
126         /// <summary>
127         /// Expands the DropdownList popup from code.
128         /// </summary>
129         public void Expand()
130         {
131             if (!IsExpanded)
132             {
133                 var arg = new ExpandRequestArgs { IsExpand = true };
134                 ExpandChangeRequested?.Invoke(this, arg);
135             }
136         }
137
138         /// <summary>
139         /// Collapses the DropdownList popup from code.
140         /// </summary>
141         public void Collapse()
142         {
143             if (IsExpanded)
144             {
145                 var arg = new ExpandRequestArgs();
146                 ExpandChangeRequested?.Invoke(this, arg);
147             }
148         }
149
150         static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
151         {
152             (bindable as DropdownList).OnItemsSourceChanged(oldValue as INotifyCollectionChanged, newValue as INotifyCollectionChanged);
153         }
154
155         void OnItemsSourceChanged(INotifyCollectionChanged oldValue, INotifyCollectionChanged newValue)
156         {
157             if (oldValue != null)
158             {
159                 oldValue.CollectionChanged -= OnNotifyCollectionChanged;
160             }
161             if (newValue != null)
162             {
163                 newValue.CollectionChanged += OnNotifyCollectionChanged;
164             }
165         }
166
167         void OnNotifyCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
168         {
169             CollectionChanged?.Invoke(this, e);
170         }
171
172         static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
173         {
174             var dropdownList = (DropdownList)bindable;
175             dropdownList.ItemSelected?.Invoke(bindable, new SelectedItemChangedEventArgs(newValue));
176         }
177
178         static void OnIsExpandedPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
179         {
180             var element = bindable as DropdownList;
181             if (element != null)
182             {
183                 var isExpanded = (bool)newvalue;
184                 if (isExpanded)
185                 {
186                     element.Expanded?.Invoke(element, EventArgs.Empty);
187                 }
188                 else
189                 {
190                     element.Collapsed?.Invoke(element, EventArgs.Empty);
191                 }
192             }
193         }
194
195         [EditorBrowsable(EditorBrowsableState.Never)]
196         public class ExpandRequestArgs : EventArgs
197         {
198             public bool IsExpand { get; set; }
199         }
200     }
201 }