using KanSan.Base; using KanSan.Base.Interfaces; using KanSan.Base.Models; using Microsoft.Win32; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Text; namespace KanSan.ViewModel { public class MainWindowViewModel : PropertyChangedClass, INotifyPropertyChanged { IUnitOfWork unitOfWork = new UnitOfWork(new KanSanContext()); RegistryKey registry; const string REGISTRYKEY = "HKEY_CURRENT_USER\\Software\\Cosysda\\KanSan"; private Kunde _selectedKunde; private Projekt _selectedProjekt; private Baustelle _selectedBaustelle; public string ApplicationTitle { get { string gitVersion; using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("KanSan.ViewModel.version.txt")) using (StreamReader reader = new StreamReader(stream)) { gitVersion = reader.ReadToEnd(); } return string.Format("KanalSanierungsverwaltung : {0}",gitVersion); } } public Kunde SelectedKunde { get { return _selectedKunde; } set { if (_selectedKunde != null) { if (_selectedKunde.GuidNr.Equals(value.GuidNr)) return; } _selectedKunde = value; _selectedProjekt = null; _selectedBaustelle = null; SaveInRegistry("LastKunde", value.GuidNr.ToString()); OnPropertyChanged(); } } public Projekt SelectedProjekt { get { return _selectedProjekt; } set { if(_selectedProjekt != null) { if (_selectedProjekt.GuidNr.Equals(value.GuidNr)) return; } _selectedProjekt = value; SaveInRegistry("LastProjekt", value.GuidNr.ToString()); OnPropertyChanged(); } } public Baustelle SelectedBaustelle { get { return _selectedBaustelle; } set { if(_selectedBaustelle != null) { if (_selectedBaustelle.GuidNr.Equals(value.GuidNr)) return; } _selectedBaustelle = value; SaveInRegistry("LastBaustelle", value.GuidNr.ToString()); OnPropertyChanged(); } } private void SaveInRegistry(string key, string value) { Registry.SetValue(REGISTRYKEY, key, value); } private void LadeRegistry() { registry = Registry.CurrentUser.OpenSubKey("Software\\Cosysda\\KanSan"); if (registry == null) InitRegistry(); string clientGuidStr = (string)registry.GetValue("LastKunde"); if (clientGuidStr != null) { Guid clientGuid = Guid.Parse(clientGuidStr); if (clientGuid != null) { IEnumerable loadedKunden = unitOfWork.KundenRepository.Get(x => x.GuidNr.Equals(clientGuid)); if (loadedKunden.Count() == 1) _selectedKunde = loadedKunden.First(); } } string projekteGuidStr = (string)registry.GetValue("LastProjekt"); if (projekteGuidStr != null) { Guid projekteGuid = Guid.Parse(projekteGuidStr); if (projekteGuid != null) { IEnumerable loadedProjekte = unitOfWork.ProjekteRepository.Get(x => x.GuidNr.Equals(projekteGuid)); if (loadedProjekte.Count() == 1) _selectedProjekt = loadedProjekte.First(); } } string baustellenGuidStr = (string)registry.GetValue("LastBaustelle"); if (baustellenGuidStr != null) { Guid baustellenGuid = Guid.Parse(baustellenGuidStr); if (baustellenGuid != null) { IEnumerable loadedBaustelle = unitOfWork.BaustelleRepository.Get(x => x.GuidNr.Equals(baustellenGuid)); if (loadedBaustelle.Count() == 1) _selectedBaustelle = loadedBaustelle.First(); } } } private void InitRegistry() { Registry.CurrentUser.CreateSubKey("Software\\Cosysda\\KanSan"); LadeRegistry(); } public MainWindowViewModel() { LadeRegistry(); } } }