public void GetFilterControlVm()
 {
     var items = StateItem.All;
     ListCollectionView view = new(items);
     IEnumerable <FilterInitializer> initializers = FilterInitializersManager.Default;
     FilterPresenter?filterPresenter = FilterPresenter.Get(view);
     {
         string key         = nameof(StateItem.Id);
         var    vm          = filterPresenter.TryGetFilterControlVm(key, initializers);
         var    filterTypes = vm.Select(v => v.GetType()).ToList();
         Assert.Contains(typeof(ObjectEqualFilter), filterTypes);
     }
     {
         string key         = nameof(StateItem.StateId);
         var    vm          = filterPresenter.TryGetFilterControlVm(key, initializers);
         var    filterTypes = vm.Select(v => v.GetType()).ToList();
         Assert.Contains(typeof(EqualFilter <int>), filterTypes);
         Assert.Contains(typeof(LessOrEqualFilter <int>), filterTypes);
         Assert.Contains(typeof(GreaterOrEqualFilter <int>), filterTypes);
         Assert.Contains(typeof(RangeFilter <int>), filterTypes);
     }
     {
         string key         = nameof(StateItem.State);
         var    vm          = filterPresenter.TryGetFilterControlVm(key, initializers);
         var    filterTypes = vm.Select(v => v.GetType()).ToList();
         Assert.Contains(typeof(EnumFilter <StateEnum>), filterTypes);
     }
     {
         string key         = nameof(StateItem.StateText);
         var    vm          = filterPresenter.TryGetFilterControlVm(key, initializers);
         var    filterTypes = vm.Select(v => v.GetType()).ToList();
         Assert.Contains(typeof(EqualFilter <string>), filterTypes);
         Assert.Contains(typeof(StringFilter), filterTypes);
     }
 }
Beispiel #2
0
        public void TryGetFilter()
        {
#pragma warning disable CS8604 // Possible null reference argument.
#pragma warning disable CS8602 // Dereference of a possibly null reference.
            var items       = StateItem.All;
            var compareFrom = StateEnum.State2;
            var compareTo   = StateEnum.State3;
            var expected    = Enum.GetValues <StateEnum>()
                              .Where(st => st >= compareFrom & st <= compareTo)
                              .Select(st => new StateItem(st))
                              .ToList();
            ListCollectionView view = new(items);
            Assert.IsTrue(view.CanFilter);
            Assert.IsNotNull(view.ItemProperties);
            FilterPresenter?filterPresenter = FilterPresenter.Get(view);
            Assert.IsNotNull(filterPresenter);

            RangeFilterInitializer initializer = new();
            RangeFilter <int>?     filter      = initializer.TryCreateFilter(filterPresenter, nameof(StateItem.StateId)) as RangeFilter <int>;
            Assert.IsNotNull(filter);
            Assert.IsFalse(filter.IsActive);
            Assert.AreSame(filterPresenter, filter.FilterPresenter);
            Assert.AreEqual(view.Count, items.Count);

            filter.CompareTo   = (int)compareTo;
            filter.CompareFrom = (int)compareFrom;
            Assert.IsTrue(filter.IsActive);
            var filtered = FilterPresenterTest.GetCollection(view);
            Assert.AreEqual(expected.Count, filtered.Count);
            Assert.AreEqual(expected[0].StateId, ((StateItem)filtered[0]).StateId);
            Assert.AreEqual(expected[^ 1].StateId, ((StateItem)filtered[^ 1]).StateId);
        public void TryGetFilter()
        {
#pragma warning disable CS8604 // Possible null reference argument.
#pragma warning disable CS8602 // Dereference of a possibly null reference.
            var items = StateItem.All;
            ListCollectionView view = new(items);
            Assert.IsTrue(view.CanFilter);
            Assert.IsNotNull(view.ItemProperties);

            FilterPresenter?filterPresenter = FilterPresenter.Get(view);
            Assert.IsNotNull(filterPresenter);
            EqualFilterInitializer initializer = new();
            EqualFilter?           filter      = initializer.TryCreateFilter(filterPresenter, nameof(StateItem.StateId)) as EqualFilter;
            Assert.IsNotNull(filter);
            Assert.IsFalse(filter.IsActive);
            Assert.AreSame(filterPresenter, filter.FilterPresenter);
            Assert.AreEqual(items.Count, view.Count);

            List <int> selected   = new(new int[] { (int)StateEnum.State1, (int)StateEnum.State4 });
            List <int> unselected = new();
            filter.SelectedValuesChanged(addedItems: selected, removedItems: unselected);
            Assert.IsTrue(filter.IsActive);

            var filtered = FilterPresenterTest.GetCollection(view);

            Assert.AreEqual(selected.Count, filtered.Count);
            Assert.AreEqual(selected[0], ((StateItem)filtered[0]).StateId);
            Assert.AreEqual(selected[1], ((StateItem)filtered[1]).StateId);
            //filterPresenter.IsFilterActive = false;
            filtered = FilterPresenterTest.GetCollection(view);
            Assert.AreEqual(items.Count, filtered.Count);

#pragma warning restore CS8602 // Dereference of a possibly null reference.
#pragma warning restore CS8604 // Possible null reference argument.
        }
Beispiel #4
0
        public void TryGetFilter()
        {
#pragma warning disable CS8604 // Possible null reference argument.
#pragma warning disable CS8602 // Dereference of a possibly null reference.
            var items     = StateItem.All;
            var compareTo = StateEnum.State3;

            ListCollectionView view = new(items);
            Assert.IsTrue(view.CanFilter);
            Assert.IsNotNull(view.ItemProperties);
            FilterPresenter?filterPresenter = FilterPresenter.Get(view);
            Assert.IsNotNull(filterPresenter);

            StringFilterInitializer initializer = new();
            StringFilter?           filter      = initializer.TryCreateFilter(filterPresenter, nameof(StateItem.StateText)) as StringFilter;
            Assert.IsNotNull(filter);
            Assert.IsFalse(filter.IsActive);
            Assert.AreSame(filterPresenter, filter.FilterPresenter);
            Assert.AreEqual(view.Count, items.Count);

            filter.Value = compareTo.ToString();
            filter.Mode  = StringFilterMode.Equals;
            var expected = Enum.GetValues <StateEnum>()
                           .Where(st => st == compareTo)
                           .Select(st => new StateItem(st))
                           .ToList();
            Assert.IsTrue(filter.IsActive);
            var filtered = FilterPresenterTest.GetCollection(view);
            Assert.AreEqual(1, filtered.Count);
            Assert.AreEqual(expected[0].StateId, ((StateItem)filtered[0]).StateId);

            filter.Value = "4";
            filter.Mode  = StringFilterMode.EndsWith;
            compareTo    = StateEnum.State4;
            expected     = Enum.GetValues <StateEnum>()
                           .Where(st => st == compareTo)
                           .Select(st => new StateItem(st))
                           .ToList();
            Assert.IsTrue(filter.IsActive);
            filtered = FilterPresenterTest.GetCollection(view);
            Assert.AreEqual(1, filtered.Count);
            Assert.AreEqual(expected[0].StateId, ((StateItem)filtered[0]).StateId);

            filter.Value = "St";
            filter.Mode  = StringFilterMode.StartsWith;
            expected     = Enum.GetValues <StateEnum>()
                           .Select(st => new StateItem(st))
                           .ToList();
            Assert.IsTrue(filter.IsActive);
            filtered = FilterPresenterTest.GetCollection(view);
            Assert.AreEqual(expected.Count, filtered.Count);
            Assert.AreEqual(expected[0].StateId, ((StateItem)filtered[0]).StateId);
            Assert.AreEqual(expected[^ 1].StateId, ((StateItem)filtered[^ 1]).StateId);
Beispiel #5
0
        public void CTOR()
        {
            int[] source = { 1, 2, 3 };
            //var fp_empty = FilterPresenter.Get(null);
            //Assert.IsNull(fp_empty);
            var fp1 = FilterPresenter.Get(source);

            Assert.IsNotNull(fp1);
            var fp2 = FilterPresenter.Get(source);

            Assert.AreSame(fp1, fp2);
        }
Beispiel #6
0
        public void TryGetFilter()
        {
#pragma warning disable CS8604 // Possible null reference argument.
#pragma warning disable CS8602 // Dereference of a possibly null reference.
            var items = FilterPresenterTest.GetCollection(StateItem.All);
            ListCollectionView view            = new(items);
            FilterPresenter?   filterPresenter = FilterPresenter.Get(view);
            Assert.IsNotNull(filterPresenter);
            EqualFilterInitializer initializer = new();
            ObjectEqualFilter?     filter      = initializer.TryCreateFilter(filterPresenter, nameof(StateItem.Box)) as ObjectEqualFilter;

            Assert.IsNotNull(filter);
            Assert.IsFalse(filter.IsActive);
            Assert.AreSame(filterPresenter, filter.FilterPresenter);
            Assert.AreEqual(view.Count, items.Count);

            List <object?> selected   = new(new object[] { StateEnum.State0, StateEnum.State4 });
            List <object?> unselected = new();

            filter.SelectedValuesChanged(addedItems: selected, removedItems: unselected);

            Assert.IsTrue(filter.IsActive);
            var filtered = FilterPresenterTest.GetCollection(view);
            Assert.AreEqual(selected.Count, filtered.Count);
            Assert.AreEqual(selected[0], ((StateItem)filtered[0]).Box);
            Assert.AreEqual(selected[1], ((StateItem)filtered[1]).Box);

            filter.IsActive = false;

            filtered = FilterPresenterTest.GetCollection(view);
            Assert.AreEqual(items.Count, filtered.Count);

            //selected.Clear();
            selected.Add(null);
            ((StateItem)items[1]).Box = null;
            filter.SelectedValuesChanged(addedItems: selected, removedItems: unselected);
            Assert.IsTrue(filter.IsActive);
            //Assert.IsTrue(filterPresenter.IsFilterActive);
            filtered = FilterPresenterTest.GetCollection(view);
            Assert.AreEqual(3, filtered.Count);
            Assert.AreEqual(((StateItem)items[0]).Box, ((StateItem)filtered[0]).Box);
            Assert.AreEqual(((StateItem)items[1]).Box, ((StateItem)filtered[1]).Box);
            Assert.AreEqual(((StateItem)items[4]).Box, ((StateItem)filtered[2]).Box);

#pragma warning restore CS8602 // Dereference of a possibly null reference.
#pragma warning restore CS8604 // Possible null reference argument.
        }
Beispiel #7
0
        public void Filter()
        {
            var items = (List <StateItem>)StateItem.All;
            ListCollectionView view = new(items);
            IEnumerable <FilterInitializer> initializers = FilterInitializersManager.Default;
            FilterPresenter?filterPresenter = FilterPresenter.Get(view);
            var             vmId            = filterPresenter.TryGetFilterControlVm(nameof(StateItem.Id), initializers);
            {
                var filterTypes = vmId.Select(v => v.GetType()).ToList();
                Assert.Contains(typeof(ObjectEqualFilter), filterTypes);
            }
            var vmStateId = filterPresenter.TryGetFilterControlVm(nameof(StateItem.StateId), initializers);
            {
                var filterTypes = vmStateId.Select(v => v.GetType()).ToList();
                Assert.Contains(typeof(EqualFilter <int>), filterTypes);
                Assert.Contains(typeof(LessOrEqualFilter <int>), filterTypes);
                Assert.Contains(typeof(GreaterOrEqualFilter <int>), filterTypes);
                Assert.Contains(typeof(RangeFilter <int>), filterTypes);
            }
            var vmState = filterPresenter.TryGetFilterControlVm(nameof(StateItem.State), initializers);
            {
                var filterTypes = vmState.Select(v => v.GetType()).ToList();
                Assert.Contains(typeof(EnumFilter <StateEnum>), filterTypes);
            }
            //Assert.IsFalse(filterPresenter.IsFilterActive);
            var filtered = GetCollection(view);

            Assert.AreEqual(items.Count, filtered.Count);
            var idLesssFilter = vmStateId.OfType <LessOrEqualFilter <int> >().First();
            var compareId     = (int)StateEnum.State5;

            idLesssFilter.CompareTo = compareId;
            //Assert.IsTrue(filterPresenter.IsFilterActive);
            filtered = GetCollection(view);
            Assert.AreEqual(items.Where(v => v.StateId <= compareId).Count(), filtered.Count);

            var stateEnumFilter         = vmState.OfType <EnumFilter <StateEnum> >().First();
            List <StateEnum> selected   = new(new StateEnum[] { StateEnum.State1, StateEnum.State4, StateEnum.State6 });
            List <StateEnum> unselected = new();

            stateEnumFilter.SelectedValuesChanged(addedItems: selected, removedItems: unselected);
            filtered = GetCollection(view);
            Assert.AreEqual(2, filtered.Count);
        }
        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (filter is not null)
            {
                filter.PropertyChanged -= filter_PropertyChanged;
                filter = null;
            }
            if (e.NewValue is OrdersVm vm)
            {
                FilterPresenter fpr = FilterPresenter.Get(vm.OrdersCollectionView);
                ;
                if (fpr.TryGetFilter("Employee", new EqualFilterInitializer(), out filter))
                {
#pragma warning disable CS8602 // Dereference of a possibly null reference.
                    VisualStateManager.GoToState(this, filter.IsActive ? "Filtered" : "Unfiltered", false);
                    filter.PropertyChanged += filter_PropertyChanged;
#pragma warning restore CS8602 // Dereference of a possibly null reference.
                }
            }
        }
Beispiel #9
0
        public void TestAttach()
        {
            LessOrEqualFilter <int> filter = GetLessOrEqualFilter <int>();
            var source          = enumSource.ToList();
            var filterPresenter = FilterPresenter.Get(source);

            Assert.IsNotNull(filterPresenter);
            Assert.IsFalse(filter.IsActive);
            filter.Attach(filterPresenter);
            var view     = filterPresenter.CollectionView;
            var filtered = GetCollection(view);

            Assert.AreEqual(source.Count, filtered.Count);
            filter.CompareTo = (int)StateEnum.State3;
            Assert.IsTrue(filter.IsActive);
            filtered = GetCollection(view);
            Assert.AreEqual(4, filtered.Count);
            Assert.AreEqual((int)StateEnum.State0, filtered[0]);
            Assert.AreEqual((int)StateEnum.State1, filtered[1]);
            Assert.AreEqual((int)StateEnum.State2, filtered[2]);
            Assert.AreEqual((int)StateEnum.State3, filtered[3]);
        }
Beispiel #10
0
 private void CategoryFilterView_DataContextChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
 {
     if (DataContext is IList <Product> source)
     {
         // Define Filter that must be use.
         EqualFilterInitializer initializer = new();
         FilterPresenter        fp;
         // Get FilterPresenter that connected to default collection view for Workspace.This.Products collection.
         fp = FilterPresenter.Get(source);
         // Get EqualFilter that use Category item property.
         if (fp?.TryGetFilter("Category", initializer, out var f) == true &&
             f is EqualFilter filter)
         {
             // Use instance of EqualFilter as Model.
             Model = filter;
         }
     }
     else
     {
         Model = null;
     }
 }
Beispiel #11
0
        public void TestAttach()
        {
            EqualFilter <int> filter = GetEqualFilter <int>();
            var source          = enumSource.ToList();
            var filterPresenter = FilterPresenter.Get(source);

            Assert.IsNotNull(filterPresenter);
            Assert.IsFalse(filter.IsActive);
            filter.Attach(filterPresenter);
            var view     = filterPresenter.CollectionView;
            var filtered = FilterPresenterTest.GetCollection(view);

            Assert.AreEqual(source.Count, filtered.Count);
            List <int> selected   = new(new int[] { (int)StateEnum.State1, (int)StateEnum.State4 });
            List <int> unselected = new();

            filter.SelectedValuesChanged(addedItems: selected, removedItems: unselected);
            Assert.IsTrue(filter.IsActive);
            filtered = FilterPresenterTest.GetCollection(view);
            Assert.AreEqual(selected.Count, filtered.Count);
            Assert.AreEqual(selected[0], filtered[0]);
            Assert.AreEqual(selected[1], filtered[1]);
        }