Esempio n. 1
0
        public void ClearEntries()
        {
            _isClearingLog = true;

            // Note: we need to dispatch because the FastObservableCollection automatically dispatches (which is a good thing
            // when coming from a background thread). However... the ReplaceRange will be executed *outside* the lock
            // which is not good. So the lock is inside the dispatcher handler, and we manually dispatcher here.
            // Note: don't use BeginInvoke here because we need to wait until action will be processed
            _dispatcherService.Invoke(() =>
            {
                lock (_lock)
                {
                    using (_logEntries.SuspendChangeNotifications())
                    {
                        _logEntries.Clear();

                        var typeNames = TypeNames;
                        if (typeNames != null)
                        {
                            using (typeNames.SuspendChangeNotifications())
                            {
                                typeNames.ReplaceRange(new[] { _defaultComboBoxItem });
                            }
                        }
                    }
                }

                ResetEntriesCount();

                _isClearingLog = false;
            }, false);
        }
            public void CascadedAddingItemsInAddingModeWithInterceptingDisposing()
            {
                var counter   = 0;
                var eventArgs = (NotifyCollectionChangedEventArgs)null;

                var fastCollection = new FastObservableCollection <int>();

                fastCollection.AutomaticallyDispatchChangeNotifications = false;
                fastCollection.CollectionChanged += (sender, e) =>
                {
                    counter++;
                    eventArgs = e;
                };

                var firstToken  = fastCollection.SuspendChangeNotifications(SuspensionMode.Adding);
                var secondToken = fastCollection.SuspendChangeNotifications(SuspensionMode.Adding);

                fastCollection.Add(1);
                fastCollection.Add(2);

                secondToken.Dispose();
                Assert.AreEqual(0, counter);
                Assert.IsNull(eventArgs);

                fastCollection.Add(3);
                fastCollection.Add(4);
                fastCollection.Add(5);

                firstToken.Dispose();
                Assert.AreEqual(1, counter);
                // ReSharper disable PossibleNullReferenceException
                Assert.AreEqual(NotifyCollectionChangedAction.Add, eventArgs.Action);
                CollectionAssert.AreEqual(eventArgs.NewItems, new[] { 1, 2, 3, 4, 5 });
                // ReSharper restore PossibleNullReferenceException
            }
            public void ThrowsInvalidOperationExceptionForChangingMode()
            {
                var fastCollection = new FastObservableCollection <int> {
                    0
                };

                using (fastCollection.SuspendChangeNotifications(SuspensionMode.Adding))
                {
                    Assert.Throws <InvalidOperationException>(() => { using (fastCollection.SuspendChangeNotifications(SuspensionMode.Removing)) { } });
                }
            }
Esempio n. 4
0
            public void ReturnsFalseAfterChangedDisposing()
            {
                var fastCollection = new FastObservableCollection <int>();

                var firstToken  = fastCollection.SuspendChangeNotifications();
                var secondToken = fastCollection.SuspendChangeNotifications();

                firstToken.Dispose();
                secondToken.Dispose();

                Assert.IsFalse(fastCollection.NotificationsSuspended);
            }
Esempio n. 5
0
            public void RemovingItemsInRemovingMode()
            {
                var counter   = 0;
                var eventArgs = (NotifyCollectionChangedEventArgs)null;

                var fastCollection = new FastObservableCollection <int> {
                    1, 2, 3, 4, 5
                };

                fastCollection.AutomaticallyDispatchChangeNotifications = false;
                fastCollection.CollectionChanged += (sender, e) =>
                {
                    counter++;
                    eventArgs = e;
                };

                using (fastCollection.SuspendChangeNotifications(SuspensionMode.Removing))
                {
                    fastCollection.Remove(1);
                    fastCollection.Remove(2);
                    fastCollection.Remove(3);
                    fastCollection.Remove(4);
                    fastCollection.Remove(5);
                }

                Assert.AreEqual(1, counter);
                Assert.AreEqual(NotifyCollectionChangedAction.Remove, eventArgs.Action);
                CollectionAssert.AreEqual(eventArgs.OldItems, new[] { 1, 2, 3, 4, 5 });
            }
 private void ReplaceRange(FastObservableCollection <NavigationNode> selectedItems, List <NavigationNode> navigationNodes)
 {
     using (selectedItems.SuspendChangeNotifications())
     {
         ((ICollection <NavigationNode>)SelectedItems).ReplaceRange(navigationNodes);
     }
 }
            public void HandlesChangesOfSuspendedFastObservableCollectionCorrectly()
            {
                var       collection = new FastObservableCollection <TestModel>();
                TestModel model      = null;

                for (int i = 0; i < 10; i++)
                {
                    var randomModel = new TestModel();
                    collection.Add(randomModel);
                }

                var wrapper = new ChangeNotificationWrapper(collection);

                var collectionItemPropertyChanged = false;

                wrapper.CollectionItemPropertyChanged += (sender, e) => collectionItemPropertyChanged = true;

                var newModel = new TestModel();

                using (collection.SuspendChangeNotifications())
                {
                    collection.Clear();
                    collection.Add(newModel);
                }

                newModel.FirstName = "Geert";

                Assert.IsTrue(collectionItemPropertyChanged);
            }
            public void SuspendsValidationWhileAddingAndRemovingItems()
            {
                int counter = 0;

                var fastCollection = new FastObservableCollection<int>();
                fastCollection.AutomaticallyDispatchChangeNotifications = false;
                fastCollection.CollectionChanged += (sender, e) => counter++;

                using (fastCollection.SuspendChangeNotifications())
                {
                    fastCollection.Add(1);
                    fastCollection.Add(2);
                    fastCollection.Add(3);
                    fastCollection.Add(4);
                    fastCollection.Add(5);

                    fastCollection.Remove(5);
                    fastCollection.Remove(4);
                    fastCollection.Remove(3);
                    fastCollection.Remove(2);
                    fastCollection.Remove(1);
                }

                Assert.AreEqual(1, counter);
            }
Esempio n. 9
0
            public void HandlesChangesOfSuspendedFastObservableCollectionCorrectly()
            {
                var collection = new FastObservableCollection <TestModel>
                {
                    AutomaticallyDispatchChangeNotifications = false
                };

                for (var i = 0; i < 10; i++)
                {
                    var randomModel = new TestModel();
                    collection.Add(randomModel);
                }

                var wrapper = new ChangeNotificationWrapper(collection);

                var collectionItemPropertyChanged = false;

                wrapper.CollectionItemPropertyChanged += (sender, e) => collectionItemPropertyChanged = true;

                var newModel = new TestModel();

                using (collection.SuspendChangeNotifications())
                {
                    collection.Clear();
                    collection.Add(newModel);
                }

                newModel.FirstName = "Geert";

                Assert.IsTrue(collectionItemPropertyChanged, "Collection item property should have changed");
            }
Esempio n. 10
0
        public ManageFontModel()
        {
            Fonts      = new FastObservableCollection <string>();
            FontStyles = new FastObservableCollection <string>();
            FontSizes  = new FastObservableCollection <string>();

            using (var installedFontCollection = new InstalledFontCollection())
            {
                var fonts = installedFontCollection.Families;

                foreach (var font in fonts)
                {
                    Fonts.Add(font.Name);
                }
            }

            using (FontStyles.SuspendChangeNotifications())
            {
                foreach (var propertyInfo in typeof(FontStyles).GetProperties())
                {
                    FontStyles.Add(propertyInfo.Name);
                }
            }

            using (FontSizes.SuspendChangeNotifications())
            {
                for (var i = MinFontSize; i < MaxFontSize; i = i + FontSizeStep)
                {
                    FontSizes.Add($"{i}");
                }
            }
        }
Esempio n. 11
0
            public void HandlesClearOfSuspendedFastObservableCollectionCorrectly()
            {
                var collection = new FastObservableCollection <TestModel>
                {
                    AutomaticallyDispatchChangeNotifications = false
                };

                TestModel model = null;

                for (var i = 0; i < 10; i++)
                {
                    var randomModel = new TestModel();
                    collection.Add(randomModel);
                }

                model = collection[0];

                var wrapper = new ChangeNotificationWrapper(collection);

                var collectionItemPropertyChanged = false;

                wrapper.CollectionItemPropertyChanged += (sender, e) => collectionItemPropertyChanged = true;

                using (collection.SuspendChangeNotifications())
                {
                    collection.Clear();
                }

                model.FirstName = "Geert";

                Assert.IsFalse(collectionItemPropertyChanged);
            }
Esempio n. 12
0
            public void CallingResetWhileAddingItemsInAddingMode()
            {
                var counter   = 0;
                var eventArgs = (NotifyCollectionChangedEventArgs)null;

                var fastCollection = new FastObservableCollection <int>();

                fastCollection.AutomaticallyDispatchChangeNotifications = false;
                fastCollection.CollectionChanged += (sender, e) =>
                {
                    counter++;
                    eventArgs = e;
                };

                var token = fastCollection.SuspendChangeNotifications(SuspensionMode.Adding);

                fastCollection.Add(1);
                fastCollection.Add(2);

                fastCollection.Reset();
                Assert.AreEqual(0, counter);

                fastCollection.Add(3);
                fastCollection.Add(4);
                fastCollection.Add(5);
                token.Dispose();

                Assert.AreEqual(1, counter);
                Assert.AreEqual(NotifyCollectionChangedAction.Add, eventArgs.Action);
                CollectionAssert.AreEqual(eventArgs.NewItems, new[] { 1, 2, 3, 4, 5 });
            }
Esempio n. 13
0
            public void SuspendsValidationWhileAddingAndRemovingItems()
            {
                var counter = 0;

                var fastCollection = new FastObservableCollection <int>();

                fastCollection.AutomaticallyDispatchChangeNotifications = false;
                fastCollection.CollectionChanged += (sender, e) => counter++;

                using (fastCollection.SuspendChangeNotifications())
                {
                    fastCollection.Add(1);
                    fastCollection.Add(2);
                    fastCollection.Add(3);
                    fastCollection.Add(4);
                    fastCollection.Add(5);

                    fastCollection.Remove(5);
                    fastCollection.Remove(4);
                    fastCollection.Remove(3);
                    fastCollection.Remove(2);
                    fastCollection.Remove(1);
                }

                Assert.AreEqual(0, counter);
            }
 public static void MoveItemToTop <T>(this FastObservableCollection <T> collection, T item)
 {
     using (collection.SuspendChangeNotifications())
     {
         collection.Remove(item);
         collection.Insert(0, item);
     }
 }
Esempio n. 15
0
            public void ThrowsInvalidOperationExceptionForClearingInAddingMode()
            {
                var fastCollection = new FastObservableCollection <int>();

                using (fastCollection.SuspendChangeNotifications(SuspensionMode.Adding))
                {
                    Assert.Throws <InvalidOperationException>(() => fastCollection.Clear());
                }
            }
Esempio n. 16
0
        protected override async Task InitializeAsync()
        {
            await base.InitializeAsync();

            var items = await TodoStorage.GetItemsAsync();

            using (_todoItems.SuspendChangeNotifications())
            {
                _todoItems.ReplaceRange(items);
            }
        }
Esempio n. 17
0
            public void CleanedUpSuspensionContextAfterDoingNothing()
            {
                var fastCollection = new FastObservableCollection <int>();

                using (fastCollection.SuspendChangeNotifications(SuspensionMode.Adding))
                {
                }

                var context = this.GetSuspensionContext(fastCollection);

                Assert.IsNull(context);
            }
            public void ReturnsTrueWhenPendingNotificationsAreListed()
            {
                var fastCollection = new FastObservableCollection<int>();

                using (fastCollection.SuspendChangeNotifications())
                {
                    fastCollection.Add(1);

                    Assert.IsTrue(fastCollection.IsDirty);
                }

                Assert.IsFalse(fastCollection.IsDirty);
            }
Esempio n. 19
0
            public void ReturnsTrueWhenPendingNotificationsAreListed()
            {
                var fastCollection = new FastObservableCollection <int>();

                using (fastCollection.SuspendChangeNotifications())
                {
                    fastCollection.Add(1);

                    Assert.IsTrue(fastCollection.IsDirty);
                }

                Assert.IsFalse(fastCollection.IsDirty);
            }
Esempio n. 20
0
            public void ReturnsFalseAfterDisposing()
            {
                var fastCollection = new FastObservableCollection <int>();

                using (fastCollection.SuspendChangeNotifications())
                {
                    fastCollection.Add(1);

                    Assert.IsTrue(fastCollection.NotificationsSuspended);
                }

                Assert.IsFalse(fastCollection.NotificationsSuspended);
            }
Esempio n. 21
0
            public void HandlesCollectionChangesCorrectlyInSuspensionModeMixedConsolidate()
            {
                var collection = new FastObservableCollection <TestModel>
                {
                    AutomaticallyDispatchChangeNotifications = false
                };

                var wrapper = new ChangeNotificationWrapper(collection);

                var itemsReset   = false;
                var itemsAdded   = false;
                var itemsRemoved = false;

                var model = new TestModel();

                collection.Add(model);

                wrapper.CollectionChanged += (sender, e) =>
                {
                    if (e.OldItems != null)
                    {
                        itemsRemoved = true;
                    }

                    if (e.Action == NotifyCollectionChangedAction.Reset)
                    {
                        itemsReset = true;
                    }

                    if (e.NewItems != null)
                    {
                        itemsAdded = true;
                    }
                };

                using (collection.SuspendChangeNotifications(SuspensionMode.MixedConsolidate))
                {
                    collection.ReplaceRange(new [] { new TestModel() });
                }

                Assert.IsTrue(itemsAdded, "Items should be added");
                Assert.IsTrue(itemsRemoved, "Items should be removed");
                Assert.IsFalse(itemsReset, "Items should not be reset");
            }
Esempio n. 22
0
            public void RaisesTwoEvents()
            {
                var eventArgsList  = new List <NotifyCollectionChangedEventArgs>();
                var fastCollection = new FastObservableCollection <int>();

                fastCollection.AddItems(new[] { 1, 2, 3, 4 });

                fastCollection.CollectionChanged += (sender, args) => { eventArgsList.Add(args); };

                using (fastCollection.SuspendChangeNotifications(SuspensionMode.Mixed))
                {
                    fastCollection.RemoveItems(new[] { 4 });
                    fastCollection.AddItems(new[] { 5 });
                }

                Assert.AreEqual(2, eventArgsList.Count);

                Assert.Contains(5, eventArgsList.First(args => args.Action == NotifyCollectionChangedAction.Add).NewItems);
                Assert.Contains(4, eventArgsList.First(args => args.Action == NotifyCollectionChangedAction.Remove).OldItems);
            }
            public void HandlesCollectionChangesByResetCorrectly()
            {
                var collection = new FastObservableCollection <TestModel>();
                var wrapper    = new ChangeNotificationWrapper(collection);

                var itemsReset   = false;
                var itemsAdded   = false;
                var itemsRemoved = false;

                var model = new TestModel();

                collection.Add(model);

                wrapper.CollectionChanged += (sender, e) =>
                {
                    if (e.OldItems != null)
                    {
                        itemsRemoved = true;
                    }

                    if (e.Action == NotifyCollectionChangedAction.Reset)
                    {
                        itemsReset = true;
                    }

                    if (e.NewItems != null)
                    {
                        itemsAdded = true;
                    }
                };

                using (collection.SuspendChangeNotifications())
                {
                    collection.ReplaceRange(new [] { new TestModel() });
                }

                Assert.IsFalse(itemsAdded);
                Assert.IsFalse(itemsRemoved);
                Assert.IsTrue(itemsReset);
            }
        protected override async Task InitializeAsync()
        {
            await base.InitializeAsync();

            var ranges = new FastObservableCollection <DateRange>();

            using (ranges.SuspendChangeNotifications())
            {
                ranges.Add(PredefinedDateRanges.Today);
                ranges.Add(PredefinedDateRanges.Yesterday);
                ranges.Add(PredefinedDateRanges.ThisWeek);
                ranges.Add(PredefinedDateRanges.LastWeek);
                ranges.Add(PredefinedDateRanges.ThisMonth);
                ranges.Add(PredefinedDateRanges.LastMonth);
            }

            Ranges        = ranges;
            StartDate     = ranges[0].Start;
            EndDate       = ranges[0].End;
            Span          = ranges[0].Duration;
            SelectedRange = ranges[0];
        }
Esempio n. 25
0
            public void RaisesSingleAddEventIfTheRemovedItemsAreASubSetOfTheAddedItems()
            {
                var count = 0;
                NotifyCollectionChangedEventArgs eventArgs = null;
                var fastCollection = new FastObservableCollection <int>();

                fastCollection.CollectionChanged += (sender, args) =>
                {
                    count++;
                    eventArgs = args;
                };

                using (fastCollection.SuspendChangeNotifications(SuspensionMode.Mixed))
                {
                    fastCollection.AddItems(new[] { 1, 2, 3, 4 });
                    fastCollection.RemoveItems(new[] { 2, 3 });
                }

                Assert.AreEqual(NotifyCollectionChangedAction.Add, eventArgs.Action);
                Assert.AreEqual(1, count);
                Assert.AreEqual(new[] { 1, 4 }, eventArgs.NewItems.OfType <int>().ToArray());
            }
            public void HandlesCollectionChangesByResetCorrectly()
            {
                var collection = new FastObservableCollection<TestModel>();
                var wrapper = new ChangeNotificationWrapper(collection);

                var itemsReset = false;
                var itemsAdded = false;
                var itemsRemoved = false;

                var model = new TestModel();
                collection.Add(model);

                wrapper.CollectionChanged += (sender, e) =>
                {
                    if (e.OldItems != null)
                    {
                        itemsRemoved = true;
                    }

                    if (e.Action == NotifyCollectionChangedAction.Reset)
                    {
                        itemsReset = true;
                    }

                    if (e.NewItems != null)
                    {
                        itemsAdded = true;
                    }
                };

                using (collection.SuspendChangeNotifications())
                {
                    collection.ReplaceRange(new [] { new TestModel() });
                }

                Assert.IsFalse(itemsAdded);
                Assert.IsFalse(itemsRemoved);
                Assert.IsTrue(itemsReset);
            }
            public void HandlesClearOfSuspendedFastObservableCollectionCorrectly()
            {
                var collection = new FastObservableCollection<TestModel>();
                TestModel model = null;

                for (int i = 0; i < 10; i++)
                {
                    var randomModel = new TestModel();
                    collection.Add(randomModel);
                }

                model = collection[0];

                var wrapper = new ChangeNotificationWrapper(collection);

                var collectionItemPropertyChanged = false;
                wrapper.CollectionItemPropertyChanged += (sender, e) => collectionItemPropertyChanged = true;

                using (collection.SuspendChangeNotifications())
                {
                    collection.Clear();
                }

                model.FirstName = "Geert";

                Assert.IsFalse(collectionItemPropertyChanged);
            }