Example #1
0
            private void DispatchRemovals <T>(List <PostponedUpdate> postponedUpdates,
                                              MvxObservableCollection <T> updateCallback, IList <T> source, int start, int count, int globalIndex)
            {
                if (!mDetectMoves)
                {
                    //updateCallback.onRemoved(start, count);
                    updateCallback.RemoveRange(start, count);
                    return;
                }
                for (int i = count - 1; i >= 0; i--)
                {
                    int status = mOldItemStatuses[globalIndex + i] & FLAG_MASK;
                    switch (status)
                    {
                    case 0:     // real removal
                        //updateCallback.onRemoved(start + i, 1);
                        updateCallback.RemoveAt(start + i);
                        foreach (PostponedUpdate upd in postponedUpdates)
                        {
                            upd.currentPos -= 1;
                        }
                        break;

                    case FLAG_MOVED_CHANGED:
                    case FLAG_MOVED_NOT_CHANGED:
                        int             pos    = mOldItemStatuses[globalIndex + i] >> FLAG_OFFSET;
                        PostponedUpdate update = RemovePostponedUpdate(postponedUpdates, pos,
                                                                       false);
                        // the item was moved to that position. we do -1 because this is a move not
                        // add and removing current item offsets the target move by 1
                        //noinspection ConstantConditions
                        //updateCallback.onMoved(start + i, update.currentPos - 1);
                        updateCallback.RemoveAt(start + i);
                        updateCallback.Insert(update.currentPos - 1, source[update.currentPos - 1]);
                        //if (status == FLAG_MOVED_CHANGED)
                        //{
                        //    // also dispatch a change
                        //    updateCallback.onChanged(update.currentPos - 1, 1,
                        //            mCallback.getChangePayload(globalIndex + i, pos));
                        //}
                        break;

                    case FLAG_IGNORE:     // ignoring this
                        postponedUpdates.Add(new PostponedUpdate(globalIndex + i, start + i, true));
                        break;

                    default:
                        throw new Exception(
                                  "unknown flag for pos " + (globalIndex + i) + " " + status);
                    }
                }
            }
            public void CollectionShouldBeUpdated(int index, int count)
            {
                Setup();

                var items     = new MvxObservableCollection <int[]>(new[] { new[] { 1, 2 }, new[] { 3 }, new[] { 4, 5, 6 } });
                var flatItems = GetFlatItems(items.Take(index, count)).ToArray();
                var flatCount = GetFlatItems(items).Count() - flatItems.Length;

                var sut = new FlatObservableCollection <int>(items);

                items.RemoveRange(index, count);

                Assert.AreEqual(flatCount, sut.Count);
                CollectionAssert.IsNotSubsetOf(flatItems, sut);
            }
            public void CollectionShouldBeUpdated(int start, int count)
            {
                Setup();

                var items = new MvxObservableCollection <int> {
                    1, 2, 3, 4, 5, 6
                };
                var oldItems = Transform(items.ToArray().Take(start, count));

                var sut = new WrappedObservableCollection <int, string>(items, Factory);

                items.RemoveRange(start, count);

                Assert.AreEqual(items.Count, sut.Count);
                CollectionAssert.IsNotSubsetOf(oldItems, sut);
            }
            public void RemoveEventShouldBeRaised(int index, int count)
            {
                Setup();

                var events    = new List <NotifyCollectionChangedEventArgs>();
                var items     = new MvxObservableCollection <int[]>(new[] { new[] { 1, 2 }, new[] { 3 }, new[] { 4, 5, 6 } });
                var flatIndex = GetFlatSectionIndex(items, index);
                var flatItems = GetFlatItems(items.Take(index, count));

                var sut = new FlatObservableCollection <int>(items);

                sut.CollectionChanged += (_, args) => events.Add(args);

                items.RemoveRange(index, count);

                Assert.AreEqual(1, events.Count);
                Assert.AreEqual(NotifyCollectionChangedAction.Remove, events[0].Action);
                Assert.AreEqual(flatIndex, events[0].OldStartingIndex);
                Assert.AreEqual(flatItems, events[0].OldItems);
            }
            public void RemoveEventShouldBeRaised(int start, int count)
            {
                Setup();

                var events = new List <NotifyCollectionChangedEventArgs>();
                var items  = new MvxObservableCollection <int> {
                    1, 2, 3, 4, 5, 6
                };
                var oldItems = Transform(items.ToArray().Take(start, count));

                var sut = new WrappedObservableCollection <int, string>(items, Factory);

                sut.CollectionChanged += (_, args) => events.Add(args);

                items.RemoveRange(start, count);

                Assert.AreEqual(1, events.Count);
                Assert.AreEqual(NotifyCollectionChangedAction.Remove, events[0].Action);
                Assert.AreEqual(start, events[0].OldStartingIndex);
                Assert.AreEqual(oldItems, events[0].OldItems);
            }
        public void RemoveRangeThrowsArgumentOutOfRangeTest(int start, int count, string[] items)
        {
            var collection = new MvxObservableCollection <string>(items);

            Assert.Throws <ArgumentOutOfRangeException>(() => collection.RemoveRange(start, count));
        }