Removing Tizen.Xamarin.Forms.Extensions
[profile/tv/apps/dotnet/mediahub.git] / TVMediaHub / TVMediaHub.Tizen / Extensions / DialogImplementation.cs
1 using System;
2 using Xamarin.Forms;
3 using Xamarin.Forms.Platform.Tizen;
4 using TForms = Xamarin.Forms.Platform.Tizen.Forms;
5 using EPopup = ElmSharp.Popup;
6 using Tizen.Xamarin.Forms.Extension.Renderer;
7
8 [assembly: Dependency(typeof(DialogImplementation))]
9
10 namespace Tizen.Xamarin.Forms.Extension.Renderer
11 {
12     class DialogImplementation : IDialog, IDisposable
13     {
14         EPopup _control;
15         View _content;
16         Button _positive;
17         Button _neutral;
18         Button _negative;
19         string _title;
20         string _subtitle;
21         StackLayout _contentView;
22         LayoutOptions _horizontalOption = LayoutOptions.Center;
23         LayoutOptions _verticalOption = LayoutOptions.End;
24
25         LayoutOptions _previousHorizontalOption = LayoutOptions.Center;
26
27         bool _isDisposed = false;
28
29         ElmSharp.Button _nativePositive;
30
31         ElmSharp.Button _nativeNeutral;
32         ElmSharp.Button _nativeNegative;
33         ElmSharp.EvasObject _nativeContent;
34
35         public event EventHandler Hidden;
36
37         public event EventHandler OutsideClicked;
38
39         public event EventHandler Shown;
40
41         public event EventHandler BackButtonPressed;
42
43         public DialogImplementation()
44         {
45             _control = new EPopup((TForms.Context as FormsApplication).MainWindow);
46
47             _control.ShowAnimationFinished += ShowAnimationFinishedHandler;
48             _control.Dismissed += DismissedHandler;
49             _control.OutsideClicked += OutsideClickedHandler;
50             _control.BackButtonPressed += BackButtonPressedHandler;
51
52             _contentView = new StackLayout();
53         }
54
55         ~DialogImplementation()
56         {
57             Dispose(false);
58         }
59
60         public View Content
61         {
62             get
63             {
64                 return _content;
65             }
66             set
67             {
68                 _content = value;
69                 UpdateContent();
70             }
71         }
72
73         public Button Positive
74         {
75             get
76             {
77                 return _positive;
78             }
79             set
80             {
81                 _positive = value;
82                 UpdatePositive();
83             }
84         }
85
86         public Button Neutral
87         {
88             get
89             {
90                 return _neutral;
91             }
92             set
93             {
94                 _neutral = value;
95                 UpdateNeutral();
96             }
97         }
98
99         public Button Negative
100         {
101             get
102             {
103                 return _negative;
104             }
105             set
106             {
107                 _negative = value;
108                 UpdateNegative();
109             }
110         }
111
112         public string Title
113         {
114             get
115             {
116                 return _title;
117             }
118             set
119             {
120                 _title = value;
121                 UpdateTitle();
122             }
123         }
124
125         public string Subtitle
126         {
127             get
128             {
129                 return _subtitle;
130             }
131             set
132             {
133                 _subtitle = value;
134                 UpdateSubtitle();
135             }
136         }
137
138         public LayoutOptions HorizontalOption
139         {
140             get
141             {
142                 return _horizontalOption;
143             }
144             set
145             {
146                 _horizontalOption = value;
147                 UpdateHorizontalOption();
148             }
149         }
150
151         public LayoutOptions VerticalOption
152         {
153             get
154             {
155                 return _verticalOption;
156             }
157             set
158             {
159                 _verticalOption = value;
160                 UpdateVerticalOption();
161             }
162         }
163
164         public void Show()
165         {
166             if (Application.Current.Platform == null)
167             {
168                 throw new Exception("When the Application's Platform is null, can not show the Dialog.");
169             }
170             if (_contentView.Platform == null)
171             {
172                 UpdateContent();
173             }
174             _control.Show();
175         }
176
177         public void Hide()
178         {
179             _control.Hide();
180         }
181
182         public void Dispose()
183         {
184             Dispose(true);
185             GC.SuppressFinalize(this);
186         }
187
188         protected virtual void Dispose(bool disposing)
189         {
190             if (_isDisposed)
191                 return;
192
193             if (disposing)
194             {
195                 if (_nativePositive != null)
196                 {
197                     _nativePositive.Unrealize();
198                     _nativePositive = null;
199                 }
200                 if (_nativeNeutral != null)
201                 {
202                     _nativeNeutral.Unrealize();
203                     _nativeNeutral = null;
204                 }
205                 if (_nativeNegative != null)
206                 {
207                     _nativeNegative.Unrealize();
208                     _nativeNegative = null;
209                 }
210                 if (_nativeContent != null)
211                 {
212                     _nativeContent.Unrealize();
213                     _nativeContent = null;
214                 }
215
216                 if (_control != null)
217                 {
218                     _control.ShowAnimationFinished -= ShowAnimationFinishedHandler;
219                     _control.Dismissed -= DismissedHandler;
220                     _control.OutsideClicked -= OutsideClickedHandler;
221                     _control.BackButtonPressed -= BackButtonPressedHandler;
222
223                     _control.Unrealize();
224                     _control = null;
225                 }
226             }
227
228             _isDisposed = true;
229         }
230
231         void ShowAnimationFinishedHandler(object sender, EventArgs e)
232         {
233             _nativeContent?.MarkChanged();
234             Shown?.Invoke(this, EventArgs.Empty);
235         }
236
237         void DismissedHandler(object sender, EventArgs e)
238         {
239             Hidden?.Invoke(this, EventArgs.Empty);
240         }
241
242         void OutsideClickedHandler(object sender, EventArgs e)
243         {
244             OutsideClicked?.Invoke(this, EventArgs.Empty);
245         }
246
247         void BackButtonPressedHandler(object sender, EventArgs e)
248         {
249             BackButtonPressed?.Invoke(this, EventArgs.Empty);
250         }
251
252         void UpdateContent()
253         {
254             if (Application.Current.Platform == null)
255                 return;
256
257             _contentView.Children.Clear();
258
259             if (Content != null)
260             {
261                 _contentView.Children.Add(Content);
262
263                 _contentView.Platform = Application.Current.Platform;
264
265                 var renderer = Platform.GetOrCreateRenderer(_contentView);
266                 (renderer as LayoutRenderer)?.RegisterOnLayoutUpdated();
267
268                 var sizeRequest = _contentView.Measure((TForms.Context as FormsApplication).MainWindow.ScreenSize.Width, (TForms.Context as FormsApplication).MainWindow.ScreenSize.Height).Request.ToPixel();
269
270                 _nativeContent = renderer.NativeView;
271                 _nativeContent.MinimumHeight = sizeRequest.Height;
272
273                 _control.SetPartContent("default", _nativeContent, true);
274             }
275             else
276             {
277                 _control.SetPartContent("default", null, true);
278             }
279         }
280
281         void UpdatePositive()
282         {
283             _nativePositive?.Hide();
284
285             if (Positive != null)
286             {
287                 _nativePositive = (ElmSharp.Button)Platform.GetOrCreateRenderer(Positive).NativeView;
288                 _nativePositive.Style = "popup";
289             }
290             else
291             {
292                 _nativePositive = null;
293             }
294
295             _control.SetPartContent("button1", _nativePositive, true);
296         }
297
298         void UpdateNeutral()
299         {
300             _nativeNeutral?.Hide();
301
302             if (Neutral != null)
303             {
304                 _nativeNeutral = (ElmSharp.Button)Platform.GetOrCreateRenderer(Neutral).NativeView;
305                 _nativeNeutral.Style = "popup";
306             }
307             else
308             {
309                 _nativeNeutral = null;
310             }
311
312             _control.SetPartContent("button2", _nativeNeutral, true);
313         }
314
315         void UpdateNegative()
316         {
317             _nativeNegative?.Hide();
318
319             if (Negative != null)
320             {
321                 _nativeNegative = (ElmSharp.Button)Platform.GetOrCreateRenderer(Negative).NativeView;
322                 _nativeNegative.Style = "popup";
323             }
324             else
325             {
326                 _nativeNegative = null;
327             }
328
329             _control.SetPartContent("button3", _nativeNegative, true);
330         }
331
332         void UpdateTitle()
333         {
334             _control.SetPartText("title,text", Title);
335         }
336
337         void UpdateSubtitle()
338         {
339             _control.SetPartText("subtitle,text", Subtitle);
340         }
341
342         void UpdateHorizontalOption()
343         {
344             switch (HorizontalOption.Alignment)
345             {
346                 case LayoutAlignment.Start:
347                     _control.AlignmentX = 0.0;
348                     break;
349
350                 case LayoutAlignment.Center:
351                     _control.AlignmentX = 0.5;
352                     break;
353
354                 case LayoutAlignment.End:
355                     _control.AlignmentX = 1.0;
356                     break;
357
358                 case LayoutAlignment.Fill:
359                     _control.AlignmentX = -1;
360                     break;
361             }
362             if (HorizontalOption.Alignment == LayoutAlignment.Fill || _previousHorizontalOption.Alignment == LayoutAlignment.Fill)
363             {
364                 UpdateContent();
365                 _previousHorizontalOption = HorizontalOption;
366             }
367         }
368
369         void UpdateVerticalOption()
370         {
371             switch (VerticalOption.Alignment)
372             {
373                 case LayoutAlignment.Start:
374                     _control.AlignmentY = 0.0;
375                     break;
376
377                 case LayoutAlignment.Center:
378                     _control.AlignmentY = 0.5;
379                     break;
380
381                 case LayoutAlignment.End:
382                     _control.AlignmentY = 1.0;
383                     break;
384
385                 case LayoutAlignment.Fill:
386                     _control.AlignmentY = -1;
387                     break;
388             }
389         }
390     }
391 }