Removing Tizen.Xamarin.Forms.Extensions
[profile/tv/apps/dotnet/mediahub.git] / TVMediaHub / TVMediaHub.Tizen / Extensions / ContextPopupImplementation.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Runtime.CompilerServices;
5 using Xamarin.Forms.Platform.Tizen;\r
6 using EContextPopup = ElmSharp.ContextPopup;
7 using EContextPopupDirection = ElmSharp.ContextPopupDirection;
8 using EContextPopupItem = ElmSharp.ContextPopupItem;
9 using EIcon = ElmSharp.Icon;
10 using TForms = Xamarin.Forms.Platform.Tizen.Forms;
11 using XForms = Xamarin.Forms;
12 using XFPlatformTizen = Xamarin.Forms.Platform.Tizen;
13
14 [assembly: XForms.Dependency(typeof(Tizen.Xamarin.Forms.Extension.Renderer.ContextPopupImplementation))]
15 namespace Tizen.Xamarin.Forms.Extension.Renderer
16 {
17     internal class ContextPopupImplementation : IContextPopup, INotifyPropertyChanged, IDisposable
18     {
19         EContextPopup _popup;
20         IDictionary<ContextPopupItem, EContextPopupItem> _items;
21         bool _isAutoHidingEnabled = true;
22
23         ContextPopupDirectionPriorities _priorities =
24             new ContextPopupDirectionPriorities(ContextPopupDirection.Up, ContextPopupDirection.Left, ContextPopupDirection.Right, ContextPopupDirection.Down);
25
26         ContextPopupItem _selectedItem = null;
27
28         bool _isDisposed;
29
30         public ContextPopupImplementation()
31         {
32             _popup = new EContextPopup((TForms.Context as FormsApplication).MainWindow);
33
34             _popup.BackButtonPressed += (s, e) =>
35             {
36                 _popup.Dismiss();
37             };
38
39             _popup.Dismissed += (s, e) =>
40             {
41                 Dismissed?.Invoke(this, EventArgs.Empty);
42             };
43
44             _items = new Dictionary<ContextPopupItem, EContextPopupItem>();
45         }
46
47         ~ContextPopupImplementation()
48         {
49             Dispose(false);
50         }
51
52         public event PropertyChangedEventHandler PropertyChanged;
53
54         public event EventHandler ItemSelected;
55
56         public event EventHandler Dismissed;
57
58         public bool IsAutoHidingEnabled
59         {
60             get
61             {
62                 return _isAutoHidingEnabled;
63             }
64
65             set
66             {
67                 _isAutoHidingEnabled = value;
68                 UpdateIsAutoHidingEnabled();
69                 OnPropertyChanged();
70             }
71         }
72
73         public ContextPopupDirectionPriorities DirectionPriorities
74         {
75             get
76             {
77                 return _priorities;
78             }
79
80             set
81             {
82                 _priorities = value;
83                 UpdateDirectionPriorities();
84                 OnPropertyChanged();
85             }
86         }
87
88         public ContextPopupItem SelectedItem
89         {
90             get
91             {
92                 return _selectedItem;
93             }
94
95             set
96             {
97                 _selectedItem = value;
98                 OnPropertyChanged();
99             }
100         }
101
102         public void Dismiss()
103         {
104             _popup.Dismiss();
105         }
106
107         public void AddItems(IEnumerable<ContextPopupItem> items)
108         {
109             foreach (var item in items)
110             {
111                 item.PropertyChanged += ContextPopupItemPropertyChanged;
112                 AddItem(item);
113             }
114         }
115
116         public void RemoveItems(IEnumerable<ContextPopupItem> items)
117         {
118             foreach (var item in items)
119             {
120                 item.PropertyChanged -= ContextPopupItemPropertyChanged;
121                 if (_items.ContainsKey(item))
122                 {
123                     var nativeItem = _items[item];
124                     nativeItem.Delete();
125                     _items.Remove(item);
126                 }
127             }
128         }
129
130         public void ClearItems()
131         {
132             foreach (var item in _items.Keys)
133                 item.PropertyChanged -= ContextPopupItemPropertyChanged;
134
135             _items.Clear();
136             _popup.Clear();
137         }
138
139         public void Show(XForms.View anchor, int xAnchorOffset, int yAnchorOffset)
140         {
141             var geometry = XFPlatformTizen.Platform.GetRenderer(anchor).NativeView.Geometry;
142             _popup.Move(geometry.X + xAnchorOffset, geometry.Y + yAnchorOffset);
143             _popup.Show();
144         }
145
146         public bool TryGetContextPopupDirection(out ContextPopupDirection direction)
147         {
148             var nativeDirection = _popup.Direction;
149             if (nativeDirection != EContextPopupDirection.Unknown)
150             {
151                 direction = (ContextPopupDirection)nativeDirection;
152                 return true;
153             }
154             else
155             {
156                 direction = default(ContextPopupDirection);
157                 return false;
158             }
159         }
160
161         public void UpdateContextPopupItemLabel(ContextPopupItem item)
162         {
163             EContextPopupItem nativeItem = _items[item];
164             nativeItem.SetPartText("default", item.Label);
165         }
166
167         public void UpdateContextPopupItemIcon(ContextPopupItem item)
168         {
169             if(string.IsNullOrEmpty(item.Icon))
170                 _items[item]?.SetPartContent("icon", null);
171             else
172                 AppendOrModifyItemWithIcon(item);
173         }
174
175         public void Dispose()
176         {
177             Dispose(true);
178             GC.SuppressFinalize(this);
179         }
180
181         protected virtual void Dispose(bool isDisposing)
182         {
183             if (_isDisposed)
184                 return;
185
186             if (isDisposing)
187             {
188                 if (_popup != null)
189                 {
190                     _popup.Unrealize();
191                     _popup = null;
192                 }
193             }
194
195             _isDisposed = true;
196         }
197
198         protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
199         {
200             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
201         }
202
203         void ContextPopupItemPropertyChanged(object sender, PropertyChangedEventArgs e)
204         {
205             var item = sender as ContextPopupItem;
206
207             if (e.PropertyName == nameof(ContextPopupItem.Label))
208             {
209                 // If the native item already has a label
210                 UpdateContextPopupItemLabel(item);
211             }
212             else if (e.PropertyName == nameof(ContextPopupItem.Icon))
213             {
214                 // If the native item already has an icon
215                 UpdateContextPopupItemIcon(item);
216             }
217         }
218
219         void UpdateDirectionPriorities()
220         {
221             _popup.SetDirectionPriorty(
222                 (EContextPopupDirection)_priorities.First,
223                 (EContextPopupDirection)_priorities.Second,
224                 (EContextPopupDirection)_priorities.Third,
225                 (EContextPopupDirection)_priorities.Fourth);
226         }
227
228         void UpdateIsAutoHidingEnabled()
229         {
230             _popup.AutoHide = IsAutoHidingEnabled;
231         }
232
233         void AddItem(ContextPopupItem item)
234         {
235             if (_items.ContainsKey(item))
236                 return;
237
238             EContextPopupItem nativeItem;
239             if (string.IsNullOrEmpty(item.Icon))
240             {
241                 nativeItem = _popup.Append(item.Label);
242             }
243             else
244             {
245                 nativeItem = AppendOrModifyItemWithIcon(item);
246             }
247
248             _items.Add(item, nativeItem);
249
250             nativeItem.Selected += (s, e) =>
251             {
252                 SelectedItem = item; // This will invoke SelectedIndexChanged if the index has changed
253                 ItemSelected?.Invoke(this, EventArgs.Empty);
254             };
255         }
256
257         EContextPopupItem AppendOrModifyItemWithIcon(ContextPopupItem item)
258         {
259             EContextPopupItem nativeItem = null;
260             EIcon icon = new EIcon(_popup);
261             icon.StandardIconName = item.Icon;
262             if (!string.IsNullOrEmpty(icon.StandardIconName))
263             {
264                 if (!_items.ContainsKey(item))
265                     nativeItem = _popup.Append(item.Label, icon);
266                 else
267                     _items[item].SetPartContent("icon", icon);
268             }
269             else
270             {
271                 //Not a standard icon
272                 XFPlatformTizen.Native.Image iconImage = new XFPlatformTizen.Native.Image(_popup);
273                 var task = iconImage.LoadFromImageSourceAsync(item.Icon);
274                 if (!_items.ContainsKey(item))
275                     nativeItem = _popup.Append(item.Label, iconImage);
276                 else
277                     _items[item].SetPartContent("icon", iconImage);
278             }
279
280             return nativeItem;
281         }
282     }
283 }