summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.iOS/Forms.cs
blob: eb4100d85aafb3d69a2e56453f8cbdd2787b5565 (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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Forms.Internals;
#if __UNIFIED__
using UIKit;
using CoreFoundation;
using Foundation;
#else
using MonoTouch.UIKit;
using MonoTouch.CoreFoundation;
using MonoTouch.Foundation;
#endif
using Xamarin.Forms.Platform.iOS;

namespace Xamarin.Forms
{
	public static class Forms
	{
		//Preserve GetCallingAssembly
		static readonly bool nevertrue = false;

		static bool? s_isiOS7OrNewer;

		static bool? s_isiOS8OrNewer;

		static bool? s_isiOS9OrNewer;

		static Forms()
		{
			if (nevertrue)
				Assembly.GetCallingAssembly();
		}

		public static bool IsInitialized { get; private set; }

		internal static bool IsiOS7OrNewer
		{
			get
			{
				if (!s_isiOS7OrNewer.HasValue)
					s_isiOS7OrNewer = UIDevice.CurrentDevice.CheckSystemVersion(7, 0);
				return s_isiOS7OrNewer.Value;
			}
		}

		internal static bool IsiOS8OrNewer
		{
			get
			{
				if (!s_isiOS8OrNewer.HasValue)
					s_isiOS8OrNewer = UIDevice.CurrentDevice.CheckSystemVersion(8, 0);
				return s_isiOS8OrNewer.Value;
			}
		}

		internal static bool IsiOS9OrNewer
		{
			get
			{
				if (!s_isiOS9OrNewer.HasValue)
					s_isiOS9OrNewer = UIDevice.CurrentDevice.CheckSystemVersion(9, 0);
				return s_isiOS9OrNewer.Value;
			}
		}

		public static void Init()
		{
			if (IsInitialized)
				return;
			IsInitialized = true;
			Color.Accent = Color.FromRgba(50, 79, 133, 255);

			Log.Listeners.Add(new DelegateLogListener((c, m) => Trace.WriteLine(m, c)));

			Device.OS = TargetPlatform.iOS;
			Device.PlatformServices = new IOSPlatformServices();
			Device.Info = new IOSDeviceInfo();

			Registrar.RegisterAll(new[] { typeof(ExportRendererAttribute), typeof(ExportCellAttribute), typeof(ExportImageSourceHandlerAttribute) });

			Device.Idiom = UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad ? TargetIdiom.Tablet : TargetIdiom.Phone;

			ExpressionSearch.Default = new iOSExpressionSearch();
		}

		public static event EventHandler<ViewInitializedEventArgs> ViewInitialized;

		internal static void SendViewInitialized(this VisualElement self, UIView nativeView)
		{
			var viewInitialized = ViewInitialized;
			if (viewInitialized != null)
				viewInitialized(self, new ViewInitializedEventArgs { View = self, NativeView = nativeView });
		}

		class iOSExpressionSearch : ExpressionVisitor, IExpressionSearch
		{
			List<object> _results;
			Type _targetType;

			public List<T> FindObjects<T>(Expression expression) where T : class
			{
				_results = new List<object>();
				_targetType = typeof(T);
				Visit(expression);
				return _results.Select(o => o as T).ToList();
			}

			protected override Expression VisitMember(MemberExpression node)
			{
				if (node.Expression is ConstantExpression && node.Member is FieldInfo)
				{
					var container = ((ConstantExpression)node.Expression).Value;
					var value = ((FieldInfo)node.Member).GetValue(container);

					if (_targetType.IsInstanceOfType(value))
						_results.Add(value);
				}
				return base.VisitMember(node);
			}
		}

		internal class IOSDeviceInfo : DeviceInfo
		{
			readonly NSObject _notification;
			readonly Size _scaledScreenSize;
			readonly double _scalingFactor;

			public IOSDeviceInfo()
			{
				_notification = UIDevice.Notifications.ObserveOrientationDidChange((sender, args) => CurrentOrientation = UIDevice.CurrentDevice.Orientation.ToDeviceOrientation());

				_scalingFactor = UIScreen.MainScreen.Scale;
				_scaledScreenSize = new Size(UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height);
				PixelScreenSize = new Size(_scaledScreenSize.Width * _scalingFactor, _scaledScreenSize.Height * _scalingFactor);
			}

			public override Size PixelScreenSize { get; }

			public override Size ScaledScreenSize
			{
				get { return _scaledScreenSize; }
			}

			public override double ScalingFactor
			{
				get { return _scalingFactor; }
			}

			protected override void Dispose(bool disposing)
			{
				_notification.Dispose();
				base.Dispose(disposing);
			}
		}

		class IOSPlatformServices : IPlatformServices
		{
			static readonly MD5CryptoServiceProvider Checksum = new MD5CryptoServiceProvider();

			public void BeginInvokeOnMainThread(Action action)
			{
				NSRunLoop.Main.BeginInvokeOnMainThread(action.Invoke);
			}

			public Ticker CreateTicker()
			{
				return new CADisplayLinkTicker();
			}

			public Assembly[] GetAssemblies()
			{
				return AppDomain.CurrentDomain.GetAssemblies();
			}

			public string GetMD5Hash(string input)
			{
				var bytes = Checksum.ComputeHash(Encoding.UTF8.GetBytes(input));
				var ret = new char[32];
				for (var i = 0; i < 16; i++)
				{
					ret[i * 2] = (char)Hex(bytes[i] >> 4);
					ret[i * 2 + 1] = (char)Hex(bytes[i] & 0xf);
				}
				return new string(ret);
			}

			public double GetNamedSize(NamedSize size, Type targetElementType, bool useOldSizes)
			{
				// We make these up anyway, so new sizes didn't really change
				// iOS docs say default button font size is 15, default label font size is 17 so we use those as the defaults.
				switch (size)
				{
					case NamedSize.Default:
						return typeof(Button).IsAssignableFrom(targetElementType) ? 15 : 17;
					case NamedSize.Micro:
						return 12;
					case NamedSize.Small:
						return 14;
					case NamedSize.Medium:
						return 17;
					case NamedSize.Large:
						return 22;
					default:
						throw new ArgumentOutOfRangeException("size");
				}
			}

			public async Task<Stream> GetStreamAsync(Uri uri, CancellationToken cancellationToken)
			{
				using (var client = GetHttpClient())
				using (var response = await client.GetAsync(uri, cancellationToken))
					return await response.Content.ReadAsStreamAsync();
			}

			public IIsolatedStorageFile GetUserStoreForApplication()
			{
				return new _IsolatedStorageFile(IsolatedStorageFile.GetUserStoreForApplication());
			}

			public bool IsInvokeRequired
			{
				get { return !NSThread.IsMain; }
			}

			public void OpenUriAction(Uri uri)
			{
				UIApplication.SharedApplication.OpenUrl(new NSUrl(uri.AbsoluteUri));
			}

			public void StartTimer(TimeSpan interval, Func<bool> callback)
			{
				NSTimer timer = null;
#if __UNIFIED__
				timer = NSTimer.CreateRepeatingScheduledTimer(interval, t =>
				{
#else
				timer = NSTimer.CreateRepeatingScheduledTimer (interval, () => {
				#endif
					if (!callback())
#if __UNIFIED__
						t.Invalidate();
#else
						timer.Invalidate ();
						#endif
				});
				NSRunLoop.Main.AddTimer(timer, NSRunLoopMode.Common);
			}

			HttpClient GetHttpClient()
			{
				var proxy = CFNetwork.GetSystemProxySettings();
				var handler = new HttpClientHandler();
				if (!string.IsNullOrEmpty(proxy.HTTPProxy))
				{
					handler.Proxy = CFNetwork.GetDefaultProxy();
					handler.UseProxy = true;
				}
				return new HttpClient(handler);
			}

			static int Hex(int v)
			{
				if (v < 10)
					return '0' + v;
				return 'a' + v - 10;
			}

			public class _Timer : ITimer
			{
				readonly Timer _timer;

				public _Timer(Timer timer)
				{
					_timer = timer;
				}

				public void Change(int dueTime, int period)
				{
					_timer.Change(dueTime, period);
				}

				public void Change(long dueTime, long period)
				{
					_timer.Change(dueTime, period);
				}

				public void Change(TimeSpan dueTime, TimeSpan period)
				{
					_timer.Change(dueTime, period);
				}

				public void Change(uint dueTime, uint period)
				{
					_timer.Change(dueTime, period);
				}
			}

			public class _IsolatedStorageFile : IIsolatedStorageFile
			{
				readonly IsolatedStorageFile _isolatedStorageFile;

				public _IsolatedStorageFile(IsolatedStorageFile isolatedStorageFile)
				{
					_isolatedStorageFile = isolatedStorageFile;
				}

				public Task CreateDirectoryAsync(string path)
				{
					_isolatedStorageFile.CreateDirectory(path);
					return Task.FromResult(true);
				}

				public Task<bool> GetDirectoryExistsAsync(string path)
				{
					return Task.FromResult(_isolatedStorageFile.DirectoryExists(path));
				}

				public Task<bool> GetFileExistsAsync(string path)
				{
					return Task.FromResult(_isolatedStorageFile.FileExists(path));
				}

				public Task<DateTimeOffset> GetLastWriteTimeAsync(string path)
				{
					return Task.FromResult(_isolatedStorageFile.GetLastWriteTime(path));
				}

				public Task<Stream> OpenFileAsync(string path, FileMode mode, FileAccess access)
				{
					Stream stream = _isolatedStorageFile.OpenFile(path, (System.IO.FileMode)mode, (System.IO.FileAccess)access);
					return Task.FromResult(stream);
				}

				public Task<Stream> OpenFileAsync(string path, FileMode mode, FileAccess access, FileShare share)
				{
					Stream stream = _isolatedStorageFile.OpenFile(path, (System.IO.FileMode)mode, (System.IO.FileAccess)access, (System.IO.FileShare)share);
					return Task.FromResult(stream);
				}
			}
		}
	}
}