/// <summary> /// Method to search for more recipes asynchronously /// </summary> private async void SearchForMoreRecipesAsync() { // Show refreshing symbol RecipeListView.IsRefreshing = true; // Get the data var result = await App.MasterController.EdamamApiHelper.QueryAsync(RecipeFilterText.Trim(), edamamResponse.From + 10, edamamResponse.To + 10); edamamResponse = result; // Populate the list view RecipeListView.IsRefreshing = false; result.Hits.ForEach(hit => Recipes.Add(hit.Recipe)); }
/// <summary> /// Method to search for recipes asynchronously /// </summary> private async void SearchForRecipeAsync() { // Clear the list view and display refreshing symbol Recipes.Clear(); RecipeListView.IsRefreshing = true; // Get the data var result = await App.MasterController.EdamamApiHelper.QueryAsync(RecipeFilterText.Trim()); edamamResponse = result; // Populate the list view RecipeListView.IsRefreshing = false; result.Hits.ForEach(hit => Recipes.Add(hit.Recipe)); }
/// <summary> /// Method to query the api using the given url /// </summary> /// <param name="url"></param> /// <returns></returns> private async Task <EdamamResponse> QueryUrlAsync(string url) { try { // Get the json var queryResponse = await GetStringResponse(url); // Parse the json string to an object var content = EdamamResponse.FromJson(queryResponse); SanitiseEdamamResponse(content); // Return the response object return(content); } catch (Exception e) { App.Log.Error("EdamamQueryUrlAsync", "Could not query url " + url + "\n" + e.StackTrace + "\n" + e.Message); return(null); } }
/// <summary> /// Method to parse ingredients in the EdamamResponse given pulling the quantity, measurement and food items out of the concatenated text part. /// </summary> /// <param name="response"></param> private void SanitiseEdamamResponse(EdamamResponse response) { try { foreach (var hit in response.Hits) { foreach (var ing in hit.Recipe.Ingredients) { // Attempt to populate the data fields from the text if they have not been populated by the API // Skip this iteration if they have been populated if (ing.Food != null || ing.Measure != null || ing.Quantity != 0.0) { continue; } // First we want to remove any text within brackets var text = ing.Text; if (text.Any(c => c == '(') && text.Any(c => c == ')')) { // Find start bracket var start = text.IndexOf('('); var end = text.LastIndexOf(')'); // Remove any text between the brackets text.Remove(start, end - start); } // Split into constituent parts var split = text.Split(' '); string food; if (double.TryParse(split.First(), out var quantity)) { var measure = split[1]; food = string.Empty; if (split.Length == 2) { measure = "<unit>"; food = split[1]; } else { for (var i = 2; i < split.Length; i++) { food += split[i] + " "; } } ing.Food = food; ing.Measure = measure; ing.Quantity = quantity; } else { // Check for fractional first value, otherwise assume the entire string is the item text var first = split.First().Split('/'); if (double.TryParse(first.First(), out var firstNum) && double.TryParse(first.Last(), out var secondNum)) { food = ""; for (var i = 2; i < split.Length; i++) { food += split[i] + " "; } ing.Food = food; ing.Quantity = firstNum / secondNum; ing.Measure = split[1]; } else { ing.Food = text; ing.Measure = "grams"; ing.Quantity = Math.Round(ing.Weight, 0); ing.Quantity = ing.Weight; } } } } } catch (Exception ex) { App.Log.Error("SanitiseEdamamResponse", ex.Message + "\n" + ex.StackTrace); } }