private void btnAccept_Click(object sender, RoutedEventArgs e) { WeightRecord rec = new WeightRecord(); // OK to update profile? bool isOkToUpdate = true; // Validate weigh-in date. if (DataValidation.IsValidWeighInDate(dpDate)) { rec.date = (DateTime)dpDate.Value; txtDateMsg.Text = ""; } else { isOkToUpdate = false; txtDateMsg.Text = "Invalid date."; } // Validate start weight. if (DataValidation.IsValidWeight(tbWeight)) { rec.weight = Convert.ToDouble(tbWeight.Text); txtWeightMsg.Text = ""; } else { isOkToUpdate = false; txtWeightMsg.Text = "Weight must be greater than 0."; } if (isOkToUpdate) { // Data has been validated, store in db. WeightDB db = new WeightDB(); rec.daysOut = Convert.ToInt32((rec.date - startDate).TotalDays); db.weightrecords.InsertOnSubmit(rec); db.SubmitChanges(); somaAdViewer.StopAds(); NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); } }
private void PopulateListbox() { // 1. get profile data. RemoteProfileRecordV2 profile = (Application.Current as DailyCals.App).ProfileDataObject; startDate = profile.startdate; bool isStandard = (profile.isstandard == 1 ? true : false); // 2. query the db WeightDB wdb = new WeightDB(); IQueryable<WeightRecord> results = from rec in wdb.weightrecords orderby rec.date descending select rec; lbHistory.Items.Clear(); idarray = new int[results.Count()]; // 3. populate list box foreach (WeightRecord cr in results) { ListBoxItem lbi = new ListBoxItem(); lbi.Content = cr.date.ToShortDateString() + ", " + cr.weight.ToString("0.0") + (isStandard ? " lbs." : " kgs."); lbi.FontSize = 32; // Don't allow start weight record to be selectable. if (cr.isStartWeight) { lbi.IsEnabled = false; } lbHistory.Items.Add(lbi); idarray[lbHistory.Items.IndexOf(lbi)] = cr.id; } // 4. smaato ad //somaAdViewer.Pub = 0; // Developer pub ID for testing //somaAdViewer.Adspace = 0; // Developer adSpace ID for testing somaAdViewer.StartAds(); }
private void btnDelete_Click(object sender, RoutedEventArgs e) { WeightDB wdb = new WeightDB(); WeightRecord wr = (from rec in wdb.weightrecords where (rec.id == idarray[lbHistory.SelectedIndex]) select rec).First(); wdb.weightrecords.DeleteOnSubmit(wr); wdb.SubmitChanges(); // Navigate back to main page, chart will update (hitting Back won't work) NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); }
private void InitForm() { // Get the application's profile data object. RemoteProfileRecordV2 profile = (Application.Current as DailyCals.App).ProfileDataObject; // Hide/display the Demo indicator. if (DailyCalories.demoMode) { txtDemoIndicator.Visibility = Visibility.Visible; } else { txtDemoIndicator.Visibility = Visibility.Collapsed; } if (profile == null) { // Profile has not been loaded yet. Deactivate // controls and activate special message. btnWeighIn.IsEnabled = false; btnViewPlan.IsEnabled = false; chartProgress.Visibility = Visibility.Collapsed; tbWelcome.Visibility = Visibility.Visible; btnMyProfile.Content = "Login"; return; } DateTime p_goaldate = profile.goaldate; double p_height = profile.height; DateTime p_birthdate = profile.birthdate; bool p_isfemale = (profile.isfemale == 1 ? true : false); double p_goalweight = profile.goalweight; double p_activitylevel = profile.activitylevel; double p_startweight = profile.startweight; DateTime p_startdate = profile.startdate; bool p_isstandard = (profile.isstandard == 1 ? true : false); // Activate controls. btnWeighIn.IsEnabled = true; btnViewPlan.IsEnabled = true; chartProgress.Visibility = Visibility.Visible; tbWelcome.Visibility = Visibility.Collapsed; btnMyProfile.Content = "My Profile"; // Current weight will be the most recent weigh-in. WeightDB wdb = new WeightDB(); // .Last() throws NotSupportedException WeightRecord wr = (from rec in wdb.weightrecords orderby rec.date descending select rec).First(); double lastWeight = wr.weight; DateTime lastDate = wr.date; double daysRemaining = (p_goaldate - lastDate).TotalDays; // Do some calculations. txtSummaryMsg.Text = "Current weight loss progress based on the most "; txtSummaryMsg.Text += "recent weigh-in of "; txtSummaryMsg.Text += lastWeight.ToString("0.0"); txtSummaryMsg.Text += (p_isstandard ? " pounds recorded on " : " kilograms recorded on "); txtSummaryMsg.Text += lastDate.ToShortDateString(); txtSummaryMsg.Text += ":"; if (lastDate >= p_goaldate) { txtMaxCals.Text = "Goal Date Reached"; txtMaxCals.TextWrapping = TextWrapping.Wrap; txtMaxCals.FontSize = 34; txtMaxCals.Foreground = new SolidColorBrush(Color.FromArgb(255, 30, 144, 255)); txtMaxCalsMsg.Text = "You have reached your goal date with your most recent weigh-in."; } else { if (lastWeight > p_goalweight) { // Last weight not at goal. txtMaxCals.Text = DailyCalories.DailyCals(lastWeight, p_height, p_birthdate, p_isfemale, lastDate, p_goaldate, lastWeight, p_goalweight, p_activitylevel, p_isstandard).ToString("0"); txtMaxCalsMsg.Text = "approximate maximum daily calories allowable to lose "; txtMaxCalsMsg.Text += (DailyCalories.WeightLossPerDay(lastDate, p_goaldate, lastWeight, p_goalweight) * 7.0).ToString("0.0"); txtMaxCalsMsg.Text += (p_isstandard ? " pounds per week." : " kilograms per week."); } else { // Goal reached. double cpdtr = DailyCalories.CaloriesPerDayToReduce(lastDate, p_goaldate, lastWeight, p_goalweight, p_isstandard); double dc = DailyCalories.DailyCals(lastWeight, p_height, p_birthdate, p_isfemale, lastDate, p_goaldate, lastWeight, p_goalweight, p_activitylevel, p_isstandard); txtMaxCals.Text = (DailyCalories.DailyCals(lastWeight, p_height, p_birthdate, p_isfemale, lastDate, p_goaldate, lastWeight, p_goalweight, p_activitylevel, p_isstandard) + cpdtr).ToString("0"); txtMaxCals.Foreground = new SolidColorBrush(Color.FromArgb(255, 30, 144, 255)); txtMaxCalsMsg.Text = "approximate maximum daily calories allowable to maintain current weight."; } } double pc = DailyCalories.PercentageCompleted(p_startweight, p_goalweight, lastWeight); if (pc < 0) { // Negative percentage (gaining weight) txtPercComp.Foreground = new SolidColorBrush(Colors.Red); } else if (pc >= 100) { // Beyond goal weight. txtPercComp.Foreground = new SolidColorBrush(Color.FromArgb(255, 30, 144, 255)); } else { // Positive percentage (losing weight) txtPercComp.Foreground = new SolidColorBrush(Colors.Green); } txtPercComp.Text = pc.ToString("0") + "%"; txtPercCompMsg.Text = "percentage to goal having "; txtPercCompMsg.Text += (pc < 0 ? "gained " : "lost "); txtPercCompMsg.Text += (Math.Abs(p_startweight - lastWeight)).ToString("0.0"); txtPercCompMsg.Text += (p_isstandard ? " pounds " : " kilograms "); if (pc < 0) { // Negative percentage (gaining weight) txtPercCompMsg.Text += "(goal is a "; txtPercCompMsg.Text += (p_startweight - p_goalweight).ToString("0.0"); txtPercCompMsg.Text += (p_isstandard ? " pound weight loss)." : " kilogram weight loss)."); } else { // Positive percentage (losing weight) txtPercCompMsg.Text += "out of "; txtPercCompMsg.Text += (p_startweight - p_goalweight).ToString("0.0"); txtPercCompMsg.Text += (p_isstandard ? " pounds total." : " kilograms total."); } txtDays.Text = Math.Abs(daysRemaining).ToString("0"); if (daysRemaining == 0) { // at goal date txtDays.Foreground = new SolidColorBrush(Color.FromArgb(255, 30, 144, 255)); txtDaysMsg.Text = "days until " + p_goaldate.ToShortDateString() + " goal date, "; } else if (daysRemaining < 0 && lastWeight > p_goalweight) { // beyond goal date but not at goal weight txtDays.Foreground = new SolidColorBrush(Colors.Yellow); txtDaysMsg.Text = "days since " + p_goaldate.ToShortDateString() + " goal date, "; } else if (daysRemaining < 0 && lastWeight <= p_goalweight) { // beyond goal date and goal weight txtDays.Foreground = new SolidColorBrush(Color.FromArgb(255, 30, 144, 255)); txtDaysMsg.Text = "days since " + p_goaldate.ToShortDateString() + " goal date, "; } else { txtDays.Foreground = new SolidColorBrush(Colors.Green); txtDaysMsg.Text = "days until " + p_goaldate.ToShortDateString() + " goal date, "; } txtDaysMsg.Text += "currently in week " + DailyCalories.CurrentWeek(p_startdate) + " of weight loss plan."; }
public static bool IsValidWeighInDate(Microsoft.Phone.Controls.DatePicker dp) { // Convert the datepicker value. DateTime date = (DateTime)dp.Value; // Get the most recent weigh-in date. WeightDB wdb = new WeightDB(); WeightRecord wr = (from rec in wdb.weightrecords orderby rec.date descending select rec).First(); DateTime lastDate = wr.date; // Get today's date. Use canned date if demo mode. DateTime today = DateTime.Today; if (DailyCalories.demoMode) { today = DateTime.Today.AddYears(2); } // Date is invalid if it occurs prior to most recent weigh-in // or if the date is in the future. Today is ok. This code // will also prevent a duplicate date from being entered. if (date <= lastDate || date > today) { dp.Background = new SolidColorBrush(Colors.Red); return false; } // Default background color. dp.Background = new SolidColorBrush(Color.FromArgb(0xBF, 0xFF, 0xFF, 0xFF)); return true; }
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(); }