Exemple #1
0
        protected override async void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.heightView);
            MobileServiceClient client = ToDoActivity.CurrentActivity.CurrentClient;

            UsersTableRef = client.GetTable <UsersTable>();

            enterHeight            = FindViewById <EditText>(Resource.Id.enterHeight);
            enterHeight.Visibility = ViewStates.Gone;
            heightText             = FindViewById <TextView>(Resource.Id.heightText);

            updateMyHeight            = FindViewById <Button>(Resource.Id.updateButton);
            updateMyHeight.Visibility = ViewStates.Gone;

            enterHeight.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) => {
                //https://msdn.microsoft.com/en-us/library/0xh24xh4.aspx
                string value = e.Text.ToString();
                System.Globalization.NumberStyles style;
                System.Globalization.CultureInfo  culture;
                float number;
                style   = System.Globalization.NumberStyles.Number;
                culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
                if (Single.TryParse(value, style, culture, out number))
                {
                    Console.WriteLine("Converted '{0}' to {1}.", value, number);
                    string message = "Converted " + value + "to " + number;
                    enteredHeight = number;
                }

                else
                {
                    Console.WriteLine("Unable to convert '{0}'.", value);
                    string message = "Unable to convert " + value;
                    enteredHeight = 0;
                }
            };

            updateMyHeight.Click += (sender, e) =>
            {
                heightText.Text           = "Please Enter Your Height as a Decimal Number";
                enterHeight.Visibility    = ViewStates.Visible;
                updateMyHeight.Visibility = ViewStates.Gone;
                OKupdateButton.Visibility = ViewStates.Visible;
            };


            OKButton            = FindViewById <Button>(Resource.Id.OKButton);
            OKButton.Visibility = ViewStates.Gone;
            OKButton.Click     += async(sender, e) =>
            {
                if (enteredHeight > 0)
                {
                    try
                    {
                        OKButton.Visibility    = ViewStates.Gone;
                        heightText.Visibility  = ViewStates.Gone;
                        enterHeight.Visibility = ViewStates.Gone;
                        //enter user's info to UsersTable
                        var userUpdated = new UsersTable
                        {
                            UniqueUsername = ourUserId,
                            height         = enteredHeight,
                        };
                        await UsersTableRef.InsertAsync(userUpdated);

                        heightText.Text           = "Height Inserted to Database Successfully";
                        heightText.Visibility     = ViewStates.Visible;
                        finishActivity.Visibility = ViewStates.Visible;
                    }
                    catch (Exception ex)
                    {
                        CreateAndShowDialog(ex, "Error");
                    }
                }
                else
                {
                    CreateAndShowDialog("Please insert a decimal number larger than 0", "Input Error");
                }
            };

            OKupdateButton            = FindViewById <Button>(Resource.Id.OKUpdateCaseButton);
            OKupdateButton.Visibility = ViewStates.Gone;
            OKupdateButton.Click     += async(sender, e) =>
            {
                if (enteredHeight > 0 && ourUser != null)
                {
                    try
                    {
                        OKupdateButton.Visibility = ViewStates.Gone;
                        ourUser.height            = enteredHeight;
                        heightText.Visibility     = ViewStates.Gone;
                        enterHeight.Visibility    = ViewStates.Gone;
                        await UsersTableRef.UpdateAsync(ourUser);

                        heightText.Text           = "Height Updated Successfully";
                        heightText.Visibility     = ViewStates.Visible;
                        finishActivity.Visibility = ViewStates.Visible;
                    }
                    catch (Exception ex)
                    {
                        CreateAndShowDialog(ex, "Error");
                    }
                }
                else
                {
                    CreateAndShowDialog("Please insert a decimal number larger than 0", "Input Error");
                }
            };

            finishActivity            = FindViewById <Button>(Resource.Id.FinishActivity);
            finishActivity.Visibility = ViewStates.Gone;
            finishActivity.Click     += (sender, e) =>
            {
                Finish();
            };


            try
            {
                ourUser = await fetchHeightAsync();
            }
            catch (Exception ex)
            {
                CreateAndShowDialog(ex, "Error");
            }
        }
Exemple #2
0
        protected override async void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Create your application here
            SetContentView(Resource.Layout.BMIview);
            MobileServiceClient client = ToDoActivity.CurrentActivity.CurrentClient;

            weighTableRef = client.GetTable <weighTable>();
            UsersTableRef = client.GetTable <UsersTable>();


            var enterHeight = FindViewById <EditText>(Resource.Id.enterHeight);
            var heightText  = FindViewById <TextView>(Resource.Id.heightText);
            var BMI_text    = FindViewById <TextView>(Resource.Id.BMI_TextView);

            BMI_text.Visibility = ViewStates.Gone;
            FindViewById <TextView>(Resource.Id.Category_TextView).Visibility = ViewStates.Gone;

            heightText.Visibility  = ViewStates.Gone;
            enterHeight.Visibility = ViewStates.Gone;
            var wikiLinkButton = FindViewById <Button>(Resource.Id.BMIwikiLink);

            wikiLinkButton.Visibility = ViewStates.Gone;

            wikiLinkButton.Click += delegate {
                var uri    = Android.Net.Uri.Parse("https://en.wikipedia.org/wiki/Body_mass_index#Categories");
                var intent = new Intent(Intent.ActionView, uri);
                StartActivity(intent);
            };


            enterHeight.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) => {
                //https://msdn.microsoft.com/en-us/library/0xh24xh4.aspx
                string value = e.Text.ToString();
                System.Globalization.NumberStyles style;
                System.Globalization.CultureInfo  culture;
                float number;
                style   = System.Globalization.NumberStyles.Number;
                culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
                if (Single.TryParse(value, style, culture, out number))
                {
                    Console.WriteLine("Converted '{0}' to {1}.", value, number);
                    string message = "Converted " + value + "to " + number;
                    enteredHeight = number;
                }

                else
                {
                    Console.WriteLine("Unable to convert '{0}'.", value);
                    string message = "Unable to convert " + value;
                    enteredHeight = 0;
                }
            };

            var OKButton = FindViewById <Button>(Resource.Id.OKButton);

            OKButton.Visibility = ViewStates.Gone;
            OKButton.Click     += async(sender, e) =>
            {
                if (enteredHeight > 0)
                {
                    //enter user's info to UsersTable and then calculate BMI
                    var userUpdated = new UsersTable
                    {
                        UniqueUsername = ourUserId,
                        height         = enteredHeight,
                    };
                    try
                    {
                        await UsersTableRef.InsertAsync(userUpdated);

                        OKButton.Visibility    = ViewStates.Gone;
                        heightText.Visibility  = ViewStates.Gone;
                        enterHeight.Visibility = ViewStates.Gone;
                        await CalculateUserBMI();
                    }
                    catch (Exception ex)
                    {
                        CreateAndShowDialog(ex, "Error");
                    }
                }
                else
                {
                    CreateAndShowDialog("Please insert a decimal number larger than 0", "Input Error");
                }
            };



            try
            {
                var userRecord = await UsersTableRef.Where(item => (item.UniqueUsername == ourUserId)).ToListAsync();

                if (userRecord.Count == 0)
                {
                    heightText.Visibility  = ViewStates.Visible;
                    enterHeight.Visibility = ViewStates.Visible;
                    OKButton.Visibility    = ViewStates.Visible;
                }
                //user already entered height:
                else
                {
                    var   user   = userRecord[0];
                    float height = user.height;
                    //check if there are weighs of this user in the weighTable:
                    var weighRecords = await weighTableRef.Where(item => (item.username == ourUserId)).ToListAsync();

                    if (weighRecords.Count == 0)
                    {
                        CreateAndShowDialog("No previous weighs were found. Please weigh yourself and try again", "Cannot calculate BMI");
                    }
                    else
                    {
                        //calculate BMI from user's most recent weighing:
                        var   mostRecendWeigh = weighRecords[weighRecords.Count - 1];
                        float weight          = mostRecendWeigh.weigh;
                        //BMI formula from wikipedia:   BMI = weight in kg / (height in meters)^2
                        //https://en.wikipedia.org/wiki/Body_mass_index
                        double heigtSquared      = Math.Pow(height, 2);
                        double BMI               = weight / heigtSquared;
                        string BMIInStringFormat = String.Format("{0:0.00}", BMI);
                        string BMI_message       = "Your most recent weight = " + weight + "\nYour BMI = " + BMIInStringFormat;
                        var    BMItext           = FindViewById <TextView>(Resource.Id.BMI_TextView);
                        BMItext.Visibility = ViewStates.Visible;
                        BMItext.Text       = BMI_message;
                        CalculateBMICategory(BMI);
                    }
                }
            }
            catch (Exception e)
            {
                CreateAndShowDialog(e, "Error");
            }
        }