async void OnCloseButtonClicked(object sender, EventArgs e) { var answer = await DisplayAlert("Close poll", "Are you sure you want to close this poll?", "Yes", "No"); if (answer) { //If user confirm to close the poll, send the HTTP request activityIndicator.IsVisible = true; activityIndicator.IsRunning = true; string httpTask = await Task.Run <string>(() => HttpRequestHandler.PostClosePoll(poll.pollID, adminAuth.Username, adminAuth.Password)); string httpResult = httpTask.ToString(); activityIndicator.IsVisible = false; activityIndicator.IsRunning = false; if (httpResult == "Successfully closed poll!") { await DisplayAlert("Success", "Poll has been successfully closed!", "OK"); var page = App.Current.MainPage as rootPage; var pollPage = new pollPage(); page.changePage(pollPage); } else { await DisplayAlert("Failed", httpResult, "OK"); } } }
async void OnSubmitButtonClicked(object sender, EventArgs e) { if (selectedOption == "") { await DisplayAlert("Failed", "Please select an answer", "OK"); } else { //Submit user's aswer for poll activityIndicator.IsVisible = true; activityIndicator.IsRunning = true; string optionID = ""; foreach (pollOption option in poll.pollOptions) { if (selectedOption == option.optionTitle) { optionID = option.optionID; } } string httpTask = await Task.Run <string>(() => HttpRequestHandler.PostSubmitPollAnswer(poll.pollID, optionID, userSession.username, "")); string httpResult = httpTask.ToString(); activityIndicator.IsVisible = false; activityIndicator.IsRunning = false; if (httpResult == "Answer has been successfully submitted. Thank you for participating!") { await DisplayAlert("Success", httpResult, "OK"); var page = App.Current.MainPage as rootPage; var pollPage = new pollPage(); page.changePage(pollPage); } else { await DisplayAlert("Failed", httpResult, "OK"); } } }
async void OnDeleteButtonClicked(object sender, EventArgs e) { if (adminAuth.Username != null && adminAuth.Password != null) { var answer = await DisplayAlert("Delete", "Are you sure you want to delete the option?", "Yes", "No"); Button deleteButton = (Button)sender; string optionID = deleteButton.StyleId; if (answer) { //If user confirm to delete the option, send the HTTP request activityIndicator.IsVisible = true; activityIndicator.IsRunning = true; string httpTask = await Task.Run <string>(() => HttpRequestHandler.PostDeleteOption(adminAuth.Username, adminAuth.Password, optionID)); string httpResult = httpTask.ToString(); activityIndicator.IsVisible = false; activityIndicator.IsRunning = false; if (httpResult == "Poll option successfully deleted!") { await DisplayAlert("Success", "Poll option has been successfully deleted!", "OK"); var page = App.Current.MainPage as rootPage; var pollPage = new pollPage(); page.changePage(pollPage); } else { await DisplayAlert("Failed", httpResult, "OK"); } } } }
async void OnPublishButtonClicked(object sender, EventArgs e) { bool isErrorPresent = false; string errorMessage = ""; string pollQuestion = ""; //Validate inputs if (pollQuestionInput.Text == "") { isErrorPresent = true; errorMessage = "Please enter poll question. "; } if (selectedOption == "") { isErrorPresent = true; errorMessage = "Please enter number of poll option. "; } //Get option inputs string[] options = new string[optionListLayout.Children.Count()]; int count = 0; //Obtain the option inputs foreach (StackLayout layout in optionListLayout.Children) { foreach (View view in layout.Children) { if (view.GetType() == typeof(Frame)) { if (((Frame)view).Content.GetType() == typeof(Entry)) { Entry entry = (Entry)((Frame)view).Content; options[count] = entry.Text; count++; } } } } if (options.Count() <= 0) { isErrorPresent = true; errorMessage = "Please enter poll option fields. "; } if (!isErrorPresent) { if (adminAuth.Username != null && adminAuth.Password != null) { activityIndicator.IsVisible = true; activityIndicator.IsRunning = true; string httpTask = await Task.Run <string>(() => HttpRequestHandler.PostAddPoll(adminAuth.Username, adminAuth.Password, pollQuestionInput.Text, selectedOption, options)); string httpResult = httpTask.ToString(); activityIndicator.IsVisible = false; activityIndicator.IsRunning = false; if (httpResult == "You have succesfully published a poll!") { await DisplayAlert("Success", "Poll has been successfully published!", "OK"); var page = App.Current.MainPage as rootPage; var pollPage = new pollPage(); page.changePage(pollPage); } else { await DisplayAlert("Failed", httpResult, "OK"); } } else { Console.WriteLine("User is not logged in"); } } else { await DisplayAlert("Failed", errorMessage, "OK"); } }