Skip to content

Instantly share code, notes, and snippets.

@markjulmar
Created August 4, 2016 20:19
Show Gist options
  • Save markjulmar/afecc0a1fc51f37075f8563f4eddcb00 to your computer and use it in GitHub Desktop.
Save markjulmar/afecc0a1fc51f37075f8563f4eddcb00 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using CastMyVote.Models;
using CastMyVote.Services;
using CastMyVote.ViewModels;
using Xamarin.Forms;
namespace CastMyVote
{
public class MainPage2 : ContentPage
{
ISurveyQuestionService service = new MockSurveyQuestionService();
private List<SurveyQuestion> questions;
private List<SurveyChoice> answers;
private string userName;
private Picker questionPicker;
private StackLayout answerGroup;
private SurveyResponse response;
public MainPage2()
{
Title = "Cast My Vote";
ToolbarItems.Add(new ToolbarItem("Show Results", null, OnShowResults));
ToolbarItems.Add(new ToolbarItem("Delete", null, OnDelete));
Entry nameEntry = new Entry();
nameEntry.TextChanged += OnNameChanged;
Content = new StackLayout
{
Spacing = 10,
Padding = 20,
Children =
{
new Label {Text = "Enter your name:"},
nameEntry,
new Label {Text = "Question:"},
(questionPicker = new Picker()),
(answerGroup = new StackLayout() {Spacing = 20, Padding = 20})
}
};
questionPicker.SelectedIndexChanged += OnQuestionChanged;
}
protected override async void OnAppearing()
{
base.OnAppearing();
IsBusy = true;
try
{
// Add the questions
questions = (await service.GetQuestionsAsync()).ToList();
foreach (var q in questions)
questionPicker.Items.Add(q.Text);
questionPicker.SelectedIndex = 0;
}
catch (Exception ex)
{
await this.DisplayAlert("Error", "Failed to download questions: " + ex.Message, "OK");
}
finally
{
IsBusy = false;
}
}
public SurveyQuestion SelectedQuestion
{
get
{
return (questionPicker.SelectedIndex >= 0)
? questions[questionPicker.SelectedIndex]
: null;
}
}
private async void OnQuestionChanged(object sender, EventArgs e)
{
answerGroup.Children.Clear();
if (SelectedQuestion == null)
return;
try
{
IsBusy = true;
answers = (await service.GetChoicesForSurveyAsync(SelectedQuestion.Id)).ToList();
foreach (var answer in answers)
{
Button button = new Button
{
Text = answer.Answer,
BindingContext = answer
};
button.Clicked += OnSelectAnswer;
answerGroup.Children.Add(button);
}
}
catch (Exception ex)
{
await this.DisplayAlert("Error", "Failed to download questions: " + ex.Message, "OK");
}
finally
{
IsBusy = false;
}
}
private async void OnSelectAnswer(object sender, EventArgs e)
{
Debug.Assert(SelectedQuestion != null);
Debug.Assert(string.IsNullOrEmpty(userName) == false);
SurveyChoice answer = (SurveyChoice) ((Button) sender).BindingContext;
response = await service.AddOrUpdateSurveResponseAsync(SelectedQuestion.Id, userName, answer);
SetSelectedAnswer();
}
private async void OnNameChanged(object sender, TextChangedEventArgs e)
{
userName = ((Entry) sender).Text;
answerGroup.IsEnabled = !string.IsNullOrEmpty(userName);
await GetSelectedAnswerAsync();
}
private async Task GetSelectedAnswerAsync()
{
response = !string.IsNullOrEmpty(userName)
? await service.GetResponseForSurveyAsync(SelectedQuestion.Id, userName)
: null;
}
private void SetSelectedAnswer()
{
string responseId = (response == null) ? null : response.ResponseId;
foreach (Button button in answerGroup.Children)
{
var answer = (SurveyChoice) button.BindingContext;
button.BackgroundColor = answer.Id == responseId
? Color.Green
: Color.Default;
}
}
private async void OnDelete()
{
if (response != null)
{
await service.DeleteSurveyResponseAsync(response);
response = null;
SetSelectedAnswer();
}
}
private async void OnShowResults()
{
if (SelectedQuestion == null)
return;
await Navigation.PushAsync(new ResultsPage
{
BindingContext = new ResultsViewModel(service, SelectedQuestion)
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment