using FSI.Lib.Guis.Prj.Mgt.Model; using FSI.Lib.MVVM; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Configuration; using System.Diagnostics; using System.IO; using System.Linq; using System.Windows.Data; using System.Windows.Input; namespace FSI.Lib.Guis.Prj.Mgt.ViewModel { public class ViewModelPrj : MVVM.ViewModelBase { readonly IPrjDataProvider _prjDataProvider; private string _search; private ICollectionView _collView; public ICommand RefreshCommand => new DelegateCommand(RefreshData); private ICommand _cmdQuickSearch; private ICommand _cmdOpen; public ViewModelPrj(IPrjDataProvider prjDataProvider) { Prjs = new ObservableCollection(); _prjDataProvider = prjDataProvider; _cmdQuickSearch = new RelayCommand(ExecuteQuickSearch, CanExecuteQuickSearch); _cmdOpen = new RelayCommand(ExecuteOpen, CanExecuteOpen); } public ObservableCollection Prjs { get; } public ObservableCollection PrjsFiltered { get; set; } public Model.Prj SeletctedPrj { get; set; } public string DataPath { get; set; } public bool ShowPdf { get; set; } public string[] EplExes { get; set; } public void Load() { var prjs = _prjDataProvider.Load(DataPath, ShowPdf); Prjs.Clear(); if (prjs != null) { foreach (Model.Prj prj in prjs) { Prjs.Add(prj); } } PrjsFiltered = new ObservableCollection(Prjs); _collView = CollectionViewSource.GetDefaultView(PrjsFiltered); } public string Search { get => _search; set { _search = value; _collView.Filter = e => { var item = (Model.Prj)e; return item != null && ((item.Plant?.StartsWith(_search, StringComparison.OrdinalIgnoreCase) ?? false) || (item.SubPlant?.StartsWith(_search, StringComparison.OrdinalIgnoreCase) ?? false) #if NET472 || (item.Description?.Contains(_search) ?? false) || (item.DescriptionDetail?.Contains(_search) ?? false) #elif NET6_0 || (item.Description?.Contains(_search, StringComparison.OrdinalIgnoreCase) ?? false) || (item.DescriptionDetail?.Contains(_search, StringComparison.OrdinalIgnoreCase) ?? false) #endif ); }; _collView.Refresh(); PrjsFiltered = new ObservableCollection(_collView.OfType().ToList()); OnPropertyChanged(); } } public void QuickSearch(string search) { Search = search + " "; } private void RefreshData(object obj) { Search = string.Empty; } private bool CanExecuteQuickSearch(object obj) { return true; } private void ExecuteQuickSearch(object obj) { QuickSearch(obj.ToString()); } public ICommand CmdQuickSearch { get { return _cmdQuickSearch; } set => _cmdQuickSearch = value; } private bool CanExecuteOpen(object obj) { if (SeletctedPrj != null) return true; else return false; } private void ExecuteOpen(object obj) { if (ShowPdf) { new Process { StartInfo = new ProcessStartInfo(SeletctedPrj.FullName) { UseShellExecute = true } }.Start(); } else { var arguments = " /Variant:\"Electric P8\" ProjectOpen /Project:\"" + SeletctedPrj.FullName + "\""; string fileName = string.Empty; string path = string.Empty; for (int i = 0; i <= EplExes.Length - 1; i++) { if (File.Exists(EplExes[i].Trim())) { fileName = EplExes[i].Trim(); } } new Process { StartInfo = new ProcessStartInfo() { FileName = fileName, Arguments = arguments, } }.Start(); } } public ICommand CmdOpen { get { return _cmdOpen; } set => _cmdOpen = value; } } public class PrjDataProvider : IPrjDataProvider { public IEnumerable Load(string path, bool showPdf) { var prjs = new ObservableCollection(); string[] files = Directory.GetFiles(path); // alle PDF-Dateien einlesen foreach (var file in files) { if (((file.EndsWith(".elk") || file.EndsWith(".elp") || file.EndsWith(".els") || file.EndsWith(".elx") || file.EndsWith(".elr") || file.EndsWith(".ell") || file.EndsWith(".elf")) && !showPdf) || (file.EndsWith(".pdf") && showPdf)) { Model.Prj prj = new Model.Prj(); FileInfo fileInfo = new FileInfo(file); string[] nameNo = fileInfo.Name.Substring(0, 27 - 1).Split('-'); if (nameNo.Length == 3) // 3 Zahlenblöcke vorhanden? { if (nameNo[0].Length == 10 || Int64.TryParse(nameNo[0], out _) || nameNo[1].Length == 10 || Int64.TryParse(nameNo[1], out _) || nameNo[2].Length == 4 || !Int64.TryParse(nameNo[2], out _)) // Länge der Zahlenblöcke überprüfen { prj.PlantNo = Int64.Parse(nameNo[0]); prj.SubPlantNo = Int64.Parse(nameNo[1]); prj.No = Int64.Parse(nameNo[2]); string[] fileStrings = System.IO.Path.GetFileNameWithoutExtension(fileInfo.Name).Split(' '); prj.Plant = fileStrings[1]; prj.SubPlant = fileStrings[2]; prj.Description = System.IO.Path.GetFileNameWithoutExtension(fileInfo.Name).Replace(nameNo[0] + "-" + nameNo[1] + "-" + nameNo[2] + " " + fileStrings[1] + " " + fileStrings[2], ""); prj.Description = prj.Description.Trim(); prj.DescriptionDetail = prj.Plant + " " + prj.SubPlant + " " + prj.Description; prj.FullName = fileInfo.FullName; prjs.Add(prj); } } } } return prjs; } } }