Files
2025-02-05 15:00:15 +01:00

270 lines
8.4 KiB
C#

using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Xml.Serialization;
using Autofac;
using Client.Controllers;
using Client.Models;
using Client.Views;
using Microsoft.Win32;
namespace Client.ViewModels;
public class MainWindowViewModel : ViewModelBase
{
// Konstruktor
public MainWindowViewModel(MainWindow mainWindow, App application, WebserviceClient serviceClient)
{
this._mainWindow = mainWindow;
this._application = application;
this._serviceClient = serviceClient;
this.AddCommand = new RelayCommand(ExecuteAddCommand);
this.DeleteCommand = new RelayCommand(ExecuteDeleteCommand, CanExecuteCommand);
this.EditCommand = new RelayCommand(ExecuteEditCommand, CanExecuteCommand);
this.ImportCommand = new RelayCommand(ExecuteImportCommand);
this.ExportCommand = new RelayCommand(ExecuteExportCommand, CanExecuteCommand);
this.ReloadCommand = new RelayCommand(ExecuteReloadCommand);
}
// Attribute für die Auswahliste
public ObservableCollection<Game> Models { get; set; } = new ObservableCollection<Game>();
private Game _selectedModel;
public Game SelectedModel
{
get => _selectedModel;
set
{
if (Equals(value, _selectedModel)) return;
_selectedModel = value;
OnPropertyChanged();
}
}
// Methode und Attribute zum Starten der App
private MainWindow _mainWindow;
private App _application;
public void Initialize()
{
_mainWindow.DataContext = this;
_mainWindow.ShowDialog();
_application.MainWindow = _mainWindow;
}
// Methoden und Attribute für die Buttons
private WebserviceClient _serviceClient;
private bool CanExecuteCommand(object command)
{
return this.SelectedModel != null;
}
public ICommand AddCommand { get; }
private async void ExecuteAddCommand(object command)
{
AddAndEditViewModel childWindow = this._application.Container.Resolve<AddAndEditViewModel>();
Game newGame = childWindow.AddGame();
// this.ReloadCommand.Execute(null);
var existingGame = this.Models.FirstOrDefault(m => m.Titel == newGame.Titel);
if (existingGame != null)
{
this.SwitchToExistingGame(existingGame);
}
else if (childWindow.dialogResult)
{
string response = await _serviceClient.AddGame(newGame);
this.ShowServerResponse(response);
this.ReloadCommand.Execute(null);
}
}
public ICommand DeleteCommand { get; }
private async void ExecuteDeleteCommand(object command)
{
string response = await _serviceClient.DeleteGame(this.SelectedModel.Id);
this.ShowServerResponse(response);
this.ReloadCommand.Execute(null);
}
public ICommand EditCommand { get; }
private async void ExecuteEditCommand(object command)
{
AddAndEditViewModel childWindow = this._application.Container.Resolve<AddAndEditViewModel>();
Game editedGame = childWindow.EditGame(this.SelectedModel);
// this.ReloadCommand.Execute(null);
var existingGame = this.Models.FirstOrDefault(m => m.Titel == editedGame.Titel
&& m.Id != editedGame.Id);
if (existingGame != null)
{
this.SwitchToExistingGame(existingGame);
}
else if (childWindow.dialogResult)
{
string response = await _serviceClient.EditGame(editedGame.Id, editedGame);
this.ShowServerResponse(response);
this.ReloadCommand.Execute(null);
}
}
public ICommand ImportCommand { get; }
private async void ExecuteImportCommand(object command)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "XML-Datei (*.xml)|*.xml",
Title = "Spiel importieren"
};
if (openFileDialog.ShowDialog() == true)
{
XmlSerializer serializer = new XmlSerializer(typeof(GameToExport));
using (FileStream fileStream = new FileStream(openFileDialog.FileName, FileMode.Open))
{
try
{
var deserializedGame = (GameToExport)serializer.Deserialize(fileStream);
deserializedGame.Id = 0;
Game importedGame = new Game();
importedGame.Id = deserializedGame.Id;
importedGame.Titel = deserializedGame.Titel;
importedGame.Zustand = deserializedGame.Zustand;
importedGame.Kommentar = deserializedGame.Kommentar;
if (importedGame.Titel == null)
{
// Es tut mir ja leid! Nein, ich renne nicht gegen geschlossene Türen!
throw new ArgumentNullException();
}
else
{
// this.ReloadCommand.Execute(null);
var existingGame = this.Models.FirstOrDefault(m => m.Titel == importedGame.Titel);
if (existingGame != null)
{
this.SwitchToExistingGame(existingGame);
}
else
{
await _serviceClient.AddGame(importedGame);
this.ReloadCommand.Execute(null);
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
MessageBox.Show(
"Das Game im XML-Dokument hat kein gültiges Format.",
"Ungültige Eingabe",
MessageBoxButton.OK,
MessageBoxImage.Warning
);
return;
}
}
}
}
public ICommand ExportCommand { get; }
private void ExecuteExportCommand(object command)
{
SaveFileDialog saveFileDialog = new SaveFileDialog
{
Filter = "XML-Datei (*.xml)|*.xml",
Title = "Spiel speichern",
FileName = this.SelectedModel.Titel
};
if (saveFileDialog.ShowDialog() == true)
{
GameToExport gameToExport = new GameToExport();
gameToExport.Id = this.SelectedModel.Id;
gameToExport.Titel = this.SelectedModel.Titel;
gameToExport.Zustand = this.SelectedModel.Zustand;
if (this.SelectedModel.Kommentar == null)
{
gameToExport.Kommentar = "";
}
else
{
gameToExport.Kommentar = this.SelectedModel.Kommentar;
}
XmlSerializer serializer = new XmlSerializer(typeof(GameToExport));
using (FileStream fileStream = new FileStream(saveFileDialog.FileName, FileMode.Create))
{
serializer.Serialize(fileStream, gameToExport);
}
}
}
public ICommand ReloadCommand { get; }
private async void ExecuteReloadCommand(object command)
{
this.Models.Clear();
foreach (Game game in await _serviceClient.GetAllGames())
{
this.Models.Add(game);
}
this.SelectedModel = null;
OnPropertyChanged();
}
// Methoden für die Messageboxen
private void SwitchToExistingGame(Game existingGame)
{
var editTitle = MessageBox.Show(
"Diesen Titel gibt es bereits. \n" +
"Wollen sie stattdessen den vorhandenen Titel bearbeiten?",
"Ungültige Eingabe",
MessageBoxButton.YesNo,
MessageBoxImage.Question
);
if (editTitle == MessageBoxResult.Yes)
{
this.ReloadCommand.Execute(null);
this.SelectedModel = existingGame;
this.EditCommand.Execute(null);
}
}
private void ShowServerResponse(string response)
{
return;
// Beta: siehe README
/*
var serverResponse = MessageBox.Show(
$"Serverantwort: {response}",
"Serverantwort",
MessageBoxButton.OK,
MessageBoxImage.Information
);
*/
}
}