public async Task <string> OnBloodSugarAddButtonPressed() { if (!String.IsNullOrEmpty(_level.Text) && !String.IsNullOrEmpty(_mealName.Text)) { if (float.Parse(_level.Text) <= 1000 && float.Parse(_level.Text) > 0 && !String.IsNullOrEmpty(_mealName.Text)) { DateTime timeNow = DateTime.Now; PatientData patientData = new PatientData(); Patient patient; try { patient = await _restService.ReadPatientAsync(); } catch (Exception) { return("No Connection"); } PatientBloodSugar patientBlood = new PatientBloodSugar() { UserId = patient.UserId, ReadingType = (ReadingType)Enum.Parse(typeof(ReadingType), _readingType.SelectedItem.ToString()), Level = float.Parse(_level.Text), TimeOfDay = timeNow }; MealItem mealItem; try { mealItem = await _restService.ReadMealItemAsync(_mealName.Text); if (!(mealItem is null)) { patientBlood.MealId = mealItem.MealId; } else { mealItem = new MealItem { FoodName = _mealName.Text, Carbs = Int32.Parse(_carbs.Text), MealTime = (MealType)Enum.Parse(typeof(MealType), _mealType.SelectedItem.ToString()) }; await _restService.CreateMealItemAsync(mealItem); mealItem = await _restService.ReadMealItemAsync(_mealName.Text); patientBlood.MealId = mealItem.MealId; } }
public async Task <string> OnBloodSugarAddButtonPressed() { #region old code if (float.Parse(_level.Text) <= 1000 && float.Parse(_level.Text) > 0) { DateTime timeNow = DateTime.Now; PatientData patientData = new PatientData(); Patient patient; try { patient = await _restService.ReadPatientAsync(); } catch (Exception) { return("No Connection"); } PatientBloodSugar patientBlood = new PatientBloodSugar() { UserId = patient.UserId, ReadingType = (ReadingType)Enum.Parse(typeof(ReadingType), _readingType.SelectedItem.ToString()), Level = float.Parse(_level.Text), TimeOfDay = timeNow }; patientData.PatientBloodSugars.Add(patientBlood); await _restService.CreatePatientData(patientData); return("Success"); } else { return("Invalid Range"); } #endregion }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.activity_modify); _readings = FindViewById <AppCompatSpinner>(Resource.Id.spinner); _carbs = FindViewById <AppCompatEditText>(Resource.Id.bottom_field); _level = FindViewById <AppCompatEditText>(Resource.Id.middle_field); _mealName = FindViewById <AppCompatEditText>(Resource.Id.top_field); _mealType = FindViewById <AppCompatSpinner>(Resource.Id.mealtype_spinner); AppCompatButton bloodSugarEditButton = FindViewById <AppCompatButton>(Resource.Id.submit_button); AppCompatButton bloodSugarDeleteButton = FindViewById <AppCompatButton>(Resource.Id.delete_button); ArrayAdapter <MealType> adapter = new ArrayAdapter <MealType>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, (MealType[])Enum.GetValues(typeof(MealType))); _mealType.Adapter = adapter; _level.Hint = "Reading"; _mealName.Hint = "Meal Name"; _mealName.InputType = Android.Text.InputTypes.ClassText; _mealType.Visibility = ViewStates.Gone; _carbs.Visibility = ViewStates.Gone; _level.Visibility = ViewStates.Gone; _mealName.Visibility = ViewStates.Gone; bloodSugarEditButton.Click += async delegate { bloodSugarEditButton.Enabled = false; bloodSugarDeleteButton.Enabled = false; string status = await OnBloodSugarEditButtonPressed(); if (status == "Success") { Finish(); } else { bloodSugarEditButton.Enabled = true; bloodSugarDeleteButton.Enabled = true; RunOnUiThread(() => { Toast.MakeText(this, status, ToastLength.Long).Show(); }); } }; bloodSugarDeleteButton.Click += async delegate { bloodSugarEditButton.Enabled = false; bloodSugarDeleteButton.Enabled = false; string status = await Task.Run(() => OnBloodSugarDeleteButtonPressed()); if (status == "Success") { Finish(); } else { bloodSugarEditButton.Enabled = true; bloodSugarDeleteButton.Enabled = true; RunOnUiThread(() => { Toast.MakeText(this, status, ToastLength.Long).Show(); }); } }; Android.Support.V7.Widget.Toolbar toolbar = FindViewById <Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar_modify); toolbar.Title = "Modify A Blood Sugar Reading"; SetSupportActionBar(toolbar); DrawerLayout drawer = FindViewById <DrawerLayout>(Resource.Id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close); drawer.AddDrawerListener(toggle); toggle.SyncState(); _readings.ItemSelected += delegate { PatientBloodSugar bs = _readings.SelectedItem.Cast <PatientBloodSugar>(); if (bs.Meal is null) { _mealName.Visibility = ViewStates.Gone; _level.Visibility = ViewStates.Visible; _mealName.Text = null; _level.Text = bs.Level.ToString(); } else { _level.Visibility = ViewStates.Visible; _mealName.Visibility = ViewStates.Visible; _level.Text = bs.Level.ToString(); _mealName.Text = bs.Meal.FoodName; } }; NavigationView navigationView = FindViewById <NavigationView>(Resource.Id.nav_view_modify); navigationView.SetNavigationItemSelectedListener(this); }