summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls/App.cs
blob: 53424cce73ae77ec99b50df839e504579430215e (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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;

namespace Xamarin.Forms.Controls
{
	public class AppLifeCycle : Application
	{
		protected override void OnStart ()
		{
			base.OnStart ();
		}

		protected override void OnSleep ()
		{
			base.OnSleep ();
		}

		protected override void OnResume ()
		{
			base.OnResume ();
		}

		public AppLifeCycle ()
		{
			MainPage = new ContentPage {
				Content = new Label {
					Text = "Testing Lifecycle events"
				}
			};
		}
	}

	public class SimpleApp : Application
	{
		Label label;

		public SimpleApp ()
		{
			label = new Label { VerticalOptions = LayoutOptions.CenterAndExpand };

			if (Current.Properties.ContainsKey ("LabelText")) {
				label.Text = (string) Current.Properties["LabelText"] + " Restored!";
				Debug.WriteLine ("Initialized");
			} else {
				Current.Properties["LabelText"] = "Wowza";
				label.Text = (string) Current.Properties["LabelText"] + " Set!";
				Debug.WriteLine ("Saved");
			}

			MainPage = new ContentPage {
				Content = new StackLayout {
					Children = { 
						label 
					} 
				} 
			};

			SerializeProperties ();
		}

		async void SerializeProperties ()
		{
			await Current.SavePropertiesAsync ();
		}
	}

	public class App : Application
	{
		public string InsightsApiKey = Secrets["InsightsApiKey"];
		public const string AppName = "XamarinFormsControls";
		public const string AppVersion = "1.4.3";
		public static int IOSVersion = -1;
		readonly ITestCloudService _testCloudService;
		// make sure serialized data is available here

		//protected override void OnStart ()
		//{
		//	// called right after property store is populated
		//	MainPage.BackgroundColor = Color.Green;
		//	Current.Properties["TimesStarted"] = ((int)Current.Properties["TimesStarted"]) + 1;
		//	((MainPageLifeCycleTests)MainPage).UpdateLabels ();
		//}

		//protected override void OnResume ()
		//{
		//	MainPage.BackgroundColor = Color.White;
		//	Current.Properties["TimesResumed"] = ((int)Current.Properties["TimesResumed"]) + 1;
		//	((MainPageLifeCycleTests)MainPage).UpdateLabels ();
		//}

		//protected override void OnSleep ()
		//{
		//	MainPage.BackgroundColor = Color.Red;
		//	Current.Properties["TimesSlept"] = ((int)Current.Properties["TimesSlept"]) + 1;
		//	((MainPageLifeCycleTests)MainPage).UpdateLabels ();
		//}
		public static List<string> AppearingMessages = new List<string>();

		public static ContentPage MenuPage { get; set; }

		

		public App ()
		{
			_testCloudService = DependencyService.Get<ITestCloudService> ();
			InitInsights ();
			// MainPage = new MainPageLifeCycleTests ();
			MainPage = new MasterDetailPage () {
				Master = new ContentPage { Title = "Master", BackgroundColor = Color.Red },
				Detail = CoreGallery.GetMainPage ()
			};
		}

		public void SetMainPage (Page rootPage)
		{
			MainPage = rootPage;
		}

		void InitInsights ()
		{
			if (Insights.IsInitialized) {
				Insights.ForceDataTransmission = true;
				if (_testCloudService != null && _testCloudService.IsOnTestCloud ())
					Insights.Identify (_testCloudService.GetTestCloudDevice (), "Name", _testCloudService.GetTestCloudDeviceName ());
				else
					Insights.Identify ("DemoUser", "Name", "Demo User");
			}
		}

		public static Assembly GetAssembly (out string assemblystring)
		{
			assemblystring = typeof(App).AssemblyQualifiedName.Split (',')[1].Trim ();
			var assemblyname = new AssemblyName (assemblystring);
			return Assembly.Load (assemblyname);
		}

		public static async Task<string> LoadResource (string filename)
		{
			string assemblystring;
			Assembly assembly = GetAssembly (out assemblystring);

			Stream stream = assembly.GetManifestResourceStream ($"{assemblystring}.{filename}");
			string text;
			using (var reader = new StreamReader (stream)) {
				text = await reader.ReadToEndAsync ();
			}
			return text;
		}

		public static void InitSecrets ()
		{
			secrets = new Dictionary<string, string> ();

			string keyData = LoadResource ("secrets.txt").Result;
			string[] entries = keyData.Split ("\n\r".ToCharArray (), StringSplitOptions.RemoveEmptyEntries);
			foreach (string entry in entries) {
				string[] parts = entry.Split (':');
				if (parts.Length < 2) {
					continue;
				}

				secrets.Add (parts[0].Trim (), parts[1].Trim ());
			}
		}

		private static Dictionary<string, string> secrets;

		public static Dictionary<string, string> Secrets
		{
			get
			{
				if (secrets == null) {
					InitSecrets ();
				}

				return secrets;
			}
		} 
	}

	// Not quite sure how to turn this into a test case, effectively replace the normal Application with this to repro issues reported.
	// Full repro requires assignment to MainPage, hence the issue
	public class NavReproApp : Application
	{
		NavigationPage navPage1 = new NavigationPage ();

		public NavReproApp ()
		{

			var btn = new Button () { Text = "Start" };

			btn.Clicked += Btn_Clicked;

			navPage1.PushAsync (new ContentPage () { Content = btn });

			MainPage = navPage1;

		}

		async void Btn_Clicked (object sender, EventArgs e)
		{
			await navPage1.PushAsync (new ContentPage () { Content = new Label () { Text = "Page 2" } });
			await navPage1.PushAsync (new ContentPage () { Content = new Label () { Text = "Page 3" } });


			var navPage2 = new NavigationPage ();

			var btn = new Button () { Text = "Start Next" };
			btn.Clicked += Btn_Clicked1;

			await navPage2.PushAsync (new ContentPage () { Content = btn });

			MainPage = navPage2;


		}

		async void Btn_Clicked1 (object sender, EventArgs e)
		{
			MainPage = navPage1;
			await navPage1.PopAsync ();


			await navPage1.PushAsync (new ContentPage () { Content = new Label () { Text = "Page 3a" } });
		}

		protected override void OnStart ()
		{
			// Handle when your app starts
		}

		protected override void OnSleep ()
		{
			// Handle when your app sleeps
		}

		protected override void OnResume ()
		{
			// Handle when your app resumes
		}
	}

	public class MainPageLifeCycleTests : ContentPage
	{
		int numTimesStarted;
		int numTimesSlept;
		int numTimesResumed;

		StackLayout numTimesStartedLayout;
		StackLayout numTimesSleptLayout;
		StackLayout numTimesResumedLayout;

		public MainPageLifeCycleTests ()
		{
			object timesStarted;
			if (!Application.Current.Properties.TryGetValue ("TimesStarted", out timesStarted)) {
				Application.Current.Properties["TimesStarted"] = 0;
			} 
			var numTimesStarted = (int)Application.Current.Properties["TimesStarted"];
			

			object timesSlept;
			if (!Application.Current.Properties.TryGetValue ("TimesSlept", out timesSlept)) {
				Application.Current.Properties["TimesSlept"] = 0;
			}
			var numTimesSlept = (int)Application.Current.Properties["TimesSlept"];
	

			object timesResumed;
			if (!Application.Current.Properties.TryGetValue ("TimesResumed", out timesResumed)) {
				Application.Current.Properties["TimesResumed"] = 0;
			}
			var numTimesResumed = (int)Application.Current.Properties["TimesResumed"];

			numTimesStartedLayout = BuildLabelLayout ("TimesStarted", numTimesStarted);
			numTimesSleptLayout = BuildLabelLayout ("TimesSlept", numTimesSlept);
			numTimesResumedLayout = BuildLabelLayout ("TimesResumed", numTimesResumed);

			var layout = new StackLayout {
				Children = {
					numTimesStartedLayout,
					numTimesSleptLayout,
					numTimesResumedLayout
				}
			};

			Content = layout;
		}

		StackLayout BuildLabelLayout (string title, int property)
		{
			var labelTitle = new Label {
				Text = title
			};

			var valueLabel = new Label {
				Text = property.ToString ()
			};

			return new StackLayout {
				Children = {
					labelTitle,
					valueLabel
				}
			};
		}

		public void UpdateLabels ()
		{
			((Label)numTimesStartedLayout.Children[1]).Text = ((int)Application.Current.Properties["TimesStarted"]).ToString ();
			((Label)numTimesSleptLayout.Children[1]).Text = ((int)Application.Current.Properties["TimesSlept"]).ToString ();
			((Label)numTimesResumedLayout.Children[1]).Text = ((int)Application.Current.Properties["TimesResumed"]).ToString ();
		}
	}
}