Esempio n. 1
0
        public async void LoginRequested(object sender, EventArgs e)
        { //When login button is pressed enable alerts and transition to MainViewController if right conditions are met
            var bounds = UIScreen.MainScreen.Bounds;

            loader = new LoadingOverlay(bounds, "Checking credentials...");
            View.Add(loader);


            LoginButton.Enabled = false; //Unable to click login button two times or more while loading


            MainViewController viewcontroller = Storyboard.InstantiateViewController("MainViewController") as MainViewController;
            KPITableModel      MySender       = sender as KPITableModel;

            if (viewcontroller != null)
            { //If the view controller is null for somereason, does not transfer
                string username = UsernameTextfield.Text;
                string password = PasswordTextfield.Text;

                Task <HttpResponseMessage> TaskResponse = vdaGateway.VerifyLogin(username, password);

                var response = await TaskResponse as HttpResponseMessage;

                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    //Alert for Invalid Credentials
                    var invalidcontroller = UIAlertController.Create("Invalid Credentails", "Please enter a valid Username and/or Password", UIAlertControllerStyle.Alert);
                    invalidcontroller.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                    PresentViewController(invalidcontroller, true, null);
                }

                else
                {
                    string json_string = await response.Content.ReadAsStringAsync();

                    var verifiedLogin = JsonConvert.DeserializeObject <VerifyLogin>(json_string) as VerifyLogin;

                    if (verifiedLogin == null)
                    {
                        //Alert for Server Error
                        var errorcontroller = UIAlertController.Create("Server Error", "Server response unable to read, please try again later", UIAlertControllerStyle.Alert);
                        errorcontroller.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                        PresentViewController(errorcontroller, true, null);
                    }

                    else
                    {
                        viewcontroller.login_info = verifiedLogin;                          //Pass dealer's name to next view
                        this.NavigationController.PushViewController(viewcontroller, true); //This code changes the view
                    }
                }
            }
            LoginButton.Enabled = true;
            loader.Hide();
        }
        public async void ProcessQuery(object sender, EventArgs e)
        {
            var bounds = UIScreen.MainScreen.Bounds;

            loader = new LoadingOverlay(bounds, "Processing query...");
            View.Add(loader);

            HomeSubmitButton.Enabled = false;

            var kpiViewController = Storyboard.InstantiateViewController("KpiViewController") as KPIViewController;
            var MySender          = sender as KPITableModel;

            //Resets our list variables
            kpiViewController.neededKpi = new List <Kpi>();
            neededKpi = new List <Kpi>();

            relatedKpi = new Kpi();

            //Recieve related kpi
            if (string.IsNullOrEmpty(final_query))
            {
                if (!string.IsNullOrEmpty(Querybox.Text))
                {
                    final_query = Querybox.Text;
                }
                else
                {
                    //Error Alert
                    var errorcontroller = UIAlertController.Create("Error", "Entry required", UIAlertControllerStyle.ActionSheet);
                    errorcontroller.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                    PresentViewController(errorcontroller, true, null); //Display action

                    //new UIAlertView("Error", "Entry required", null, "OK", null).Show();
                    HomeSubmitButton.Enabled = true;
                    loader.Hide();
                    YouAskedLabel.Text = "";
                    final_query        = "";
                    return;
                }
            }

            using (var response = await vdaGateway.RelatedKpi(final_query, login_info.dealer_name) as HttpResponseMessage)
            {
                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
                    {
                        //Error Alert
                        var response1controller = UIAlertController.Create("Server Error", "Chat bot service could not identify relevant KPI for your query", UIAlertControllerStyle.ActionSheet);
                        response1controller.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                        PresentViewController(response1controller, true, null); //Display action
                    }
                    else
                    {
                        //Error Alert
                        var response2controller = UIAlertController.Create("Server Error", "Server status for retrieving your relevant KPI: " + response.StatusCode.ToString(), UIAlertControllerStyle.ActionSheet);
                        response2controller.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                        PresentViewController(response2controller, true, null); //Display action
                    }
                    HomeSubmitButton.Enabled = true;
                    loader.Hide();
                    final_query = "";
                    resetTexts();
                    loader.Hide();
                    return;
                }

                string json_string = response.Content.ReadAsStringAsync().Result;
                relatedKpi = JsonConvert.DeserializeObject <Kpi>(json_string);

                if (relatedKpi == null)
                {
                    //Error Alert
                    var relatedkpicontroller = UIAlertController.Create("Server Error", "Server returned incompatable model for relevant KPIs", UIAlertControllerStyle.ActionSheet);
                    relatedkpicontroller.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                    PresentViewController(relatedkpicontroller, true, null); //Display action

                    HomeSubmitButton.Enabled = true;
                    loader.Hide();
                    final_query = "";
                    resetTexts();
                    loader.Hide();
                    return;
                }

                kpiViewController.relatedKpi = relatedKpi;
            }

            using (var response = await vdaGateway.NeededKpi(login_info.dealer_name))
            {
                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    //Similar to above//////////////////////////////////////////////////////////////////////but want to be cautious of asyncs
                    var response2controller = UIAlertController.Create("Server Error", "Server status for retrieving your relevant KPI: " + response.StatusCode.ToString(), UIAlertControllerStyle.ActionSheet);
                    response2controller.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                    PresentViewController(response2controller, true, null); //Display action

                    HomeSubmitButton.Enabled = true;
                    loader.Hide();
                    final_query = "";
                    resetTexts();
                    return;
                }

                string json_string = response.Content.ReadAsStringAsync().Result;
                neededKpi = JsonConvert.DeserializeObject <List <Kpi> >(json_string);

                if (neededKpi == null)
                {
                    //Similar to above//////////////////////////////////////////////////////////////////////but want to be cautious of asyncs
                    var relatedkpicontroller = UIAlertController.Create("Server Error", "Server returned incompatable model for relevant KPIs", UIAlertControllerStyle.ActionSheet);
                    relatedkpicontroller.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                    PresentViewController(relatedkpicontroller, true, null); //Display action

                    HomeSubmitButton.Enabled = true;
                    final_query = "";
                    resetTexts();
                    loader.Hide();
                    return;
                }

                kpiViewController.neededKpi = neededKpi;
            }
            this.NavigationController.PushViewController(kpiViewController, true); //This code changes the view
            HomeSubmitButton.Enabled = true;
            loader.Hide();
            final_query = "";
            resetTexts();
            loader.Hide();
        }