Files
BeWoPlaner/BeWoLauncher/Logic/Controller/ViewController.cs
2022-09-14 10:57:51 +02:00

359 lines
12 KiB
C#

using BeWoLauncher.Logic.Controller;
using BeWoLauncher.Utils;
using BeWoLauncher.View.Pages;
using BeWoLauncher.WindowImp;
using BS.SharedLauncher.Extensions;
using BS.SharedLauncher.Information;
using BS.SharedLauncher.Logic;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Threading;
namespace BeWoLauncher.Logic.Controller
{
public static class ViewController
{
static bool isBeWoLoginBorderPresent = false;
static BeWoLoginBorder BeWoLoginBorder = null;
static LauncherPageType currentPage;
private static Exception error;
public enum LauncherPageType
{
Login,
InstallInfo,
InstallProgress,
InstallFailed,
UpdateInfo,
UpdateProgress,
UpdateFailed,
UninstallInfo,
UninstallProgress,
UninstallFailed,
UninstallSuccessful,
LoadingScreen,
NoPermissionInfo
}
public static LauncherWindow LauncherWindow { get; set; }
public static Dispatcher Dispatcher => LauncherWindow.Dispatcher;
public static void StartView()
{
ShowPage(LauncherPageType.Login);
}
public static void RestartView()
{
ShowPage(LauncherPageType.Login);
LauncherWindow.Show();
LauncherWindow.Activate();
}
public static void StartWaiting() => LauncherWindow.StartWaiting();
public static void EndWaiting() => LauncherWindow.EndWaiting();
public static bool CheckForPermissions(string path)
{
return FileController.IsDirectoryWritable(path, false);
}
public static void CheckForNewestVersion()
{
Task.Run(() =>
{
DownloadController.Start();
DownloadController.Manager.StartDownloadInfo();
Dispatcher.BeginInvoke(DispatcherPriority.Render,
(Action)delegate
{
if (DownloadController.Manager.ResponseInfo.HasSomethingToDo)
{
if (BeWoLocalConfig.GetFirstInstallationSuccess() &&
File.Exists(BeWoPaths.GetPlanerExePath()))
{
ShowPage(LauncherPageType.UpdateInfo);
}
else
{
ShowPage(LauncherPageType.InstallInfo);
}
}
else
{
ShowPage(LauncherPageType.LoadingScreen);
}
EndWaiting();
});
});
}
public static void InstallationFailed(Exception exception)
{
error = exception;
ShowPage(LauncherPageType.InstallFailed);
}
public static void UpdateFailed(Exception exception)
{
error = exception;
ShowPage(LauncherPageType.UpdateFailed);
}
public static void UninstallFailed(Exception exception)
{
error = exception;
ShowPage(LauncherPageType.UninstallFailed);
}
public static void CheckForAutoLogin(object sender, EventArgs e)
{
var autologin = bool.TryParse(ConfigurationManager.AppSettings.Get("AutoLogin"), out bool c) && c;
if (autologin)
if (sender is LoginPage loginPage)
loginPage.ClickLogin();
}
public static void LoginSuccessful(object sender, EventArgs e)
{
#region confirm demo
#if DEBUG
//if (ConnectionInformation.Tenant == "demo")
//{
// var dlg = new ConfirmDemoDialog();
// dlg.ShowDialog();
//}
#else
if (ConnectionInformation.Tenant == "demo")
{
var dlg = new ConfirmDemoDialog();
dlg.ShowDialog();
}
#endif
#endregion
BeWoPaths.ActualPlanerLocation = BeWoPaths.GetInstallLocationFromSetting();
if (BeWoPaths.ActualPlanerLocation is object && !CheckForPermissions(BeWoPaths.ActualPlanerLocation))
{
ShowPage(LauncherPageType.NoPermissionInfo);
}
else
{
CheckForNewestVersion();
}
}
private static UserControl GetNewPage(LauncherPageType type)
{
switch (type)
{
case LauncherPageType.Login:
var login = new LoginPage();
login.Loaded += CheckForAutoLogin;
login.LoginSuccessful += LoginSuccessful;
return login;
case LauncherPageType.InstallInfo:
var install = new InstallInfo();
install.Accept += (s, e) =>
{
if (CheckForPermissions(install.InstallLocation))
{
BeWoPaths.SetAndSaveInstallLocation(install.InstallLocation);
BeWoLocalConfig.SetFirstInstallationSuccess(true);
ShowPage(LauncherPageType.InstallProgress);
}
else
{
ShowPage(LauncherPageType.NoPermissionInfo);
}
};
install.Abort += (s, e) => { ShowPage(LauncherPageType.Login); };
return GetControlInBorder(install);
case LauncherPageType.InstallProgress:
var installprogress = new InstallProgressInfo();
installprogress.InstallSuccessful += (s, e) =>
{
BeWoLocalConfig.SetFirstInstallationSuccess(true);
ProcessController.StartPlanerProcess();
};
return GetControlInBorder(installprogress);
case LauncherPageType.InstallFailed:
var installError = new InfoScreen();
installError.Title = "Installation fehlgeschlagen!";
installError.Text = error.ToString();
installError.Button1Click += (s, e) => { ShowPage(LauncherPageType.Login); };
return GetControlInBorder(installError);
case LauncherPageType.UpdateInfo:
var update = new UpdateInfo();
update.Accept += (s, e) => { ShowPage(LauncherPageType.UpdateProgress); };
update.Abort += (s, e) => { ShowPage(LauncherPageType.Login); };
return GetControlInBorder(update);
case LauncherPageType.UpdateProgress:
var updateProgress = new UpdateProgressInfo();
updateProgress.UpdateSuccessful += (s, e) => { ShowPage(LauncherPageType.LoadingScreen); };
return GetControlInBorder(updateProgress);
case LauncherPageType.UpdateFailed:
var updateError = new InfoScreen();
updateError.Title = "Update fehlgeschlagen!";
updateError.Text = error.ToString();
updateError.Button1Click += (s, e) => { ShowPage(LauncherPageType.Login); };
return GetControlInBorder(updateError);
case LauncherPageType.UninstallInfo:
var uninstall = new UninstallInfo();
uninstall.Accept += (s, e) => { ShowPage(LauncherPageType.UpdateProgress); };
uninstall.Abort += (s, e) => { ShowPage(LauncherPageType.Login); };
return GetControlInBorder(uninstall);
case LauncherPageType.UninstallProgress:
var uninstallProgress = new UninstallProgressInfo();
uninstallProgress.UninstallSuccessful += (s, e) => { ShowPage(LauncherPageType.UninstallSuccessful); };
return GetControlInBorder(uninstallProgress);
case LauncherPageType.UninstallFailed:
var uninstallFailed = new InfoScreen();
uninstallFailed.Title = "Deinstallation fehlgeschlagen!";
uninstallFailed.Text = error.ToString();
uninstallFailed.Button1Click += (s, e) => { ShowPage(LauncherPageType.Login); };
return GetControlInBorder(uninstallFailed);
case LauncherPageType.UninstallSuccessful:
var uninstallSuccessful = new InfoScreen();
uninstallSuccessful.Title = "Deinstallation erfolgreich!";
uninstallSuccessful.Text = "Kehre zum Login zurück...";
uninstallSuccessful.CollapseCopyButton();
uninstallSuccessful.Button1Click += (s, e) => { ShowPage(LauncherPageType.Login); };
return GetControlInBorder(uninstallSuccessful);
case LauncherPageType.LoadingScreen:
var loading = new LoadingScreen();
loading.Loaded += (s, e) =>
{
Task.Run(() => ProcessController.StartPlanerProcess());
};
return GetControlInBorder(loading);
case LauncherPageType.NoPermissionInfo:
var noper = new InfoScreen();
noper.Title = "Keine Rechte auf Speicherort!";
noper.Text = "Es liegt eine neue Version. Sie besitzen aber nicht die benötigten Rechte, um diese anzuwenden. Bitte wenden Sie sich an Ihren System-Administrator!";
noper.Button1Click += (s, e) => { ShowPage(LauncherPageType.Login); };
return GetControlInBorder(noper);
}
throw new NotImplementedException();
}
private static UserControl GetControlInBorder(UserControl uc)
{
if (isBeWoLoginBorderPresent)
return uc;
BeWoLoginBorder = new BeWoLoginBorder();
isBeWoLoginBorderPresent = true;
BeWoLoginBorder.contentGrid.Children.Clear();
BeWoLoginBorder.contentGrid.Children.Add(uc);
return BeWoLoginBorder;
}
private static void ShowPage(LauncherPageType type)
{
bool before = isBeWoLoginBorderPresent;
var page = GetNewPage(type);
if (type == LauncherPageType.Login)
{
isBeWoLoginBorderPresent = false;
BeWoLoginBorder = null;
Dispatcher.BeginInvoke(DispatcherPriority.Render,
(Action)delegate
{
LauncherWindow.SetMainContent(page);
});
}
else
{
// Neu Laden
if (!before)
{
Dispatcher.BeginInvoke(DispatcherPriority.Render,
(Action)delegate
{
LauncherWindow.SetMainContent(page);
});
}
// Bereits geladen
else
{
Dispatcher.BeginInvoke(DispatcherPriority.Render,
(Action)delegate
{
BeWoLoginBorder.contentGrid.Children.Clear();
BeWoLoginBorder.contentGrid.Children.Add(page);
});
}
}
currentPage = type;
}
}
}