5c28ec3683cfaf77af5d496d23b0499deca942d1
[profile/tv/apps/dotnet/mediahub.git] / TVMediaHub / TVMediaHub.Tizen / Views / FooterNormalStatus.xaml.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19
20 using Xamarin.Forms;
21 using Xamarin.Forms.Xaml;
22 using Tizen.Xamarin.Forms.Extension;
23 using TVMediaHub.Tizen.Utils;
24 using TVMediaHub.Tizen.DataModels;
25
26 namespace TVMediaHub.Tizen.Views
27 {
28     /// <summary>
29     /// A custom view for displaying footer when the status of page is normal
30     /// </summary>
31     [XamlCompilation(XamlCompilationOptions.Compile)]
32     public partial class FooterNormalStatus : RelativeLayout
33     {
34         public static readonly BindableProperty IsFooterEnabledProperty = BindableProperty.Create("IsFooterEnabled", typeof(bool), typeof(FooterNormalStatus), true);
35
36         public bool IsFooterEnabled
37         {
38             get { return (bool)GetValue(IsFooterEnabledProperty); }
39             set { SetValue(IsFooterEnabledProperty, value); }
40         }
41
42         /// <summary>
43         /// Identifies the SourceList bindable property
44         /// </summary
45         public static readonly BindableProperty SourceListProperty = BindableProperty.Create("SourceList", typeof(IEnumerable<string>), typeof(FooterNormalStatus), default(IEnumerable<string>));
46
47         /// <summary>
48         /// Gets or sets list about source of contents
49         /// </summary>
50         public IEnumerable<string> SourceList
51         {
52             get { return (IEnumerable<string>)GetValue(SourceListProperty); }
53             set { SetValue(SourceListProperty, value); }
54         }
55
56         /// <summary>
57         /// Identifies the SortOptions bindable property
58         /// </summary
59         public static readonly BindableProperty SortOptionsProperty = BindableProperty.Create("SortOptions", typeof(IEnumerable<string>), typeof(FooterNormalStatus), default(IEnumerable<string>));
60
61         /// <summary>
62         /// Gets or sets sort options
63         /// </summary>
64         public IEnumerable<string> SortOptions
65         {
66             get { return (IEnumerable<string>)GetValue(SortOptionsProperty); }
67             set { SetValue(SortOptionsProperty, value); }
68         }
69
70         /// <summary>
71         /// Identifies the OptionList bindable property
72         /// </summary
73         public static readonly BindableProperty OptionListProperty = BindableProperty.Create("OptionList", typeof(IEnumerable<string>), typeof(FooterNormalStatus), default(IEnumerable<string>));
74
75         /// <summary>
76         /// Gets or sets list about options
77         /// </summary>
78         public IEnumerable<string> OptionList
79         {
80             get { return (IEnumerable<string>)GetValue(OptionListProperty); }
81             set { SetValue(OptionListProperty, value); }
82         }
83
84         /// <summary>
85         /// A EventHandler for DropdownSource ItemSelected event
86         /// </summary>
87         public EventHandler<SelectedItemChangedEventArgs> OnDropdownSourceItemSelected;
88
89         /// <summary>
90         /// A EventHandler for DropdownSort ItemSelected event
91         /// </summary>
92         public EventHandler<SelectedItemChangedEventArgs> OnDropdownSortItemSelected;
93
94         /// <summary>
95         /// A EventHandler for DropdownOption ItemSelected event
96         /// </summary>
97         public EventHandler<ContextPopupSelectedEventArgs> OnSelectedOptionIndexChanged;
98
99         /// <summary>
100         /// A DropdownList for displaying sources of contents
101         /// </summary>
102         public DropdownList DropdownSource;
103
104         /// <summary>
105         /// A DropdownList for displaying sort options
106         /// </summary>
107         public DropdownList DropdownSort;
108
109         /// <summary>
110         /// A Button for displaying page options
111         /// </summary>
112         public Button ButtonOption;
113         private bool isPopupShowing;
114
115         /// <summary>
116         /// A Constructor
117         /// </summary>
118         public FooterNormalStatus()
119         {
120             InitializeComponent();
121             InitializeFooterItems();
122             PropertyChanged += FooterNormalStatusPropertyChanged;
123         }
124
125         /// <summary>
126         /// This method is called when the properties is changed
127         /// </summary>
128         /// <param name="sender">The source of the event</param>
129         /// <param name="e">A propertyChanged event argument</param>
130         private void FooterNormalStatusPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
131         {
132             if (e.PropertyName.Equals("SourceList"))
133             {
134                 DropdownSource.ItemsSource = SourceList;
135                 var list = SourceList as List<string>;
136                 DropdownSource.SelectedItem = list[0];
137             }
138             else if (e.PropertyName.Equals("SortOptions"))
139             {
140                 DropdownSort.ItemsSource = SortOptions;
141                 var list = SortOptions as List<string>;
142                 DropdownSort.SelectedItem = list[0];
143             }
144             else if (e.PropertyName.Equals("IsFooterEnabled"))
145             {
146                 DropdownSource.IsEnabled = IsFooterEnabled;
147                 DropdownSort.IsEnabled = IsFooterEnabled;
148                 ButtonOption.IsEnabled = IsFooterEnabled;
149                 if (!IsFooterEnabled)
150                 {
151                     ButtonOption.Text = "";
152                 }
153                 else
154                 {
155                     ButtonOption.Text = "OPTION";
156                 }
157             }
158         }
159
160         /// <summary>
161         /// A method for initializing footer items
162         /// </summary>
163         private void InitializeFooterItems()
164         {
165             DropdownSource = new DropdownList();
166             DropdownSort = new DropdownList();
167             ButtonOption = new Button();
168
169             ButtonOption.Text = "OPTION";
170
171             DropdownSource.ItemsSource = SourceList;
172             DropdownSort.ItemsSource = SortOptions;
173
174             DropdownSource.ItemSelected += (s, e) =>
175             {
176                 OnDropdownSourceItemSelected?.Invoke(s, e);
177             };
178
179             DropdownSort.ItemSelected += (s, e) =>
180             {
181                 OnDropdownSortItemSelected?.Invoke(s, e);
182             };
183
184             ButtonOption.Command = new Command(() =>
185             {
186                 ShowContextPopup();
187             });
188
189             // TODO : Change yConstraint.
190             // Tab Size - 128
191
192             Children.Add(DropdownSource,
193                 heightConstraint: Constraint.Constant(SizeUtils.GetHeightSize(80)),
194                 widthConstraint: Constraint.Constant(SizeUtils.GetWidthSize(300)),
195                 yConstraint: Constraint.Constant(SizeUtils.GetHeightSize(752)),
196                 xConstraint: Constraint.Constant(SizeUtils.GetWidthSize(96)));
197
198             Children.Add(DropdownSort,
199                 heightConstraint: Constraint.Constant(SizeUtils.GetHeightSize(80)),
200                 widthConstraint: Constraint.Constant(SizeUtils.GetWidthSize(300)),
201                 yConstraint: Constraint.Constant(SizeUtils.GetHeightSize(752)),
202                 xConstraint: Constraint.Constant(SizeUtils.GetWidthSize(1220)));
203
204             Children.Add(ButtonOption,
205                 heightConstraint: Constraint.Constant(SizeUtils.GetHeightSize(80)),
206                 widthConstraint: Constraint.Constant(SizeUtils.GetWidthSize(300)),
207                 yConstraint: Constraint.Constant(SizeUtils.GetHeightSize(752)),
208                 xConstraint: Constraint.Constant(SizeUtils.GetWidthSize(1524)));
209
210             ButtonOption.FontSize = SizeUtils.GetFontSize(28);
211         }
212
213         /// <summary>
214         /// A method for showing context popup
215         /// </summary>
216         private void ShowContextPopup()
217         {
218             if (isPopupShowing)
219             {
220                 return;
221             }
222
223             ContextPopup popup = new ContextPopup
224             {
225                 IsAutoHidingEnabled = true,
226                 Orientation = ContextPopupOrientation.Vertical,
227                 DirectionPriorities = new ContextPopupDirectionPriorities(ContextPopupDirection.Up, ContextPopupDirection.Right, ContextPopupDirection.Left, ContextPopupDirection.Down),
228             };
229
230             if (OptionList != null)
231             {
232                 foreach (var item in OptionList)
233                 {
234                     popup.Items.Add(new ContextPopupItem(item));
235                 }
236             }
237
238             //TODO: need to change the event callback
239             popup.SelectedIndexChanged += (s, args) =>
240             {
241                 var index = popup.SelectedIndex;
242                 var item = popup.SelectedItem as ContextPopupItem;
243                 OnSelectedOptionIndexChanged.Invoke(s, new ContextPopupSelectedEventArgs(index, item));
244                 popup.Dismiss();
245             };
246
247             popup.Dismissed += (s, args) =>
248             {
249                 isPopupShowing = false;
250             };
251
252             popup.Show(ButtonOption, (ButtonOption.Width / 2), 0);
253             isPopupShowing = true;
254         }
255     }
256 }