public static async Task <HNQuestionRoot> PostQuestion(HNQuestionRoot postQuestion) { HttpClient http = new HttpClient(); ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings; http.DefaultRequestHeaders.Add("Authorization", "Token token=\"" + localSettings.Values["API_Token"] + "\""); string request = _host + "questions"; var c = JsonConvert.SerializeObject(postQuestion, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore }); HttpContent content = new StringContent(c, Encoding.UTF8, "application/json"); var res = await http.PostAsync(request, content); var json = await res.Content.ReadAsStringAsync(); var response = JsonConvert.DeserializeObject <HNQuestionRoot>(json); #region Fix Formatting if (response.question.user.image_url != "missing_thumb.png") { response.question.user.profile_image = new BitmapImage { UriSource = new Uri(response.question.user.image_url) }; } else { response.question.user.profile_image = new BitmapImage { UriSource = _missingImage }; } response.question.text = SetQuestionText(response.question); #endregion return(response); }
private void InitializeCommands() { ToggleMenuCommand = new RelayCommand(() => { App.ViewModelLocator.Shell.IsMenuOpen = !App.ViewModelLocator.Shell.IsMenuOpen; }); PostQuestionCommand = new RelayCommand(async() => { #region Build Question InCall = true; var question = new HNQuestionRoot(); question.question = new HNQuestion(); question.type = QuestionType; var language = SelectedTopic as HNLanguage; #region Type switch (QuestionType) { case "MeaningQuestion": question.question.question_keywords_attributes = new ObservableCollection <HNQuestionKeywordsAttribute>(); question.question.language_id = language.language_id; question.question.prior = 0; question.question.question_keywords_attributes.Add(new HNQuestionKeywordsAttribute { _destroy = 0, name = PrimaryQuestionText }); break; case "ChoiceQuestion": question.question.question_keywords_attributes = new ObservableCollection <HNQuestionKeywordsAttribute>(); question.question.language_id = language.language_id; question.question.prior = 0; question.question.question_keywords_attributes.Add(new HNQuestionKeywordsAttribute { _destroy = 0, name = PrimaryQuestionText }); break; case "DifferenceQuestion": question.question.question_keywords_attributes = new ObservableCollection <HNQuestionKeywordsAttribute>(); question.question.language_id = language.language_id; question.question.prior = 0; question.question.question_keywords_attributes.Add(new HNQuestionKeywordsAttribute { _destroy = 0, name = DifferenceQuestionText1 }); question.question.question_keywords_attributes.Add(new HNQuestionKeywordsAttribute { _destroy = 0, name = DifferenceQuestionText2 }); if (!string.IsNullOrWhiteSpace(DifferenceQuestionText3)) { question.question.question_keywords_attributes.Add(new HNQuestionKeywordsAttribute { _destroy = 0, name = DifferenceQuestionText3 }); } if (!string.IsNullOrWhiteSpace(DifferenceQuestionText4)) { question.question.question_keywords_attributes.Add(new HNQuestionKeywordsAttribute { _destroy = 0, name = DifferenceQuestionText4 }); } break; case "WhatsayQuestion": question.question.question_keywords_attributes = new ObservableCollection <HNQuestionKeywordsAttribute>(); question.question.language_id = language.language_id; question.question.prior = 0; question.question.question_keywords_attributes.Add(new HNQuestionKeywordsAttribute { _destroy = 0, name = PrimaryQuestionText }); break; case "ExampleQuestion": question.question.question_keywords_attributes = new ObservableCollection <HNQuestionKeywordsAttribute>(); question.question.language_id = language.language_id; question.question.prior = 0; question.question.question_keywords_attributes.Add(new HNQuestionKeywordsAttribute { _destroy = 0, name = PrimaryQuestionText }); break; case "FreeQuestion": question.question.question_keywords_attributes = new ObservableCollection <HNQuestionKeywordsAttribute>(); question.question.language_id = language.language_id; question.question.prior = 0; question.question.question_keywords_attributes.Add(new HNQuestionKeywordsAttribute { _destroy = 0, name = PrimaryQuestionText }); break; } #endregion if (!string.IsNullOrWhiteSpace(AdditionalText)) { question.question.supplement = AdditionalText; } #endregion #region Attachments if (UploadImages.Count > 0) { try { var file = await StorageFile.GetFileFromPathAsync(UploadImages[0].UriSource.AbsolutePath); var response = await DataService.UploadAttachment(file, true, false); question.image = new HNImage { id = response.image.id }; } catch (Exception) { await new MessageDialog("We're having trouble uploading that image").ShowAsync(); LoggerService.LogEvent("Image_upload_failed"); } } #endregion HNQuestionRoot result = new HNQuestionRoot(); try { result = await DataService.PostQuestion(question); LoggerService.LogEvent("Question_posted"); InCall = false; App.ViewModelLocator.Question.CurrentQuestion = result.question; App.ViewModelLocator.Question.LoadAnswers((int)result.question.id); Random rnd = new Random(); _navigationService.NavigateTo(typeof(QuestionPage)); } catch (Exception ex) { if (ex is HttpRequestException) { await new MessageDialog("We're having trouble connecting to the HiNative servers").ShowAsync(); } else { await new MessageDialog("There was an error posting this question").ShowAsync(); } LoggerService.LogEvent("Posting_question_failed"); } }); #region Attachments SelectPhotoCommand = new RelayCommand(async() => { var picker = new FileOpenPicker(); picker.ViewMode = PickerViewMode.Thumbnail; picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; picker.FileTypeFilter.Add(".jpg"); picker.FileTypeFilter.Add(".jpeg"); picker.FileTypeFilter.Add(".png"); var imageFile = await picker.PickSingleFileAsync(); if (imageFile != null) { // Application now has read/write access to the picked file Debug.WriteLine("Picked photo: " + imageFile.Name); var uploadImage = new BitmapImage(); FileRandomAccessStream stream = (FileRandomAccessStream)await imageFile.OpenAsync(FileAccessMode.Read); uploadImage.SetSource(stream); UploadImages = new ObservableCollection <BitmapImage> { uploadImage }; } else { Debug.WriteLine("Operation cancelled."); } }); TakePhotoCommand = new RelayCommand(async() => { CameraCaptureUI captureUI = new CameraCaptureUI(); captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg; var imageFile = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo); if (imageFile == null) { return; } IRandomAccessStream stream = await imageFile.OpenAsync(FileAccessMode.Read); BitmapImage image = new BitmapImage(); await image.SetSourceAsync(stream); UploadImages.Clear(); UploadImages = new ObservableCollection <BitmapImage> { image }; }); RecordAudioCommand = new RelayCommand(() => { }); #endregion }