private void btnMain_Click(object sender, RoutedEventArgs e) { // Save to global data. DailyCalories.demoMode = (bool)cbDemo.IsChecked; DailyCalories.demoStartDate = (DateTime)dpStartDate.Value; DailyCalories.demoLocalLogin = (bool)cbLogin.IsChecked; if (cbDemo.IsChecked == true) { // Check for local login, handle accordingly. if (cbLogin.IsChecked == false) { // this is really not enough, a lot more needs to be unwound // here besides setting this object to null. (Application.Current as DailyCals.App).ProfileDataObject = null; } else { RemoteProfileRecordV2 profile = new RemoteProfileRecordV2(); profile.email = "*****@*****.**"; profile.birthdate = Convert.ToDateTime("12/12/1976"); profile.startdate = DailyCalories.demoStartDate; profile.goaldate = profile.startdate.AddDays(180); profile.isfemale = 1; profile.activitylevel = 1.0; profile.profilep = "demo"; if (rbStandard.IsChecked == true) { profile.height = 67; profile.startweight = 215; profile.goalweight = 195; profile.isstandard = 1; } else { profile.height = 170; profile.startweight = 97.5; profile.goalweight = 88.5; profile.isstandard = 0; } (Application.Current as DailyCals.App).ProfileDataObject = profile; (Application.Current as DailyCals.App).SaveProfileDataToIsolatedStorage(); DailyCalories.UpdateWeightDB(profile); } } // Navigate to MainPage.xaml. DBs will reload. NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); }
private void SaveProfileToStateObject(string filename, RemoteProfileRecordV2 profile) { IDictionary<string, object> stateStore = PhoneApplicationService.Current.State; stateStore.Remove(filename); stateStore.Add(filename, profile); }
private bool LoadProfileFromStateObject(string filename, out RemoteProfileRecordV2 result) { IDictionary<string, object> stateStore = PhoneApplicationService.Current.State; result = null; if (!stateStore.ContainsKey(filename)) { return false; } result = (RemoteProfileRecordV2)stateStore[filename]; return true; }
private void btnUpdateProfile_Click(object sender, RoutedEventArgs e) { // Get the application's profile data object. // TODO: need to consolidate the profile obj in PopulteForm //_profile = (Application.Current as DailyCals.App).ProfileDataObject; // Is this profile new? bool isNewProfile = false; // OK to update profile? bool isOkToUpdate = true; // Dirty bit. bool dirty_bit = false; // Date used for total days calculation. DateTime startDate = DailyCalories.GetToday(); if (_profile == null) { // user has not created his or her profile yet. // create new record, and set flag. _profile = new RemoteProfileRecordV2(); isNewProfile = true; } else { // Since there exists a profile record, use the setup // date as the dateForCalculations. startDate = _profile.startdate; } // Birthdate validation. if (DataValidation.IsValidBirthdate(dpBirthdate)) { DateTime birthdate = (DateTime)dpBirthdate.Value; if (_profile.birthdate != birthdate) { _profile.birthdate = birthdate; dirty_bit = true; } txtBirthdateMsg.Text = ""; } else { isOkToUpdate = false; txtBirthdateMsg.Text = "Chk birthdate."; } // Validate height. if (DataValidation.IsValidHeight(tbHeight)) { double height = Convert.ToDouble(tbHeight.Text); if (_profile.height != height) { _profile.height = height; dirty_bit = true; } txtHeightMsg.Text = ""; } else { isOkToUpdate = false; txtHeightMsg.Text = "Chk height."; } // Is user female? if ((bool)rbFemale.IsChecked) { if (_profile.isfemale == 0) { _profile.isfemale = 1; dirty_bit = true; } } else { if (_profile.isfemale == 1) { _profile.isfemale = 0; dirty_bit = true; } } // Validate start weight. if (DataValidation.IsValidWeight(tbStartWeight)) { double startweight = Convert.ToDouble(tbStartWeight.Text); if (_profile.startweight != startweight) { _profile.startweight = startweight; dirty_bit = true; } txtStartWeightMsg.Text = ""; } else { isOkToUpdate = false; txtStartWeightMsg.Text = "Weight must be greater than 0."; } // Validate goal weight. if (DataValidation.IsValidWeight(tbGoalWeight)) { double goalweight = Convert.ToDouble(tbGoalWeight.Text); if (_profile.goalweight != goalweight) { _profile.goalweight = goalweight; dirty_bit = true; } txtGoalWeightMsg.Text = ""; } else { isOkToUpdate = false; txtGoalWeightMsg.Text = "Weight must be greater than 0."; } // Validate start weight > goal weight. This happens only // after both weights have already been validated first. if (isOkToUpdate) { if (DataValidation.IsStartWeightGreaterThanGoalWeight(tbStartWeight, tbGoalWeight)) { txtGoalWeightMsg.Text = ""; } else { isOkToUpdate = false; txtStartWeightMsg.Text = "Start weight must be larger than goal."; } } // Standard units? if ((bool)rbStandard.IsChecked) { if (_profile.isstandard == 0) { _profile.isstandard = 1; dirty_bit = true; } } else { if (_profile.isstandard == 1) { _profile.isstandard = 0; dirty_bit = true; } } // Validate goal date. if (DataValidation.IsValidGoalDate(dpGoalDate)) { DateTime goaldate = (DateTime)dpGoalDate.Value; if (_profile.goaldate != goaldate) { _profile.goaldate = goaldate; dirty_bit = true; } txtGoalDateMsg.Text = ""; } else { isOkToUpdate = false; txtGoalDateMsg.Text = "Goal date must be later than today."; } // Activity level. if (_profile.activitylevel != slActivityLevel.Value) { _profile.activitylevel = slActivityLevel.Value; dirty_bit = true; } // If everything is OK to update, then update the profile. // Otherwise, return the user to the My Profile page. if (isOkToUpdate && dirty_bit) { // If this is a new profile, set the setup date. if (isNewProfile) { _profile.startdate = DailyCalories.GetToday(); _profile.email = CreateProfile.Email; _profile.profilep = CreateProfile.Profilep; } // Save profile data to db. DailyCalsServiceV2Client dcsc = new DailyCalsServiceV2Client(); // Set progress bar, and clear some stuff. performanceProgressBar.Visibility = Visibility.Visible; if (isNewProfile) { dcsc.InsertProfileCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(dcsc_InsertProfileCompleted); dcsc.InsertProfileAsync(_profile); } else { dcsc.UpdateProfileCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(dcsc_UpdateProfileCompleted); dcsc.UpdateProfileAsync(_profile); } } else if (isOkToUpdate && !dirty_bit) { // dirty it not set, just go back to main page NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); } }
private void PopulateForm() { // Get the application's profile data object. _profile = (Application.Current as DailyCals.App).ProfileDataObject; // Empty message text boxes. txtBirthdateMsg.Text = ""; txtHeightMsg.Text = ""; txtStartWeightMsg.Text = ""; txtGoalWeightMsg.Text = ""; txtGoalDateMsg.Text = ""; if (_profile == null) { // user has not created his or her profile yet. return; } // Birthdate. dpBirthdate.Value = _profile.birthdate; // Height tbHeight.Text = Convert.ToString(_profile.height); // Female or male? if (_profile.isfemale == 1) { rbFemale.IsChecked = true; rbMale.IsChecked = false; } else { rbFemale.IsChecked = false; rbMale.IsChecked = true; } // Start and goal weights. tbStartWeight.Text = Convert.ToString(_profile.startweight); tbGoalWeight.Text = Convert.ToString(_profile.goalweight); // Standard or Metric units? if (_profile.isstandard == 1) { rbStandard.IsChecked = true; rbMetric.IsChecked = false; txtHeight.Text = "Height (in)"; txtStart.Text = "Start Weight (lbs)"; txtGoal.Text = "Goal Weight (lbs)"; } else { rbStandard.IsChecked = false; rbMetric.IsChecked = true; txtHeight.Text = "Height (cm)"; txtStart.Text = "Start Weight (kgs)"; txtGoal.Text = "Goal Weight (kgs)"; } // Goal date. dpGoalDate.Value = _profile.goaldate; // Activity level. slActivityLevel.Value = _profile.activitylevel; }
public static void UpdateWeightDB(RemoteProfileRecordV2 pr) { // Load the weight db. WeightDB wdb = new WeightDB(); WeightRecord wr = null; try { // Retrieve the initial weigh-in (isStartWeight is true) and update. wr = (from rec in wdb.weightrecords where (rec.isStartWeight) select rec).First(); wr.weight = pr.startweight; } catch { // No existing WeightDB. Insert a new record. wr = new WeightRecord(); wr.date = DailyCalories.GetToday(); wr.weight = pr.startweight; wr.isStartWeight = true; wdb.weightrecords.InsertOnSubmit(wr); } // Submit changes to WeightDB. wdb.SubmitChanges(); }