private void replacePatient(IUICommand command) { Windows.Data.Json.JsonObject updatedPatient = new Windows.Data.Json.JsonObject(); string patString = command.Id.ToString(); updatedPatient = Windows.Data.Json.JsonObject.Parse(patString); for (uint i = 0; i < patients.Count; i++) { if (updatedPatient["Name"].GetString() == patients.GetObjectAt(i).GetNamedValue("Name").GetString() && updatedPatient["Birthday"].GetString() == patients.GetObjectAt(i).GetNamedValue("Birthday").GetString()) { patients.GetObjectAt(i).SetNamedValue("Education", updatedPatient["Education"]); patients.GetObjectAt(i).SetNamedValue("Sex", updatedPatient["Sex"]); } } }
async void saveUserData() { try { //get file Windows.Storage.StorageFile myFile = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(filename); //read //String data = await Windows.Storage.FileIO.ReadTextAsync(myFile); //save //string allUserDataString = name + ", " + birthday + ", " + age + ", " + sex + ", " + educationLevel; Windows.Data.Json.JsonObject newPatient = new Windows.Data.Json.JsonObject(); newPatient["Doctor"] = Windows.Data.Json.JsonValue.CreateStringValue(docName); newPatient["Name"] = Windows.Data.Json.JsonValue.CreateStringValue(name); newPatient["Birthday"] = Windows.Data.Json.JsonValue.CreateStringValue(birthday.ToString()); newPatient["Age"] = Windows.Data.Json.JsonValue.CreateNumberValue(age); newPatient["Sex"] = Windows.Data.Json.JsonValue.CreateStringValue(sex.ToString()); newPatient["Education"] = Windows.Data.Json.JsonValue.CreateStringValue(educationLevel); //check if user is already saved bool addNew = true; for (uint i=0; i < patients.Count; i++){ if (patients.GetObjectAt(i).GetNamedValue("Name").GetString() == newPatient["Name"].GetString() && patients.GetObjectAt(i).GetNamedValue("Birthday").GetString() == newPatient["Birthday"].GetString()) { //patient info already saved // Create the message dialog and set its content var messageDialog = new MessageDialog("We already have a patient with that name and birthday. Do you want to replace the existing patient?"); // Add commands and set their callbacks string jsonPatient = newPatient.Stringify(); messageDialog.Commands.Add(new UICommand("Replace Old Patient", new UICommandInvokedHandler(this.replacePatient), jsonPatient)); messageDialog.Commands.Add(new UICommand("Keep Original Patient", new UICommandInvokedHandler(this.doNothing))); // Set the command that will be invoked by default messageDialog.DefaultCommandIndex = 0; // Set the command to be invoked when escape is pressed messageDialog.CancelCommandIndex = 1; // Show the message dialog await messageDialog.ShowAsync(); addNew = false; } } if(addNew) { patients.Add(newPatient); } string jsonAllPatientsString = patients.Stringify(); await Windows.Storage.FileIO.WriteTextAsync(myFile, jsonAllPatientsString); //await Windows.Storage.FileIO.WriteTextAsync(myFile, ""); //uncomment this to erase database } catch (FileNotFoundException) { //file did not exist so create it - SHOULD NEVER HAPPEN because of "openIfExists" Popup p = new Popup(); TextBlock t = new TextBlock(); t.Text = "Error: file could not be opened"; p.Child = t; create_file(); } }