public void Initialize(IEnumerable<IParticipant> participants, FoodContainerViewModel foodContainer, bool ignoreFood = false) { if (Participants != null && participants != null) { Participants.Clear(); foreach (var p in participants) { Participants.Add(new ParticipantViewModel(p)); } } if (foodContainer != null) { _foodContainer = foodContainer; if (foodContainer.Food != null) { if (!ignoreFood) // need because food now gets from list picker page and binds directly { Food = foodContainer.Food; SelectedFoodItem = AvailableFood.FirstOrDefault(f => f.Name == Food.Name) ?? AvailableFood.First(); } else { Food.Price = foodContainer.Food.Price; } if (foodContainer.Participants != null && Participants != null) { // move ate coefficient from selected participants to a list of available foreach (var par in foodContainer.Participants) { var equal = Participants.FirstOrDefault(p => p.Equals(par)); if (equal != null) equal.ParticipantAteCoefficient = par.ParticipantAteCoefficient; } } } if (foodContainer.Participants != null) { var newOnes = foodContainer.Participants.ToArray(); SelectedParticipants.Clear(); foreach (var p in newOnes) { SelectedParticipants.Add(new ParticipantViewModel(p)); } } UpdateActivatedAteCoefficientOnParticipants(AteRangeActivated); } CurrencySymbol = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol; }
public void RemoveFood(FoodContainerViewModel fc) { FoodContainers.Remove(fc); var eaten = ParticipantContainers.Where(p => p.Participant.EatenFood.Contains(fc.Food)).ToArray(); foreach (var e in eaten) { e.Participant.EatenFood.Remove(fc.Food); } }
public FoodContainerViewModel GetFoodContainer() { if (_isCanceled) return null; var selectedPartisipants = SelectedParticipants; // to be ensure that ate coefficient is turned off if (!AteRangeActivated) { foreach (var p in selectedPartisipants) { p.ParticipantAteCoefficient = 0; } } // cyclic link var eaters = selectedPartisipants.ToList(); Food.Name = SelectedFoodItem.Name; (Food as Food).Icon = SelectedFoodItem.Icon; //TODO: bad idea var food = Food; foreach (var e in eaters) { if (e.EatenFood == null) e.EatenFood = new List<IFood>(); if(!e.EatenFood.Contains(food)) e.EatenFood.Add(food); } food.Eaters = eaters; //Food.Eaters = selectedPartisipants; //foreach (var eater in Food.Eaters) //{ // if (eater.EatenFood == null) // eater.EatenFood = new List<IFood>(); // if(!eater.EatenFood.ToList().Contains(Food)) // eater.EatenFood.ToList().Add(Food); //} //var isEditing = _foodContainer != null ? _foodContainer.IsEditing : false; var id = _foodContainer != null ? _foodContainer.Id : Guid.NewGuid(); var container = new FoodContainerViewModel(id, food, new List<IParticipant>(SelectedParticipants)); return container; }
public void AddFoodContainer(FoodContainerViewModel foodContainer) { var eaters = foodContainer.Food.Eaters.ToList(); var food = foodContainer.Food; foreach (var e in eaters) { if (e.EatenFood == null) e.EatenFood = new List<IFood>(); if (!e.EatenFood.Contains(food)) e.EatenFood.Add(food); } food.Eaters = eaters; if (!FoodContainers.Contains(foodContainer)) { FoodContainers.Add(new FoodContainerViewModel(foodContainer.Id, food, eaters)); } else { var foundContainer = FoodContainers.FirstOrDefault(fc => fc.Equals(foodContainer)); if (foundContainer != null) { foundContainer.Food = food; foodContainer.Participants.Clear(); foreach (var eater in eaters) { foodContainer.Participants.Add(eater); } } } RearangeParticipantContainers(); }