viewmodel ins wpf gepackt, build failed
This commit is contained in:
211
DaSaSo.Wpf/ViewModel/Window/MainWindowViewModel.cs
Normal file
211
DaSaSo.Wpf/ViewModel/Window/MainWindowViewModel.cs
Normal file
@@ -0,0 +1,211 @@
|
||||
using DaSaSo.Domain.Enums;
|
||||
using DaSaSo.Domain.Model;
|
||||
using DaSaSo.Domain.Services;
|
||||
using DaSaSo.EntityFramework;
|
||||
using DaSaSo.EntityFramework.Services;
|
||||
using DaSaSo.Wpf.ViewModel.Commands;
|
||||
using DaSaSo.Wpf.ViewModel.Enums;
|
||||
using DaSaSo.Wpf.ViewModel.Interface;
|
||||
using DaSaSo.Wpf.ViewModel.State.Navigation;
|
||||
using Microsoft.Toolkit.Mvvm.Input;
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace DaSaSo.Wpf.ViewModel
|
||||
{
|
||||
public sealed class MainWindowViewModel : BaseViewModel
|
||||
{
|
||||
private readonly IViewModelAbstractFactory viewModelFactory;
|
||||
|
||||
public MainWindowViewModel(IViewModelAbstractFactory viewModelFactory)
|
||||
{
|
||||
this.viewModelFactory = viewModelFactory;
|
||||
}
|
||||
|
||||
private readonly IActualProject _actualProject;
|
||||
private readonly IDataService<Client> _clientDataService;
|
||||
private readonly IDataService<Project> _projectDataService;
|
||||
private readonly IDataService<Buildingsite> _buildingsiteDataService;
|
||||
|
||||
private string _clientname = "";
|
||||
private string _projektname = "";
|
||||
private string _projektnummer = "";
|
||||
private string _buildingsitename = "";
|
||||
private RegistryKey? registry;
|
||||
const string REGISTRYKEY = "HKEY_CURRENT_USER\\Software\\Cosysda\\DaSaSo";
|
||||
public bool CanSelectBuildingSite { get => _actualProject.AktuellProjekt != null; }
|
||||
public bool CanSelectSewerObjects { get => _actualProject.AktuellBaustelle != null; }
|
||||
|
||||
public IMainWindowNavigator Navigator { get; set; }
|
||||
public ICommand UpdateCurrentViewModelCommand { get; }
|
||||
public BaseViewModel CurrentViewModel => Navigator.CurrentViewModel;
|
||||
|
||||
public string ClientName
|
||||
{
|
||||
get => _clientname;
|
||||
set
|
||||
{
|
||||
if(_clientname != value)
|
||||
{
|
||||
_clientname = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string Projektname
|
||||
{
|
||||
get => _projektname;
|
||||
set
|
||||
{
|
||||
if (_projektname != value)
|
||||
{
|
||||
_projektname = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
public string Projektnummer
|
||||
{
|
||||
get => _projektnummer;
|
||||
set
|
||||
{
|
||||
if (_projektnummer != value)
|
||||
{
|
||||
_projektnummer = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
public string Buildingsitename
|
||||
{
|
||||
get => _buildingsitename;
|
||||
set
|
||||
{
|
||||
if (_buildingsitename != value)
|
||||
{
|
||||
_buildingsitename = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
public static string ApplicationTitle
|
||||
{
|
||||
get
|
||||
{
|
||||
string gitVersion;
|
||||
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
|
||||
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DaSaSo.ViewModel.version.txt"))
|
||||
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
|
||||
#pragma warning disable CS8604 // Possible null reference argument.
|
||||
using (StreamReader reader = new(stream))
|
||||
#pragma warning restore CS8604 // Possible null reference argument.
|
||||
{
|
||||
gitVersion = reader.ReadToEnd();
|
||||
}
|
||||
return string.Format("Cosysda Sanierung Software : {0}",gitVersion);
|
||||
}
|
||||
}
|
||||
|
||||
public MainWindowViewModel(IMainWindowNavigator navigator,IViewModelAbstractFactory viewModelFactory,
|
||||
IActualProject actualProject,
|
||||
IDataService<Client> clientDataService,
|
||||
IDataService<Project> projectDataService,
|
||||
IDataService<Buildingsite> buildingsiteDataService
|
||||
)
|
||||
{
|
||||
this.Navigator = navigator;
|
||||
this.viewModelFactory = viewModelFactory;
|
||||
_clientDataService = clientDataService;
|
||||
_projectDataService = projectDataService;
|
||||
_buildingsiteDataService = buildingsiteDataService;
|
||||
|
||||
registry = Registry.CurrentUser.OpenSubKey("Software\\Cosysda\\DaSaSo");
|
||||
if (registry == null) InitRegistry();
|
||||
|
||||
Navigator.StateChanged += Navigator_StateChanged;
|
||||
|
||||
UpdateCurrentViewModelCommand = new UpdateCurrentViewModelCommand(navigator, viewModelFactory);
|
||||
UpdateCurrentViewModelCommand.Execute(EMainWindowViewType.Home);
|
||||
_actualProject = actualProject;
|
||||
_actualProject.ProjectChanged += ActualProject_ProjectChanged;
|
||||
_actualProject.BuildingSiteChanged += ActualProject_BuildingSiteChanged;
|
||||
_actualProject.SewerObjectChanged += ActualProject_SewerObjectChanged;
|
||||
|
||||
LadeRegistry();
|
||||
}
|
||||
|
||||
private async void LadeRegistry()
|
||||
{
|
||||
int projectid = Convert.ToInt32((string)registry.GetValue("lastproject", "-1"));
|
||||
if (projectid == -1)
|
||||
return;
|
||||
Project lastProject = await _projectDataService.Get(projectid);
|
||||
if(lastProject == null)
|
||||
{
|
||||
SaveInRegistry("lastproject","-1");
|
||||
return;
|
||||
}
|
||||
_actualProject.SetProject(lastProject);
|
||||
|
||||
string? value = registry.GetValue("lastbuildingsite", "-1") as string;
|
||||
int buildingsiteid = Convert.ToInt32(value);
|
||||
if (buildingsiteid == -1)
|
||||
return;
|
||||
Buildingsite lastBuildingiste = await _buildingsiteDataService.Get(buildingsiteid);
|
||||
if(lastBuildingiste == null)
|
||||
{
|
||||
SaveInRegistry("lastbuildingsite","-1");
|
||||
return;
|
||||
}
|
||||
_actualProject.SetBuildingSite(lastBuildingiste);
|
||||
|
||||
}
|
||||
|
||||
private static void SaveInRegistry(string key, string value)
|
||||
{
|
||||
Registry.SetValue(REGISTRYKEY, key, value);
|
||||
}
|
||||
|
||||
private void InitRegistry()
|
||||
{
|
||||
registry = Registry.CurrentUser.CreateSubKey("Software\\Cosysda\\DaSaSo");
|
||||
LadeRegistry();
|
||||
}
|
||||
|
||||
private void ActualProject_SewerObjectChanged(object? sender, EventArgs e)
|
||||
{
|
||||
UpdateCurrentViewModelCommand.Execute(EMainWindowViewType.SewerMainMenu);
|
||||
}
|
||||
|
||||
private void Navigator_StateChanged()
|
||||
{
|
||||
OnPropertyChanged(nameof(CurrentViewModel));
|
||||
}
|
||||
|
||||
private void ActualProject_BuildingSiteChanged(object? sender, EventArgs e)
|
||||
{
|
||||
SaveInRegistry("lastbuildingsite", _actualProject.AktuellBaustelle.Id.ToString());
|
||||
Buildingsitename = _actualProject.AktuellBaustelle.BuildingSiteNumber;
|
||||
OnPropertyChanged(nameof(CanSelectSewerObjects));
|
||||
UpdateCurrentViewModelCommand.Execute(EMainWindowViewType.SewerObjects);
|
||||
}
|
||||
|
||||
private void ActualProject_ProjectChanged(object? sender, EventArgs e)
|
||||
{
|
||||
SaveInRegistry("lastproject", _actualProject.AktuellProjekt.Id.ToString());
|
||||
Projektname = _actualProject.AktuellProjekt.Name;
|
||||
Projektnummer = _actualProject.AktuellProjekt.Projektnummer;
|
||||
OnPropertyChanged(nameof(CanSelectBuildingSite));
|
||||
UpdateCurrentViewModelCommand.Execute(EMainWindowViewType.Buildingsites);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user