Exemplo n.º 1
0
        public void UseInventory(Ingredient ingredient)
        {
            var totalIndex = TotalInv.IndexOf(TotalInv.Where(i => i.Name == ingredient.Name).FirstOrDefault());

            if (totalIndex == -1 || TotalInv[totalIndex].Weight < ingredient.Weight)
            {
                Console.WriteLine($"There is not enough {ingredient.Name} in your inventory");
                return;
            }
            var usedWeight = ingredient.Weight;

            while (usedWeight > 0)
            {
                var listIndex = InventoryList.IndexOf(InventoryList.Where(i => i.Name == ingredient.Name).FirstOrDefault());
                if (InventoryList[listIndex].Weight < usedWeight)
                {
                    usedWeight -= InventoryList[listIndex].Weight;
                    InventoryList.RemoveAt(listIndex);
                }
                else
                {
                    InventoryList[listIndex].Weight -= usedWeight;
                    usedWeight = 0;
                }
            }
            AdjustTotalInventory(ingredient);
        }
        public override async void OnNavigatedTo(INavigationParameters parameters)
        {
            try
            {
                if (parameters.ContainsKey("inventoryList"))
                {
                    List <InventoryThumbnail> inventoryList = (List <InventoryThumbnail>)parameters["inventoryList"];

                    foreach (var inventoryThumbnail in inventoryList)
                    {
                        var wrappedInventory = InventoryList.FirstOrDefault(x => x.Item.inventory_id == inventoryThumbnail.inventory_id);
                        if (wrappedInventory == null)
                        {
                            WrappedSelection <InventoryThumbnail> temp = new WrappedSelection <InventoryThumbnail>()
                            {
                                Item = inventoryThumbnail, IsSelected = false
                            };
                            InventoryList.Add(temp);
                        }
                        else
                        {
                            int index = _inventoryList.IndexOf(wrappedInventory);
                            InventoryList.Remove(wrappedInventory);
                            InventoryList.Insert(index, new WrappedSelection <InventoryThumbnail> {
                                Item = inventoryThumbnail, IsSelected = wrappedInventory.IsSelected
                            });
                        }
                    }

                    AccessionCount = InventoryList.Select(i => i.Item.accession_id).Distinct().Count();
                }
                if (parameters.ContainsKey("InventoryThumbnail"))
                {
                    InventoryThumbnail inventoryThumbnail = (InventoryThumbnail)parameters["InventoryThumbnail"];

                    WrappedSelection <InventoryThumbnail> inventoryThumbnailItem = _inventoryList.FirstOrDefault(item => item.Item.inventory_id == inventoryThumbnail.inventory_id);
                    if (inventoryThumbnailItem == null)
                    {
                        InventoryList.Add(new WrappedSelection <InventoryThumbnail> {
                            Item = inventoryThumbnail, IsSelected = false
                        });
                    }
                    else
                    {
                        int index = InventoryList.IndexOf(inventoryThumbnailItem);
                        InventoryList.Remove(inventoryThumbnailItem);
                        InventoryList.Insert(index, new WrappedSelection <InventoryThumbnail> {
                            Item = inventoryThumbnail, IsSelected = false
                        });
                    }
                }
                if (parameters.ContainsKey("message"))
                {
                    await _pageDialogService.DisplayAlertAsync("Message", (string)parameters["message"], "OK");
                }
            }
            catch (Exception ex)
            {
                await _pageDialogService.DisplayAlertAsync("Error", ex.Message, "OK");
            }
        }