Эх сурвалжийг харах

Добавлена фильтрация списка клиентов

Ahatov Artur 6 сар өмнө
parent
commit
734ec811fa

+ 14 - 3
src/DontHarmDesktop/Pages/Clients.xaml

@@ -45,21 +45,32 @@
                     Grid.Column="0"
                     Grid.Row="0"/>
                 <TextBox
+                    Text="{Binding FilterName, UpdateSourceTrigger=PropertyChanged}"
                     Grid.Column="0"
-                    Grid.Row="1"/>
+                    Grid.Row="1"
+                    Margin="0,0,8,0"/>
 
                 <TextBlock 
                     Text="Поиск по домену почты"
                     Grid.Column="1"
                     Grid.Row="0"/>
                 <ComboBox
+                    SelectedIndex="{Binding FilterEmailDomain}"
                     Grid.Column="1"
                     Grid.Row="1"
-                    />
+                    Margin="0,0,8,0"
+                    >
+                    <ComboBoxItem Content="(все домены)"/>
+                    <ComboBoxItem Content="ru"/>
+                    <ComboBoxItem Content="com"/>
+                </ComboBox>
+                
 
 
                 <CheckBox
+                    IsChecked="{Binding FilterShowHidden}"
                     Grid.Column="2"
+                    Grid.Row="2"
                     Content="Отображать скрытые"/>
 
             </Grid>
@@ -69,7 +80,7 @@
                 Grid.Row="2" 
                 Grid.ColumnSpan="2"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled"
-                ItemsSource="{Binding Clients}">
+                ItemsSource="{Binding ClientsView}">
 
                 <ListView.ItemsPanel>
                     <ItemsPanelTemplate>

+ 110 - 3
src/DontHarmDesktop/ViewModels/ClientsViewModel.cs

@@ -6,18 +6,125 @@ using System.Text;
 using System.Threading.Tasks;
 using DontHarmDesktop.Models;
 using Prism.Mvvm;
+using System.Windows.Data;
+using System.ComponentModel;
 
 namespace DontHarmDesktop.ViewModels
 {
     public class ClientsViewModel : BindableBase
     {
-        public ObservableCollection<clients> Clients { get; set; }
+        private CollectionViewSource clientsCollection;
+        private string filterName;
+        private bool filterShowHidden;
+        private int filterEmailDomain;
+
+        public ICollectionView ClientsView
+        {
+            get
+            {
+                return clientsCollection.View;
+            }
+        }
+
+        public CollectionViewSource ClientsCollection
+        {
+            get { return clientsCollection; }
+            set { clientsCollection = value; }
+        }
+
+        public string FilterName
+        {
+            get
+            {
+                return filterName;
+            }
+            set
+            {
+                filterName = value;
+                clientsCollection.View.Refresh();
+                RaisePropertyChanged();
+                RaisePropertyChanged(nameof(ClientsView));
+            }
+        }
+
+        public bool FilterShowHidden
+        {
+            get
+            {
+                return filterShowHidden;
+            }
+            set
+            {
+                filterShowHidden = value;
+                clientsCollection.View.Refresh();
+                RaisePropertyChanged();
+                RaisePropertyChanged(nameof(ClientsView));
+            }
+        }
+
+        public int FilterEmailDomain
+        {
+            get
+            {
+                return filterEmailDomain;
+            }
+            set
+            {
+                filterEmailDomain = value;
+                clientsCollection.View.Refresh();
+                RaisePropertyChanged();
+                RaisePropertyChanged(nameof(ClientsView));
+            }
+        }
+
+        private void ClientsFilter(object sender, FilterEventArgs e)
+        {
+            clients c = e.Item as clients;
+            bool disallow = false;
+
+            // Фильтр по имени
+            if (!string.IsNullOrEmpty(filterName))
+            {
+                if (!c.HumanName.ToLower().Contains(filterName.ToLower()))
+                {
+                    disallow = true;
+                }
+            }
+
+            // Клиент скрыт и не надо показывать скрытых
+            if (!disallow && c.hidden && !filterShowHidden)
+            {
+                disallow = true;
+            }
+
+            // По домене почты
+            if (!disallow && filterEmailDomain > 0)
+            {
+                string domain = c.email.Split('.')[1];
+                if (
+                    (filterEmailDomain == 1 && domain != "ru") ||
+                    (filterEmailDomain == 2 && domain != "com"))
+                {
+                    disallow = true;
+                }
+            }
+
+            e.Accepted = !disallow;
+        }
 
         public ClientsViewModel()
         {
+            filterName = string.Empty;
+            filterShowHidden = false;
+            filterEmailDomain = -1;
+
             var db = new Models.Entities();
-            var clients = db.clients.Where(c => c.hidden == false);
-            Clients = new ObservableCollection<clients>(clients);
+            var clients = new ObservableCollection<clients>(db.clients.ToList());
+
+            clientsCollection = new CollectionViewSource();
+            clientsCollection.Source = clients;
+            clientsCollection.Filter += ClientsFilter;
+            clientsCollection.View.Refresh();
         }
     }
 }