Files
BeWoPlaner/BeWoLauncher/WindowImp/MainWindow.xaml.cs
2021-09-30 15:31:13 +02:00

305 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using BeWoLauncher.Components;
using BeWoLauncher.PageImp;
using BS.SharedLauncher;
using BS.SharedLauncher.Information;
using BS.SharedLauncher.Interfaces;
using BS.SharedLauncher.Pages;
using BS.SharedLauncher.Windows;
namespace BeWoLauncher.WindowImp
{
public partial class MainWindow : Window, IMainWindow
{
private readonly Stack<Control> _ModalPopUpControls;
private Control _CurrentModalPopUp;
private Grid _ModalPopUpBackGround;
private WaitLayer2 _WaitLayer;
public IBeWoPage CurrentPage { get; set; }
//public ILoginPage LoginPage { get; set; }
//public IUpdatePage UpdatePage { get; set; }
//public IMainPage MainPage { get; set; }
public MainWindow()
{
InitializeComponent();
_ModalPopUpControls = new Stack<Control>();
LauncherApp.Window = this;
Session.MainWindow = this;
ShowLoginView();
}
private void LoginSuccessful(object s, EventArgs e)
{
#if DEBUG
//if (ConnectionInformation.Tenant == "demo")
//{
// var dlg = new ConfirmDemoDialog();
// dlg.ShowDialog();
//}
#else
if (ConnectionInformation.Tenant == "demo")
{
var dlg = new ConfirmDemoDialog();
dlg.ShowDialog();
}
#endif
bool useUpdater = bool.TryParse(ConfigurationManager.AppSettings.Get("UseUpdater"), out bool b) && b;
if (useUpdater)
{
ShowUpdateView();
}
else
{
throw new NotImplementedException("#3423543513254");
}
}
private void UpdateSuccessful(object s, EventArgs e)
{
}
private void UpdateFinished(object s, EventArgs e)
{
ShowMainPage();
}
private void MainLogout(object s, EventArgs e)
{
ShowLoginView();
}
private void MainWindow_OnClosing(object s, CancelEventArgs e)
{
if (CurrentPage is object)
{
if (CurrentPage is IMainPage mainPage)
{
if (!mainPage.DoSaveCheck())
{
e.Cancel = true;
return;
}
mainPage.DoMoreSaveStuff();
}
}
Application.Current.Shutdown();
}
public void LoadLocal()
{
Assembly[] tmp = new Assembly[2];
foreach (var file in Session.DirectoryRoot.GetFiles())
{
if (file.Extension != ".dll")
continue;
//tmp[0] = Assembly.LoadFile(Path.Combine(Session.DirectoryRootString, file.Name));
tmp[0] = Assembly.LoadFrom(file.FullName);
Assembly assembly = Assembly.GetExecutingAssembly();
if (tmp[1] is null && file.Name == Session.Assembly2LaunchName)
{
tmp[1] = tmp[0];
}
}
if (tmp[1] is null)
throw new NotImplementedException("#565463944325");
StringBuilder sb = new StringBuilder();
foreach (var typ in tmp[1].GetExportedTypes())
{
sb.Append(typ.Name + ',');
}
var testString = sb.ToString();
var testtype = typeof(IStartRoutine);
//var startRoutineType2 = tmp[1].ExportedTypes.ToArray()[9];
//var sdf = typeof(IStartRoutine).IsAssignableFrom(startRoutineType2);
// var startRoutineType = tmp[1].ExportedTypes.First(type => type.ImplementsInterface(typeof(IStartRoutine)));
Type startRoutineType = null;
foreach (var type in tmp[1].GetExportedTypes())
{
if (typeof(IStartRoutine).IsAssignableFrom(type))
startRoutineType = type;
}
//var startRoutineType = Array.Find(tmp[1].GetExportedTypes(), type => typeof(IStartRoutine).IsAssignableFrom(type));
var startRoutineInstance = Activator.CreateInstance(startRoutineType) as IStartRoutine;
startRoutineInstance.OnStartup(this, Application.Current);
//var test = FindResource("toolbarImageStyle");
var MainPageType = Array.Find(tmp[1].GetExportedTypes(), type => typeof(IMainPage).IsAssignableFrom(type));
var MainPageInstance = Activator.CreateInstance(MainPageType) as IMainPage;
CurrentPage = MainPageInstance;
}
public void ShowBeWoPage(IBeWoPage beWo)
{
if (beWo is UserControl beWoUC)
{
Session.BeginInvokeNormal(() =>
{
rootGrid.Children.Clear();
rootGrid.Children.Add(beWoUC);
});
}
else
{
throw new NotImplementedException("#1249873429");
}
}
private void ShowLoginView()
{
var loginPage = new LoginPage();
loginPage.LoginSuccessful += LoginSuccessful;
ShowBeWoPage(loginPage);
if (bool.TryParse(ConfigurationManager.AppSettings.Get("AutoLogin"), out bool b) && b)
loginPage.Login();
}
private void ShowUpdateView()
{
var updatePage = new UpdatePage(bool.TryParse(ConfigurationManager.AppSettings.Get("ShowUpdateDelay"), out bool b) && b,
bool.TryParse(ConfigurationManager.AppSettings.Get("ForceUpdate"), out bool c) && c);
updatePage.UpdateSuccessful += UpdateSuccessful;
updatePage.UpdateFinished += UpdateFinished;
ShowBeWoPage(updatePage);
}
private void ShowMainPage()
{
LoadLocal();
ShowBeWoPage(CurrentPage);
}
public void ShowError(string message, Exception ex, bool showDetails)
{
Session.BeginInvokeNormal(() =>
ShowControlAsModalPopUp(new ExceptionMessageBox(message, ex, showDetails)));
}
public void ShowError(string message, string ex, bool showDetails)
{
Session.BeginInvokeNormal(() =>
ShowControlAsModalPopUp(new ExceptionMessageBox(message, ex, showDetails)));
}
private void ShowModalBackground()
{
_ModalPopUpBackGround = new Grid();
_ModalPopUpBackGround.IsHitTestVisible = true;
_ModalPopUpBackGround.Background = Brushes.White;
_ModalPopUpBackGround.Opacity = .5;
Grid.SetColumnSpan(_ModalPopUpBackGround, 3);
Grid.SetRowSpan(_ModalPopUpBackGround, 3);
rootGrid.Children.Add(_ModalPopUpBackGround);
Panel.SetZIndex(_ModalPopUpBackGround, int.MaxValue - 1);
}
public void ShowControlAsModalPopUp(Control control)
{
ShowControlAsModalPopUp(control, HorizontalAlignment.Center, VerticalAlignment.Center);
}
public void ShowControlAsModalPopUp(Control control, HorizontalAlignment ha, VerticalAlignment va)
{
Session.BeginInvokeNormal(() =>
{
if (_CurrentModalPopUp != null)
{
rootGrid.Children.Remove(_CurrentModalPopUp);
_ModalPopUpControls.Push(_CurrentModalPopUp);
}
else
{
this.ShowModalBackground();
}
_CurrentModalPopUp = control;
control.HorizontalAlignment = ha;
control.VerticalAlignment = va;
Grid.SetColumnSpan(control, 3);
Grid.SetRowSpan(control, 3);
this.rootGrid.Children.Add(control);
Panel.SetZIndex(control, int.MaxValue);
});
}
public void CloseCurrentPopUp()
{
if (_ModalPopUpBackGround != null)
{
Session.BeginInvokeNormal(() =>
{
rootGrid.Children.Remove(_CurrentModalPopUp);
_CurrentModalPopUp = null;
if (_ModalPopUpControls.Count == 0)
{
rootGrid.Children.Remove(_ModalPopUpBackGround);
_ModalPopUpBackGround = null;
}
else
{
var control = this._ModalPopUpControls.Pop();
_CurrentModalPopUp = control;
rootGrid.Children.Add(control);
Panel.SetZIndex(control, int.MaxValue);
}
});
}
}
public void StartWaiting()
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal,
(Action)delegate
{
if (_WaitLayer == null)
{
_WaitLayer = new WaitLayer2();
_WaitLayer.HorizontalAlignment = HorizontalAlignment.Center;
_WaitLayer.VerticalAlignment = VerticalAlignment.Center;
rootGrid.Children.Add(_WaitLayer);
Panel.SetZIndex(_WaitLayer, int.MaxValue);
_WaitLayer.Dispatcher.Invoke(DispatcherPriority.Render, (Action)delegate () { });
}
});
}
public void EndWaiting()
{
Dispatcher.BeginInvoke(
DispatcherPriority.Normal,
(Action)delegate
{
if (_WaitLayer != null)
{
rootGrid.Children.Remove(_WaitLayer);
_WaitLayer = null;
}
});
}
}
}