Files
laatzen/Common/OmniPlus/InspectionUI/Plugins/MainWindowVM.cs
T
2024-11-13 11:24:17 +01:00

274 lines
9.1 KiB
C#

namespace InspectionUI
{
using InspectionUI.Plugins.Omni;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
public class MainWindowVM : VM
{
private bool loadEnabled;
private bool startEnabled;
private bool cancelEnabled;
private readonly string plugin = "";
private string notifications;
private readonly TestBench testBench;
private readonly PluginsManager pluginsManager;
private Visibility notificationsVisibility;
private Visibility pluginsDropdownVisibility;
public MainWindowVM() : base()
{
this.ItemsGridSource = new ItemsGridVM();
this.Plugins = new ObservableCollection<string>();
this.LoadInspectionParameters = new Command(this.OnLoadInspectionParameters);
this.StartInspection = new Command(this.OnStartInspection);
this.CancelInspection = new Command(this.OnCancelInspection);
this.CloseNotification = new Command(this.OnCloseNotification);
this.CopyNotification = new Command(this.OnCopyNotification);
this.pluginsManager = new PluginsManager();
this.notificationsVisibility = Visibility.Collapsed;
this.pluginsDropdownVisibility = Visibility.Collapsed;
this.testBench = new TestBench(this.ItemsGridSource, App.Appsettings);
this.testBench.Notification += notification
=> this.Notifications = notification;
this.testBench.NotificationAppend += notification
=> this.Notifications += notification;
this.LoadAvailablePlugins();
}
public bool IsLoadEnabled
{
get => this.loadEnabled;
set => this.Set(() => this.loadEnabled = value);
}
public bool IsStartEnabled
{
get => this.startEnabled;
set => this.Set(() => this.startEnabled = value);
}
public bool IsCancelEnabled
{
get => this.cancelEnabled;
set => this.Set(() => this.cancelEnabled = value);
}
public string SearchToken
{
get => this.testBench.SearchToken;
set => this.Set(() => this.testBench.SearchToken = value);
}
public string Plugin
{
get => this.plugin;
set
{
//this.Set(() => this.plugin = value);
//this.Notifications = default(string);
//this.IsCancelEnabled = false;
//this.IsLoadEnabled = false;
//this.IsStartEnabled = false;
//this.testBench.Layout.Clear();
//this.testBench.Layout.Refresh();
//Task.Factory
// .StartNew(() =>
// {
// this.TestBatch = this.pluginsManager.Activate(value, this.testBench);
// })
// .ContinueWith(x =>
// {
// if (x.Exception != null)
// {
// this.Notifications = x.Exception.ToString();
// }
// this.IsLoadEnabled = this.TestBatch != null;
// this.TestBatch?.Initialize();
// });
}
}
public string Notifications
{
get => this.notifications;
set
{
this.Set(() => this.notifications = value);
if (string.IsNullOrWhiteSpace(this.notifications))
{
this.NotificationsVisibility = Visibility.Collapsed;
}
else
{
this.NotificationsVisibility = Visibility.Visible;
}
}
}
public Visibility NotificationsVisibility
{
get => this.notificationsVisibility;
set => this.Set(() => this.notificationsVisibility = value);
}
public Visibility PluginsDropdownVisibility
{
get => this.pluginsDropdownVisibility;
set => this.Set(() => this.pluginsDropdownVisibility = value);
}
public ItemsGridVM ItemsGridSource { get; }
public ICommand LoadInspectionParameters { get; }
public ICommand StartInspection { get; }
public ICommand CancelInspection { get; }
public ICommand CloseNotification { get; }
public ICommand CopyNotification { get; }
public ObservableCollection<string> Plugins { get; }
protected TestBatch TestBatch { get; private set; }
public override void Dispose()
{
base.Dispose();
this.testBench?.Dispose();
this.TestBatch?.Dispose();
}
private void OnLoadInspectionParameters()
{
Task.Factory
.StartNew(() =>
{
this.Notifications = default(string);
this.IsCancelEnabled = false;
this.IsLoadEnabled = false;
this.IsStartEnabled = false;
this.TestBatch.LoadInspection();
})
.ContinueWith(task =>
{
if (task.Exception != null)
{
this.Notifications = task.Exception.ToString();
}
this.IsCancelEnabled = false;
this.IsLoadEnabled = true;
this.IsStartEnabled = this.TestBatch?.CanStart ?? false;
});
}
private void OnStartInspection()
{
Task.Factory
.StartNew(() =>
{
this.Notifications = default(string);
this.IsCancelEnabled = true;
this.IsLoadEnabled = false;
this.IsStartEnabled = false;
this.TestBatch.StartInspection();
})
.ContinueWith(task =>
{
if (task.Exception != null)
{
this.Notifications = task.Exception.ToString();
}
this.IsCancelEnabled = false;
this.IsLoadEnabled = true;
this.IsStartEnabled = false;
});
}
private void OnCancelInspection()
{
Task.Factory
.StartNew(() =>
{
this.Notifications = default(string);
this.IsCancelEnabled = false;
this.IsLoadEnabled = false;
this.IsStartEnabled = false;
this.TestBatch?.CancelInspection();
})
.ContinueWith(task =>
{
if (task.Exception != null)
{
this.Notifications = task.Exception.ToString();
}
this.IsCancelEnabled = false;
this.IsLoadEnabled = true;
this.IsStartEnabled = false;
});
}
private void OnCloseNotification()
=> this.Notifications = default(string);
private void OnCopyNotification()
=> Clipboard.SetText(this.Notifications);
private void LoadAvailablePlugins()
{
Task.Factory
.StartNew(() =>
{
//this.DispatcherInvoke(() =>
//{
// this.Plugins.Clear();
// //foreach (var plugin in this.pluginsManager.AvailablePlugins())
// //{
// // this.Plugins.Add(plugin);
// //}
// this.PluginsDropdownVisibility = Visibility.Collapsed;
// //= this.Plugins.Count > 1
// //? Visibility.Visible
// //: Visibility.Collapsed;
// this.TestBatch = this.pluginsManager.Activate(null, this.testBench);
// this.IsLoadEnabled = this.TestBatch != null;
// this.TestBatch?.Initialize();
//});
this.DispatcherInvoke(this.Plugins.Clear);
this.PluginsDropdownVisibility
= this.Plugins.Count > 1
? Visibility.Visible
: Visibility.Collapsed;
this.TestBatch = this.pluginsManager.Activate(null, this.testBench);
this.IsLoadEnabled = this.TestBatch != null;
this.TestBatch?.Initialize();
})
.ContinueWith(task =>
{
if (task.Exception != null)
{
this.Notifications = task.Exception.ToString();
}
});
}
}
}