private static string GenerateRelativeUri(Nutrients nutrients, FoodOptions options, List <int> listKeep = null, List <int> listChange = null) { if (nutrients == null) { //return null; throw new ArgumentException("Null nutrients", nameof(nutrients)); } if (options == null) { options = new FoodOptions(); } if (double.IsNaN(nutrients.Energy) || (nutrients.Energy < 0)) { #if SHOW_DEBUG Console.WriteLine($"Invalid energy, {nutrients.Energy}"); #endif //return null; throw new ArgumentException($"Invalid energy, {nutrients.Energy}", nameof(nutrients.Energy)); } double dbEnergy = Math.Round(nutrients.Energy, 2); if (double.IsNaN(nutrients.Proteins) || (nutrients.Proteins <= 0)) { #if SHOW_DEBUG Console.WriteLine($"Invalid proteins, {nutrients.Proteins}"); #endif //return null; throw new ArgumentException($"Invalid proteins, {nutrients.Proteins}", nameof(nutrients.Proteins)); } double dbProteins = Math.Round(nutrients.Proteins, 2); if (double.IsNaN(nutrients.Carbohydrates) || (nutrients.Carbohydrates <= 0)) { #if SHOW_DEBUG Console.WriteLine($"Invalid carbohydrates, {nutrients.Carbohydrates}"); #endif //return null; throw new ArgumentException($"Invalid carbohydrates, {nutrients.Carbohydrates}", nameof(nutrients.Carbohydrates)); } double dbCarbohydrates = Math.Round(nutrients.Carbohydrates, 2); if (double.IsNaN(nutrients.TotalFats) || (nutrients.TotalFats <= 0)) { #if SHOW_DEBUG Console.WriteLine($"Invalid fats, {nutrients.TotalFats}"); #endif //return null; throw new ArgumentException($"Invalid fats, {nutrients.TotalFats}", nameof(nutrients.TotalFats)); } double dbTotalFats = Math.Round(nutrients.TotalFats, 2); string strDiet = nutrients.Diet.ToString(); try { CheckDietString(ref strDiet); strDiet = strDiet.Replace(' ', '_').ToLower(); } catch (Exception ex) { throw ex; } string strKeep = "", strChange = ""; if (listKeep != null) { strKeep = string.Join(",", listKeep.ToArray()); } if (listChange != null) { strChange = string.Join(",", listChange.ToArray()); } return($"?CarbohydrateAmount_g={dbCarbohydrates}" + $"&EnergyAmount_kcal={dbEnergy}" + $"&ProteinAmount_g={dbProteins}" + $"&TotalFatAmount_g={dbTotalFats}" + $"&diet={strDiet}" + $"&isVegan={options.IsVegan}" + $"&isVegetarian={options.IsVegetarian}" + $"&isHalal={options.IsHalal}" + $"&containsBeef={options.ContainsBeef}" + $"&isAlcohol={options.IsAlcohol}" + $"&food_keep_index={strKeep}" + $"&food_change_index={strChange}"); }
// Use this for initialization void Start() { Instance = this; HideSelections(); }
/*public static async Task<List<Food>> FoodRecommend(UserProfile userProfile, Uri uriFoodRecommend = null) * { * if (uriFoodRecommend == null) * { * if (_apiRoot == null) * { * _ = await ReadApiRoot(); * } * * if ((_apiRoot != null) && (_apiRoot.UserToFoodRecLink != null)) * { * uriFoodRecommend = new Uri(_apiRoot.UserToFoodRecLink.OriginalString); * } * } * * if (uriFoodRecommend == null) * { * return null; * } * * string strUriRelative = GenerateRelativeUri(userProfile); * if (strUriRelative == null) * return null; * * try * { * var streamTask = _client.GetStreamAsync(new Uri(uriFoodRecommend + strUriRelative)); * var foodList = await JsonSerializer.DeserializeAsync<List<Models.Food>>(await streamTask); * #if SHOW_DEBUG * Console.WriteLine($"Name = \t{foodList[0].Name}"); * Console.WriteLine($"Group = \t{foodList[0].Group}"); * Console.WriteLine($"Energy = \t{foodList[0].Energy}"); * Console.WriteLine($"Proteins = \t{foodList[0].Proteins}"); * Console.WriteLine($"Carbohydrates = \t{foodList[0].Carbs}"); * Console.WriteLine($"Fats = \t{foodList[0].Fats}"); #endif * * return foodList; * } * catch (Exception ex) * { * Console.WriteLine("\nException Caught!"); * Console.WriteLine("Message :{0} ", ex.Message); * return null; * } * }*/ public static async Task <List <Food> > FoodRecommend(Nutrients nutrients, FoodOptions options, List <int> listKeep = null, List <int> listChange = null, Uri uriFoodRecommend = null) { if (uriFoodRecommend == null) { if (_apiRoot == null) { _ = await ReadApiRoot(); } if ((_apiRoot != null) && (_apiRoot.NutrientsToFoodRecLink != null)) { uriFoodRecommend = new Uri(_apiRoot.NutrientsToFoodRecLink.OriginalString); } } if (uriFoodRecommend == null) { //return null; throw new ArgumentException("Unable to get link from API", nameof(uriFoodRecommend)); } string strUriRelative = ""; try { strUriRelative = GenerateRelativeUri(nutrients, options, listKeep, listChange); } catch (Exception ex) { throw ex; } if (strUriRelative == null) { //return null; throw new ArgumentException("Null relative uri generated", nameof(strUriRelative)); } try { /*var streamTask = _client.GetStreamAsync(new Uri(uriFoodRecommend + strUriRelative)); * string strStreamRead = string.Empty; * using (StreamReader sr = new StreamReader(await streamTask)) * { * string strLine; * while ((strLine = sr.ReadLine()) != null) * { * strStreamRead += strLine; * } * }*/ var streamTask = _client.GetStreamAsync(new Uri(uriFoodRecommend + strUriRelative)); var foodList = await JsonSerializer.DeserializeAsync <List <Food> >(await streamTask); _keepChangeIndex = new KeepChangeIndex(); if (foodList != null) { if (foodList.Last().Keep != null) { string[] strKeepIndex = foodList.Last().Keep.Split(','); if (strKeepIndex != null) { foreach (string strIndex in strKeepIndex) { if (int.TryParse(strIndex, out int nIndex)) { if (_keepChangeIndex.Keep == null) { _keepChangeIndex.Keep = new List <int>(); } if (!_keepChangeIndex.Keep.Contains(nIndex)) { _keepChangeIndex.Keep.Add(nIndex); } } } } } if (foodList.Last().Change != null) { string[] strChangeIndex = foodList.Last().Change.Split(','); if (strChangeIndex != null) { foreach (string strIndex in strChangeIndex) { if (int.TryParse(strIndex, out int nIndex)) { if (_keepChangeIndex.Change == null) { _keepChangeIndex.Change = new List <int>(); } if (!_keepChangeIndex.Change.Contains(nIndex)) { _keepChangeIndex.Change.Add(nIndex); } } } } } foodList.RemoveAt(foodList.Count - 1); foodList.Sort(delegate(Food x, Food y) { if (x.MealType.ToLower() == y.MealType.ToLower()) { return(0); } else if ((x.MealType.ToLower() == "breakfast") || (y.MealType.ToLower() == "dinner")) { return(-1); } else if ((x.MealType.ToLower() == "dinner") || (y.MealType.ToLower() == "breakfast")) { return(1); } else { return(0); } }); foreach (Food food in foodList) { if (!_listFoodHistory.Contains(food.Index)) { _listFoodHistory.Add(food.Index); } if ((_keepChangeIndex != null) && (_keepChangeIndex.Keep != null)) { food.CheckKeep = _keepChangeIndex.Keep.Contains(food.Index); } } } return(foodList); } catch (Exception ex) { #if SHOW_DEBUG Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ", ex.Message); #endif //return null; throw ex; } }