417 lines
14 KiB
C#
417 lines
14 KiB
C#
using RomeOnlinePartner.Models;
|
|
using RomeOnlinePartner.Utils.Growls;
|
|
using GraphQL.Client;
|
|
using GraphQL;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using Firebase.Auth;
|
|
using ToastNotifications.Messages;
|
|
using Microsoft.Win32;
|
|
|
|
namespace RomeOnlinePartner.ViewModels
|
|
{
|
|
public partial class MainViewModel : BaseViewModel
|
|
{
|
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
/* * */
|
|
|
|
public MainViewModel()
|
|
{
|
|
logger.Trace("Main VM constructed.");
|
|
|
|
BackgroundWorker bw = new BackgroundWorker();
|
|
bw.DoWork += bw_InitVm;
|
|
bw.WorkerReportsProgress = true;
|
|
bw.ProgressChanged += Bw_ProgressChanged;
|
|
bw.RunWorkerCompleted += Bw_RunWorkerCompleted;
|
|
bw.RunWorkerAsync();
|
|
|
|
Growler = new GrowlNotification(this);
|
|
Utils.JobProcessingQueue.SetGrowler(Growler);
|
|
Utils.QuickBooksInterop.SetGrowler(Growler);
|
|
}
|
|
|
|
private void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
|
{
|
|
((BackgroundWorker)sender).Dispose();
|
|
Progress = 0;
|
|
}
|
|
|
|
private void Bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
|
|
{
|
|
Progress = e.ProgressPercentage;
|
|
}
|
|
|
|
private void bw_InitVm(object sender, DoWorkEventArgs e)
|
|
{
|
|
BackgroundWorker _callingThread = sender as BackgroundWorker;
|
|
_callingThread.ReportProgress(1);
|
|
|
|
//Restore list of paths, convert to monitor object.
|
|
List<string> listOfPaths = Properties.Settings.Default.MonitoringPaths;
|
|
if (listOfPaths == null) listOfPaths = new List<string>();
|
|
foreach (var p in listOfPaths)
|
|
{
|
|
MonitoringPaths.Add(new Models.Monitor() { FilePath = p });
|
|
}
|
|
|
|
MonitoringPaths.CollectionChanged += MonitoringPathsChanged;
|
|
|
|
Utils.PPGMixData.StartMixTimer();
|
|
|
|
_callingThread.ReportProgress(30);
|
|
|
|
_updateCheckTimer.Elapsed += _updateTimer_Elapsed;
|
|
_updateCheckTimer.AutoReset = true;
|
|
_updateCheckTimer.Start();
|
|
|
|
_callingThread.ReportProgress(50);
|
|
//Cannot use await.
|
|
LoadBodyshopData().Wait(); ;
|
|
|
|
_callingThread.ReportProgress(80);
|
|
logger.Debug("VM Init Complete");
|
|
Task.Run(() => Utils.HTTPServer.InitHttpServer(AddHttpStatus));
|
|
Task.Run(() => updateCheck());
|
|
StartAllFolderMonitors();
|
|
Utils.PowerModeEventHandler.InitEventHandler();
|
|
_callingThread.ReportProgress(100);
|
|
}
|
|
|
|
|
|
private async Task updateCheck()
|
|
{
|
|
|
|
|
|
|
|
|
|
#if (!DEBUG)
|
|
if (!Utils.AppMetaData.IsTest)
|
|
{
|
|
logger.Debug("Checking if updates are available.");
|
|
UpdateAvailable = await Utils.UpdateHandler.AreUpdatesAvailable();
|
|
if (UpdateAvailable)
|
|
{
|
|
logger.Debug("Updates are available!");
|
|
await Utils.UpdateHandler.UpdateAppEasy((val) => UpdateProgress = val);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
// string msg = "An update to Rome Online Partner is Available. It will be automatically downloaded and applied.";
|
|
// Utils.Notifications.notifier.ShowInformation(msg);
|
|
// logger.Debug("Updates are available! Installing.");
|
|
// try
|
|
// {
|
|
// await Utils.UpdateHandler.ApplyUpdates((val) => UpdateProgress = val, (val) => UpdateProgress = val);
|
|
|
|
// }
|
|
// catch (Exception Ex)
|
|
// {
|
|
// logger.Error("Error while updating." + Ex.ToString());
|
|
|
|
// }
|
|
//}
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
private async Task InstallUpdates()
|
|
{
|
|
logger.Debug("Updates are available! Installing.");
|
|
try
|
|
{
|
|
await Utils.UpdateHandler.ApplyUpdates((val) => UpdateProgress = val, (val) => UpdateProgress = val);
|
|
|
|
}
|
|
catch (Exception Ex)
|
|
{
|
|
logger.Error("Error while updating." + Ex.ToString());
|
|
|
|
}
|
|
}
|
|
|
|
private async void _updateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
|
|
{
|
|
await updateCheck();
|
|
if (UpdateAvailable)
|
|
{
|
|
await InstallUpdates();
|
|
}
|
|
}
|
|
|
|
private void MonitoringPathsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
|
|
{
|
|
IndeterminateLoading = true;
|
|
StopAllFolderMonitors();
|
|
StartAllFolderMonitors();
|
|
IndeterminateLoading = false;
|
|
}
|
|
|
|
public void AddFolderMonitoringPath()
|
|
{
|
|
var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog();
|
|
if (dialog.ShowDialog().GetValueOrDefault())
|
|
{
|
|
logger.Debug("Adding folder {0} to monitoring paths.", dialog.SelectedPath);
|
|
MonitoringPaths.Add(new Models.Monitor() { FilePath = dialog.SelectedPath });
|
|
Properties.Settings.Default.MonitoringPaths = MonitoringPaths.Select(x => x.FilePath).ToList();
|
|
Properties.Settings.Default.Save();
|
|
}
|
|
}
|
|
|
|
public void RemoveFolderMonitoringPath(Monitor m)
|
|
{
|
|
logger.Debug("Removing folder {0} to monitoring paths.", m.FilePath);
|
|
m.StopMonitor();
|
|
MonitoringPaths.Remove(m);
|
|
Properties.Settings.Default.MonitoringPaths = MonitoringPaths.Select(x => x.FilePath).ToList();
|
|
Properties.Settings.Default.Save();
|
|
}
|
|
|
|
public void StartAllFolderMonitors()
|
|
{
|
|
IndeterminateLoading = true;
|
|
if (MonitoringPaths.Count > 0)
|
|
foreach (var m in MonitoringPaths)
|
|
{
|
|
m.StopMonitor();
|
|
}
|
|
|
|
foreach (var p in MonitoringPaths)
|
|
{
|
|
p.StartMonitor();
|
|
}
|
|
IndeterminateLoading = false;
|
|
}
|
|
|
|
|
|
public void RemoveAllFolderMonitors()
|
|
{
|
|
IndeterminateLoading = true;
|
|
if (MonitoringPaths.Count > 0)
|
|
foreach (var m in MonitoringPaths.ToList())
|
|
{
|
|
logger.Debug("Removing folder {0} to monitoring paths.", m.FilePath);
|
|
m.StopMonitor();
|
|
MonitoringPaths.Remove(m);
|
|
|
|
}
|
|
Properties.Settings.Default.MonitoringPaths = MonitoringPaths.Select(x => x.FilePath).ToList();
|
|
Properties.Settings.Default.Save();
|
|
|
|
IndeterminateLoading = false;
|
|
}
|
|
|
|
public void BrowseForQbFolder()
|
|
{
|
|
|
|
OpenFileDialog openFileDialog = new OpenFileDialog();
|
|
openFileDialog.ValidateNames = false;
|
|
openFileDialog.Multiselect = false;
|
|
openFileDialog.Filter = "QuickBooks files (*.qbw)|*.qbw|All files (*.*)|*.*";
|
|
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
|
if (openFileDialog.ShowDialog() == true)
|
|
{
|
|
|
|
Properties.Settings.Default.QuickBooksFilePath = openFileDialog.FileName;
|
|
Properties.Settings.Default.Save();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public void BrowseForEmsFolder()
|
|
{
|
|
var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog();
|
|
dialog.SelectedPath = Properties.Settings.Default.EmsExportPath;
|
|
if (dialog.ShowDialog().GetValueOrDefault())
|
|
{
|
|
Properties.Settings.Default.EmsExportPath = dialog.SelectedPath;
|
|
Properties.Settings.Default.Save();
|
|
}
|
|
}
|
|
public void BrowseForPpcFolder()
|
|
{
|
|
var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog();
|
|
dialog.SelectedPath = Properties.Settings.Default.PpcExportPath;
|
|
if (dialog.ShowDialog().GetValueOrDefault())
|
|
{
|
|
Properties.Settings.Default.PpcExportPath = dialog.SelectedPath;
|
|
Properties.Settings.Default.Save();
|
|
}
|
|
}
|
|
|
|
public void BrowseForPaintScalePath()
|
|
{
|
|
|
|
var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog();
|
|
dialog.SelectedPath = Properties.Settings.Default.PaintScalePath;
|
|
if (dialog.ShowDialog().GetValueOrDefault())
|
|
{
|
|
Properties.Settings.Default.PaintScalePath = dialog.SelectedPath;
|
|
Properties.Settings.Default.Save();
|
|
}
|
|
}
|
|
|
|
public void BrowseForPaintScaleImportPath()
|
|
{
|
|
|
|
var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog();
|
|
dialog.SelectedPath = Properties.Settings.Default.PaintScaleImportPath;
|
|
if (dialog.ShowDialog().GetValueOrDefault())
|
|
{
|
|
Properties.Settings.Default.PaintScaleImportPath = dialog.SelectedPath;
|
|
Properties.Settings.Default.Save();
|
|
}
|
|
}
|
|
|
|
public void BrowseForArmsPath()
|
|
{
|
|
|
|
var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog();
|
|
dialog.SelectedPath = Properties.Settings.Default.ArmsExportPath;
|
|
if (dialog.ShowDialog().GetValueOrDefault())
|
|
{
|
|
Properties.Settings.Default.ArmsExportPath = dialog.SelectedPath;
|
|
Properties.Settings.Default.Save();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public void StopAllFolderMonitors()
|
|
{
|
|
IndeterminateLoading = true;
|
|
if (MonitoringPaths.Count > 0)
|
|
foreach (var m in MonitoringPaths)
|
|
{
|
|
m.StopMonitor();
|
|
}
|
|
IndeterminateLoading = false;
|
|
}
|
|
|
|
|
|
public void Logout()
|
|
{
|
|
Properties.Settings.Default.AuthToken = "";
|
|
Properties.Settings.Default.RefreshToken = "";
|
|
Properties.Settings.Default.Save();
|
|
App.Current.Shutdown();
|
|
}
|
|
|
|
|
|
public async Task LoadBodyshopData()
|
|
{
|
|
var r = new GraphQLRequest
|
|
{
|
|
Query = @"query QUERY_BODYSHOPS {
|
|
bodyshops {
|
|
shopname
|
|
id
|
|
region_config
|
|
associations {
|
|
id
|
|
active
|
|
}
|
|
}
|
|
}"
|
|
};
|
|
|
|
try
|
|
{
|
|
|
|
var Data = await Utils.GraphQL.ExecuteQuery(r);
|
|
logger.Debug("Bodyshop Query Data");
|
|
logger.Debug(JsonConvert.SerializeObject(Data));
|
|
if (Data != null)
|
|
{
|
|
ShopData = Data.bodyshops.ToObject<ObservableCollection<Bodyshop>>();
|
|
}
|
|
|
|
ActiveShop = ShopData.Where(_ => _.AssociationActive == true).FirstOrDefault();
|
|
Utils.AppMetaData.ActiveShopId = ActiveShop?.Id;
|
|
Utils.AppMetaData.ActiveShopName = ActiveShop?.ShopName;
|
|
Utils.AppMetaData.ShopRegion = ActiveShop?.RegionConfig;
|
|
Properties.Settings.Default.LastSelectedShop = ActiveShop.Id ?? null;
|
|
Properties.Settings.Default.Save();
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
logger.Error("Error initializing shop.");
|
|
logger.Error(ex);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
public async Task SetActiveBodyshop()
|
|
{
|
|
//IndeterminateLoading = true;
|
|
//foreach (var s in ShopData)
|
|
//{
|
|
// if (s.Id == ActiveShop?.Id) s.AssociationActive = true;
|
|
// else s.AssociationActive = false;
|
|
// var r = new GraphQLRequest
|
|
// {
|
|
// Query = @"
|
|
// mutation UPDATE_ASSOCIATION($assocId: uuid, $assocActive: Boolean) {
|
|
// update_associations(where: {id: {_eq: $assocId}}, _set: {active: $assocActive}) {
|
|
// returning {
|
|
// id
|
|
// active
|
|
// }
|
|
// }
|
|
// }
|
|
//",
|
|
// Variables = new
|
|
// {
|
|
// assocId = s.AssociationId,
|
|
// assocActive = s.AssociationActive
|
|
// }
|
|
// };
|
|
// await Utils.GraphQL.ExecuteQuery(r);
|
|
//}
|
|
//Properties.Settings.Default.LastSelectedShop = ActiveShop?.Id;
|
|
//Properties.Settings.Default.Save();
|
|
//Utils.AppMetaData.ActiveShopId = ActiveShop?.Id;
|
|
//Utils.AppMetaData.ShopRegion = ActiveShop?.RegionConfig;
|
|
//IndeterminateLoading = false;
|
|
}
|
|
|
|
public void AddHttpStatus(string s)
|
|
{
|
|
string newLine = $"\n{DateTime.Now.ToString("MM/dd/yy hh:mm:ss tt")}: {s}";
|
|
HttpServerLog += newLine;
|
|
logger.Trace(newLine);
|
|
}
|
|
|
|
private FirebaseAuthLink al = RomeOnlinePartner.Utils.Auth.authlink;
|
|
public async Task TestGql()
|
|
{
|
|
|
|
Utils.PPGMixData.test();
|
|
|
|
}
|
|
|
|
public void ConfigurePaintScale()
|
|
{
|
|
Utils.ScheduledTaskConfig.RegisterPaintScale();
|
|
}
|
|
|
|
}
|
|
}
|