using System; using System.Collections.Generic; using System.IO; using System.Linq; using LoginGenerator.Models; using ReactiveUI; namespace LoginGenerator.ViewModels { public class DataOutputViewModel : ReactiveObject { public string[] AllData; private List? users; public List? Userss { get => users; set => this.RaiseAndSetIfChanged(ref users, value); } public string[] OneUser; public Users u; public DataOutputViewModel() { Userss = new List(); FillingUsersList(); Sort = new List() { "Дата по убыванию", "Дата по возрастанию" }; SelectedSort = Sort[0]; } public void FillingUsersList() { if (!File.Exists(FilePath.PATH)) { var file = File.Create(FilePath.PATH); file.Close(); } AllData = File.ReadAllLines(FilePath.PATH); List? newUsers = new List(); foreach (string user in AllData) { if (!string.IsNullOrEmpty(user)) { u = new Users(); OneUser = user.Split(' '); u.Surname = OneUser[0].ToString(); u.Name = OneUser[1].ToString(); u.Patronymic = OneUser[2].ToString(); u.Login = OneUser[3].ToString(); u.Password = OneUser[4].ToString(); u.DateReg = Convert.ToDateTime(OneUser[5]); newUsers.Add(u); } } Userss = newUsers; } private string? _findPass; public string? FindPass { get => _findPass; set { _findPass = value; Filter(); } } private string? _findLogin; public string? FindLogin { get => _findLogin; set { _findLogin = value; Filter(); } } private string? _findFio; public string? FindFio { get => _findFio; set { _findFio = value; Filter(); } } private static List? _sort; public static List? Sort { get => _sort; set => _sort = value; } private string? _selectedSort; public string? SelectedSort { get { return _selectedSort; } set { _selectedSort = value; Filter(); } } public void Filter() { FillingUsersList(); switch (SelectedSort) { case "Дата по убыванию": Userss = Userss.OrderByDescending(x => x.DateReg).ToList(); break; case "Дата по возрастанию": Userss = Userss.OrderBy(x => x.DateReg).ToList(); break; } if (FindFio != null && FindFio != "") { Userss = Userss.Where(x => x.Name.ToUpper().Contains(FindFio.ToString().ToUpper()) || x.Surname.ToUpper().Contains(FindFio.ToString().ToUpper()) || x.Patronymic.ToUpper().Contains(FindFio.ToString().ToUpper())).ToList(); } if (FindLogin != null && FindLogin != "") { Userss = Userss.Where(x => x.Login.Contains(FindLogin.ToString())).ToList(); } if (FindPass != null && FindPass != "") { Userss = Userss.Where(x => x.Password.Contains(FindPass.ToString())).ToList(); } } } }