示例#1
0
        /// <summary>
        ///     Add the filter to the predicate dictionary
        /// </summary>
        public void AddFilter(Dictionary <string, Predicate <object> > criteria)
        {
            if (IsFiltered)
            {
                return;
            }

            bool Predicate(object o)
            {
                var value = o.GetType().GetProperty(FieldName)?.GetValue(o, null);

                return(!PreviouslyFilteredItems.Contains(value));
            }

            criteria.Add(FieldName, Predicate);

            IsFiltered = true;
        }
示例#2
0
        /// <summary>
        ///     Build the item tree
        /// </summary>
        /// <param name="dates"></param>
        /// <param name="currentFilter"></param>
        /// <param name="uncheckPrevious"></param>
        /// <returns></returns>
        public IEnumerable <FilterItem> BuildTree(IEnumerable <object> dates, string lastFilter = null)
        {
            if (dates == null)
            {
                return(null);
            }

            try
            {
                var dateTimes       = dates.ToList();
                var uncheckPrevious = FieldName == lastFilter;
                var type            = typeof(DateTime); //dateTimes.GetType().GenericTypeArguments[0];

                Tree = new List <FilterItem>
                {
                    new FilterItem(this)
                    {
                        Label = Translate.All, CurrentFilter = this, Content = 0, Level = 0, SetDateState = true, FieldType = type
                    }
                };

                // iterate over all items that are not null
                // INFO:
                // SetDateState : does not raise OnDateStatusChanged event
                // IsChecked    : raise OnDateStatusChanged event
                // (see the FilterItem class for more informations)

                foreach (var y in from date in dateTimes.Where(d => d != null)
                         .Select(d => (DateTime)d).OrderBy(o => o.Year)
                         group date by date.Year into year
                         select new FilterItem(this)
                {
                    // YEAR
                    Level = 1,
                    CurrentFilter = this,
                    Content = year.Key,
                    Label = year.First().ToString("yyyy", Translate.Culture),
                    SetDateState = true,
                    FieldType = type,

                    Children = (from date in year
                                group date by date.Month
                                into month
                                select new FilterItem(this)
                    {
                        // MOUNTH
                        Level = 2,
                        CurrentFilter = this,
                        Content = month.Key,
                        Label = month.First().ToString("MMMM", Translate.Culture),
                        SetDateState = true,
                        FieldType = type,

                        Children = (from day in month
                                    select new FilterItem(this)
                        {
                            // DAY
                            Level = 3,
                            CurrentFilter = this,
                            Content = day.Day,
                            Label = day.ToString("dd", Translate.Culture),
                            SetDateState = true,
                            FieldType = type,
                            Children = new List <FilterItem>()
                        }).ToList()
                    }).ToList()
                })
                {
                    y.Children.ForEach(m =>
                    {
                        m.Parent = y;

                        m.Children.ForEach(d =>
                        {
                            d.Parent = m;

                            // set the state of the ischecked property based on the items already filtered (unchecked)
                            if (PreviouslyFilteredItems != null && uncheckPrevious)
                            {
                                d.IsChecked = PreviouslyFilteredItems
                                              .Any(u => u != null && u.Equals(new DateTime((int)y.Content, (int)m.Content, (int)d.Content))) == false;
                            }

                            // reset initialization
                            d.InitialState = d.IsChecked;
                        });

                        // reset initialization
                        m.InitialState = m.IsChecked;
                    });

                    // reset initialization
                    y.InitialState = y.IsChecked;

                    Tree.Add(y);
                }

                // last empty item if exist in collection
                if (dateTimes.Any(d => d == null))
                {
                    Tree.Add(
                        new FilterItem(this)
                    {
                        Label         = Translate.Empty, // translation
                        CurrentFilter = this,
                        Content       = null,
                        Level         = -1,
                        FieldType     = type,
                        SetDateState  = PreviouslyFilteredItems?.Any(u => u == null) == false,
                        Children      = new List <FilterItem>()
                    }
                        );
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"FilterCommon.BuildTree : {ex.Message}");
                throw;
            }

            return(Tree);
        }
示例#3
0
        /// <summary>
        ///     Build the item tree
        /// </summary>
        /// <param name="dates"></param>
        /// <param name="currentFilter"></param>
        /// <param name="uncheckPrevious"></param>
        /// <returns></returns>
        public IEnumerable <FilterItem> BuildTree(IEnumerable <object> dates, bool uncheckPrevious = false)
        {
            if (dates == null)
            {
                return(null);
            }

            try
            {
                var dateTimes = dates.ToList();

                Tree = new List <FilterItem>
                {
                    new FilterItem
                    {
                        Label = Loc.All, CurrentFilter = this, Content = 0, Level = 0, SetDateState = true
                    }
                };

                // event subscription
                Tree.First().OnIsCheckedDate += UpdateTree;

                // iterate over all items that are not null
                // INFO:
                // SetDateState  : does not raise OnIsCheckedDate event
                // IsDateChecked : raise OnIsCheckedDate event
                // (see the FilterItem class for more informations)

                foreach (var y in from date in dateTimes.Where(d => d != null)
                         .Select(d => (DateTime)d).OrderBy(o => o.Year)
                         group date by date.Year
                         into year
                         select new FilterItem
                {
                    // YEAR
                    Level = 1,
                    CurrentFilter = this,
                    Content = year.Key,
                    Label = year.First().ToString("yyyy"),
                    SetDateState = true,

                    Children = (from date in year
                                group date by date.Month
                                into month
                                select new FilterItem
                    {
                        // MOUNTH
                        Level = 2,
                        CurrentFilter = this,
                        Content = month.Key,
                        Label = month.First().ToString("MMMM", Loc.Culture),
                        SetDateState = true,

                        Children = (from day in month
                                    select new FilterItem
                        {
                            // DAY
                            Level = 3,
                            CurrentFilter = this,
                            Content = day.Day,
                            Label = day.ToString("dd", Loc.Culture),
                            SetDateState = true,

                            Children = new List <FilterItem>()
                        }).ToList()
                    }).ToList()
                })
                {
                    y.OnIsCheckedDate += UpdateTree;

                    y.Children.ForEach(m =>
                    {
                        m.Parent           = y;
                        m.OnIsCheckedDate += UpdateTree;

                        m.Children.ForEach(d =>
                        {
                            d.Parent           = m;
                            d.OnIsCheckedDate += UpdateTree;

                            if (PreviouslyFilteredItems != null && uncheckPrevious)
                            {
                                d.IsDateChecked = PreviouslyFilteredItems
                                                  .Any(u => u != null && u.Equals(new DateTime((int)y.Content, (int)m.Content,
                                                                                               (int)d.Content))) == false;
                            }
                        });
                    });
                    Tree.Add(y);
                }

                // last empty item
                if (dateTimes.Any(d => d == null))
                {
                    Tree.Add(
                        new FilterItem
                    {
                        Label         = Loc.Empty, // translation
                        CurrentFilter = this,
                        Content       = -1,
                        Level         = -1,
                        SetDateState  = !PreviouslyFilteredItems?.Any(u => u == null) == true,
                        Children      = new List <FilterItem>()
                    }
                        );
                }

                // event subscription if an empty element exists
                if (Tree.LastOrDefault(c => c.Level == -1) != null)
                {
                    Tree.Last(c => c.Level == -1).OnIsCheckedDate += UpdateTree;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"BuildTree : {ex.Message}");
            }

            return(Tree);
        }