public static async Task AddProductToCategoryAsync(CategoryVM category, IEnumerable <ProductVM> products) { await Db.Categories.AddProductsAsync(category.Model, products.Select(p => p.Model).ToList()).ConfigureAwait(false); foreach (var productVm in products) { if (category.AllProducts.Any(p => p.Model.Id == productVm.Model.Id)) { continue; } var isGoodProduct = true; if (category.IsFilterSetted) { isGoodProduct = Filters.IsGoodProduct(productVm.Model, category.FiltersManagerVM.FiltersVM.Model); } if (isGoodProduct) { await AsyncHelper.ExecuteAtUI(() => category.Products.Add(productVm)).ConfigureAwait(false); } category.AllProducts.Add(productVm); } }
private async void LoadProducts(CategoryVM category) { if (category is null) { return; } category.IsLoading = true; await AsyncHelper.ExecuteAtUI(() => category.Products.Clear()).ConfigureAwait(false); var productsFromDb = await Db.Categories.GetAllProductsAsync(category.Model).ConfigureAwait(false); var productVmForDataGrid = new List <ProductVM>(); foreach (var product in productsFromDb) { ProductVM productVm; if (!_allProducts.ContainsKey(product.Id)) { productVm = new ProductVM(product); _allProducts.Add(product.Id, productVm); } else { productVm = _allProducts[product.Id]; } productVmForDataGrid.Add(productVm); } await AsyncHelper.ExecuteAtUI(() => category.Products.AddRange(productVmForDataGrid)).ConfigureAwait(false); category.AllProducts.Clear(); category.AllProducts.AddRange(category.Products); category.IsLoading = false; category.IsProductsLoaded = true; }
private async Task GetCategoriesFromDb() { var expandedIds = GetExpandedCategoriesFromSettings(); var openedCategories = GetOpenedCategoriesFromSettings(); int selectedCategoryLastSesstion = 0; if (!string.IsNullOrWhiteSpace(Settings.Default.SelectedCategoryId)) { selectedCategoryLastSesstion = Convert.ToInt32(Settings.Default.SelectedCategoryId); } void ParseChildren(CategoryVM currentNode) { if (openedCategories.Contains(currentNode.Model.Id)) { var currentSelectedCategory = SelectedCategory; OpenCategory(currentNode); SelectedCategory = currentNode.Model.Id == selectedCategoryLastSesstion ? currentNode : currentSelectedCategory; } if (expandedIds.Contains(currentNode.Model.Id)) { currentNode.IsExpanded = true; } if (currentNode.Model.Children is null) { return; } foreach (var child in currentNode.Model.Children.OrderBy(c => c.SortOrder)) { var childVm = new CategoryVM(child) { ParentCollection = currentNode.ChildCategories }; currentNode.ChildCategories.Add(childVm); ParseChildren(childVm); } } var allCategories = await Db.Categories.AllAsync().ConfigureAwait(false); var firstLevel = allCategories.Where(c => c.ParentId == null).OrderBy(c => c.SortOrder).ToList(); if (!firstLevel.Any(c => c.Title == "Избранное" && c.IsFixed)) { await CreateFixedCategory("Избранное", 100).ConfigureAwait(false); } if (!firstLevel.Any(c => c.Title == "Черный список" && c.IsFixed)) { await CreateFixedCategory("Черный список", 200).ConfigureAwait(false); } foreach (var child in firstLevel) { var vm = new CategoryVM(child) { ParentCollection = Categories }; ParseChildren(vm); Categories.Add(vm); } }