Beispiel #1
0
        public HomePage()
        {
            this.InitializeComponent();
            this.navigationHelper            = new NavigationHelper(this);
            this.navigationHelper.LoadState += navigationHelper_LoadState;
            this.navigationHelper.SaveState += navigationHelper_SaveState;

            // MVVM is not used in this example. We're porting an existing sample from Silverlight MVC
            // we will make use of the existing pattern to achieve the results
            model      = new DietCalculatorModel();
            controller = new DietCalculatorController(model);


            // bydefault Male is Selected, hence collapse hips
            txtHips.Visibility = tblHips.Visibility = Visibility.Collapsed;

            cmbGender.SelectionChanged += (sender, args) =>
            {
                var isSelectedItemMale = ((string)cmbGender.SelectedItem == "Male");
                txtHips.Visibility = tblHips.Visibility = isSelectedItemMale ? Visibility.Collapsed : Visibility.Visible;

                // corner case: reset hipsText to empty if the user had typed in hips and later changed the gender
                if (isSelectedItemMale)
                {
                    txtHips.Text = string.Empty;
                }
            };

            // subscribe to IdealBMI and IdealWeight changed events on the model to update the UI
            // as per the implementation in the core project,
            // any change in IdealBMI or IdealWeight are notified using appropriate event handlers
            model.IdealBMIChanged += (sender, e) =>
            {
                txtIdealBMI.Text = e.IdealBMI.ToString();
            };

            model.IdealWeightChanged += (sender, e) =>
            {
                txtIdealWeight.Text = e.IdealWeight.ToString();
            };

            txtIdealWeight.TextChanged += (sender, e) =>
            {
                controller.SetWeight(StringToNumberUtility.GetDouble(txtWeight.Text, 0.00));
                controller.SetHeight(StringToNumberUtility.GetDouble(txtHeight.Text, 0.00));
                controller.SetIdealWeight(StringToNumberUtility.GetDouble(txtIdealWeight.Text, 0.00));
            };
            txtIdealBMI.TextChanged += (sender, e) =>
            {
                controller.SetWeight(StringToNumberUtility.GetDouble(txtWeight.Text, 0.00));
                controller.SetHeight(StringToNumberUtility.GetDouble(txtHeight.Text, 0.00));
                controller.SetIdealBMI(StringToNumberUtility.GetDouble(txtIdealBMI.Text, 0.00));
            };
        }
        private void btnCalculate_Click(object sender, RoutedEventArgs e)
        {
            controller.SetAge(StringToNumberUtility.GetInt32(txtAge.Text, 0));
            controller.SetGender((listGender.SelectedIndex == 0) ? true : false);
            controller.SetWeight(StringToNumberUtility.GetDouble(txtWeight.Text, 0.00));
            controller.SetHeight(StringToNumberUtility.GetDouble(txtHeight.Text, 0.00));
            controller.SetWaist(StringToNumberUtility.GetDouble(txtWaist.Text, 0.00));
            controller.SetHips(StringToNumberUtility.GetDouble((!string.IsNullOrEmpty(txtHips.Text) ? txtHips.Text : null), 0.00));
            controller.SetIdealWeight(StringToNumberUtility.GetDouble(txtIdealWeight.Text, 0.00));
            controller.SetIdealBMI(StringToNumberUtility.GetDouble(txtIdealBMI.Text, 0.00));
            controller.SetCholesterol(StringToNumberUtility.GetDouble(txtCholestrol.Text, 0.00));
            controller.SetHDL(StringToNumberUtility.GetDouble(txtHDL.Text, 0.00));
            controller.SetNeck(StringToNumberUtility.GetDouble(txtNeck.Text, 0.00));
            var selectedActivity = (string)listActivity.SelectedItem;

            controller.SetActivity((LevelOfActivity)Enum.Parse(typeof(LevelOfActivity), selectedActivity));

            // set the model to a public static property. This will be accessed from ResultsPage
            CalculatedModel = model;

            Frame.Navigate(typeof(ResultsPage));
        }
Beispiel #3
0
        private void CaclculateGridViewItem_Tapped(object sender, TappedRoutedEventArgs e)
        {
            controller.SetAge(StringToNumberUtility.GetInt32(txtAge.Text, 0));
            controller.SetGender((cmbGender.SelectedIndex == 0) ? true : false);
            controller.SetWeight(StringToNumberUtility.GetDouble(txtWeight.Text, 0.00));
            controller.SetHeight(StringToNumberUtility.GetDouble(txtHeight.Text, 0.00));
            controller.SetWaist(StringToNumberUtility.GetDouble(txtWaist.Text, 0.00));
            controller.SetHips(StringToNumberUtility.GetDouble((!string.IsNullOrEmpty(txtHips.Text) ? txtHips.Text : null), 0.00));
            controller.SetIdealWeight(StringToNumberUtility.GetDouble(txtIdealWeight.Text, 0.00));
            controller.SetIdealBMI(StringToNumberUtility.GetDouble(txtIdealBMI.Text, 0.00));
            controller.SetCholesterol(StringToNumberUtility.GetDouble(txtCholestrol.Text, 0.00));
            controller.SetHDL(StringToNumberUtility.GetDouble(txtHDL.Text, 0.00));
            controller.SetNeck(StringToNumberUtility.GetDouble(txtNeck.Text, 0.00));
            var selectedActivity = (string)cmbLevelOfActivity.SelectedItem;

            controller.SetActivity((LevelOfActivity)Enum.Parse(typeof(LevelOfActivity), selectedActivity));

            // HACK: force refresh UI
            resultsControl.DataContext = null;
            resultsControl.DataContext = model;
            resultsControl.Visibility  = Visibility.Visible;
            MainContent.Width          = 3000;
            MainScroll.ChangeView(1400, null, null);
        }
Beispiel #4
0
        /// <summary>
        /// OnCreate
        /// </summary>
        /// <param name="bundle"></param>
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            model      = new DietCalculatorModel();
            controller = new DietCalculatorController(model);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // retrieve the widgets in UI using FindByViewId to interact programmatically
            var ageText                = FindViewById <EditText>(Resource.Id.ageText);
            var genderSpinner          = FindViewById <Spinner>(Resource.Id.genderSpinner);
            var levelOfActivitySpinner = FindViewById <Spinner>(Resource.Id.levelOfActivitySpinner);
            var calculateButton        = FindViewById <Button>(Resource.Id.calculateButton);

            var weightText = FindViewById <EditText>(Resource.Id.weightText);
            var heightText = FindViewById <EditText>(Resource.Id.heightText);

            var waistText  = FindViewById <EditText>(Resource.Id.waistText);
            var hipsLayout = FindViewById <LinearLayout>(Resource.Id.hipsLayout);
            var hipstText  = FindViewById <EditText>(Resource.Id.hipsText);

            var idealWeightText = FindViewById <EditText>(Resource.Id.idealWeightText);
            var idealBMIText    = FindViewById <EditText>(Resource.Id.idealBMIText);

            var cholestrolText = FindViewById <EditText>(Resource.Id.cholestrolText);
            var hdlText        = FindViewById <EditText>(Resource.Id.hdlText);

            var neckText = FindViewById <EditText>(Resource.Id.neckText);

            // Assign "Male", "Female" values to spinner using ArrayAdapter. Refer Resources/Values/String.xml
            ArrayAdapter genderAdapter = ArrayAdapter.CreateFromResource(this, Resource.Array.gender_array,
                                                                         Resource.Layout.diet_spinner_item);

            genderAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            genderSpinner.Adapter = genderAdapter;

            // Hips should be visible only if "Female" is selected
            genderSpinner.ItemSelected += (sender, args) =>
            {
                var isSelectedItemMale = ((string)genderSpinner.SelectedItem == "Male");
                hipsLayout.Visibility = isSelectedItemMale ? ViewStates.Gone : ViewStates.Visible;
                // corner case: reset hipsText to empty if the user had typed in hips and later changed the gender
                if (isSelectedItemMale)
                {
                    hipstText.Text = string.Empty;
                }
            };
            // Assign "Active", "Moderate", "Sedentary" values to spinner using ArrayAdapter. Refer Resources/Values/String.xml
            ArrayAdapter levelOfActivityAdapter = ArrayAdapter.CreateFromResource(this, Resource.Array.levelOfActivity_array,
                                                                                  Resource.Layout.diet_spinner_item);

            levelOfActivityAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            levelOfActivitySpinner.Adapter = levelOfActivityAdapter;

            // On Button Click, set appropriate values in the Controller Class
            calculateButton.Click += (sender, args) =>
            {
                controller.SetAge(StringToNumberUtility.GetInt32(ageText.Text, 0));
                controller.SetGender((genderSpinner.SelectedItemId == 0) ? true : false);
                controller.SetWeight(StringToNumberUtility.GetDouble(weightText.Text, 0.00));
                controller.SetHeight(StringToNumberUtility.GetDouble(heightText.Text, 0.00));
                controller.SetWaist(StringToNumberUtility.GetDouble(waistText.Text, 0.00));
                controller.SetHips(StringToNumberUtility.GetDouble((!string.IsNullOrEmpty(hipstText.Text) ? hipstText.Text : null), 0.00));
                controller.SetIdealWeight(StringToNumberUtility.GetDouble(idealWeightText.Text, 0.00));
                controller.SetIdealBMI(StringToNumberUtility.GetDouble(idealBMIText.Text, 0.00));
                controller.SetCholesterol(StringToNumberUtility.GetDouble(cholestrolText.Text, 0.00));
                controller.SetHDL(StringToNumberUtility.GetDouble(hdlText.Text, 0.00));
                controller.SetNeck(StringToNumberUtility.GetDouble(neckText.Text, 0.00));
                var selectedActivity = (string)levelOfActivitySpinner.SelectedItem;
                controller.SetActivity((LevelOfActivity)Enum.Parse(typeof(LevelOfActivity), selectedActivity));

                // Since Model classes are already populated with the results, pass them to Result Activity
                // using PutExtra method
                var resultIntent = new Intent(this, typeof(ResultsActivity));
                resultIntent.PutExtra("CaloriesPerDay", model.CaloriesPerDay.ToString());
                resultIntent.PutExtra("LeanBodyMass", model.LeanBodyMass.ToString());
                resultIntent.PutExtra("PercentBodyFat", model.PercentBodyFat.ToString() + " %");
                resultIntent.PutExtra("WaistHipsRatio", model.WaistHipsRatio.ToString() + " cm");
                resultIntent.PutExtra("BMI", model.BMI.ToString());
                resultIntent.PutExtra("CholesterolRatio", model.CholesterolRatio.ToString() + " mmol/L");
                resultIntent.PutExtra("WaistHeightRatio", model.WaistHeightRatio.ToString() + " cm");
                resultIntent.PutExtra("IdealWeight", model.IdealWeight.ToString() + " kg");
                StartActivity(resultIntent);
            };

            // subscribe to IdealBMI and IdealWeight changed events on the model to update the UI
            // as per the implementation in the core project,
            // any change in IdealBMI or IdealWeight are notified using appropriate event handlers
            model.IdealBMIChanged += (sender, e) =>
            {
                idealBMIText.Text = e.IdealBMI.ToString();
            };

            model.IdealWeightChanged += (sender, e) =>
            {
                idealWeightText.Text = e.IdealWeight.ToString();
            };

            // Ideal BMI has to be calculated if user changes the IdealWeight.
            // Set all the fields in the controller that are used for calcaulation

            idealWeightText.TextChanged += (sender, e) =>
            {
                controller.SetWeight(StringToNumberUtility.GetDouble(weightText.Text, 0.00));
                controller.SetHeight(StringToNumberUtility.GetDouble(heightText.Text, 0.00));
                controller.SetIdealWeight(StringToNumberUtility.GetDouble(idealWeightText.Text, 0.00));
            };
            idealBMIText.TextChanged += (sender, e) =>
            {
                controller.SetWeight(StringToNumberUtility.GetDouble(weightText.Text, 0.00));
                controller.SetHeight(StringToNumberUtility.GetDouble(heightText.Text, 0.00));
                controller.SetIdealBMI(StringToNumberUtility.GetDouble(idealBMIText.Text, 0.00));
            };
        }
        /// <summary>
        /// Creates the UI for DialogViewController using Element API
        /// </summary>
        private void CreateUI()
        {
            EntryElement ageElement, weightElement, heightElement, waistElement, hipsElement = null,
                         idealWeightElement, idealBMIElement, cholestrolElement, hdlElement, neckElement;
            StringElement calculateButtonElement;

            Section waistHipsSection = null;

            // Gender Group and Section
            var genderRadioGroup    = new RadioGroup("gender", 0);
            var femaleRadioElement  = new DietRadioElement("Female", "gender");
            var maleRadioElement    = new DietRadioElement("Male", "gender");
            var radioElementSection = new Section()
            {
                maleRadioElement,
                femaleRadioElement
            };

            // add hips element to wasit & hips section if female is selected
            femaleRadioElement.Tapped += delegate {
                if (!waistHipsSection.Elements.Contains(hipsElement = new NumericEntryElement("Hips (in cm)", "ex. 88")))
                {
                    waistHipsSection.Add(hipsElement);
                }
            };

            // remove hips element if male is selected.
            maleRadioElement.Tapped += delegate {
                if (waistHipsSection.Elements.Contains(hipsElement))
                {
                    waistHipsSection.Remove(hipsElement);
                }
            };

            // Level of ActivityGroup & Section
            var levelOfActivityRadioGroup = new RadioGroup("levelOfActivity", 2);
            var levelOfActivitySection    = new Section()
            {
                new DietRadioElement(LevelOfActivity.Active.ToString(), "levelOfActivity"),
                new DietRadioElement(LevelOfActivity.Moderate.ToString(), "levelOfActivity"),
                new DietRadioElement(LevelOfActivity.Sedentary.ToString(), "levelOfActivity")
            };

            // Create Elements on the rootElement. This will be added to the DialogViewController's root.
            rootElement = new RootElement("Diet Calculator")
            {
                new Section()
                {
                    (ageElement = new NumericEntryElement("Age", "ex. 25")
                    {
                        KeyboardType = UIKeyboardType.NumberPad
                    }),
                    (new DietRootElement("Gender", genderRadioGroup)
                    {
                        radioElementSection
                    })
                },
                new Section("Height & Weight")
                {
                    (weightElement = new NumericEntryElement("Weight (in kg)", "ex. 65")),
                    (heightElement = new NumericEntryElement("Height (in cm)", "ex. 170"))
                }, (waistHipsSection = new Section("Waist & Hips")
                {
                    (waistElement = new NumericEntryElement("Waist (in cm)", "ex. 47"))
                    /* hips element will be added here if female is selected*/
                }),
                new Section("Ideal Weight & BMI")
                {
                    (idealWeightElement = new NumericEntryElement("Ideal Weight (in kg)", "ex. 45")),
                    (idealBMIElement = new NumericEntryElement("Ideal BMI (in kg/m2)", "ex. 18"))
                },
                new Section("Cholestrol & HDL")
                {
                    (cholestrolElement = new NumericEntryElement("Cholestrol (in mmol/L)", "ex. 5.17")),
                    (hdlElement = new NumericEntryElement("HDL (in mmol/L)", "ex. 1.56"))
                },
                new Section("Neck")
                {
                    (neckElement = new NumericEntryElement("Neck (in cm)", "ex. 30")),
                },
                new Section()
                {
                    new DietRootElement("Level Of Activity", levelOfActivityRadioGroup)
                    {
                        levelOfActivitySection
                    }
                },
                /* Calculate Button*/
                new Section()
                {
                    (calculateButtonElement = new DietStringElement("Calculate",
                                                                    /* On Calculate Click create the resulting UI*/
                                                                    delegate {
                        /* exising code from SL MVC project */
                        controller.SetAge(StringToNumberUtility.GetInt32(ageElement.Value, 0));
                        controller.SetGender(genderRadioGroup.Selected == 0 ? true : false);
                        controller.SetWeight(StringToNumberUtility.GetDouble(weightElement.Value, 0.00));
                        controller.SetHeight(StringToNumberUtility.GetDouble(heightElement.Value, 0.00));
                        controller.SetWaist(StringToNumberUtility.GetDouble(waistElement.Value, 0.00));
                        controller.SetHips(StringToNumberUtility.GetDouble((hipsElement == null ? null : hipsElement.Value), 0.00));
                        controller.SetIdealWeight(StringToNumberUtility.GetDouble(idealWeightElement.Value, 0.00));
                        controller.SetIdealBMI(StringToNumberUtility.GetDouble(idealBMIElement.Value, 0.00));
                        controller.SetCholesterol(StringToNumberUtility.GetDouble(cholestrolElement.Value, 0.00));
                        controller.SetHDL(StringToNumberUtility.GetDouble(hdlElement.Value, 0.00));
                        controller.SetNeck(StringToNumberUtility.GetDouble(neckElement.Value, 0.00));

                        var selectedActivity = levelOfActivitySection.Elements[levelOfActivityRadioGroup.Selected].Caption;
                        controller.SetActivity(( LevelOfActivity )Enum.Parse(typeof(LevelOfActivity), selectedActivity));

                        resultSection = new Section("Results")
                        {
                            new DietStringElement("Calories Per Day: ", model.CaloriesPerDay.ToString()),
                            new DietStringElement("Lean Body Mass: ", model.LeanBodyMass.ToString()),
                            new DietStringElement("Fat: ", model.PercentBodyFat.ToString() + " %"),
                            new DietStringElement("Waist Hips Label: ", model.WaistHipsRatio.ToString() + " cm"),
                            new DietStringElement("BMI Ratio: ", model.BMI.ToString()),
                            new DietStringElement("Cholestrol Ratio: ", model.CholesterolRatio.ToString() + " mmol/L"),
                            new DietStringElement("Waist Height Ratio: ", model.WaistHeightRatio.ToString() + " cm"),
                            new DietStringElement("Ideal Weight: ", model.IdealWeight.ToString() + " kg"),
                        };

                        resultDvc = new DietDialogViewController(new RootElement("Results")
                        {
                            resultSection
                        }, true);
                        nvc.PushViewController(resultDvc, true);
                    }))
                }
            };

            // subscribe to IdealBMI and IdealWeight changed events on the model to update the UI
            model.IdealBMIChanged += (sender, e) => {
                idealBMIElement.Value = e.IdealBMI.ToString();
            };

            model.IdealWeightChanged += (sender, e) => {
                idealWeightElement.Value = e.IdealWeight.ToString();
            };

            // Since Ideal BMI has to be calculated based on the Ideal weight, set all the fields in
            // the controller that are used for calcaulation
            idealWeightElement.Changed += (sender, e) => {
                controller.SetWeight(StringToNumberUtility.GetDouble(weightElement.Value, 0.00));
                controller.SetHeight(StringToNumberUtility.GetDouble(heightElement.Value, 0.00));

                controller.SetIdealWeight(StringToNumberUtility.GetDouble(idealWeightElement.Value, 0.00));
            };
            idealBMIElement.Changed += (sender, e) => {
                controller.SetWeight(StringToNumberUtility.GetDouble(weightElement.Value, 0.00));
                controller.SetHeight(StringToNumberUtility.GetDouble(heightElement.Value, 0.00));
                controller.SetIdealBMI(StringToNumberUtility.GetDouble(idealBMIElement.Value, 0.00));
            };

            calculateButtonElement.Alignment = UITextAlignment.Center;
        }