async private Task <Boolean> UpdateChild(Child child) { Context CurrentContext; ContextDatabaseAccess contextDB = new ContextDatabaseAccess(); contextDB.InitializeSync(); try { CurrentContext = contextDB.GetContextSync(); } // Can't find definitions for SQLiteNetExtensions exceptions, so catch generic Exception e and assume there is no context. catch (Exception e) { CurrentContext = null; } if (CurrentContext == null) { if (child == null) { CurrentContext = new Context(-1, Language.ENGLISH, new Units(DistanceUnits.IN, WeightUnits.OZ)); } else { CurrentContext = new Context(child.GetID(), Language.ENGLISH, new Units(DistanceUnits.IN, WeightUnits.OZ)); } NotifyPagesOfChildUpdate(child); contextDB.SaveContextSync(CurrentContext); contextDB.CloseSyncConnection(); return(true); } else { if (child == null) { CurrentContext.ChildId = -1; } else { CurrentContext.ChildId = child.GetID(); } NotifyPagesOfChildUpdate(child); contextDB.SaveContextSync(CurrentContext); contextDB.CloseSyncConnection(); return(true); } }
async void AddChildButton_Clicked(object sender, EventArgs e) { string nameEntered = nameEntry.Text; DateTime birthdayEntered = birthdayEntry.Date; Gender genderSelected = newChildGender; Boolean missingInfo = false; int index = SexEntry.SelectedIndex; string sex; if (nameEntered == null || nameEntered == "") { await DisplayAlert("Failed", "Please Enter your child's Name", "OK"); missingInfo = true; } else if (birthdayEntered == null) { await DisplayAlert("Failed", "Please Enter your child's birthday.", "OK"); missingInfo = true; } else if (SexEntry.SelectedIndex == -1) { await DisplayAlert("Failed", "Please Enter your child's sex.", "OK"); missingInfo = true; } if (missingInfo) { return; } else { sex = (string)SexEntry.ItemsSource[index]; if (sex == "Male") { newChildGender = Gender.MALE; } else { newChildGender = Gender.FEMALE; } ChildDatabaseAccess childDatabase = new ChildDatabaseAccess(); childDatabase.InitializeSync(); Child newChild = new Child(nameEntered, birthdayEntered, newChildGender); childDatabase.SaveUserChildSync(newChild); childDatabase.CloseSyncConnection(); ContextDatabaseAccess contextDB = new ContextDatabaseAccess(); contextDB.InitializeSync(); Context context = contextDB.GetContextSync(); context.ChildId = newChild.ID; contextDB.SaveContextSync(context); contextDB.CloseSyncConnection(); } await Navigation.PopModalAsync(); }