Ejemplo n.º 1
0
        public WindowBorderLess()
        {
            InitializeComponent();

            Scheduler = TaskScheduler.FromCurrentSynchronizationContext();

            LoadingSplash.Visibility = Visibility.Visible;

            LoadTask = Task.Factory.StartNew <ClientApp.Model.FoodSource>(() =>
            {
                FoodData       = new ClientApp.Model.FoodSource();
                FoodData.Title = "Reverse Vending Machine - Derpton Middle School";

                Directory.CreateDirectory(AppDataPath);     //it already checks for it

                try
                {
                    if (System.IO.File.Exists(AppDataPath + "new_products.csv"))
                    {
                        System.IO.File.Delete(AppDataPath + "products.csv");
                        System.IO.File.Copy(AppDataPath + "new_products.csv", AppDataPath + "products.csv");
                        System.IO.File.Delete(AppDataPath + "new_products.csv");
                    }
                }
                catch
                {
                    try
                    {
                        System.IO.File.Delete(AppDataPath + "new_products.csv");
                    }
                    catch
                    {
                    }
                }

                FoodData.Foods = new ObservableCollection <ClientApp.Model.Food>();

                List <ClientApp.Model.Food> tempFoods = new List <ClientApp.Model.Food>();
                foreach (var food in DownloadAndParse())
                {
                    string[] searchprops = { food.Category, food.SubCat1, food.SubCat2, food.Name, food.UPC };
                    foreach (var word in string.Join(" ", searchprops).Split())
                    {
                        string target = StripPunctuation(word.ToLower());
                        if (FoodCache.ContainsKey(target) == false)
                        {
                            FoodCache.Add(target, new List <ClientApp.Model.Food>());
                        }
                        FoodCache[target].Add(food);
                    }
                    string[] foodpath = { food.Category, food.SubCat1, food.SubCat2, "" };     //the empty string is for the final slash
                    food.Category     = string.Join("/", foodpath);
                    tempFoods.Add(food);
                }
                var foods = from food in tempFoods
                            orderby food.Name ascending
                            select food;
                foreach (ClientApp.Model.Food food in foods)
                {
                    FoodData.Foods.Add(food);
                }
                return(FoodData);
            }).ContinueWith((i) => {
                FoodData                = i.Result;
                DataContext             = i.Result;
                FoodsView.ItemsSource   = i.Result.Foods;
                WithIngredientsFoodData = i.Result.Foods;
                Task.Factory.StartNew <ObservableCollection <Food> >((x) =>
                {
                    return(new ObservableCollection <Food>(FoodData.Foods.Where(food => food.Ingredients != "")));
                }, Scheduler).ContinueWith((y) =>
                {
                    NoIngredientsFoodData    = y.Result;
                    LoadingSplash.Visibility = Visibility.Hidden;
                    CacheBuilt = true;
                });
            }, Scheduler);
        }
Ejemplo n.º 2
0
        void UpdateSearch()
        {
            if (SearchBox.Text == "")
            {
                SearchLabel.Visibility = Visibility.Visible;

                DataContext           = FoodData;
                FoodsView.ItemsSource = FoodData.Foods;
            }
            else
            {
                SearchLabel.Visibility = Visibility.Hidden;

                _foodSource       = new ClientApp.Model.FoodSource();
                _foodSource.Title = FoodData.Title;
                _foodSource.Foods = new ObservableCollection <ClientApp.Model.Food>();
                Dictionary <ClientApp.Model.Food, int> _resultsDict = new Dictionary <ClientApp.Model.Food, int>();
                String[]      searchWords = SearchBox.Text.ToLower().Split();
                List <String> orWords     = new List <String>();
                List <String> andWords    = new List <String>();
                List <String> notWords    = new List <String>();
                bool          andDefault  = (searchWords[0].StartsWith("+"));
                if (andDefault)
                {
                    searchWords[0] = searchWords[0].Substring(1);
                }
                foreach (string searchWord in searchWords)
                {
                    if (searchWord == "")
                    {
                        return;
                    }
                    if (searchWord.StartsWith("-"))
                    {
                        notWords.Add(StripPunctuation(searchWord.Substring(1)));
                    }
                    else if (andDefault)
                    {
                        andWords.Add(StripPunctuation(searchWord));
                    }
                    else
                    {
                        orWords.Add(StripPunctuation(searchWord));
                    }
                }
                bool firstWord = true;
                if (orWords.Count > 0)
                {
                    foreach (KeyValuePair <String, List <ClientApp.Model.Food> > foodEntry in FoodCache)
                    {
                        firstWord = true;
                        foreach (string searchWord in orWords)
                        {
                            if (foodEntry.Key.StartsWith(searchWord))
                            {
                                foreach (ClientApp.Model.Food food in foodEntry.Value)
                                {
                                    if (_resultsDict.ContainsKey(food))
                                    {
                                        _resultsDict[food] += 1;
                                    }
                                    else if (firstWord)
                                    {
                                        _resultsDict.Add(food, 0);
                                    }
                                }
                            }
                            firstWord = false;
                        }
                    }
                }
                else if (andWords.Count > 0)
                {
                    Dictionary <ClientApp.Model.Food, List <String> > _andDict = new Dictionary <ClientApp.Model.Food, List <String> >();
                    foreach (KeyValuePair <String, List <ClientApp.Model.Food> > foodEntry in FoodCache)
                    {
                        foreach (string searchWord in andWords)
                        {
                            if (foodEntry.Key.StartsWith(searchWord))
                            {
                                foreach (ClientApp.Model.Food food in foodEntry.Value)
                                {
                                    if (_andDict.ContainsKey(food))
                                    {
                                        if (!_andDict[food].Contains(searchWord))
                                        {
                                            _andDict[food].Add(searchWord);
                                        }
                                    }
                                    else
                                    {
                                        _andDict.Add(food, new List <String>());
                                        _andDict[food].Add(searchWord);
                                    }
                                    if (_andDict[food].Count == andWords.Count && _resultsDict.ContainsKey(food) == false)
                                    {
                                        _resultsDict.Add(food, 0);
                                    }
                                }
                            }
                        }
                    }
                }
                else if (notWords.Count > 0)
                {
                    foreach (ClientApp.Model.Food food in FoodData.Foods)
                    {
                        _resultsDict.Add(food, 0);
                    }
                }
                if (notWords.Count > 0)
                {
                    foreach (KeyValuePair <String, List <ClientApp.Model.Food> > foodEntry in FoodCache)
                    {
                        foreach (string searchWord in notWords)
                        {
                            if (foodEntry.Key.StartsWith(searchWord))
                            {
                                foreach (ClientApp.Model.Food food in foodEntry.Value)
                                {
                                    if (_resultsDict.ContainsKey(food))
                                    {
                                        _resultsDict.Remove(food);
                                    }
                                }
                            }
                        }
                    }
                }
                if (andWords.Count == 0)
                {
                    var foods = from food in _resultsDict
                                orderby food.Value descending
                                select food.Key;
                    foreach (ClientApp.Model.Food food in foods)
                    {
                        _foodSource.Foods.Add(food);
                    }
                }
                else
                {
                    _foodSource.Foods = new ObservableCollection <Food>(_resultsDict.Keys);
                }
                DataContext           = _foodSource;
                FoodsView.ItemsSource = _foodSource.Foods;
            }
        }