ItemsSetter CreateItem itemIndex issue

Issue #91 resolved
Former user created an issue

Hi,

I have created a subclass of ItemsSetter, and I have noticed that CreateItem receives different associations of "itemContext" and "itemIndex" for the same collection.

After further investigation, it looks like if ItemsSetter.OnCollectionItemAdded calls CreateItem with an index of "collection.count" (therefore passing an index of 1 to CreateItem for the first itemContext added to the collection). On the other hand, ItemsSetter.CreateItems calls CreateItem with an index of 0 for the first item in the IEnumerable. In my case, ItemsSetter.CreateItems gets called upon enabling the behavior, even when items have not changed in the collection, and my implementation of CreateItem.

Question: is there a way for CreateItem to always receive the same itemIndex for the same itemContext when the collection is unchanged?

Thanks Nicolas

In ItemsSetter.OnCollectionItemAdded, CreateItem is called with an index of 1 for the first item added to the collection (I am tempted to change it to "this.collection.Count - 1" as a temporary fix.)

 private void OnCollectionItemAdded(object item)
        {
            // Create game object for item.
            this.CreateItem(item, this.collection.Count);
            this.OnItemsChanged();
        }

In ItemsSetter.CreateItems(IEnumerable itemContexts), CreateItem is called with an index of 0 for the first item added to the collection.

 // Fill with objects from collection.
            var itemIndex = 0;
            foreach (var itemContext in itemContexts)
            {
                // Create game object for item.
                this.CreateItem(itemContext, itemIndex++);
            }

            this.OnItemsChanged();

Comments (5)

  1. Christian Oeing repo owner
    • changed status to open

    Will create a unit test for this issue and fix it afterwards. It definitivly looks like a "off-by-one" bug.

  2. Christian Oeing repo owner

    Added two unit tests to check the passed item index during initialization and after adding an item. The fix was indeed to subtract 1 from this.collection.Count in OnCollectionItemAdded. Will be live with the next release.

  3. Log in to comment