Exemplo n.º 1
0
 public void AddItem(Item item, bool update = true)
 {
     if (item.GetMaxStack() > 1)
     {
         for (int i = 0; i < items.Count; i++)
         {
             if (items[i].Stackable(item) && items[i].GetQuantity() < items[i].GetMaxStack())
             {
                 int addAmount = Math.Min(items[i].GetMaxStack() - items[i].GetQuantity(), item.GetQuantity());
                 items[i].AddQuantity(addAmount);
                 item.AddQuantity(-addAmount);
                 if (update)
                 {
                     ItemModified?.Invoke(this, items[i], i);
                 }
                 if (item.GetQuantity() <= 0)
                 {
                     break;
                 }
             }
         }
     }
     if (item.GetQuantity() >= 1)
     {
         items.Add(item);
         if (update)
         {
             ItemAddedToInventory?.Invoke(this, item, items.Count - 1);
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Gets or sets the element at the specified index.
        /// </summary>
        /// <value>
        /// The item.
        /// </value>
        /// <param name="index">The index.</param>
        /// <returns>
        /// The item at the specified index.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// </exception>
        public virtual T this[int index]
        {
            get
            {
                if (index < 0 || index >= Count)
                {
                    throw new ArgumentOutOfRangeException(nameof(index));
                }

                return(InternalList[index]);
            }
            set
            {
                if (index < 0 || index >= Count)
                {
                    throw new ArgumentOutOfRangeException(nameof(index));
                }

                T old = InternalList[index];
                if (!EqualityComparer <T> .Default.Equals(old, value))
                {
                    InternalList[index] = value;
                    ItemModified?.Invoke(this, old, value);
                }
            }
        }
Exemplo n.º 3
0
        public virtual void AddOrUpdate(IEnumerable <TEntity> collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            foreach (var entity in collection)
            {
                // Check to see if this is a new entity (by checking the key)
                if (IsNew(entity))
                {
                    InternalSet.Add(entity);
                    ItemAdded?.Invoke(entity);
                }
                else
                {
                    // Is this entity already attached?
                    var entry = DataContext.ChangeTracker.Entries <TEntity>().SingleOrDefault(n => KeysEqual(n.Entity, entity));
                    if (entry == null)
                    {
                        // BUG: Entity exists but is not the one they saved here...
                        InternalSet.Add(entity);
                        entry = DataContext.ChangeTracker.Entries <TEntity>().SingleOrDefault(n => KeysEqual(n.Entity, entity));
                    }

                    entry.State = EntityState.Modified;

                    ItemModified?.Invoke(entity);
                }
            }
        }
Exemplo n.º 4
0
 void OnItemModified(SolutionItemModifiedEventArgs e)
 {
     if (ParentFolder == null && ParentSolution != null)
     {
         ParentSolution.OnEntryModified(e);
     }
     ItemModified?.Invoke(this, e);
 }
Exemplo n.º 5
0
 public void UpdateItem(Item item, int index, bool update = false)
 {
     items[index] = item;
     if (update)
     {
         ItemModified?.Invoke(this, items[index], index);
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// Raises <see cref="CollectionModified" /> and <see cref="ItemModified" /> events
 /// </summary>
 /// <param name="e">An <see cref="EasyTabs.EventList.ListItemEventArgs" /> that contains the event data</param>
 protected virtual void OnItemModified(ListItemEventArgs e)
 {
     if (IgnoreEvents)
     {
         return;
     }
     ItemModified?.Invoke(this, e);
     OnCollectionModified(new ListModificationEventArgs(ListModification.ItemModified, e.ItemIndex, 1));
 }
Exemplo n.º 7
0
 private PropertyWatcherList <T> SetupPropertyWatcher()
 {
     if (_propertiesToWatch != null)
     {
         var watcherList = new PropertyWatcherList <T>(_propertiesToWatch);
         watcherList.ValueChanged += (i, v) => ItemModified?.Invoke(this, i, v);
         return(watcherList);
     }
     return(null);
 }
Exemplo n.º 8
0
        public virtual void AddOrUpdate(IEnumerable <TEntity> collection)
        {
            foreach (var entity in collection ?? throw new ArgumentNullException(nameof(collection)))
            {
                // Check to see if this is a new entity (by checking the key)
                if (IsNew(entity))
                {
                    InternalSet.Add(entity);
                    ItemAdded?.Invoke(entity);
                }
                else
                {
                    // Is this entity already attached?
                    var entry = GetEntryByKey(entity);
                    if (entry.Entity.GetHashCode() != entity.GetHashCode())                     // Objects are NOT the same!
                    {
                        throw new NotSupportedException("A different entity object with the same key already exists in the ChangeTracker");
                    }

                    entry.State = EntityState.Modified;
                    ItemModified?.Invoke(entity);
                }
            }
        }