void loadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            try
            {
                using (var reader = new StreamReader(e.Result))
                {
                    String       json     = reader.ReadToEnd();
                    JObject      o        = JObject.Parse(json);
                    Menu.Venue[] tempVens = new Menu.Venue[30];
                    int          i        = 0;

                    String pass = (String)o["PASSOVER"];
                    if (pass.Equals("true"))
                    {
                        (App.Current as App).passover = true;
                    }
                    else
                    {
                        (App.Current as App).passover = false;
                    }

                    if (o[meal.Text.ToUpper()] != null && o[meal.Text.ToUpper()].HasValues)
                    {
                        foreach (JToken venue in o[meal.Text.ToUpper()].Children())
                        {
                            //TODO allocate smarter
                            Menu.Venue.Dish[] tempDishes = new Menu.Venue.Dish[60];
                            String            temp       = venue.ToString().Substring(1, 40);
                            String            venName    = temp.Remove(temp.IndexOf("\""));
                            int j = 0;
                            foreach (JToken dish in venue.Children().Children())
                            {
                                bool ovolacto, vegan, halal, passover, hasNutrition, gf;

                                String name = (String)dish["name"];

                                String ovo = (String)dish["ovolacto"];
                                if (ovo.Equals("true"))
                                {
                                    ovolacto = true;
                                }
                                else
                                {
                                    ovolacto = false;
                                }

                                String veg = (String)dish["vegan"];
                                if (veg.Equals("true"))
                                {
                                    vegan = true;
                                }
                                else
                                {
                                    vegan = false;
                                }

                                if ((App.Current as App).passover)
                                {
                                    String passO = (String)dish["passover"];
                                    if (passO.Equals("true"))
                                    {
                                        passover = true;
                                    }
                                    else
                                    {
                                        passover = false;
                                    }
                                }
                                else
                                {
                                    passover = false;
                                }

                                String hal = (String)dish["halal"];
                                if (hal.Equals("true"))
                                {
                                    halal = true;
                                }
                                else
                                {
                                    halal = false;
                                }

                                String glutenF = (String)dish["gluten_free"];
                                if (glutenF.Equals("true"))
                                {
                                    gf = true;
                                }
                                else
                                {
                                    gf = false;
                                }

                                Newtonsoft.Json.Linq.JValue dummy = new Newtonsoft.Json.Linq.JValue(false);
                                if (dummy.GetType().FullName.Equals(dish["nutrition"].GetType().FullName))
                                {
                                    hasNutrition    = false;
                                    tempDishes[j++] = new Menu.Venue.Dish(name, hasNutrition, ovolacto, vegan, passover, halal, gf);
                                }
                                else
                                {
                                    hasNutrition = true;
                                    float[] nutrition = new float[20];
                                    int     k         = 0;
                                    foreach (JToken child in dish["nutrition"])
                                    {
                                        nutrition[k++] = (float)child;
                                    }
                                    tempDishes[j++] = new Menu.Venue.Dish(name, hasNutrition, ovolacto, vegan, passover, halal, gf, nutrition);
                                }
                            }
                            tempVens[i++] = new Menu.Venue(venName, tempDishes);
                        }
                    }
                    else
                    {
                        listBox.Items.Add(new Menu.Venue("No menu available \nfor selected meal.", null));
                        return;
                    }
                    menu = new Menu(tempVens);
                }

                bool pFlag, oFlag, vFlag, gfFlag, compositeBool;
                if ((App.Current as App).passover && (App.Current as App).passoverFilter)
                {
                    pFlag = true;
                }
                else
                {
                    pFlag = false;
                }
                if ((App.Current as App).ovoFilter)
                {
                    oFlag = true;
                }
                else
                {
                    oFlag = false;
                }
                if ((App.Current as App).veganFilter)
                {
                    vFlag = true;
                }
                else
                {
                    vFlag = false;
                }
                if ((App.Current as App).gfFilter)
                {
                    gfFlag = true;
                }
                else
                {
                    gfFlag = false;
                }

                foreach (Menu.Venue ven in menu.venues)
                {
                    if (ven != null)
                    {
                        bool added = false;
                        listBox.Items.Add(ven);
                        foreach (Menu.Venue.Dish dish in ven.dishes)
                        {
                            if (dish != null)
                            {
                                if (pFlag)
                                {
                                    if (gfFlag && oFlag)
                                    {
                                        compositeBool = (dish.passover && (dish.vegan || dish.ovolacto) && dish.gf);
                                    }
                                    else if (gfFlag && vFlag)
                                    {
                                        compositeBool = (dish.passover && dish.vegan && dish.gf);
                                    }
                                    else if (oFlag)
                                    {
                                        compositeBool = (dish.passover && (dish.vegan || dish.ovolacto));
                                    }
                                    else if (vFlag)
                                    {
                                        compositeBool = (dish.passover && dish.vegan);
                                    }
                                    else if (gfFlag)
                                    {
                                        compositeBool = (dish.passover && dish.gf);
                                    }
                                    else
                                    {
                                        compositeBool = dish.passover;
                                    }
                                }
                                else
                                if (gfFlag && oFlag)
                                {
                                    compositeBool = ((dish.vegan || dish.ovolacto) && dish.gf);
                                }
                                else if (gfFlag && vFlag)
                                {
                                    compositeBool = (dish.vegan && dish.gf);
                                }
                                else if (oFlag)
                                {
                                    compositeBool = (dish.vegan || dish.ovolacto);
                                }
                                else if (vFlag)
                                {
                                    compositeBool = dish.vegan;
                                }
                                else if (gfFlag)
                                {
                                    compositeBool = dish.gf;
                                }
                                else
                                {
                                    compositeBool = true;
                                }

                                if (compositeBool)
                                {
                                    listBox.Items.Add(dish);
                                    added = true;
                                }
                            }
                        }
                        if (!added)
                        {
                            listBox.Items.Remove(ven);
                        }
                        else
                        {
                            listBox.Items.Add(new Menu.Venue("\t", null));
                        }
                    }
                }
                textBlock1.Visibility = Visibility.Collapsed;
            }
            catch (Exception except)
            {
                textBlock1.Visibility = Visibility.Collapsed;
                listBox.Items.Add(new Menu.Venue("No menu available \nfor selected meal", null));
                Console.WriteLine("Parsing exception: {0}", except);
            }
        }
Пример #2
0
        void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            try
            {
                using (var reader = new StreamReader(e.Result))
                {
                    String json = reader.ReadToEnd();
                    JObject o = JObject.Parse(json);
                    Menu.Venue[] tempVens = new Menu.Venue[30];
                    int i = 0;

                    if (o[meal.Text.ToUpper()] != null && o[meal.Text.ToUpper()].HasValues)
                        foreach (JToken venue in o[meal.Text.ToUpper()].Children())
                        {
                            Menu.Venue.Dish[] tempDishes = new Menu.Venue.Dish[60];
                            String temp = venue.ToString().Substring(1, 40);
                            String venName = temp.Remove(temp.IndexOf("\""));
                            int j = 0;
                            foreach (JToken dish in venue.Children().Children())
                            {
                                bool ovolacto, vegan, halal, passover, hasNutrition;

                                String name = (String)dish["name"];

                                String ovo = (String)dish["ovolacto"];
                                if (ovo.Equals("true"))
                                    ovolacto = true;
                                else
                                    ovolacto = false;

                                String veg = (String)dish["vegan"];
                                if (veg.Equals("true"))
                                    vegan = true;
                                else
                                    vegan = false;

                                String passO = (String)dish["passover"];
                                if (passO.Equals("true"))
                                    passover = true;
                                else
                                    passover = false;

                                String hal = (String)dish["halal"];
                                if (hal.Equals("true"))
                                    halal = true;
                                else
                                    halal = false;

                                Newtonsoft.Json.Linq.JValue dummy = new Newtonsoft.Json.Linq.JValue(false);
                                if (dummy.GetType().FullName.Equals(dish["nutrition"].GetType().FullName))
                                {
                                    hasNutrition = false;
                                    tempDishes[j++] = new Menu.Venue.Dish(name, hasNutrition, ovolacto, vegan, passover, halal);
                                }
                                else
                                {
                                    hasNutrition = true;
                                    float[] nutrition = new float[20];
                                    int k = 0;
                                    foreach (JToken child in dish["nutrition"])
                                        nutrition[k++] = (float)child;
                                    tempDishes[j++] = new Menu.Venue.Dish(name, hasNutrition, ovolacto, vegan, passover, halal, nutrition);
                                }

                            }
                            tempVens[i++] = new Menu.Venue(venName, tempDishes); ;
                        }
                    else
                        listBox.Items.Add(new Menu.Venue("No menu available \nfor selected meal", null));
                    menu = new Menu(tempVens);
                }
                textBlock1.Visibility = Visibility.Collapsed;
                if ((App.Current as App).ovoFilter)
                {
                    foreach (Menu.Venue ven in menu.venues)
                        if (ven != null)
                        {
                            bool added = false;
                            listBox.Items.Add(ven);
                            foreach (Menu.Venue.Dish dish in ven.dishes)
                                if (dish != null && (dish.ovolacto || dish.vegan))
                                {
                                    listBox.Items.Add(dish);
                                    added = true;
                                }
                            if (!added)
                                listBox.Items.Remove(ven);
                            else
                                listBox.Items.Add(new Menu.Venue("\t", null));
                        }
                }
                else if ((App.Current as App).veganFilter)
                {
                    foreach (Menu.Venue ven in menu.venues)
                        if (ven != null)
                        {
                            bool added = false;
                            listBox.Items.Add(ven);
                            foreach (Menu.Venue.Dish dish in ven.dishes)
                                if (dish != null && (dish.vegan))
                                {
                                    listBox.Items.Add(dish);
                                    added = true;
                                }
                            if (!added)
                                listBox.Items.Remove(ven);
                            else
                                listBox.Items.Add(new Menu.Venue("\t", null));
                        }
                }
                else
                    foreach (Menu.Venue ven in menu.venues)
                        if (ven != null)
                        {
                            listBox.Items.Add(ven);
                            foreach (Menu.Venue.Dish dish in ven.dishes)
                                if (dish != null)
                                    listBox.Items.Add(dish);
                            listBox.Items.Add(new Menu.Venue("\t", null));
                        }
            }
            catch (Exception execpt)
            {
                textBlock1.Visibility = Visibility.Collapsed;
                listBox.Items.Add(new Menu.Venue("No menu available \nfor selected meal", null));
            }
        }