public void ResetUser(User user) { List <User> users = GetAllUsers(); user.eaten_cal = 0; if (user.food_eaten == null) { user.food_eaten = new List <Food>(); } else { user.food_eaten.Clear(); } if (user.id == currentUser.id) { currentUser = user; } int i = users.FindIndex(x => x.id == user.id); if (i != -1) { users[i] = user; } JsonFunctions.WriteJSON(users, JsonFunctions.GetFilepath(UserForm.filename)); SetCalories(); }
public void ArchiveUsers() { List <User> users = GetAllUsers(); for (int i = 0; i < users.Count; i++) { users[i].eaten_cal = 0; if (users[i].food_eaten == null) { users[i].food_eaten = new List <Food>(); } List <Food> tempFood = users[i].food_eaten; if (users[i].food_eaten.Count != 0) { users[i].Archive(tempFood); users[i].food_eaten = new List <Food>(); } if (users[i].id == currentUser.id) { currentUser = users[i]; } } JsonFunctions.WriteJSON(users, JsonFunctions.GetFilepath(UserForm.filename)); SetCalories(); }
public void MenuDelete_Click(object sender, EventArgs e) { if (parent.flpCurrent == MainForm.flpItems.Custom) { string fp = JsonFunctions.GetFilepath(CustomForm.foodFile); CustomForm.RemoveCustomFood(fp, food); parent.PopulateResults(CustomForm.GetCustomFood(fp), edit, rightClick); } else if (parent.flpCurrent == MainForm.flpItems.Eaten) { List <User> users = FoodForm.GetAllUsers(); User user = users.Find(x => x.id == parent.currentUser.id); int i = users.IndexOf(user); int foodIndex = user.food_eaten.FindIndex(x => x.item_id == food.item_id); if (foodIndex != -1) { user.food_eaten.RemoveAt(foodIndex); user.eaten_cal = user.EatenCal(); users[i] = user; JsonFunctions.WriteJSON(users, JsonFunctions.GetFilepath(UserForm.filename)); parent.currentUser = user; parent.SetCalories(); parent.PopulateResults(user.food_eaten, edit, rightClick); } } else if (parent.flpCurrent == MainForm.flpItems.Search) { return; } }
private Food BuildFood() { Food customFood = new Food(); // Required customFood.item_name = txtName.Text; customFood.brand_name = txtBrand.Text; customFood.nf_serving_size_qty = txtServingQty.Text; customFood.nf_serving_size_unit = txtServingUnit.Text; customFood.nf_calories = txtCalories.Text; // Optional customFood.nf_calories_from_fat = (from t in optionals where t.Name == "txtCaloriesfromfat" select t).First().Text; customFood.nf_total_fat = (from t in optionals where t.Name == "txtTotalfat" select t).First().Text; customFood.nf_saturated_fat = (from t in optionals where t.Name == "txtSaturatedfat" select t).First().Text; customFood.nf_cholesterol = (from t in optionals where t.Name == "txtCholesterol" select t).First().Text; customFood.nf_sodium = (from t in optionals where t.Name == "txtSodium" select t).First().Text; customFood.nf_total_carbohydrate = (from t in optionals where t.Name == "txtTotalcarbs" select t).First().Text; customFood.nf_dietary_fiber = (from t in optionals where t.Name == "txtFiber" select t).First().Text; customFood.nf_sugars = (from t in optionals where t.Name == "txtSugar" select t).First().Text; customFood.nf_protein = (from t in optionals where t.Name == "txtProtein" select t).First().Text; customFood.nf_vitamin_a_dv = (from t in optionals where t.Name == "txtVitaminA" select t).First().Text; customFood.nf_vitamin_c_dv = (from t in optionals where t.Name == "txtVitaminC" select t).First().Text; customFood.nf_calcium_dv = (from t in optionals where t.Name == "txtCalcium" select t).First().Text; customFood.nf_iron_dv = (from t in optionals where t.Name == "txtIron" select t).First().Text; customFood.nf_potassium = (from t in optionals where t.Name == "txtPotassium" select t).First().Text; // Set ID string fp = JsonFunctions.GetFilepath(foodFile); List <Food> customFoods = GetCustomFood(fp); if (edit) { customFood.item_id = editFood.item_id; int i = customFoods.FindIndex(x => x.item_id == editFood.item_id); customFoods[i] = customFood; } else { if (customFoods.Count == 0) { customFood.item_id = "-2"; } else { int min = -2; for (int i = 0; i < customFoods.Count; i++) { min = Math.Min(min, ParseInt(customFoods[i].item_id)); } customFood.item_id = (min - 1).ToString(); } customFoods.Add(customFood); } JsonFunctions.WriteJSON(customFoods, fp); return(customFood); }
void DeleteUser(User delUser) { users.Remove(users.Find(x => x.id == delUser.id)); users[0] = users[0].SetLogin(true); currentUser = users[0]; JsonFunctions.WriteJSON(users, JsonFunctions.GetFilepath(filename)); SetupCboUsers(); }
private void UpdateUser() { double newServings = ParseDbl(txtServings.Text); List <User> users = GetAllUsers(); User user = users.Find(x => x.id == calledBy.currentUser.id); int i = users.IndexOf(user); if (user.food_eaten == null) { user.food_eaten = new List <Food>(); } if (edit) { double oldServings = food.servings; int foodIndex = user.food_eaten.IndexOf(user.food_eaten.Find(x => x.item_id == food.item_id)); food.servings = newServings; if (newServings == 0) { user.food_eaten.RemoveAt(foodIndex); } else { user.food_eaten[foodIndex] = food; } } else { int foodIndex = user.food_eaten.IndexOf(user.food_eaten.Find(x => x.item_id == food.item_id)); if (foodIndex != -1) { food.servings = newServings + user.food_eaten[foodIndex].servings; user.food_eaten[foodIndex] = food; } else { food.servings = newServings; user.food_eaten.Add(food); } } user.eaten_cal = user.EatenCal(); users[i] = user; JsonFunctions.WriteJSON(users, JsonFunctions.GetFilepath(UserForm.filename)); calledBy.currentUser = user; calledBy.SetCalories(); if (edit) { calledBy.PopulateResults(user.food_eaten, true, true); } }
void GetUsers() { if (!File.Exists(JsonFunctions.GetFilepath(filename))) { users = new List <User>(); JsonFunctions.WriteJSON(users, JsonFunctions.GetFilepath(filename)); return; } users = JsonFunctions.ReadUserJSON(JsonFunctions.GetFilepath(filename)); }
public static List <Food> GetCustomFood(string filepath) { if (!File.Exists(filepath)) { List <Food> newFoods = new List <Food>(); JsonFunctions.WriteJSON(newFoods, filepath); return(newFoods); } return(JsonFunctions.ReadFoodJSON(filepath)); }
void EditUser(User oldUser, User newUser) { int i = users.FindIndex(x => x.id == oldUser.id); users[i] = newUser; if (currentUser.id == i) { currentUser = newUser; } JsonFunctions.WriteJSON(users, JsonFunctions.GetFilepath(filename)); SetupCboUsers(); }
public static void RemoveCustomFood(string filepath, Food food) { if (!File.Exists(filepath)) { return; } List <Food> customFoods = GetCustomFood(filepath); customFoods.Remove(customFoods.Find(x => x.item_id == food.item_id)); JsonFunctions.WriteJSON(customFoods, filepath); }
bool SetLoggedIn(User newLogin) { int i = users.FindIndex(x => x.logged_in); if (i != -1) { users[i].logged_in = false; } newLogin.logged_in = true; users[users.FindIndex(x => x.id == newLogin.id)] = newLogin; JsonFunctions.WriteJSON(users, JsonFunctions.GetFilepath(filename)); return(true); }
private void btnAdd_Click(object sender, EventArgs e) { //UpdateUser(); if (!DataValidation()) { return; } List <User> users = GetAllUsers(); User user = users.Find(x => x.id == calledBy.currentUser.id); int i = users.IndexOf(user); Food food = BuildFood(); int foodIndex = user.food_eaten.IndexOf(user.food_eaten.Find(x => x.item_id == food.item_id)); if (!edit) { if (foodIndex != -1) { food.servings = 1 + user.food_eaten[foodIndex].servings; user.food_eaten[foodIndex] = food; } else { food.servings = 1; user.food_eaten.Add(food); } } else { user.food_eaten[foodIndex] = food; } user.eaten_cal = user.EatenCal(); users[i] = user; JsonFunctions.WriteJSON(users, JsonFunctions.GetFilepath(UserForm.filename)); calledBy.currentUser = user; calledBy.SetCalories(); this.Close(); }
void CreateUser(User newUser) { users.Add(newUser); JsonFunctions.WriteJSON(users, JsonFunctions.GetFilepath(filename)); SetupCboUsers(); }