private async Task GetUser(string userToken) { try { var apiresponse = await ApiManager.GetUserByToken(userToken); if (apiresponse.IsSuccessStatusCode || (apiresponse.StatusCode.Equals(HttpStatusCode.NotModified))) { var response = await apiresponse.Content.ReadAsStringAsync(); var json = await Task.Run(() => JsonConvert.DeserializeObject <UserDto>(response)); User = json; } else { await PageDialog.AlertAsync("Unable to retrieve user data", "Error", "OK"); } } catch (Exception ex) { MetricsManager.TrackException("Error getting cowstatus data", ex); await PageDialog.AlertAsync("Unable to retrieve user data", "Error", "OK"); } }
async Task GetSurvey() { var surveyResponse = await ApiManager.GetLatestSurvey(); if (surveyResponse.IsSuccessStatusCode || surveyResponse.StatusCode == HttpStatusCode.NotModified) { try { var content = await surveyResponse.Content.ReadAsStringAsync(); var survey = await Task.Run(() => JsonConvert.DeserializeObject <SurveyDto>(content)); if (survey != null && (App.LatestSurvey == null || App.LatestSurvey.Version < survey.Version)) { var fileHelper = new FileHelper(); App.LatestSurvey = survey; // Save the survey to the local filesystem await fileHelper.SaveAsync(Config.SurveyFileName, survey); } } catch (Exception ex) { MetricsManager.TrackException("Error reading survey json", ex); } } else { await PageDialog.AlertAsync("Unable to retrieve survey data", "Error", "OK"); } }
private async void DoSaveFarm() { //Add Farmtype from picker and user id from UserSettings if (SelectedType != null) { _currentFarm.FarmType_ID = SelectedType.ID; } _currentFarm.User_ID = UserSettings.UserId; FarmValidator validator = new FarmValidator(); ValidationResult result = validator.Validate(_currentFarm); if (result.IsValid) { try { if (!Config.TestMode) { await RunSafe(SaveFarm()); } await NavigationService.GoBackAsync(); } catch (Exception ex) { await PageDialog.AlertAsync("Unable to save farm data", "Error", "OK"); MetricsManager.TrackException("Error saving farm details", ex); } } else { ValidationErrorMessage = result.Errors[0].ErrorMessage; ShowValidationErrors = true; } }
async Task GetSurveyResults() { var serviceResponse = await ApiManager.GetResponseByFarmId(App.SelectedFarm.ID); if (serviceResponse.IsSuccessStatusCode || serviceResponse.StatusCode == HttpStatusCode.NotModified) { try { var response = await serviceResponse.Content.ReadAsStringAsync(); var responseData = await Task.Run(() => JsonConvert.DeserializeObject <List <SurveyResponseDto> >(response)); if (responseData != null && responseData.Count == 1) { App.LatestSurveyResponse = responseData[0]; } else if (responseData != null && responseData.Count == 2) { App.LatestSurveyResponse = responseData[0]; App.PreviousSurveyResponse = responseData[1]; CompareCommand.RaiseCanExecuteChanged(); } } catch (Exception ex) { MetricsManager.TrackException("Error reading survey json", ex); } } }
private void UpdateQuestion(bool value) { //Store answer in collection var properties = new Dictionary <string, string> { { "CurrentQuestionID", CurrentQuestion.ID.ToString() }, }; MetricsManager.TrackEvent("SurveyNextQuestion", properties); // Save the response _response.QuestionResponses.Add( new SurveyQuestionResponseDto { QuestionID = CurrentQuestion.ID, StageID = CurrentQuestion.Stage_ID, QuestionResponse = value, QuestionStatement = CurrentQuestion.QuestionStatement }); //Select next question _questionIndex++; QuestionIncrement++; if (_questionIndex >= App.LatestSurvey?.Questions.Count) { EndSurvey(); } else { //Select View based on stage var lastAnswerIndex = QuestionIndex(_response.QuestionResponses.OrderBy(q => q.QuestionID).Last().QuestionID); if (lastAnswerIndex != null && CurrentQuestion.Stage_ID > Question(lastAnswerIndex.Value).Stage_ID) { //New stage so work out if we need to show the stage view _stageIndex++; //Reset stage question counter QuestionIncrement = 1; var stages = App.LatestSurvey?.Stages; if (stages != null) { ShowStage = stages.Where(q => q.ID == CurrentQuestion.Stage_ID).First().ShowStageIntro; } else { //We should never get here otherwise we have no questions! MetricsManager.TrackException("NoStagesFound", new Exception("No Stages found in LatestSurvey")); } } else { //Same stage so continue with questions ShowStage = false; } UpdateCommands(); _eventAggregator.GetEvent <QuestionChangedEvent>().Publish(); } }
async Task GetData() { //Attempt to check for newer version of Survey, fall back to existing copy as required var surveyResponse = await ApiManager.GetLatestSurvey(); if (surveyResponse.IsSuccessStatusCode) { try { var content = await surveyResponse.Content.ReadAsStringAsync(); var survey = await Task.Run(() => JsonConvert.DeserializeObject <SurveyDto>(content)); if (survey != null && (App.LatestSurvey == null || App.LatestSurvey.Version < survey.Version)) { var fileHelper = new FileHelper(); App.LatestSurvey = survey; // Save the survey to the local filesystem await fileHelper.SaveAsync(Config.SurveyFileName, survey); } } catch (Exception ex) { MetricsManager.TrackException("Error reading survey json", ex); } } var serviceResponse = await ApiManager.GetResponseByFarmId(App.SelectedFarm.ID); if (serviceResponse.IsSuccessStatusCode || serviceResponse.StatusCode == HttpStatusCode.NotModified) { try { var response = await serviceResponse.Content.ReadAsStringAsync(); var responseData = await Task.Run(() => JsonConvert.DeserializeObject <List <SurveyResponseDto> >(response)); if (responseData != null && responseData.Count == 1) { App.LatestSurveyResponse = responseData[0]; OnSummaryCommand.RaiseCanExecuteChanged(); } else if (responseData != null && responseData.Count == 2) { App.LatestSurveyResponse = responseData[0]; App.PreviousSurveyResponse = responseData[1]; OnSummaryCommand.RaiseCanExecuteChanged(); OnCompareCommand.RaiseCanExecuteChanged(); } } catch (Exception ex) { MetricsManager.TrackException("Error reading response json", ex); } } }
private async void InitAsync() { try { await RunSafe(GetFarms()); } catch (Exception ex) { MetricsManager.TrackException("GetFarmsFailed", ex); } }
private async void PopulateCowStatusData() { try { await RunSafe(GetCowStatusData()); } catch (Exception ex) { MetricsManager.TrackException(ex.Message, ex); } }
private async Task PopulateCowStatusData() { Barrel.Current.Empty(key: "GetCowsStatusByFarmID" + App.SelectedFarm.ID); try { await RunSafe(GetCowStatusData()); } catch (Exception ex) { MetricsManager.TrackException(ex.Message, ex); } }
async void InitAsync() { FrameEnabled = false; FrameTextColour = "#cccccc"; try { await RunSafe(GetFarms()); //New user with no farms so direct to add farm page if (FarmList.Count > 1) { var result = await _dialogService.DisplayAlertAsync("No farms found", "Would you like to add a farm?", "Yes", "No"); if (result) { await NavigationService.NavigateAsync("ManageFarmsPage"); } } //If we havent already selected a farm and only a single farm found pre select it if (App.SelectedFarm == null && FarmList.Count == 1) { App.SelectedFarm = FarmList[0]; } //Set up screen based on selected farm if (App.SelectedFarm != null) { EditFarmEnabled = true; SelectedFarm = App.SelectedFarm; _eventAggregator.GetEvent <RootPageRefreshEvent>().Publish(); } else { EditFarmEnabled = false; } } catch (Exception ex) { MetricsManager.TrackException("GetFarmsFailed", ex); } //Load latest survey here try { await RunSafe(GetSurvey()); } catch (Exception ex) { MetricsManager.TrackException("GetFarmsFailed", ex); } }
public void Initialize(INavigationParameters parameters) { if (parameters != null && parameters.ContainsKey("farm")) { try { CurrentFarm = parameters["farm"] as FarmDto; } catch (Exception ex) { MetricsManager.TrackException(ex.Message, ex); } } }
private void Init() { UserDialogs.Instance.ShowLoading(); try { _cowStatusList = App.LatestCowStatusData; BuildCowData(); } catch (Exception ex) { MetricsManager.TrackException("Build CowStatus data failed", ex); } finally { UserDialogs.Instance.HideLoading(); } }
async Task GetCowStatusData() { var tryHistoric = false; var serviceResponse = await ApiManager.GetCowsStatusByFarmID(App.SelectedFarm.ID); if (serviceResponse.IsSuccessStatusCode || serviceResponse.StatusCode == HttpStatusCode.NotModified) { var response = await serviceResponse.Content.ReadAsStringAsync(); var cowStatusData = await Task.Run(() => JsonConvert.DeserializeObject <List <CowStatusDto> >(response)); if (cowStatusData != null && cowStatusData.Count > 0) { App.LatestCowStatusData = new List <CowStatusDto>(cowStatusData); OnSummaryCommand.RaiseCanExecuteChanged(); tryHistoric = true; } } if (tryHistoric) { var previousYear = App.LatestCowStatusData[0].DateAddedCalving.Value.Year - 1; var historicResponse = await ApiManager.GetCowsStatusByFarmIDandYear(App.SelectedFarm.ID, previousYear); if (historicResponse.IsSuccessStatusCode || historicResponse.StatusCode == HttpStatusCode.NotModified) { try { var response = await historicResponse.Content.ReadAsStringAsync(); var cowStatusData = await Task.Run(() => JsonConvert.DeserializeObject <List <CowStatusDto> >(response)); if (cowStatusData != null && cowStatusData.Count > 0) { App.PreviousCowStatusData = new List <CowStatusDto>(cowStatusData); OnCompareCommand.RaiseCanExecuteChanged(); } } catch (Exception ex) { MetricsManager.TrackException(ex.Message, ex); } } } }
private async void InitAsync() { try { await RunSafe(GetFarmTypes()); } catch (Exception ex) { await PageDialog.AlertAsync("Unable to load farm types", "Error", "OK"); MetricsManager.TrackException("Error loading farm types", ex); } if (CurrentFarm.FarmType_ID > 0) { SelectedType = FarmTypes.DefaultIfEmpty(null).Where(x => x.ID == CurrentFarm.FarmType_ID).FirstOrDefault(); } }
private async void EndSurvey() { _response.SubmittedDate = DateTime.Now; DependencyService.Get <IMetricsManagerService>().TrackEvent("SurveyEnded"); try { // Upload and delete the survey upon completion if (!Config.TestMode) { await UploadAndDeleteAsync(); } } catch (Exception ex) { MetricsManager.TrackException("SurveyEndException", ex); } await NavigationService.NavigateAsync("/SdctMasterDetailPage/NavigationPage/SurveyResultsPage"); }
/// <summary> /// Find the index for the given question identifier. /// </summary> /// <param name="questionId">The question identifier.</param> /// <returns>The question index.</returns> private int?QuestionIndex(int?questionId) { var questions = App.LatestSurvey?.Questions; if (questions != null) { for (var i = 0; i < questions.Count; ++i) { if (questions[i].ID == questionId) { return(i); } } } else { //We should never get here otherwise we have no questions! MetricsManager.TrackException("NoQuestionsFound", new Exception("No Questions found in LatestSurvey")); } return(null); }
private List <ChartDataModel> GenerateRadarData(IList <SurveyQuestionResponseDto> answers) { var chartData = new List <ChartDataModel>(); foreach (var stage in App.LatestSurvey.Stages) { //Hardcoded skip of stage 1, this could be a parameter in the stage model instead? if (stage.ID > 1) { try { var score = answers.Where(a => a.StageID == stage.ID && a.QuestionResponse == true).Count(); chartData.Add(new ChartDataModel(stage.StageText, score)); } catch (Exception ex) { MetricsManager.TrackException("Failed to build summary data", ex); } } } return(chartData); }
private async void InitAsync() { string surveyFileName = Config.SurveyFileName; // Check if the survey has already been downloaded var fileHelper = new FileHelper(); if (await fileHelper.ExistsAsync(surveyFileName)) { // Load the survey if it exists App.LatestSurvey = await fileHelper.LoadAsync <SurveyDto>(surveyFileName); } try { // Check if an updated survey is available from the service // Pull existing responses from the database await RunSafe(GetData()); } catch (Exception ex) { MetricsManager.TrackException("GetAssessmentDataFailed", ex); } }
private async void BuildResultData() { PageDialog.ShowLoading("Loading"); if (App.LatestSurveyResponse != null) { _response = App.LatestSurveyResponse; } else { try { await RunSafe(GetSurveyResults()); _response = App.LatestSurveyResponse; } catch (Exception ex) { MetricsManager.TrackException(ex.Message, ex); } } if (_response != null) { var chartData = new List <ChartDataModel>(); var answers = _response.QuestionResponses; var statements = new Dictionary <string, List <string> >(); var isSuitable = true; var lowestScore = 5; foreach (var stage in App.LatestSurvey.Stages) { //Hardcoded skip of stage 1, this could be a parameter in the stage model instead? if (stage.ID > 1) { try { //Add Stage to spidergraph var score = answers.Where(a => a.StageID == stage.ID && a.QuestionResponse == true).Count(); chartData.Add(new ChartDataModel(stage.StageText, score)); if (score <= 2) { isSuitable = false; } if (score < lowestScore) { lowestScore = score; } //Add any statements from questions answered 'No' if (answers.Where(a => a.StageID == stage.ID && a.QuestionResponse == false).Count() > 0) { var stageStatements = new List <string>(); foreach (var answer in answers.Where(a => a.StageID == stage.ID && a.QuestionResponse == false)) { stageStatements.Add(answer.QuestionStatement); } statements.Add(stage.StageText, stageStatements); } } catch (Exception ex) { MetricsManager.TrackException("Failed to build summary data", ex); } } } RadarData = new ObservableCollection <ChartDataModel>(chartData); Statements = new ObservableDictionary <string, List <string> >(statements); if (isSuitable) { SuitabilityStatement = AppTextResource.SurveyResultSdctSuitable; } RadarColour = ReturnHexValue(lowestScore); AssessmentDate = _response.SubmittedDate.ToShortDateString(); } PageDialog.HideLoading(); }