public void SetItems_ListChangedIsRaisedAfterBehaviors()
        {
            var actionLog = new StringBuilder();

            var changeListener = new TestChangeListener();

            var ownerProperty = PropertyStub
                                .WithBehaviors(changeListener)
                                .Build();

            var ownerVM = ViewModelStub
                          .WithProperties(ownerProperty)
                          .Build();

            var collection = new VMCollection <IViewModel>(ownerVM, ownerProperty);

            changeListener.HandleChange += delegate {
                actionLog.Append("ChangeHandlerBehavior ");
            };

            collection.ListChanged += delegate {
                actionLog.Append("ListChanged ");
            };

            IVMCollection <IViewModel> c = collection;

            c.ReplaceItems(new[] { ViewModelStub.Build(), ViewModelStub.Build() }, null);

            Assert.AreEqual("ChangeHandlerBehavior ListChanged ", actionLog.ToString());
        }
        private void Repopulate(IBehaviorContext context, IVMCollection <TItemVM> collection, IChangeReason reason)
        {
            var sourceItems = GetSourceItems(context);

            IEnumerable <TItemVM> newItems = sourceItems
                                             .Select(s => CreateAndInitializeItem(context, s));

            collection.ReplaceItems(newItems, reason);
        }
        private void Repopulate(IBehaviorContext context, IVMCollection <TItemVM> collection, IChangeReason reason)
        {
            var sourceItems = this.GetValueNext <IEnumerable <TItemSource> >(context);

            var items = sourceItems
                        .Select(sourceItem => GetItemVM(context, sourceItem));

            collection.ReplaceItems(items, reason);
        }
        public void Refresh(IBehaviorContext context, RefreshOptions options)
        {
            // Call next behavior first, because a source accessor behavior may handle
            // it.
            this.RefreshNext(context, options);
            _collectionSourceCache.Clear(context);

            // ToArray so that (1) source is only enumerated once and (2) acess by index
            // (required by the equality check) is guaranteed to be fast.
            TItemSource[]           newSourceItems = GetSourceItems(context).ToArray();
            IVMCollection <TItemVM> vmCollection   = GetValue(context);

            IEnumerable <TItemVM> itemsToRefresh = Enumerable.Empty <TItemVM>();

            if (AreCollectionContentsEqual(vmCollection, newSourceItems))
            {
                itemsToRefresh = vmCollection;
            }
            else
            {
                Dictionary <TItemSource, TItemVM> previousItemsBySource = vmCollection.ToDictionary(
                    x => x.Source,
                    _reusabilitySourceComparer
                    );

                List <TItemVM> newItems    = new List <TItemVM>();
                List <TItemVM> reusedItems = new List <TItemVM>();

                foreach (TItemSource s in newSourceItems)
                {
                    TItemVM item;
                    bool    isReusedItem = previousItemsBySource.TryGetValue(s, out item);

                    if (isReusedItem)
                    {
                        reusedItems.Add(item);
                    }
                    else
                    {
                        item = CreateAndInitializeItem(context, s);
                    }

                    newItems.Add(item);
                }

                vmCollection.ReplaceItems(newItems, RefreshReason.Create(options.ExecuteRefreshDependencies));
                itemsToRefresh = reusedItems;
            }

            if (options.Scope.HasFlag(RefreshScope.Content))
            {
                itemsToRefresh
                .ForEach(x => x.Kernel.RefreshWithoutValidation(options.ExecuteRefreshDependencies));
            }
        }
        public void ReplaceItems_CallsHandleChangeWithCollectionPopulatedArgs()
        {
            var oldItems = new[] { CreateItem(), CreateItem() };
            var newItems = new[] { CreateItem() };

            Collection.Add(oldItems[0]);
            Collection.Add(oldItems[1]);

            ResetStub();
            IVMCollection <IViewModel> c = Collection;

            c.ReplaceItems(newItems, null);

            AssertChangeArgs(
                CollectionChangedArgs <IViewModel> .CollectionPopulated(Collection, oldItems)
                );
        }