/
SergeyReHub
/
WPF_Project_TeraTeams
Обзор
Документация
Войти
/
SergeyReHub
/
WPF_Project_TeraTeams
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
ViewModel/ListViewModel.cs
348 строк
12 KB
SergeyReHub
Code_commit
08 дек 2024, 11:43
08 дек 2024, 11:43
33f488f
Код
Авторство
О чём код?
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using CursovayaRybakov.CustomElementListClient; using MySql.Data.MySqlClient; using CursovayaRybakov.Repositories; using CursovayaRybakov.View; using CursovayaRybakov.Model; using System.Windows.Controls.Primitives; using System.Windows.Threading; using GalaSoft.MvvmLight.Command; using System.Windows; using CursovayaRybakov.Navigation; using System.Windows.Navigation; using static MaterialDesignThemes.Wpf.Theme.ToolBar; using System.Windows.Media; using System.Globalization; using static Google.Protobuf.Reflection.UninterpretedOption.Types; namespace CursovayaRybakov.ViewModel { public class ListViewModel : ViewModelBase { // Properties // VisibilityProperties private bool _isViewVisible = false; private bool _isPolisBoxVisible = false; private bool _isDateVisible = false; //Info Properties private string _textInfoBlock; //private DispatcherTimer timer; private string _date; private string _textPolys; //private string _nameAndTime; //private string _years; //private string _gender; //private string _polis; //private string _username; private ElementListRepository _elementListRepository; private ObservableCollection<Client> _itemsFirstTable = new ObservableCollection<Client>(); private ObservableCollection<Client> _itemsSecondTable = new ObservableCollection<Client>(); private readonly IWindowService _windowService; private string _selectedSearchType; //fields public ObservableCollection<Client> ItemsFirstTable { get { return _itemsFirstTable; } set { _itemsFirstTable = value; OnPropertyChanged(nameof(_itemsFirstTable)); OnPropertyChanged(nameof(ItemsFirstTable)); } } public ObservableCollection<Client> ItemsSecondTable { get { return _itemsSecondTable; } set { _itemsSecondTable = value; OnPropertyChanged(nameof(_itemsSecondTable)); OnPropertyChanged(nameof(ItemsSecondTable)); } } public string SelectedSearchType { get { return _selectedSearchType; } set { _selectedSearchType = value; OnPropertyChanged(nameof(SelectedSearchType)); // Важно! Отреагируйте на изменение // например, изменив фильтр или запрос к базе данных ApplySearchFilter(); } } public string TextInfoBlock { get { return _textInfoBlock; } set { _textInfoBlock = value; OnPropertyChanged(nameof(TextInfoBlock)); } } public string Date { get { return _date; } set { _date = value; OnPropertyChanged(nameof(Date)); } } public string TextPolys { get { return _textPolys; } set { _textPolys = value; OnPropertyChanged(nameof(TextPolys)); } } //Visible Fields public bool IsViewVisible { get { return _isViewVisible; } set { _isViewVisible = value; OnPropertyChanged(nameof(IsViewVisible)); } } public bool IsPolisBoxVisible { get { return _isPolisBoxVisible; } set { _isPolisBoxVisible = value; OnPropertyChanged(nameof(IsPolisBoxVisible)); } } public bool IsDateVisible { get { return _isDateVisible; } set { _isDateVisible = value; OnPropertyChanged(nameof(IsDateVisible)); } } //Comands // From Db public ICommand DatesetComand { get; } //Set Date to textblock public ICommand DbSetCommand { get; } //Set info from Db public ICommand DbSetByPolisCommand { get; }//Set one bro by polis //Commands to windowServise public ICommand OpenFormClientCommand { get; } public ICommand ExitCommand { get; } public ICommand CloseInfoWindowCommand { get; } //Another Commands public ICommand SendInfoCommand { get; } public ICommand DateToLeftCommand { get; } public ICommand DateToRightCommand { get; } public ICommand ConfirmCommand { get; } public ICommand DeleteClientCommand { get; } public ListViewModel(IWindowService windowService) { _windowService = windowService; // set to classes interface OpenFormClientCommand = new ViewModelComand(ExecuteOpenFormClientCommand); DeleteClientCommand = new ViewModelComand(ExecuteDeleteClientCommand); SendInfoCommand = new ViewModelComand(ExecuteSendInfoCommand); DatesetComand = new ViewModelComand(ExecuteDatasetComand); DbSetCommand = new ViewModelComand(ExecuteDbSetCommandComand); DbSetByPolisCommand = new ViewModelComand(ExecuteDbSetByPolisCommand); CloseInfoWindowCommand = new ViewModelComand(ExecuteCloseInfoWindowComand); ExitCommand = new ViewModelComand(ExecuteExitCommand); DateToLeftCommand = new ViewModelComand(ExecuteDateToLeftCommand); DateToRightCommand = new ViewModelComand(ExecuteDateToRightCommand); ConfirmCommand = new ViewModelComand(ExecuteConfirmCommand); //timer = new DispatcherTimer(); // Запускает команду каждые 10 секунд //timer.Interval = TimeSpan.FromSeconds(10); //timer.Tick += Timer_Tick; //timer.Start(); SelectedSearchType = "System.Windows.Controls.ComboBoxItem: Поиск по дате";// setting the value for Comboox } private void ExecuteConfirmCommand(object obj) //for processing by clicking Enter in TextBox for Polis { DbSetByPolisCommand.Execute(null); } private void ExecuteDatasetComand(object obj) { _elementListRepository = new ElementListRepository(); Date = _elementListRepository.Datesetter(); } private void ExecuteDbSetCommandComand(object obj) { _elementListRepository = new ElementListRepository(); ItemsSecondTable.Clear(); ItemsFirstTable = _elementListRepository.ElementFilling(Date); // Функция Взятия из БД Инфы о клиентах if (ItemsFirstTable.Count > 4) { var lastTwoItems = ItemsFirstTable.Skip(ItemsFirstTable.Count - 2).ToList(); foreach (var item in lastTwoItems) { ItemsFirstTable.Remove(item); } ItemsSecondTable.Clear(); foreach (var item in lastTwoItems) { ItemsSecondTable.Add(item); } } else if(ItemsFirstTable.Count > 3)// Переместим 2 последних элемента из 1ой коллекции во вторую { var lastTwoItems = ItemsFirstTable.Skip(ItemsFirstTable.Count - 1).ToList(); foreach (var item in lastTwoItems) { ItemsFirstTable.Remove(item); } ItemsSecondTable.Clear(); foreach (var item in lastTwoItems) { ItemsSecondTable.Add(item); } } } private void ExecuteDbSetByPolisCommand(object obj) { _itemsFirstTable.Clear(); _itemsSecondTable.Clear();// Clearing collections _elementListRepository = new ElementListRepository(); _itemsFirstTable.Add(_elementListRepository.SetByPolis(TextPolys)); } public void ExecuteSendInfoCommand(object obj) { IsViewVisible = true;// turning on visibility of the border to displaying client info int clientId = (int)obj; // conversion from object to int bool flag = false; // flag that signal us that clientid was found foreach (Client client in ItemsFirstTable) { if(client.Id == clientId) { TextInfoBlock = client.Info; flag = true; break; } } if(!flag) { foreach (Client client in ItemsSecondTable) { if (client.Id == clientId) { TextInfoBlock = client.Info; break; } } } } public void ExecuteDeleteClientCommand(object obj) { _elementListRepository = new ElementListRepository(); int clientId = (int)obj; _elementListRepository.DeleteClient(clientId); DbSetCommand.Execute(null); } public void ExecuteOpenFormClientCommand(object obj) { Client client = (Client)obj; _windowService.ShowWindow(new FormClient(client)); } public void ExecuteCloseInfoWindowComand(object obj) { IsViewVisible = false; } public void ExecuteExitCommand(object obj) //Command to Exit { _windowService.ShowWindow(new MainWindow()); } public void ExecuteDateToRightCommand(object obj) { Date = _elementListRepository.DateLister(Date, true); DbSetCommand.Execute(null); } public void ExecuteDateToLeftCommand(object obj) { Date = _elementListRepository.DateLister(Date, false); DbSetCommand.Execute(null); } //Functions private void ApplySearchFilter()// processing of combobox choice { if (SelectedSearchType.Contains("Поиск по дате")) { IsDateVisible = true; // turning on date panel IsPolisBoxVisible = false; DatesetComand.Execute(null); DbSetCommand.Execute(null); } else if (SelectedSearchType.Contains("Поиск по полису")) { IsPolisBoxVisible = true;// turning on visibility of the border to displaying TextBox IsDateVisible = false; _itemsFirstTable.Clear(); _itemsSecondTable.Clear(); } } //public void Timer_Tick(object sender, EventArgs e) //{ // ApplySearchFilter(); //} } }