//--------------------------------------------------------------------------------------------------------------------- void SaveTap(object sender, EventArgs e) { int MaxQuestionLength = 150; int MaxAnswerLength = 45; string errorToShow; string errorDetails; //If any text fields are left blank raise an error if (QuestionField.Text.Trim() == @"" || CorrectAnswer.Text.Trim() == @"" || false1.Text.Trim() == @"" || false2.Text.Trim() == @"" || false3.Text.Trim() == @"") { errorToShow = "Cannot save question"; errorDetails = "Text fields left blank"; ErrorMessage(errorToShow, errorDetails); return; } //If any of the text fields are over the charcater limit, raise error if (QuestionField.Text.Length > MaxQuestionLength) { errorToShow = "Question too long"; errorDetails = "Question exceeds 150 character limit"; ErrorMessage(errorToShow, errorDetails); return; } else if (CorrectAnswer.Text.Length > MaxAnswerLength || false1.Text.Length > MaxAnswerLength || false2.Text.Length > MaxAnswerLength || false3.Text.Length > MaxAnswerLength) { errorToShow = "Answer too long"; errorDetails = "Answers must be less than 45 charcaters"; ErrorMessage(errorToShow, errorDetails); return; } //If the user is editing an already existing question, save the edits if (DataToLoad != null) { DataToLoad.question = QuestionField.Text; DataToLoad.answer = CorrectAnswer.Text; DataToLoad.falseQ1 = false1.Text; DataToLoad.falseQ2 = false2.Text; DataToLoad.falseQ3 = false3.Text; DataToLoad.DateCreated = DateTime.Now; //Remove old question var itemToRemove = TriviaQuestonsList.Single(r => r.QuestionID == DataToLoad.QuestionID); TriviaQuestonsList.Remove(itemToRemove); //Add edited question TriviaQuestonsList.Add(DataToLoad); //Save to json file var myJson = JsonConvert.SerializeObject(TriviaQuestonsList); using (var streamwriter = new StreamWriter(AppDelegate.triviaPathFile, false)) { streamwriter.Write(myJson); } } else { //Create new question TriviaQuestionsRecord NewQuestion = new TriviaQuestionsRecord(QuestionField.Text, CorrectAnswer.Text, false1.Text, false2.Text, false3.Text); //Add to list TriviaQuestonsList.Add(NewQuestion); //Save to json file var myJson = JsonConvert.SerializeObject(TriviaQuestonsList); using (var streamwriter = new StreamWriter(AppDelegate.triviaPathFile, false)) { streamwriter.Write(myJson); } } NavigationController.PopViewController(true); }
public override void ViewDidLoad() { base.ViewDidLoad(); //Add navigation bar title //NavigationItem.Title = "Trivia Mania"; NavigationController.NavigationBar.BackgroundColor = UIColor.Purple; if (File.Exists(AppDelegate.triviaPathFile) == false) { //Starter Questions TriviaQuestionsRecord Q1 = new TriviaQuestionsRecord("Which body of land is not a contient?", "Middle East", "Asia", "Antartica", "Europe"); TriviaQuestionsRecord Q2 = new TriviaQuestionsRecord("What day of the year is Christmas?", "December 25th", "December 8th", "July 4th", "I'm running out of ideas"); TriviaQuestionsRecord Q3 = new TriviaQuestionsRecord("What does the fox say?", "Ring-ding-ding-dingeringeding!", "Woof", "Meow", "Toot Toot"); TriviaQuestionsRecord Q4 = new TriviaQuestionsRecord("How many days are in a year?", "365", "300", "465", "52"); TriviaQuestionsRecord Q5 = new TriviaQuestionsRecord("What is Overwatch?", "A video game", "A movie", "A TV show", "A book"); TriviaQuestionsRecord Q6 = new TriviaQuestionsRecord("Which is not a Pokemon?", "Maurosaur", "Squirtle", "Mewtwo", "Pikachu"); TriviaQuestionsRecord Q7 = new TriviaQuestionsRecord("What is the capitol of China?", "Beijing", "Shanghai", "Tokyo", "Washington DC"); TriviaQuestionsRecord Q8 = new TriviaQuestionsRecord("What in not a monster from Monster Hunter?", "Groudon", "Rathalos", "Lagiacrus", "Gore Magala"); TriviaQuestionsRecord Q9 = new TriviaQuestionsRecord("How many Oceans are there?", "5", "7", "10", "1"); TriviaQuestionsRecord Q10 = new TriviaQuestionsRecord("Who's the secretary of state?", "Rex Tillerson", "Obama", "Bilitski", "Jeff Sessions"); TriviaQuestionsRecord Q11 = new TriviaQuestionsRecord("Who was the first Nasa astronaut to visit space twice?", "Gus Grissom", "Neil Armstrong", "Franz Viehbock", "Bilitski"); TriviaQuestionsRecord Q12 = new TriviaQuestionsRecord("2 + 2 = ?", "4", "3", "5", "Math is hard"); TriviaQuestionsRecord Q13 = new TriviaQuestionsRecord("What is the sixth planet from the sun?", "Saturn", "Jupiter", "Earth", "There are only 5 planets"); TriviaQuestionsRecord Q14 = new TriviaQuestionsRecord("When was the movie It's a Wonderful Life released?", "1946", "1949", "1996", "1962"); TriviaQuestionsRecord Q15 = new TriviaQuestionsRecord("What is the correct way to write to the console in C#", "Console.WriteLine(\"Hello\");", "Put(\"Hello\");", "cout << \"Hello\";", "System.PrintLn(\"Hello\");"); StarterQuestionsList.Add(Q1); StarterQuestionsList.Add(Q2); StarterQuestionsList.Add(Q3); StarterQuestionsList.Add(Q4); StarterQuestionsList.Add(Q5); StarterQuestionsList.Add(Q6); StarterQuestionsList.Add(Q7); StarterQuestionsList.Add(Q8); StarterQuestionsList.Add(Q9); StarterQuestionsList.Add(Q10); StarterQuestionsList.Add(Q11); StarterQuestionsList.Add(Q12); StarterQuestionsList.Add(Q13); StarterQuestionsList.Add(Q14); StarterQuestionsList.Add(Q15); //Write everything to the file var myJson = JsonConvert.SerializeObject(StarterQuestionsList); using (var streamwriter = new StreamWriter(AppDelegate.triviaPathFile, false)) { streamwriter.Write(myJson); } } if (File.Exists(AppDelegate.highScorePathFile) == false) { //Write everything to the file using (var streamwriter = new StreamWriter(AppDelegate.highScorePathFile, false)) { streamwriter.Write(0); } } }
public EnterDataViewController(TriviaQuestionsRecord QuestionObj, List <TriviaQuestionsRecord> templist) : base("EnterDataViewController", null) { TriviaQuestonsList = templist; DataToLoad = QuestionObj; }