summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/FormsApplication.cs
blob: 83efe7a20f135d3f131c15214611cf949cb7d512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using System;
using System.ComponentModel;
using System.Diagnostics;
using Tizen.Applications;
using ElmSharp;
using EButton = ElmSharp.Button;
using EProgressBar = ElmSharp.ProgressBar;
using EColor = ElmSharp.Color;
using ELabel = ElmSharp.Label;

namespace Xamarin.Forms.Platform.Tizen
{
	public class FormsApplication : CoreUIApplication
	{
		Platform _platform;
		Application _application;
		bool _isInitialStart;
		int _pageBusyCount;
		Native.Dialog _pageBusyDialog;
		Native.Window _window;

		protected FormsApplication()
		{
			_isInitialStart = true;
			_pageBusyCount = 0;
		}

		/// <summary>
		/// Gets the main window or <c>null</c> if it's not set.
		/// </summary>
		/// <value>The main window or <c>null</c>.</value>
		public Native.Window MainWindow
		{
			get
			{
				return _window;
			}

			private set
			{
				_window = value;
			}
		}

		protected override void OnPreCreate()
		{
			base.OnPreCreate();
			Application.ClearCurrent();
			CreateWindow();
		}

		protected override void OnTerminate()
		{
			base.OnTerminate();
			MessagingCenter.Unsubscribe<Page, AlertArguments>(this, "Xamarin.SendAlert");
			MessagingCenter.Unsubscribe<Page, bool>(this, "Xamarin.BusySet");
			MessagingCenter.Unsubscribe<Page, ActionSheetArguments>(this, "Xamarin.ShowActionSheet");
			if (_platform != null)
			{
				_platform.Dispose();
			}
		}

		protected override void OnAppControlReceived(AppControlReceivedEventArgs e)
		{
			base.OnAppControlReceived(e);

			if (!_isInitialStart && _application != null)
			{
				_application.SendResume();
			}
			_isInitialStart = false;
		}

		protected override void OnPause()
		{
			base.OnPause();
			if (_application != null)
			{
				_application.SendSleepAsync();
			}
		}

		protected override void OnResume()
		{
			base.OnResume();
			if (_application != null)
			{
				_application.SendResume();
			}
		}

		public void LoadApplication(Application application)
		{
			if (null == MainWindow)
			{
				throw new NullReferenceException("MainWindow is not prepared. This method should be called in OnCreated().");
			}
			if (null == application)
			{
				throw new ArgumentNullException("application");
			}
			_application = application;
			Application.Current = application;
			application.SendStart();
			application.PropertyChanged += new PropertyChangedEventHandler(this.AppOnPropertyChanged);
			SetPage(_application.MainPage);
		}

		void AppOnPropertyChanged(object sender, PropertyChangedEventArgs args)
		{
			if ("MainPage" == args.PropertyName)
			{
				SetPage(_application.MainPage);
			}
		}

		void ShowActivityIndicatorDialog(bool enabled)
		{
			if (null == _pageBusyDialog)
			{
				_pageBusyDialog = new Native.Dialog(Forms.Context.MainWindow)
				{
					Orientation = PopupOrientation.Top,
				};

				var activity = new EProgressBar(_pageBusyDialog)
				{
					Style = "process_large",
					IsPulseMode = true,
				};
				activity.PlayPulse();
				activity.Show();

				_pageBusyDialog.Content = activity;

			}
			_pageBusyCount = Math.Max(0, enabled ? _pageBusyCount + 1 : _pageBusyCount - 1);
			if (_pageBusyCount > 0)
			{
				_pageBusyDialog.Show();
			}
			else
			{
				_pageBusyDialog.Dismiss();
				_pageBusyDialog = null;
			}
		}

		void SetPage(Page page)
		{
			if (!Forms.IsInitialized)
			{
				throw new InvalidOperationException("Call Forms.Init (UIApplication) before this");
			}
			if (_platform != null)
			{
				_platform.SetPage(page);
				return;
			}
			MessagingCenter.Subscribe<Page, bool>(this, Page.BusySetSignalName, delegate (Page sender, bool enabled)
				{
					ShowActivityIndicatorDialog(enabled);
				}, null);

			MessagingCenter.Subscribe<Page, AlertArguments>(this, Page.AlertSignalName, delegate (Page sender, AlertArguments arguments)
				{
					Native.Dialog alert = new Native.Dialog(Forms.Context.MainWindow);
					alert.Title = arguments.Title;
					var message = arguments.Message.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace(Environment.NewLine, "<br>");
					var label = new ELabel(alert)
					{
						Text = "<span font_size=30 color=#000000>" + message + "<\\span>",
					};
					label.Show();

					var box = new Box(alert);
					box.Show();

					bool labelAdded = false;
					box.Resized += (s, e) =>
					{
						label.LineWrapType = WrapType.Word;
						//set 2% padding for alert text message width
						label.LineWrapWidth = (int)Math.Round(box.Geometry.Width * 0.98);
						if (!labelAdded)
						{
							/*Adding label to the box (box.PackEnd(label)) has been placed in box.Resized()
							event due to get better performance. For some reason (probably EFL bug) when
							it's placed outside of it, box.Resized() event is called far too many times.*/
							box.PackEnd(label);
							labelAdded = true;
						}
					};

					alert.Content = box;

					EButton cancel = new EButton(alert) { Text = arguments.Cancel };
					alert.NegativeButton = cancel;
					cancel.Clicked += (s, evt) =>
					{
						arguments.SetResult(false);
						alert.Dismiss();
					};

					if (arguments.Accept != null)
					{
						EButton ok = new EButton(alert) { Text = arguments.Accept };
						alert.NeutralButton = ok;
						ok.Clicked += (s, evt) =>
						{
							arguments.SetResult(true);
							alert.Dismiss();
						};
					}

					alert.BackButtonPressed += (s, evt) =>
					{
						arguments.SetResult(false);
						alert.Dismiss();
					};

					alert.Show();
				}, null);

			MessagingCenter.Subscribe<Page, ActionSheetArguments>(this, Page.ActionSheetSignalName, delegate (Page sender, ActionSheetArguments arguments)
			{
				Native.Dialog alert = new Native.Dialog(Forms.Context.MainWindow);

				alert.Title = arguments.Title;
				Box box = new Box(alert);

				if (null != arguments.Destruction)
				{
					Native.Button destruction = new Native.Button(alert)
					{
						Text = arguments.Destruction,
						TextColor = EColor.Red,
						AlignmentX = -1
					};
					destruction.Clicked += (s, evt) =>
					{
						arguments.SetResult(arguments.Destruction);
						alert.Dismiss();
					};
					destruction.Show();
					box.PackEnd(destruction);
				}

				foreach (string buttonName in arguments.Buttons)
				{
					Native.Button button = new Native.Button(alert)
					{
						Text = buttonName,
						AlignmentX = -1
					};
					button.Clicked += (s, evt) =>
					{
						arguments.SetResult(buttonName);
						alert.Dismiss();
					};
					button.Show();
					box.PackEnd(button);
				}

				box.Show();
				alert.Content = box;

				if (null != arguments.Cancel)
				{
					EButton cancel = new EButton(Forms.Context.MainWindow) { Text = arguments.Cancel };
					alert.NegativeButton = cancel;
					cancel.Clicked += (s, evt) =>
					{
						alert.Dismiss();
					};
				}

				alert.BackButtonPressed += (s, evt) =>
				{
					alert.Dismiss();
				};

				alert.Show();
			}, null);

			_platform = new Platform(this);
			if (_application != null)
			{
				_application.Platform = _platform;
			}
			_platform.SetPage(page);
		}

		void CreateWindow()
		{
			Debug.Assert(null == MainWindow);

			var window = new Native.Window();
			window.Closed += (s, e) =>
			{
				Exit();
			};
			window.RotationChanged += (sender, e) =>
			{
				switch (_window.CurrentOrientation)
				{
					case Native.DisplayOrientations.None:
						Device.Info.CurrentOrientation = DeviceOrientation.Other;
						break;

					case Native.DisplayOrientations.Portrait:
						Device.Info.CurrentOrientation = DeviceOrientation.PortraitUp;
						break;

					case Native.DisplayOrientations.Landscape:
						Device.Info.CurrentOrientation = DeviceOrientation.LandscapeLeft;
						break;

					case Native.DisplayOrientations.PortraitFlipped:
						Device.Info.CurrentOrientation = DeviceOrientation.PortraitDown;
						break;

					case Native.DisplayOrientations.LandscapeFlipped:
						Device.Info.CurrentOrientation = DeviceOrientation.LandscapeRight;
						break;
				}
			};

			MainWindow = window;
		}
		public void Run()
		{
			Run(System.Environment.GetCommandLineArgs());
		}

		/// <summary>
		/// Exits the application's main loop, which initiates the process of its termination
		/// </summary>
		public override void Exit()
		{
			if (_platform == null)
			{
				Log.Warn("Exit was already called or FormsApplication is not initialized yet.");
				return;
			}
			// before everything is closed, inform the MainPage that it is disappearing
			try
			{
				(_platform?.Page as IPageController)?.SendDisappearing();
				_platform = null;
			}
			catch (Exception e)
			{
				Log.Error("Exception thrown from SendDisappearing: {0}", e.Message);
			}

			base.Exit();
		}
	}
}