private void PackageSolutionAppliances_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
 {
     NewPackagedSolutionCmd.NotifyCanExecuteChanged();
     SavePackagedSolutionCmd.NotifyCanExecuteChanged();
     PrintEnergyLabelCmd.NotifyCanExecuteChanged();
     UpdateEei();
 }
        /// <summary>
        /// Adds an appliance to the viewed packaged solution
        /// </summary>
        /// <param name="appliance"></param>
        private void AddApplianceToPackagedSolution(ApplianceInstance appliance)
        {
            AppliancesInPackagedSolution.Add(appliance);

            // Set save state to false
            IsDataSaved = false;

            // Notify the print function of changes
            PrintEnergyLabelCmd.NotifyCanExecuteChanged();
        }
        /// <summary>
        /// Save the viewed packaged solution to the database
        /// </summary>
        private void SaveCurrentPackagedSolution()
        {
            /* IMPORTANT
             * A packaged solution should not be saved if there exists
             *  a solar collector without a container tied to it. */
            using (var ctx = new AssistantContext())
            {
                // Copy visible list of appliance instances to the list in the model
                var newApplianceInstanceList = AppliancesInPackagedSolution.ToList();

                // Mark packaged solution as added if new or modified if old
                ctx.Entry(PackagedSolution).State = PackagedSolution.Id == 0 ? EntityState.Added : EntityState.Modified;

                // Mark removed appliance instances as deleted
                if (PackagedSolution.Id > 0)
                {
                    var previousPackagedSolution = ctx.PackagedSolutions.Include(a => a.ApplianceInstances).FirstOrDefault(p => p.Id == PackagedSolution.Id);
                    previousPackagedSolution?.ApplianceInstances.ToList().ForEach(instance =>
                    {
                        if (newApplianceInstanceList.All(a => a.Id != instance.Id))
                        {
                            ctx.Entry(instance).State = EntityState.Deleted;
                        }
                    });
                }

                // Attach appliances and appliance instances to avoid duplicates
                foreach (var appInstance in newApplianceInstanceList)
                {
                    ctx.Entry(appInstance.Appliance.DataSheet).State = EntityState.Unchanged;
                    ctx.Entry(appInstance.Appliance).State           = EntityState.Unchanged;

                    ctx.Entry(appInstance).State = appInstance.Id == 0 ? EntityState.Added : EntityState.Unchanged;
                }

                // Set new appliance instance list
                PackagedSolution.ApplianceInstances = newApplianceInstanceList;

                // Set the creation date to now
                if (PackagedSolution.CreationDate == default(DateTime))
                {
                    PackagedSolution.CreationDate = DateTime.Now;
                }

                // Save database changes
                ctx.SaveChanges();
            }

            // Set save state to true and notify print function of change
            IsDataSaved = true;
            PrintEnergyLabelCmd.NotifyCanExecuteChanged();
        }