Example #1
0
		public async Task POSTRequest(string action, TKDataForm dataForm)
		{
			if (false == Monitor.TryEnter(LoginView.POSTLock)) {
				return;
			}

			var json = new Json();

			var inUsername = (NSString)((TKDataFormEntityDataSource)dataForm.DataSource).EntityModel.PropertyWithName ("Username").Value.ToString();
			var inPassword = (NSString)((TKDataFormEntityDataSource)dataForm.DataSource).EntityModel.PropertyWithName ("Password").Value.ToString();
			var inVerify = (NSString)((TKDataFormEntityDataSource)dataForm.DataSource).EntityModel.PropertyWithName ("Verify").Value.ToString();
			var mode = ((NSNumber)((TKDataFormEntityDataSource)dataForm.DataSource).EntityModel.PropertyWithName ("Mode").Value).Int32Value;
			var inQuestion = (NSString)((TKDataFormEntityDataSource)dataForm.DataSource).EntityModel.PropertyWithName ("Question").Value.ToString();
			var inAnswer = (NSString)((TKDataFormEntityDataSource)dataForm.DataSource).EntityModel.PropertyWithName ("Answer").Value.ToString();

			var isRegister = 1 == mode ? true : false;

			if (String.IsNullOrWhiteSpace(inUsername)) {
				DisplayAlert ("Alert", "Username required", "OK");
				Monitor.Exit(LoginView.POSTLock);
				return;
			}

			if (String.IsNullOrWhiteSpace(inPassword)) {
				DisplayAlert ("Alert", "Password required", "OK");
				Monitor.Exit(LoginView.POSTLock);
				return;
			}

			if (isRegister) {
				if (String.IsNullOrWhiteSpace(inVerify)) {
					DisplayAlert ("Alert", "Verify required", "OK");
					Monitor.Exit(LoginView.POSTLock);
					return;
				}

				if (inVerify != inPassword) {
					DisplayAlert ("Alert", "Passwords much match", "OK");
					Monitor.Exit(LoginView.POSTLock);
					return;
				}

				if (null == inQuestion) {
					DisplayAlert ("Alert", "Please select a security question", "OK");
					Monitor.Exit(LoginView.POSTLock);
					return;
				}

				if (String.IsNullOrWhiteSpace(inQuestion)) {
					DisplayAlert ("Alert", "Please select a security question", "OK");
					Monitor.Exit(LoginView.POSTLock);
					return;
				}

				if (String.IsNullOrWhiteSpace (inAnswer)) {
					DisplayAlert ("Alert", "Security answer required", "OK");
					Monitor.Exit(LoginView.POSTLock);
					return;
				}
			}				

			var appDelegate = UIApplication.SharedApplication.Delegate as AppDelegate;

			var notificationView = new GCDiscreetNotificationView (
				text: isRegister ? "Signing up" : "Logging in",
				activity: false,
				presentationMode: GCDNPresentationMode.Bottom,
				view: appDelegate.navigationController.View
			);

			notificationView.SetShowActivity(true, true);
			notificationView.Show (true);

			json.ContentType = "application/json";
			var data = new Dictionary<string, string>();

			data ["username"] = inUsername;
			data ["password"] = inPassword;

			if (isRegister) {
				data ["question"] = inQuestion;
				data ["answer"] = inAnswer;
			}

			try {
				json.PostData = Newtonsoft.Json.JsonConvert.SerializeObject (data);

				var endpoints = new Dictionary<string, string>();
				endpoints.Add("signin", String.Concat(Globals.EndpointBase, "/api/v1/account/signin"));
				endpoints.Add("register", String.Concat(Globals.EndpointBase, "/api/v1/account/register"));
				var endpoint = endpoints[action];

				await json.PostAsync(endpoint);

				json.XPath = "/json/status";
				if ("\"success\"" == json.XText) {
					json.XPath = "/json/data/api_key";

					var s = new Settings { 
						API_Key = json.XText.Replace("\"", ""),
						username = inUsername,
					};
					Globals.SQLite.Insert (s);

					var settings = Globals.SQLite.Table<Settings>().Where (v => v.API_Key != null);
					Globals.theSettings = settings.First();

					appDelegate.navigationController.PopToRootViewController(true);
					appDelegate.navigationController.ViewControllers = new List<UIViewController> { new TodoListView() }.ToArray();
				}
				else {
					json.XPath = "/json/data/message";
					var newStr = json.XText.Replace("\"", "");
					DisplayAlert("Alert", newStr, "OK");
				}
			}
			catch (Exception e) {
				var msgs = new Dictionary<string, string>();
				msgs.Add("signin", "Unable to signin");
				msgs.Add("register", "Unable to register");
				var msg = msgs[action];

				DisplayAlert ("Alert", msg, "OK");
			}

			notificationView.Hide (true);

			Monitor.Exit(LoginView.POSTLock);
		}